简体   繁体   中英

I can't drop foreign key in mysql

create table A(id int(9),id2 int(5),primary key(id))engine=innodb;
create table B(ide int(9),ide2 int(5))engine=innodb;
desc B;
alter table B add constraint ide3 foreign key(ide) references A(id);
desc B;
alter table B drop foreign key ide3;
desc B;

There is no key in the first "desc" command. "MUL" is written in the key column in the second "desc" command. "MUL" is still written in the key column in the third "desc" command. There is no error but key still exist, what did i do wrong?

Every foreign key needs an index which can support the foreign key check. When you create a foreign key constraint, and you don't have such index, MySQL will create it for you. When you then drop the foreign key constraint, the index will remain. If you want that index to be removed, you need to do that explicitly.

create table A(id int(9),id2 int(5),primary key(id))engine=innodb;

create table B(ide int(9),ide2 int(5))engine=innodb;

alter table B add constraint ide3 foreign key(ide) references A(id);

alter table B drop foreign key ide3;

alter table B drop index ide3; -- add this line

Demo: http://rextester.com/BFKCL96974

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM