SQL ORA-02291:完整性约束 (SYSTEM.SYS_C007150) 违反 - 未找到父密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23318805/
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
ORA-02291:INTEGRITY CONSTRAINT (SYSTEM.SYS_C007150) VIOLATED - PARENT KEY NOT FOUND
提问by joseph hassan
When I create the table EMPLOYEE
, I made ESSN
as a primary key and the SUPERSSN
a foreign key from the same table with DNO
as a foreign key from the dep table, when I want to insert values that show up and now I am confused.
当我创建表EMPLOYEE
,我做了ESSN
一个主键和SUPERSSN
来自同一个表的外键与DNO
从DEP表的外键,当我想插入值,显示了,现在我很困惑。
The table contains the following:
该表包含以下内容:
Name Null? Type
----------------------------------------- -------- --------------
ENAME NOT NULL VARCHAR2(30)
ESSN NOT NULL CHAR(14)
BDATE DATE
DNO NUMBER(38)
SUPERSSN CHAR(14)
in first time I used the following command line:
我第一次使用以下命令行:
INSERT INTO EMPLOYEE ('JOSEPH','789456','14-DEC-1986','3','123');
then I try without DNO
as that:
然后我尝试不DNO
这样做:
SQL> INSERT INTO EMPLOYEE (ENAME,ESSN,BDATE)
2 VALUES('JOSEPH','9861050560','14-DEC-1986');
-------------------------------
INSERT INTO EMPLOYEE (ENAME,ESSN,BDATE)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C007150) violated - parent key not
found
----------------------------
回答by J?cob
Most likely parent_key record which is SUPERSSN
(assuming) must be missing in parent table. You can find that out by
SUPERSSN
父表中最有可能(假设)的parent_key 记录必须丢失。你可以通过
SELECT *
FROM user_constraints
WHERE table_name = 'EMPLOYEE'
So you need to first insert values in parent table of employees table and then insert values in child table.
所以你需要先在员工表的父表中插入值,然后在子表中插入值。
To find out parent_table do as
要找出 parent_table 做
SELECT uc.constraint_name
|| CHR (10)
|| '('
|| ucc1.TABLE_NAME
|| '.'
|| ucc1.column_name
|| ')'
constraint_source,
'REFERENCES'
|| CHR (10)
|| '('
|| ucc2.TABLE_NAME
|| '.'
|| ucc2.column_name
|| ')'
references_column
FROM user_constraints uc, user_cons_columns ucc1, user_cons_columns ucc2
WHERE uc.constraint_name = ucc1.constraint_name
AND uc.r_constraint_name = ucc2.constraint_name
AND ucc1.POSITION = ucc2.POSITION
AND UC.TABLE_NAME = 'EMPLOYEE'
AND uc.constraint_type = 'R'
For more details please have a look at this.
有关更多详细信息,请查看此。
回答by Ausaf
I think the new entry(having foreign key constraint) you are entering is referring to an entry which doesnt exist
我认为您输入的新条目(具有外键约束)是指不存在的条目
回答by user8128167
Here is a query you could use to check to see if the values in your foreign key table exist or not, and of course if they do not exist then they would have to be inserted to resolve the parent key violation:
这是一个查询,您可以用来检查外键表中的值是否存在,当然,如果它们不存在,则必须插入它们以解决父键冲突:
SELECT E.* FROM EMPLOYEE E
LEFT JOIN SUPER_TABLE S
ON E.SUPERSSN = S.SUPERSSN
WHERE S.SUPERSSN IS NULL AND E.SUPERSSN IS NOT NULL;
Of course, that is assuming the problem is with the SUPERSSN key. If you have other foreign keys, then you may have to check those as well.
当然,这是假设问题出在 SUPERSSN 密钥上。如果您有其他外键,那么您可能还需要检查它们。