ITPub博客

首页 > IT职业 > IT生活 > MySQL删除外键问题小结

MySQL删除外键问题小结

IT生活 作者:emanda 时间:2021-09-09 09:38:49 0 删除 编辑

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

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

例子如下:

?

123 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 commandALTER TABLE ruler.test2child DROP FOREIGN KEY testCannot 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 thecolumns are defined as NOT NULL.

测试数据库版本为:

5.2.0-falcon-alpha-community-nt

删除外键的做法:

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

例:

?

12 alter table task drop foreign key FK_Reference_7;alter table task drop column `assigned`;

以上所述是小编给大家介绍的MySQL删除外键问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1741/viewspace-2811158/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    208
  • 访问量
    2572