oracle ORA-02298 未找到父密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28732053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:47:03  来源:igfitidea点击:

ORA-02298 Parent Keys Not Found?

oraclesqlplus

提问by Robert Skelet'man Bowry

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):

这是代码(http://i.imgur.com/6JBzTiM.jpg):

http://i.imgur.com/6JBzTiM.jpg

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)

我可以在Employees 表中创建主键并将其分配给EmployeeId。但是当尝试将其添加为 WorkPackages 表中的外键时(使用下面的代码)

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

it keeps on coming up with the validation error.

它不断提出验证错误。

What am I doing wrong?

我究竟做错了什么?

回答by Sathyajith Bhat

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.

强制执行此键时,Oracle 会检查 Workpackages 表中存在的所有 employeeid 是否存在于 Employees 表中。

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 NOVALIDATEso that existing data isn't checked, but any new inserts/updates will be validated. See this fiddlefor demo on this.

另一种选择是使用NOVALIDATE以便不检查现有数据,但将验证任何新的插入/更新。有关此问题的演示,请参阅此小提琴

回答by Rainyn

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!

更改表 A 添加约束 A_01 外键 (CODE) 引用 B(CODE);我输入 :insert into B(CODE) values(0); 然后它起作用了!