How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?

MySQLMySQLi Database

We can remove FOREIGN KEY constraint from a column of an existing table by using DROP keyword along with ALTER TABLE statement.

Syntax

ALTER TABLE table_name DROP FOREIGN KEY constraint_name

Here constraint name is the name of foreign key constraint which we applied while creating the table. If no constraint name is specified then MySQL will provide constraint name which can be checked by SHOW CREATE TABLE statement.

Example

The following query will delete the FOREIGN KEY constraint from ‘orders’ table −

mysql> Alter table orders DROP FOREIGN KEY orders_ibfk_1;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
raja
Published on 19-Feb-2018 10:14:02
Advertisements