ORA-02298 Parent Keys Not Found?

戏子无情 提交于 2019-12-23 18:02:28

问题


I've been stuck on this for ages as every time I try it - it comes up with the following error: Cannot Validate - parent keys not found.

Here's the code (http://i.imgur.com/6JBzTiM.jpg):

I can create the Primary Key in the Employees table and assign it to EmployeeId. But when trying to add that as a foreign key in the WorkPackages table (using the code below)

ALTER TABLE WORKPACKAGES
ADD FOREIGN KEY (EMPLOYEEID) REFERENCES EMPLOYEES (EMPLOYEEID);

it keeps on coming up with the validation error.

What am I doing wrong?


回答1:


ALTER TABLE WORKPACKAGES
ADD FOREIGN KEY (EMPLOYEEID) REFERENCES EMPLOYEES (EMPLOYEEID);

When this key is enforced, Oracle checks that all employeeid present in Workpackages table is present in Employees table.

Your options:

Find the offending keys by running

SELECT employeeid
FROM   workpackages
WHERE  employeeid NOT IN (SELECT employeeid
                          FROM   employees); 

and then insert them into the employee table.

Another option is to use NOVALIDATE so that existing data isn't checked, but any new inserts/updates will be validated. See this fiddle for demo on this.




回答2:


The same problem puzzled me a lot when I am trying to add constraint foreign key.Now I have made it.I key in some values in the father table.For example:

alter table A add constraint A_01 foreign key (CODE) references B(CODE); I key in :insert into B(CODE) values(0); and then it works!



来源:https://stackoverflow.com/questions/28732053/ora-02298-parent-keys-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!