可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!