MySQL DROP FOREIGN KEY Constraint

Foreign Key Constraints help you maintain referential integrity in databases, and link related tables for easier querying. Here’s how to drop foreign key constraint using MySQL DROP FOREIGN KEY command.

 

How to DROP FOREIGN KEY Constraint in MySQL

Here are the steps to drop foreign key constraint in MySQL.

Here’s the syntax for DROP FOREIGN KEY statement:

ALTER TABLE table_name
DROP FOREIGN KEY constraint_name;

In the above drop foreign key query, specify table_name from which you want to remove foreign key, in place of table_name. Specify constraint name in place of constraint_name.

Bonus Read : MySQL DROP DATABASE

Here’s an example. Let’s create 2 tables and add foreign key constraint to one of them.

mysql> create table orders3(id int auto_increment primary key,category_id int);

mysql> create table categories(id int auto_increment primary key,name varchar(255));

mysql> ALTER TABLE orders3
     ADD CONSTRAINT fk_cat
     FOREIGN KEY (category_id)
     REFERENCES categories(id);

Bonus Read : MySQL DROP COLUMN

 

How to Drop Foreign Key Constraint

Here’s the SQL query to delete foreign key constraint in MySQL.

mysql> alter table orders3 drop foreign key fk_cat;

 

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!