MySQL Removing Some Foreign keys

前端 未结 11 662
太阳男子
太阳男子 2020-11-28 01:55

I have a table whose primary key is used in several other tables and has several foreign keys to other tables.

CREATE TABLE location (
   locationID INT NOT         


        
11条回答
  •  攒了一身酷
    2020-11-28 02:25

    You usually get this error if your tables use the InnoDB engine. In that case you would have to drop the foreign key, and then do the alter table and drop the column.

    But the tricky part is that you can't drop the foreign key using the column name, but instead you would have to find the name used to index it. To find that, issue the following select:

    SHOW CREATE TABLE region; This should show you a row ,at left upper corner click the +option ,the click the full text raio button then click the go .there you will get the name of the index, something like this:

    CONSTRAINT region_ibfk_1 FOREIGN KEY (country_id) REFERENCES country (id) ON DELETE NO ACTION ON UPDATE NO ACTION Now simply issue an:

    alter table region drop foreign key region_ibfk_1;

    or

    more simply just type:- alter table TableName drop foreign key TableName_ibfk_1;

    remember the only thing is to add _ibfk_1 after your tablename to make like this:- TableName_ibfk_1

提交回复
热议问题