MySQL表外键删除报错

错误代码:

1091 Can't DROP 'category_id'; check that column/key exists

ERROR 1553 (HY000): Cannot drop index 'category_id': needed in a foreign key constraint

ERROR 1828 (HY000): Cannot drop column 'category_id': needed in a foreign key constraint 'user_ibfk_1'

在我删除外键列表后,我查看了一下表,外键仍在!

image-20211105175151150

于是我再删一遍

1
2
alter table user
DROP FOREIGN KEY category_id;

image-20211105175300339

报错认为该主键已不存在!

我们用show index from user来看看

image-20211105175549937

外键category_id竟然还存在

我们show create table user来看一看

image-20211105181755803

发现外键被CONSTRAINT user_ibfk_1 给约束住了

也就是说MYSQL给外键上了锁,不让你随便删去

我们删除这个约束再删除外键

image-20211105182118635

OK 成功!😘