MySQL - How to DROP CONSTRAINT in MySQL?
In MySQL, there’s no DROP CONSTRAINT
, you have to use DROP FOREIGN KEY
instead:
ALTER TABLE `table_name`
DROP FOREIGN KEY `id_name_fk`;
You might have to drop the index too because simply removing foreign key doesn’t remove the index.
ALTER TABLE `table_name`
DROP INDEX `id_name_fk`;
An alternative to temporarily disable all the foreign keys:
SET FOREIGN_KEY_CHECKS=0;
When you need to turn it on:
SET FOREIGN_KEY_CHECKS=1;
Using TablePlus
In TablePlus, you can remove Foreign Key Constraint on a column from the Table Structure view:
- Open the table structure view (⌘ + ^ + ])
- Click on the
foreign_key
field and select to open the current foreign relationship - From the popup, click on
DELETE
. - Press ⌘ + S to commit changes to the server.
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.