Kevin
Kevin

Reputation: 33

MySQL error 1452 (23000) cannot add or update a child row a foreign key constraint fails

This is what I entered for the tables

mysql> create table A6M351kjp.Claim (ClaimID INT (10) NOT NULL, VIN INT (10) NOT NULL, ClaimDate DateTime Null, ClaimStatus VarChar(45) Null, Primary Key (ClaimID));

added foreign key here

mysql> alter table A6M351kjp.Claim add foreign key (VIN) references Car(VIN);

enter data

mysql> insert into A6M351kjp.Claim (ClaimID, VIN, ClaimDate, ClaimStatus) values (101, 901, '2014-01-01', 'Open');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`A6M351kjp`.`Claim`, CONSTRAINT `Claim_ibfk_1` FOREIGN KEY (`VIN`) REFERENCES `Car` (`VIN`))

what is cause of this error and how can I fix this?

Thanks

Upvotes: 2

Views: 3716

Answers (1)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

901 doesn't exist in the Car table. As it is referenced as foreign key, needs to be there in the parent table.

Upvotes: 4

Related Questions