本文介绍了添加外键约束时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下查询:
ALTER TABLE ROUTE ADD FOREIGN KEY (RID) REFERENCES RESERVATION(RID) ON DELETE CASCADE
但是它会产生一个错误:
but it generates me an error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`SmarTrek`.`#sql-91e_d09`, CONSTRAINT `FK_RID` FOREIGN KEY (`RID`) REFERENCES `RESERVATION` (`RID`) ON DELETE CASCADE)
在设计器模式下,外观如下:
In designer mode, here's what it looks like:
推荐答案
这意味着您已经在ROUTE表中拥有不满足外键约束的数据.
That would meant that you already have data in the ROUTE table that does not satisfy the foreign key constraint.
要查找有问题的记录,以便将其更新为其他值(存在),可以使用
To find the offending records, so you can update them to some other value (that exists), you can use
select *
from route
where rid not in (select rid from reservation)
这篇关于添加外键约束时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!