mysql外键错误#1452 [英] mysql foreign key error #1452

查看:140
本文介绍了mysql外键错误#1452的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ALTER TABLE  `groups` ADD FOREIGN KEY (  `company_id` ) REFERENCES  `summaries`.`companies` (

`id`
) ON DELETE CASCADE ;

MySQL said: 

#1452 - Cannot add or update a child row: a foreign key constraint fails (`summaries/#sql-164a_33c`, CONSTRAINT `#sql-164a_33c_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE) 

companies.idprimary auto increment int(11)

company_idindex int(11)

我不明白该错误消息.谁能对此有所启发?

I don't understand the error message. Can anyone shed some light on this?

推荐答案

这意味着子表中至少有一行引用了父表中不存在的行.

That means you have at least one row in the child table that references a non-existent row in the parent table.

如果您完全确定可以解决这样的数据完整性问题,则可以在运行ALTER TABLE命令之前通过禁用外键检查来添加外键:

If you are absolutely sure that you are okay with having a data integrity issue like that, you can add the foreign key by disabling foreign key checks before you run the ALTER TABLE command:

SET FOREIGN_KEY_CHECKS = 0;

这篇关于mysql外键错误#1452的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆