为什么找不到父密钥?

时间:2016-12-10 14:05:46

标签: sql oracle foreign-keys primary-key

当我将数据插入ASSIGNMENTS表时,除了HARDWARE和SOFTWARE值之外,它们都能正常工作,它们的编码方式与其他值完全相同。我只是不明白。

这有效:

INSERT INTO ASSIGNMENTS (ASSIGNMENT_ID, PROJECT_ID, STAFF_ID, JOB_ID)
VALUES ('A0005','B0002','ST002','J0002');

但是当我尝试为HARDWARE或SOFTWARE包含值时,就像这样:

INSERT INTO ASSIGNMENTS (ASSIGNMENT_ID, PROJECT_ID, STAFF_ID, JOB_ID, HARDWARE_ID)
VALUES ('A0005','B0002','ST002','J0002','H0002');

我刚收到以下错误: SQL错误:ORA-02291:违反了完整性约束(JAS1224.SYS_C0028418) - 未找到父键 02291. 00000 - "违反完整性约束(%s。%s) - 未找到父密钥" *原因:外键值没有匹配的主键值。 *操作:删除外键或添加匹配的主键。

以下是所有代码,并且所有表都已正确填充(硬件和软件表的编码格式与人员表完全相同):

CREATE TABLE PROJECT
(PROJECT_ID CHAR(5) NOT NULL,
PROJECT_NAME CHAR(20),
PROJECT_TYPE CHAR(20),
START_DATE DATE,
END_DATE DATE,
PRIMARY KEY (PROJECT_ID));

CREATE TABLE HARDWARE
(HARDWARE_ID CHAR(5) NOT NULL,
HARDWARE_NAME CHAR(20),
PRIMARY KEY (HARDWARE_ID));

CREATE TABLE SOFTWARE
(SOFTWARE_ID CHAR(5) NOT NULL,
SOFTWARE_NAME CHAR(20),
PRIMARY KEY (SOFTWARE_ID));

CREATE TABLE STAFF
(STAFF_ID CHAR(5) NOT NULL,
STAFF_NAME CHAR(20),
PRIMARY KEY (STAFF_ID));

CREATE TABLE JOB
(JOB_ID CHAR(5) NOT NULL,
JOB_TYPE CHAR(20),
JOB_GRADE CHAR(20),
PRIMARY KEY (JOB_ID));

CREATE TABLE ASSIGNMENTS
(ASSIGNMENT_ID CHAR(5) NOT NULL,
PROJECT_ID CHAR(5),
STAFF_ID CHAR(5),
JOB_ID CHAR(5),
HARDWARE_ID CHAR(5),
SOFTWARE_ID CHAR(5),
PRIMARY KEY (ASSIGNMENT_ID),
FOREIGN KEY (PROJECT_ID) REFERENCES PROJECT(PROJECT_ID),
FOREIGN KEY (STAFF_ID) REFERENCES STAFF(STAFF_ID),
FOREIGN KEY (JOB_ID) REFERENCES JOB(JOB_ID),
FOREIGN KEY (HARDWARE_ID) REFERENCES HARDWARE(HARDWARE_ID),
FOREIGN KEY (SOFTWARE_ID) REFERENCES SOFTWARE(SOFTWARE_ID));

1 个答案:

答案 0 :(得分:0)

错误信息起初可能有点神秘,但很明显。

两个INSERT之间的唯一区别是HARDWARE_ID。因此'H0002'不是有效的HARDWARE_ID。它不在HARDWARE表中。