主键与外键之ORA-02292: integrity constraint

今天在删除一个表某条记录时,出现如下错误:
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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值