Alter table to remove Foreign Key or add DELETE CASCADE (MySQL)

Step 1 : Get the Foreign Key Name.

SHOW CREATE TABLE tableName;

Note the name of Foreign key (which is mostly auto generated) output will look something like


CONSTRAINT `FK4C5B93445F11A0B7` FOREIGN KEY (ID`) REFERENCES `PARENT_TABLE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Step 2: Drop the Foreign Key.

Alter table tableName drop foreign key FK4C5B93445F11A0B7

Livewire Component Library

Dont worry, this will not harm your data. It will just remove the constraint. You can add it back later

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.

Step 3: Now add the foreign key constraint back again, this time with ON DELETE CASCADE

alter table tableName add foreign key (ID) references PARENT_TABLE(ID) on DELETE CASCADE

There you Go! You can run SHOW CREATE TABLE tableName; to verify on DELETE CASCADE

screen-shot-2016-12-20-at-12-16-09-pm

Site Footer