23 Aug 2011

Drop Foreign Key Constraint using INNODB

I recently needed to make some database modifications that required removing some existing foreign key constraints. The command looks like this.

ALTER table tablename DROP FOREIGN KEY foreign_key_name

MySQL was throwing errors.

mysql> ALTER TABLE reviews DROP FOREIGN KEY priority_id;
ERROR 1025 (HY000): Error on rename of ‘./dbname/reviews’ to ‘./dbname/#sql2-62f5-145b’ (errno: 152)

If you run

SHOW CREATE TABLE tablename

You’ll see the constraint names. You can then delete the foreign key based on the constraint.

ALTER TABLE tablename DROP FOREIGN KEY `reviews_ibfk_24`;

You can then drop the column as needed.

Leave a Reply