mysqldude

Drop foreign key constraint

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Generated constraint name is child_ibfk_1 (original)
original post
✓✓✓✓✓✓✓✓✓
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1 (original)
original post
✓✓✓✓✓✓✓✓✓
The supporting index par_ind stays after the FK is dropped
version check
✓✓✓✓✓✓✓✓✓
SHOW CREATE TABLE prints int(11) as in the post (dropped in MySQL 8.0.19+)
version check
✓✓≠≠≠✓✓✓✓
ALTER TABLE ... DROP CONSTRAINT child_ibfk_1 (MySQL 8.0.19+, MariaDB 10.2+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Generated constraint name is child_ibfk_1 (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1 (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
The supporting index par_ind stays after the FK is dropped version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
SHOW CREATE TABLE prints int(11) as in the post (dropped in MySQL 8.0.19+) version check
MySQL 5.6✓5.7✓8.0≠8.4≠9≠
MariaDB 10.6✓10.11✓11.4✓11.8✓
ALTER TABLE ... DROP CONSTRAINT child_ibfk_1 (MySQL 8.0.19+, MariaDB 10.2+) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • SHOW CREATE TABLE prints int(11) as in the post (dropped in MySQL 8.0.19+) (MySQL 8.0, MySQL 8.4, MySQL 9): rows differ
  • ALTER TABLE ... DROP CONSTRAINT child_ibfk_1 (MySQL 8.0.19+, MariaDB 10.2+) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT child_ibfk_1' at line 1

The original article

First published 2011-03-15. The text below is the original; the table above shows how it behaves on current versions.

In MySQL there is no single command that returns the constraints existing on a table other than using the SHOW CREATE TABLE command. This command provides the SQL necessary to recreate the table. The create statement will also include the names of the referential and primary key constraints. Using the ALTER TABLE command these can then be dropped.

Here is an example:

mysql> CREATE TABLE parent (id INT NOT NULL,
    ->                      PRIMARY KEY (id)
    -> ) ENGINE=INNODB;
Query OK, 0 rows affected (0.07 sec)

mysql> CREATE TABLE child (id INT, parent_id INT,
    ->                     INDEX par_ind (parent_id),
    ->                     FOREIGN KEY (parent_id) REFERENCES parent(id)
    ->                       ON DELETE CASCADE
    -> ) ENGINE=INNODB;
Query OK, 0 rows affected (0.04 sec)

mysql> show create table child;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                     |
+-------+---------------------------------------------------------------------------------------------------------------------+
| child | CREATE TABLE `child` (
  `id` int(11) default NULL,
  `parent_id` int(11) default NULL,
  KEY `par_ind` (`parent_id`),
  CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table child drop foreign key child_ibfk_1;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0