Home > Database > Mysql Tutorial > MySQL添加外键失败ERROR 1452的解决_MySQL

MySQL添加外键失败ERROR 1452的解决_MySQL

WBOY
Release: 2016-06-01 13:48:00
Original
4494 people have browsed it

Mysql外键

bitsCN.com
MySQL添加外键失败ERROR 1452的解决 今天在学习数据库添加外键的时候,遇到了问题我先创建了两个表  orders 和 order_items  ,存储引擎都是InnoDB,且都有orderid这个属性(类型完全一样),但是我使用命令 [sql] alter table order_items   add foreign key (orderid) references orders (orderid);     添加外键的时候 出现了错误[sql] ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails    最后才发现,原来是我的order_items表中已经存在了数据,且orderid这个属性和orders中的不对应,因此如果添加外键,就会导致错误此时解决的办法不外乎两个: 1.删除数据,再添加外键2.在orders中添加对应的项,再添加外键 bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template