Failed to add foreign key for MySQL solution for ERROR1452 _ MySQL-mysql tutorial

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.