ORA-02291:parent key not found

来源:互联网 发布:自己的淘宝等级怎么看 编辑:程序博客网 时间:2024/05/22 00:06


Hibernate operation: Could not execute JDBC batch update; SQL [insert into

dchnpricecarchancesource (inpricecard_id, pricecard_id, count, sumcount, source_code, reason_code,

ingroup_id, op_login, op_groupid, op_time, change_source, memo1, memo2, change_id) values (?, ?,

?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; ORA-02291: integrity constraint

(DBCHNADM.FK_DCHNPRIC_REFERENCE_DCHNPRIC) violated - parent key not found ; nested exception is

java.sql.BatchUpdateException: ORA-02291: integrity constraint

(DBCHNADM.FK_DCHNPRIC_REFERENCE_DCHNPRIC) violated - parent key not found

---------------------------------------------------------------------------------------------------------------------------

参看了这本书第八章:
《Oracle Database 11g SQL开发指南》<oracle database 11g SQL Master SQL and PL/SQL in the Oracle Database>

8.5.2  外键约束

所谓外键关系就是一个表中的列引用了其他表中的列。例如,products表中的product_type_id列引用了

product_types表中的product_type_id列。product_types表称为父表(parent table),而products表则称为子

表(child table),这是因为products表中的product_type_id列依赖于product_types表中的product_type_id

列。

如果试图向products表中插入一行,但此行的product_type_id不存在,数据库就会返回ORA-02291错误。这个错

误说明数据库无法找到一个匹配的父键值(此处父键就是product_types表中的product_type_id列)。在下面这个

例子中,就返回这样一个错误,因为product_types表中不存在product_type_id为6的行:

SQL> INSERT INTO products (
2     product_id, product_type_id, name, description, price
3   ) VALUES (
4    13, 6, 'Test', 'Test', NULL
5   );
INSERT INTO products (
*
ERROR at line 1:
ORA-02291: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES)
violated - parent key not found

同理,如果试图将products表中一行的product_type_id列设置为一个不存在的父键值,数据库也会返回相同的

错误。例如:

SQL> UPDATE products
2   SET product_type_id = 6
3   WHERE product_id = 1;
UPDATE products
*
ERROR at line 1:
ORA-02291: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES)
violated - parent key not found

如果试图从父表中删除已经有依赖子行的一行,数据库就会返回ORA-02292错误。例如,如果试图删除

product_types表中 product_type_id列为1的行,数据库就会返回ORA-02292错误,因为products表中包含了

product_type_id列等于1的行:

SQL> DELETE FROM product_types
2   WHERE product_type_id = 1;
DELETE FROM product_types
*
ERROR at line 1:
ORA-02292: integrity constraint (STORE.PRODUCTS_FK_PRODUCT_TYPES)
violated - child record found

如果数据库允许执行这个删除操作,那么子行就无效了,因为它们不能指向父表中的有效值了。

---------------------------------------------------------------------------------------------------------------------

后来发现是在表中外键设置错误造成的,引以为戒:

 

 

 

转自:http://hi.baidu.com/skyforum/blog/item/37611a2e25a8205a4ec2262f.html

原创粉丝点击