当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL删除外键问题小结

MySQL删除外键问题小结

2017年12月12日  | 移动技术网IT编程  | 我要评论

柳暗花明又一村 李好,安徽太和县强拆,泗洪人力资源和社会保障网

mysql:mysql不能删除外键,抱错error on rename of ./ruler/test2child to ./ruler/#sql2-298-92 (errno: 152)

曾经这是mysql的一个bug,但是现在只是mysql的一个不友好的地方。

例子如下:

mysql> alter table ruler.test2child drop foreign key test;
error 1025 : error on rename of ''./ruler/test2child'' to ''./ruler/#sql2-298-8f'' (errno:
152)

这里并不是不能删除外键,而是使用了错误的外键名称,但是mysql报的错实在离谱。
正确的错误信息要如下查询才能知道:

mysql> show innodb status; 
------------------------
latest foreign key error
------------------------
071222 20:48:26 error in dropping of a foreign key constraint of table
"ruler"."test2child",
in sql command
alter table ruler.test2child drop foreign key test
cannot find a constraint with the given id "test".

另外外键的其他操作也有类似情况:

比如外键 新建报错:

can''t create table ''ruler.#sql-298_92'' (errno: 150)
show innodb status; 
------------------------
latest foreign key error
------------------------
071222 20:18:57 error in foreign key constraint of table ruler/#sql-298_8d:
 foreign key (test2code) references ruler.test2 (test) on delete set null:
you have defined a set null condition though some of the
columns are defined as not null.

测试数据库版本为:

5.2.0-falcon-alpha-community-nt

删除外键的做法:

//先删除外键名,再删外键字段(mysql官方提供)

例:

alter table task drop foreign key fk_reference_7;
alter table task drop column `assigned`;

以上所述是小编给大家介绍的mysql删除外键问题小结,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网