ORA-02292: 违反完整约束条件 (用户名.约束名) - 已找到子记录

来源:互联网 发布:国培网络研修成果 编辑:程序博客网 时间:2024/06/10 05:01

今天在删除一个表某条记录时,出现如下错误:
ORA-02292: integrity constraint (CICRO.FK8A82499F4C67C41) violated - child record found
于是查看了相关的主键和约束关系,发现了一些问题。

关于这个错误,oracle官方解决方法是:
Error: orA-02292: integrity constraint <constraint name> violated - child record found

Cause: You tried to Delete a record from a parent table (as referenced by a foreign key), but a record in the child table exists. 

Action: The options to resolve this oracle error are: 
This error commonly occurs when you ha a parent-child relationship established between two tables through a foreign key. You then have tried to delete a value into the parent table, but the corresponding value exists in the child table. 
To correct this problem, you need to update or delete the value into the child table first an


总结一语句话,就是:

不能删除包含主键的行,该主键被用做另一个表的外键。

举个例子如下:

首先创建两个表

Create TABLE supplier 
( supplier_id numeric(10) not null, 
 supplier_name varchar2(50) not null, 
 contact_name varchar2(50),  
 CONSTRAINT supplier_pk PRIMARY KEY (supplier_id) 
); 

Create TABLE products 
( product_id numeric(10) not null, 
 supplier_id numeric(10) not null, 
 CONSTRAINT fk_supplier 
   FOREIGN KEY (supplier_id) 
   REFERENCES supplier (supplier_id) 
); 

接着向两个表写入数据

Insert INTO supplier
(supplier_id, supplier_name, contact_name)
VALUES (1000, 'Microsoft', 'Bill Gates');

Insert INTO products
(product_id, supplier_id)
VALUES (50000, 1000);



尝试删除某条记录:

Delete from supplier
Where supplier_id = 1000;



无法删除,报错:
You would receive the following error message:

ORA-02292: integrity constraint (CICRO.FK8A82499F4C67C41) violated - child record found

尝试下面方法,即可解决:

Delete from products
Where supplier_id = 1000;

Then you can delete from the supplier table:

Delete from supplier
Where supplier_id = 1000;

0 0
原创粉丝点击