引用密钥oracle,可能是外键问题

时间:2014-08-22 04:11:06

标签: sql database oracle database-design relational-database

我一直在尝试将所有表格加载到数据库中。

每当我尝试加载引用" paper_code"我一直遇到问题错误,然后不会创建表格。

我一直试图找出错误并加载了一些文件,但似乎只有参考论文(paper_code)的表格存在问题。

代码:

DROP TABLE contacts;
DROP TABLE gender;
DROP TABLE lecture_location;
DROP TABLE enrols;
DROP TABLE teaches;
DROP TABLE staff;
DROP TABLE student;
DROP TABLE papers;
DROP TABLE departments;


CREATE TABLE departments
   (dept_id     INT     PRIMARY KEY,
   dept_location VARCHAR2(25) NOT NULL,
   dept_name     VARCHAR2(25) NOT NULL);

INSERT INTO departments VALUES
   (01, 'ALBANY STREET', 'COMPUTER SCIENCE');
INSERT INTO departments VALUES
   (02, 'UNION STREET', 'MUSIC');
INSERT INTO departments VALUES
   (03, 'HOWE STREET', 'ANATOMY');
INSERT INTO departments VALUES
   (04, 'ANZAC AVENUE', 'THEATRE');

CREATE TABLE papers
   (paper_code      INT,
   EFTS             INT     NOT NULL,
   dept_id          INT     REFERENCES departments(dept_id),
   PRIMARY KEY(paper_code, dept_id));


INSERT INTO papers VALUES
   (160, 0.18, 01);
INSERT INTO papers VALUES
   (241, 0.18, 02);
INSERT INTO papers VALUES
   (344, 0.18, 03);
INSERT INTO papers VALUES
   (444, 0.18, 04);

CREATE TABLE student
   (student_id      INT     PRIMARY KEY,
   fname            CHAR(11) NOT NULL,
   lname            CHAR(11) NOT NULL,
   degree           CHAR(15) NOT NULL);

INSERT INTO student VALUES
   (172384, 'Michael', 'McDonald', 'BSc');
INSERT INTO student VALUES
   (849294, 'Matthew', 'Brockie', 'BA');
INSERT INTO student VALUES
   (384583, 'Daniel', 'Anderson', 'BSc');

CREATE TABLE staff
   (staff_id        INT     PRIMARY KEY,
   dept_id         INT      REFERENCES departments(dept_id),
   fname           CHAR(15) NOT NULL,
   lname           CHAR(15) NOT NULL);

INSERT INTO staff VALUES
   (31, 01, 'Tony', 'Michaels');
INSERT INTO staff VALUES
   (32, 01, 'Steph', 'Cardy');
INSERT INTO staff VALUES
   (33, 02, 'Alex', 'Freeland');
INSERT INTO staff VALUES
   (34, 02, 'Sam', 'Stewart');
INSERT INTO staff VALUES
   (35, 03, 'Monique', 'Cardy');
INSERT INTO staff VALUES
   (36, 03, 'Bayan', 'Zach');


CREATE TABLE teaches
   (paper_code         INT,
   staff_id         INT      REFERENCES staff(staff_id),
   dept_id          INT,
   PRIMARY KEY(paper_code, staff_id, dept_id),
   FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));

INSERT INTO teaches VALUES
   (160, 32, 01);
INSERT INTO teaches VALUES
   (241, 31, 01);
INSERT INTO teaches VALUES
   (344, 33, 02);
INSERT INTO teaches VALUES
   (241, 34, 03);
INSERT INTO teaches VALUES
   (444, 35, 03);
INSERT INTO teaches VALUES
   (444, 36, 04);

CREATE TABLE enrols
   (paper_code         INT     REFERENCES papers(paper_code),
   student_id         INT      REFERENCES student(student_id),
   date_enrolled      DATE,
   PRIMARY KEY(paper_code, student_id));

INSERT INTO enrols VALUES
   (160, 172384, TO_DATE('22-Mar-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
   (444, 849294, TO_DATE('14-Jul-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
   (444, 172384, TO_DATE('23-Mar-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
   (160, 384583, TO_DATE('07-Aug-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES --fix
   (160, 172384, TO_DATE('30-Jul-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
   (241, 849294, TO_DATE('08-Sep-1995', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
   (241, 384583, TO_DATE('25-Dec-1996', 'dd-mon-yyyy'));


CREATE TABLE lecture_location
   (paper_code      INT     REFERENCES papers(paper_code),
   dept_id          INT     REFERENCES departments(dept_id),
   lecture_loc     VARCHAR2(15),
   PRIMARY KEY(paper_code, dept_id, lecture_loc));

INSERT INTO lecture_location VALUES
   (160, 'ARCHWAY');
INSERT INTO lecture_location VALUES
   (241, 'CASTLE');

CREATE TABLE gender
   (student_id      INT PRIMARY KEY REFERENCES student(student_id),
   gender           CHAR(9) NOT NULL);

INSERT INTO gender VALUES
   (172384, 'Female');
INSERT INTO gender VALUES
   (384583, 'Male');

CREATE TABLE contacts
   (contact_details         INT,
   staff_id                 INT REFERENCES staff(staff_id),
   PRIMARY KEY(contact_details, staff_id));

INSERT INTO contacts VALUES
   (022017456, 31);
INSERT INTO contacts VALUES
   (034737447, 31);
INSERT INTO contacts VALUES
   (02285756, 32);
INSERT INTO contacts VALUES
   (034735858, 32);
INSERT INTO contacts VALUES
   (034552097, 33);
INSERT INTO contacts VALUES
   (022867385, 33);
INSERT INTO contacts VALUES
   (021495939, 34);
INSERT INTO contacts VALUES
   (034993872, 35);
INSERT INTO contacts VALUES
   (027459278, 36);

COMMIT;

除引用paper_code之外的所有表格都在工作。

错误是:

INSERT INTO teaches VALUES
*
ERROR at line 1:
ORA-02291: integrity constraint (DAANDERSON.SYS_C00623423) violated - parent
key not found

对于enrols:

   (paper_code     INT     REFERENCES papers(paper_code),
                                                 *
    ERROR at line 2:
   ORA-02270: no matching unique or primary key for this column-list

for lecture_location

       (paper_code  INT REFERENCES papers(paper_code),
                                              *
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list

尝试了几个小时,然后才能开始工作。任何信息赞赏!

1 个答案:

答案 0 :(得分:0)

创建表teaches时,您有:

FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));

因此论文中的每篇文章(paper_code,dept_id)必须存在(paper_code,dept_id)。

您的第二个条目(241,01)不存在,您的第四个值(241,03)也不存在。 (其他人可能会失踪,我没有检查过所有这些)


您还没有将dept_id值插入lecture_location


您在enrols中重复了主键值:

(160,172384)


这部分enrols

CREATE TABLE enrols
(paper_code         INT     REFERENCES papers(paper_code),

无效,因为您必须引用唯一的外键 - 而论文中唯一的唯一键是其主键(paper_code,dept_id)


这部分lecture_location同上:

(paper_code      INT     REFERENCES papers(paper_code),

从好的方面来说,在完成上面的修复后,实际构建了http://sqlfiddle.com/#!4/5ed2c2