0

次のスキーマで 4 つのデータベース テーブルを作成していますが、整合性制約違反エラーが発生します。テーブルにデータを入力するためのクエリと一緒に私のスキーマ全体を次に示します。

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 を使用しています。

4

3 に答える 3

3

これはあなたの問題です:

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

「ユニオン パシフィック」はあなたの会社の 1 つではありません。「ユニオン パシフィック (UP)」があります。

余談ですが、とにかくこれは本当にみすぼらしい方法です。このようにテーブル間でデータを複製すると、このような問題が何度も発生することになります。

Employee実際には、参照データ テーブル ( & )ごとに代理キーが必要でCompanyあり、詳細テーブル (WorksおよびManages) はそれらを使用する必要があります。例えば:

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));
于 2013-09-18T15:13:05.250 に答える
0
foreign key(Lastname, FirstName, MidInitial) references Employee**(column)**

従業員列を忘れました。

于 2013-09-18T15:33:41.277 に答える