MySQL Removing Some Foreign keys

前端 未结 11 621
太阳男子
太阳男子 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:09

    Here's a way to drop foreign key constraint, it will work. ALTER TABLE location.location_id DROP FOREIGN KEY location_ibfk_1;

    0 讨论(0)
  • 2020-11-28 02:09

    step1: show create table vendor_locations;

    step2: ALTER TABLE vendor_locations drop foreign key vendor_locations_ibfk_1;

    it worked for me.

    0 讨论(0)
  • 2020-11-28 02:11

    Hey I followed some sequence above, and found some solution.

    SHOW CREATE TABLE footable;
    

    You will get FK Constrain Name like

    ProjectsInfo_ibfk_1
    

    Now you need to remove this constraints. by alter table commantd

    alter table ProjectsInfo drop foreign key ProjectsInfo_ibfk_1;
    

    Then drop the table column,

    alter table ProjectsInfo drop column clientId;
    
    0 讨论(0)
  • 2020-11-28 02:18

    As everyone said above, you can easily delete a FK. However, I just noticed that it can be necessary to drop the KEY itself at some point. If you have any error message to create another index like the last one, I mean with the same name, it would be useful dropping everything related to that index.

    ALTER TABLE your_table_with_fk
      drop FOREIGN KEY name_of_your_fk_from_show_create_table_command_result,
      drop KEY the_same_name_as_above
    
    0 讨论(0)
  • 2020-11-28 02:19

    Try this:

    alter table Documents drop
      FK__Documents__Custo__2A4B4B5E
    
    0 讨论(0)
  • 2020-11-28 02:20

    You can not drop the foreign key column because it is being referenced from the table assignmentStuff. So you should first drop the foreign key constraint assignmentStuff.assignmentIDX.

    A similar question has already been asked here. Check also here for more info.

    0 讨论(0)
提交回复
热议问题