未找到父键

时间:2015-09-27 22:05:42

标签: sql database oracle

我完全陷入了Sql Oracle数据库。

我试图在我的表格中添加Course_section:

Insert into Course_Section (CsectionID, MaxCapacity, CourseID, TermID, LocID)
values('14', '10', '2', '1', '1');

Insert into Course_Section (CsectionID, MaxCapacity, CourseID, TermID, LocID)
values('15', '10', '3', '1', '1');

当我试图插入第一个时,它告诉我没有找到父键。 所以我试着在我的表课程中创建两行,这些行将具有我之后将使用的相同CourseID,但它不起作用......

对不起,我还是个初学者:)

Create table Course
(
CourseName Varchar2(100) not null,
CourseID number(6) PRIMARY KEY, 
Credits number(6)
);


Create table TERM
(
TermID number(6) PRIMARY KEY,
Description Varchar2(100) not null
)
;

Create table LOCATION
(
LocID number(6) PRIMARY KEY,
Building Varchar2(100) NOT NULL,
Room Number(4)
)
;

Create table Student 
(
StudID number(6) PRIMARY KEY, 
Sname Varchar2(100) NOT NULL, 
Birthdate DATE
)
;


Create table Enrollment 
(
StudID number(6) PRIMARY KEY, 
CSectionID number(6), 
FOREIGN KEY (CsectionID) references Student (StudID),
GRADE number(2)
)
; 


Create table Course_Section 
(
CSectionID number(6) PRIMARY KEY, 
MaxCapacity number(3) NOT NULL, 
CourseID number(6),
FOREIGN KEY (COURSEID) REFERENCES COURSE (COURSEID), 
TermID number(6), 
Foreign Key (TERMID) references TERM (termID),
LocID number(6),
Foreign Key (LocID) references Location (locID)
)
;

Insert into COURSE (CourseName, CourseID, Credits)
values('Base de données', '1', '3');

2 个答案:

答案 0 :(得分:1)

问题是COURSE表中COURSEIDCOURSE表中有匹配行的行中没有行。为了完成这项工作,您需要在COURSE 2和3的COURSEID表格中创建一行:

INSERT INTO COURSE (COURSENAME, COURSEID, CREDITS)
  VALUES ('ORGANIC CHEMISTRY', 2, 4);

INSERT INTO COURSE (COURSENAME, COURSEID, CREDITS)
  VALUES ('INORGANIC CHEMISTRY', 3, 5);

您还需要提交执行这些INSERT时自动启动的事务:

COMMIT;

祝你好运。

****编辑****

还定义了其他外键约束,但由于它们都没有被命名,因此很难确定是否违反了WHICH约束。我已经修改了你的DDL以包含每个约束的名称:

Create table Course
  (CourseName Varchar2(100) not null,
   CourseID number(6) CONSTRAINT PK_COURSE PRIMARY KEY, 
   Credits number(6));

Create table TERM
  (TermID number(6) CONSTRAINT PK_TERM PRIMARY KEY,
   Description Varchar2(100) not null);

Create table LOCATION
  (LocID number(6) CONSTRAINT PK_LOCATION PRIMARY KEY,
   Building Varchar2(100) NOT NULL,
   Room Number(4));

Create table Student 
  (StudID number(6) CONSTRAINT PK_STUDENT PRIMARY KEY, 
   Sname Varchar2(100) NOT NULL, 
   Birthdate DATE);


Create table Enrollment 
  (StudID number(6) CONSTRAINT PK_ENROLLMENT PRIMARY KEY, 
   CSectionID number(6), 
   CONSTRAINT ENROLLMENT_FK1
     FOREIGN KEY (CsectionID) references Student (StudID),
   GRADE number(2)); 

Create table Course_Section 
  (CSectionID number(6) CONSTRAINT PK_COURSE_SECTION PRIMARY KEY, 
   MaxCapacity number(3) NOT NULL, 
   CourseID number(6),
   CONSTRAINT COURSE_SECTION_FK1
     FOREIGN KEY (COURSEID) REFERENCES COURSE (COURSEID), 
   TermID number(6), 
   CONSTRAINT COURSE_SECTION_FK2
     Foreign Key (TERMID) references TERM (termID),
   LocID number(6),
   CONSTRAINT COURSE_SECTION_FK3
     Foreign Key (LocID) references Location (locID));

Insert into COURSE (CourseName, CourseID, Credits)
values('Base de données', '1', '3');

INSERT INTO COURSE (COURSENAME, COURSEID, CREDITS)
  VALUES ('ORGANIC CHEMISTRY', 2, 4);

INSERT INTO COURSE (COURSENAME, COURSEID, CREDITS)
  VALUES ('INORGANIC CHEMISTRY', 3, 5);

现在,当您尝试将行插入COURSE_SECTION时,您可以看到违反了哪个约束。 (在这种情况下,它是COURSE_SECTION_FK3,意味着LOCATION表中缺少一行。)

SQLFiddle here

祝你好运。

答案 1 :(得分:0)

COURSE_SECTION有三个外键:COURSE,LOCATION和TERM:

),
FOREIGN KEY (COURSEID) REFERENCES COURSE (COURSEID), 
TermID number(6), 
Foreign Key (TERMID) references TERM (termID),
LocID number(6),
Foreign Key (LocID) references Location (locID)
).

您只是预先填充一个父表COUR,因此通过扣除过程,它是TERM或LOCATION上的外键失败。

在填充子表之前,需要填充所有父表中的行。