我收到此错误消息:
第 40 行出现错误 1217 (23000):无法
删除或更新父行:a
外键约束失败
...当我尝试删除表时:
DROP TABLE IF EXISTS `area`;
...定义如下:
CREATE TABLE `area` (
`area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
`nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
有趣的是,我已经删除了模式中具有针对 >区域
。实际上,除了area
表之外,数据库是空的。
如果数据库中没有任何其他对象,它怎么可能有子行?据我所知,InnoDB 不允许在其他模式上使用外键,是吗?
(我什至可以运行 RENAME TABLE area TO Something_else
命令:-?)
I get this error message:
ERROR 1217 (23000) at line 40: Cannot
delete or update a parent row: a
foreign key constraint fails
... when I try to drop a table:
DROP TABLE IF EXISTS `area`;
... defined like this:
CREATE TABLE `area` (
`area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
`nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
The funny thing is that I already dropped all other tables in the schema that have foreign keys against area
. Actually, the database is empty except for the area
table.
How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?
(I can even run a RENAME TABLE area TO something_else
command :-?)
发布评论
评论(9)
按需,现在作为答案...
使用 MySQL 查询浏览器或 phpMyAdmin 时,似乎为每个查询打开一个新连接(bugs.mysql.com/bug.php?id=8280),因此有必要在一个查询中编写所有 drop 语句,例如:
其中
SET FOREIGN_KEY_CHECKS=1
作为额外的安全措施......On demand, now as an answer...
When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (bugs.mysql.com/bug.php?id=8280), making it neccessary to write all the drop statements in one query, eg.
Where the
SET FOREIGN_KEY_CHECKS=1
serves as an extra security measure...两种可能性:
您可以通过在删除失败后执行“SHOW ENGINE INNODB STATUS”来查看它是哪个表(无论如何,其中之一)。
如果事实证明是后一种情况,如果可以的话,我会转储并恢复整个服务器。
MySQL 5.1 及更高版本将在错误消息中为您提供带有 FK 的表名称。
Two possibilities:
You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.
If it turns out to be the latter case, I'd dump and restore the whole server if you can.
MySQL 5.1 and above will give you the name of the table with the FK in the error message.
禁用外键检查
Disable foreign key checking
来自此博客:
您可以暂时禁用外键检查:
只需确保在完成后恢复它们乱搞:
from this blog:
You can temporarily disable foreign key checks:
Just be sure to restore them once you’re done messing around:
希望它的工作
SETforeign_key_checks = 0;
DROP TABLE
表名
;设置foreign_key_checks = 1;
hopefully its work
SET foreign_key_checks = 0;
DROP TABLE
table name
;SET foreign_key_checks = 1;
在 Rails 上,可以使用
rails 控制台
执行以下操作:On Rails, one can do the following using the
rails console
:也许您之前使用此表时收到错误。您可以重命名该表并尝试再次删除它。
Maybe you received an error when working with this table before. You can rename the table and try to remove it again.
我找到了一个简单的解决方案,导出数据库,在文本编辑器中编辑您想要编辑的内容,然后导入它。完毕
i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done
无法删除或更新父行:外键约束失败 (
table1
.user_role
, CONSTRAINTFK143BF46A8dsfsfds@#5A6BD60
FOREIGN KEY (user_id
) 参考user
(id
))我通过两个简单的步骤做了什么。首先我删除子表中的子行,例如
mysql>从 table2 中删除,其中 role_id = 2 &&用户id=20;
查询正常,1 行受影响(0.10 秒)
,第二步是
从 table1 中删除父级删除,其中 id = 20;
查询正常,1 行受影响(0.12 秒)
通过这个我解决了问题,这意味着删除子项然后删除父项
我希望你明白了。 :)
Cannot delete or update a parent row: a foreign key constraint fails (
table1
.user_role
, CONSTRAINTFK143BF46A8dsfsfds@#5A6BD60
FOREIGN KEY (user_id
) REFERENCESuser
(id
))What i did in two simple steps . first i delete the child row in child table like
mysql> delete from table2 where role_id = 2 && user_id =20;
Query OK, 1 row affected (0.10 sec)
and second step as deleting the parent
delete from table1 where id = 20;
Query OK, 1 row affected (0.12 sec)
By this i solve the Problem which means Delete Child then Delete parent
i Hope You got it. :)