添加外键约束时出错

问题描述:

我有以下查询:

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)