简体   繁体   English

添加外键约束时出错

[英]error adding a foreign key constraint

I have the following query: 我有以下查询:

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: 在设计器模式下,外观如下: 在此处输入图片说明

That would meant that you already have data in the ROUTE table that does not satisfy the foreign key constraint. 这意味着您已经在ROUTE表中拥有不满足外键约束的数据。

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)

THERE may b 2 reasons 可能有2个原因

  • there may b some row in ROUTE TABLE which have RID that does not exist in RESERVATION(RID) ROUTE TABLE中可能有一些行的RIDRESERVATION(RID)中不存在
  • or check the DATATYPE OF ROUTE (RID) & RESERVATION(RID) both should be same ( unsigned/signed) 或检查ROUTE (RID)RESERVATION(RID)数据类型应相同(未签名/已签名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM