oracle 添加外键错误:ORA-02298:无法验证 - 未找到父键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42323074/
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 01:12:48  来源:igfitidea点击:

Adding foreign keys error: ORA-02298: cannot validate - parent keys not found

oracle

提问by Orcka

I essentially have 2 tables, one named table1 and another called table2.

我基本上有 2 个表,一个名为 table1,另一个名为 table2。

I want to insert a foreign key into table2 and have it make sure it is linked as a parent to table1 (since both tables have the same column called: EMPNO).

我想在 table2 中插入一个外键,并确保它作为父级链接到 table1(因为两个表都有相同的列,称为:EMPNO)。

This is what I tried:

这是我尝试过的:

ALTER TABLE table2 
ADD FOREIGN KEY (EMPNO) 
REFERENCES table1(EMPNO)  

however I receive an error from my live SQL when running it:

但是我在运行它时从我的实时 SQL 收到一个错误:

ORA-02298: cannot validate (SQL_EOTBMPLTBLKHWFZRYEHITBYIH.) - parent keys not found

ORA-02298: 无法验证 (SQL_EOTBMPLTBLKHWFZRYEHITBYIH.) - 未找到父键

回答by mroach

This error means that table2has foreign key values that do not exist in the parent table, table1. To identify them, run a query like this:

此错误意味着table2具有父表中不存在的外键值,table1. 要识别它们,请运行如下查询:

SELECT DISTINCT EMPNO FROM table2 WHERE EMPNO NOT IN (
    SELECT EMPNO FROM table1)

Fix those bad key values and then run your ALTER TABLEagain.

修复那些错误的键值,然后ALTER TABLE再次运行。