Failed to add MySQL foreign key ERROR1452
BitsCN.com
An ERROR 1452 ERROR occurred while adding foreign keys to MySQL. today, when I learned how to add foreign keys to the database, I first created two tables orders and order_items. the storage engine is InnoDB, all of them have the orderid attribute (the type is the same), but I use the command [SQL] alter table order_items add foreign key (orderid) references orders (orderid ); when a foreign key is added, an ERROR [SQL] ERROR 1452: Cannot add or update a child row: a foreign key constraint fails is returned, it turns out that data already exists in my order_items table, and the orderid attribute does not match in orders. Therefore, if you add a foreign key, an error will occur. the solution is similar to two: 1. delete data and add a foreign key. add corresponding items in orders, and then add the foreign key bitsCN.com