简体   繁体   中英

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.

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

  • there may b some row in ROUTE TABLE which have RID that does not exist in RESERVATION(RID)
  • or check the DATATYPE OF ROUTE (RID) & RESERVATION(RID) both should be same ( unsigned/signed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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