再次出现完整性约束错误

时间:2013-09-18 15:02:03

标签: sql oracle referential-integrity

我正在通过以下模式创建四个数据库表,但是我遇到了完整性约束违规错误。这是我的整个模式以及填充表的查询:

create table Employee(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    gender      char(1),
    street      varchar(10),
    city        varchar(10),
    primary key(Lastname, FirstName, MidInitial));

create table company(
    company_name    varchar(20),
    city    varchar(10),
    primary key(company_name));

create table Works(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    company_name    varchar(20),
    salary      numeric(8,2),
    primary key(Lastname, FirstName, MidInitial, company_name),
    foreign key(Lastname, FirstName, MidInitial) references Employee,
    foreign key(company_name) references company);

create table Manages(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    ManagerLastname varchar(10),
    MFirstName  varchar(10),
    MMidInitial char(1),
    start_date  date,
    primary key(Lastname, FirstName, MidInitial, ManagerLastname, MFirstName, MMidInitial),
    foreign key(Lastname, FirstName, MidInitial) references Employee);

insert into Employee
    values('Anderson', 'Susan', 'L', 'F', '108th', 'Omaha');
insert into Employee
    values('Brandy', 'Dan', 'L', 'M', 'P street', 'Lincoln');
insert into Employee
    values('Chen', 'Peter', 'K', 'M', '124th', 'Omaha');
insert into Employee
    values('Clemson', 'Ann', 'M', 'F', 'O', 'Lincoln');
insert into Employee
    values('Dale', 'Mary', 'K', 'F', '132nd', 'Omaha');
insert into Employee
    values('Gill', 'Mary', 'L', 'F', 'P street', 'Lincoln');
insert into Employee
    values('Harrison', 'Susan', 'M', 'F', 'Old Mill', 'Omaha');
insert into Employee
    values('Jackson', 'Kim', 'A', 'F', '178th', 'Omaha');
insert into Employee
    values('Jason', 'Pat', 'M', 'M', '8th', 'C. Bluffs');
insert into Employee
    values('Kumar', 'Paul', 'T', 'M', 'Dodge', 'Omaha');
insert into Employee
    values('Orr', 'Susanne', 'S', 'F', 'Q', 'Omaha');
insert into Employee
    values('Peterson', 'Mike', 'E', 'M', 'Blondo', 'Omaha');
insert into Employee
    values('Powell', 'Tom', 'E', 'M', 'Broadway', 'C. Bluffs');
insert into Employee
    values('Quinn', 'Jacky', 'M', 'F', '168th', 'Omaha');
insert into Employee
    values('Rand', 'Pam', 'R', 'F', '13th', 'Omaha');
insert into Employee
    values('Schreck', 'Ann', 'P', 'F', '214th', 'Elkhorn');
insert into Employee
    values('Simon', 'Eric', 'K', 'M', '77th', 'Lincoln');
insert into Employee
    values('Smith', 'John', 'A', 'M', '8th', 'C. Bluffs');
insert into Employee
    values('Thomson', 'Ron', 'F', 'M', 'Farnam', 'Omaha');
insert into Employee
    values('Young', 'Don', 'R', 'M', 'Dodge', 'Omaha');
insert into Employee
    values('Wong', 'Carole', 'S', 'F', 'Broadway', 'C. Bluffs');

insert into company
    values('First Bank', 'Omaha');
insert into company
    values('FDR', 'Omaha');
insert into company
    values('Lincoln Star', 'Lincoln');
insert into company
    values('Mutual of Omaha', 'Omaha');
insert into company
    values('Union Pacific (UP)', 'Omaha');

insert into Works
    values('Anderson', 'Susan', 'L', 'Mutual of Omaha', 48000);
insert into Works
    values('Brandy', 'Dan', 'L', 'FDR', 42000);
insert into Works
    values('Chen', 'Peter', 'K', 'FDR', 53000);
insert into Works
    values('Clemson', 'Ann', 'M', 'First Bank', 39000);
insert into Works
    values('Dale', 'Mary', 'K', 'Mutual of Omaha', 58000);
insert into Works
    values('Gill', 'Mary', 'L', 'Union Pacific', 48700);
insert into Works
    values('Harrison', 'Susan', 'M', 'Union Pacific', 54320);
insert into Works
    values('Jackson', 'Kim', 'A', 'FDR', 68000);
insert into Works
    values('Jason', 'Pat', 'M', 'FDR', 83000);
insert into Works
    values('Kumar', 'Paul', 'T', 'FDR', 44000);
insert into Works
    values('Orr', 'Susanne', 'S', 'Lincoln Star', 32000);
insert into Works
    values('Peterson', 'Mike', 'E', 'First Bank', 37000);
insert into Works
    values('Powell', 'Tom', 'E', 'First Bank', 54000);
insert into Works
    values('Quinn', 'Jacky', 'M', 'First Bank', 85000);
insert into Works
    values('Rand', 'Pam', 'R', 'FDR', 37000);
insert into Works
    values('Schreck', 'Ann', 'P', 'First Bank', 36000);
insert into Works
    values('Simon', 'Eric', 'K', 'Mutual of Omaha', 45000);
insert into Works
    values('Smith', 'John', 'A', 'FDR', 55000);
insert into Works
    values('Thomson', 'Ron', 'F', 'First Bank', 39000);
insert into Works
    values('Young', 'Don', 'R', 'Mutual of Omaha', 27000);
insert into Works
    values('Wong', 'Carole', 'S', 'First Bank', 44000);

insert into Manages
    values('Anderson', 'Susan', 'L', 'Dale', 'Mary', 'K', "15-SEP-2002");
insert into Manages
    values('Brandy', 'Dan', 'L', 'Jason', 'Pat', 'M', "05-MAR-2004");
insert into Manages
    values('Chen', 'Peter', 'K', 'Jason', 'Pat', 'M', "12-JUNE-2002");
insert into Manages
    values('Clemson', 'Ann', 'M', 'Powell', 'Tom', 'E', "30-AUG-2001");
insert into Manages
    values('Gill', 'Mary', 'L', 'Harrison', 'Susan', 'M', "25-JAN-2003");
insert into Manages
    values('Jackson', 'Kim', 'A', 'Jason', 'Pat', 'M', "09-MAY-2008");
insert into Manages
    values('Kumar', 'Paul', 'T', 'Jason', 'Pat', 'M', "09-MAY-2001");
insert into Manages
    values('Rand', 'Pam', 'R', 'Smith', 'John', 'A', "15-JAN-2004");
insert into Manages
    values('Peterson', 'Mike', 'E', 'Powell', 'Tom', 'E', "30-AUG-2006");
insert into Manages
    values('Schreck', 'Ann', 'P', 'Quinn', 'Jacky', 'M', "28-FEB-1996");
insert into Manages
    values('Simon', 'Eric', 'K', 'Anderson', 'Susan', 'L', "22-FEB-2004");
insert into Manages
    values('Smith', 'John', 'A', 'Jason', 'Pat', 'M', "05-MAR-2003");
insert into Manages
    values('Thomson', 'Ron', 'F', 'Powell', 'Tom', 'E', "30-AUG-1998");
insert into Manages
    values('Wong', 'Carole', 'S', 'Quinn', 'Jacky', 'M', "28-FEB-2001");
insert into Manages
    values('Young', 'Don', 'R', 'Dale', 'Mary', 'K', "15-SEP-2007");

这是我再次犯的错误:

Schema Creation Failed: ORA-02291: integrity constraint (USER_4_3C8F2.SYS_C00778260) violated - parent key not found

让另一组眼睛看看这个并看看它出了什么问题会很棒......我想知道我是否应该以不同的顺序创建/填充表格但我认为我在做什么是正确的。

非常感谢任何帮助。谢谢!

---- UPDATE ----我也在使用SQL Fiddle和Oracle 11g R2。

3 个答案:

答案 0 :(得分:3)

这是你的问题:

insert into Works values('Gill', 'Mary', 'L', 'Union Pacific', 48700);

'Union Pacific'不是您的公司之一,您拥有'Union Pacific(UP)'

顺便说一句,无论如何,这是一个非常破旧的方式。在这样的表格中复制数据意味着你一遍又一遍地遇到这样的问题。

您的每个参考数据表(Employee& Company)都应该有一个代理键,您的详细信息表(WorksManages)应该使用那些。例如:

create table Employee(
    Id          numeric(8),
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    gender      char(1),
    street      varchar(10),
    city        varchar(10),
    primary key(Id));

create table company(
    Id           numeric(8),
    company_name varchar(20),
    city         varchar(10),
    primary key(Id));

create table Works(
    Employee_Id numeric(8),
    Company_Id  numeric(8),
    salary      numeric(8,2),
    primary key(Employee_Id, Company_Id),
    foreign key(Employee_Id) references Employee(Id),
    foreign key(Company_Id) references Company(Id);

create table Manages(
    Employee_Id,
    Manager_Id,
    start_date  date,
    primary key(Employee_Id,Manager_id),
    foreign key(Employee_Id) references Employee(Id),
    foreign key(Manager_Id) references Employee(Id));

答案 1 :(得分:0)

确定。几乎没有错误......

insert into Works 
    values('Gill', 'Mary', 'L', 'Union Pacific (UP)', 48700);
insert into Works
    values('Harrison', 'Susan', 'M', 'Union Pacific (UP)', 54320);

- 将'Union Pacific'更改为'Union Pacific(UP)'

insert into Manages
    values('Anderson', 'Susan', 'L', 'Dale', 'Mary', 'K', '15-SEP-2002');
insert into Manages
    values('Brandy', 'Dan', 'L', 'Jason', 'Pat', 'M', '05-MAR-2004');
insert into Manages
    values('Chen', 'Peter', 'K', 'Jason', 'Pat', 'M', '12-JUNE-2002');
insert into Manages
    values('Clemson', 'Ann', 'M', 'Powell', 'Tom', 'E', '30-AUG-2001');
insert into Manages
    values('Gill', 'Mary', 'L', 'Harrison', 'Susan', 'M', '25-JAN-2003');
insert into Manages
    values('Jackson', 'Kim', 'A', 'Jason', 'Pat', 'M', '09-MAY-2008');
insert into Manages
    values('Kumar', 'Paul', 'T', 'Jason', 'Pat', 'M', '09-MAY-2001');
insert into Manages
    values('Rand', 'Pam', 'R', 'Smith', 'John', 'A', '15-JAN-2004');
insert into Manages
    values('Peterson', 'Mike', 'E', 'Powell', 'Tom', 'E', '30-AUG-2006');
insert into Manages
    values('Schreck', 'Ann', 'P', 'Quinn', 'Jacky', 'M', '28-FEB-1996');
insert into Manages
    values('Simon', 'Eric', 'K', 'Anderson', 'Susan', 'L', '22-FEB-2004');
insert into Manages
    values('Smith', 'John', 'A', 'Jason', 'Pat', 'M', '05-MAR-2003');
insert into Manages
    values('Thomson', 'Ron', 'F', 'Powell', 'Tom', 'E', '30-AUG-1998');
insert into Manages
    values('Wong', 'Carole', 'S', 'Quinn', 'Jacky', 'M', '28-FEB-2001');
insert into Manages
    values('Young', 'Don', 'R', 'Dale', 'Mary', 'K', '15-SEP-2007');

- 将所有双引号更改为单引号

答案 2 :(得分:0)

foreign key(Lastname, FirstName, MidInitial) references Employee**(column)**

您忘记了员工专栏。