MySQL Cannot drop index needed in a foreign key constraint
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
MySQL error 1553 ("Cannot drop index 'index_name': needed in a foreign key constraint") means you are trying to remove an index that MySQL is currently using to enforce a foreign key relationship. MySQL requires an index on the foreign key columns of both the parent and child tables. If you try to drop that index while the foreign key constraint still references it, MySQL blocks the operation to protect referential integrity. The fix is to drop the foreign key constraint first, then drop the index, and optionally recreate the constraint afterward.
Why MySQL Requires Indexes for Foreign Keys
Unlike PostgreSQL, which can enforce foreign keys without a dedicated index (though it recommends one for performance), MySQL's InnoDB engine requires an index on the referencing columns. When you create a foreign key, InnoDB automatically creates an index on the child table's foreign key columns if one does not already exist. This index is used to efficiently check constraint violations during inserts, updates, and deletes.
The parent table's referenced columns must also be indexed. Typically this is the primary key, but it can be any unique index.
In this example, idx_orders_customer_id is the index that supports the foreign key fk_orders_customer. Dropping this index triggers error 1553.
Reproducing the Error
MySQL is telling you that removing the index would leave the foreign key constraint without the index it needs to function.
Step-by-Step Fix
Step 1: Identify the Foreign Key Constraint
Before dropping anything, find out which foreign keys depend on the index. Use INFORMATION_SCHEMA to query the relationships:
This returns the constraint name (e.g., fk_orders_customer), which you need for the next step.
You can also use SHOW CREATE TABLE for a quick view:
The output includes all constraints and indexes, making it easy to see which index belongs to which foreign key.
Step 2: Drop the Foreign Key Constraint
This removes the constraint but leaves the index in place. MySQL does not automatically drop the index when you drop the foreign key.
Step 3: Drop the Index
Now the index is no longer required by any constraint:
Step 4: Recreate the Foreign Key (If Needed)
If you still need the referential integrity but wanted to replace the index (e.g., changing it to a composite index), recreate the constraint:
MySQL will use the new composite index to enforce the foreign key, as long as the foreign key columns are a leftmost prefix of the index.
Doing It in a Single ALTER TABLE
MySQL allows combining multiple operations in one ALTER TABLE statement, which is faster because it only rebuilds the table once:
This approach reduces downtime on large tables because InnoDB performs a single table rebuild instead of four separate ones.
Using FOREIGN_KEY_CHECKS as a Last Resort
In maintenance windows or migrations, you can temporarily disable foreign key checks:
This bypasses the protection and lets you drop the index without first removing the constraint. However, this is risky. If you forget to recreate the supporting index, queries that rely on the foreign key relationship will perform full table scans, and data integrity checks will be less efficient.
Only use this approach during controlled migrations, never in application code.
Finding All Foreign Key Dependencies in a Database
When you are refactoring indexes across multiple tables, it helps to see all foreign key relationships at once:
This gives you a complete map of foreign keys, their supporting columns, and the cascade rules, which is essential before making index changes.
Index Selection Rules for Foreign Keys
MySQL selects an index for a foreign key based on specific rules. Understanding these prevents surprises:
| Rule | Example | Supported? |
| Exact match on FK columns | INDEX (customer_id) for FK on customer_id | Yes |
| Leftmost prefix of composite index | INDEX (customer_id, order_date) for FK on customer_id | Yes |
| Non-leftmost column of composite index | INDEX (order_date, customer_id) for FK on customer_id | No |
| Unique index | UNIQUE (customer_id) for FK on customer_id | Yes |
| Primary key | PRIMARY KEY (customer_id) for FK on customer_id | Yes |
The key takeaway: a composite index only satisfies a foreign key if the FK columns are a leftmost prefix of the index columns. INDEX (a, b) works for a foreign key on (a) but not for a foreign key on (b).
Common Pitfalls
Trying to drop the index before the foreign key. MySQL enforces the dependency strictly. Always drop the foreign key first, then the index. Or combine both in a single ALTER TABLE statement.
Assuming MySQL auto-drops the index when you drop the foreign key. It does not. After dropping a foreign key, the auto-created index remains and must be removed separately if you no longer need it.
Using FOREIGN_KEY_CHECKS = 0 without re-enabling it. If your session ends abnormally or you forget to reset it, subsequent sessions are not affected (it is session-scoped), but data inserted during that session will not be validated.
Not checking which index the foreign key uses. If you have multiple indexes that could satisfy the foreign key (e.g., both INDEX (customer_id) and INDEX (customer_id, status)), MySQL uses one of them. Dropping the "wrong" one might succeed while dropping the one MySQL chose triggers the error. Use SHOW CREATE TABLE to see which index is associated with the constraint.
Forgetting cascade rules when recreating. When you drop and recreate a foreign key, remember to specify ON DELETE and ON UPDATE rules. The default is RESTRICT, which may differ from what the original constraint had.
Summary
- Error 1553 means the index is required by a foreign key constraint and cannot be dropped independently.
- Drop the foreign key first with
ALTER TABLE ... DROP FOREIGN KEY, then drop the index. - Combine both operations in a single
ALTER TABLEstatement to minimize table rebuilds on large tables. - Use
INFORMATION_SCHEMA.KEY_COLUMN_USAGEorSHOW CREATE TABLEto identify which constraints depend on which indexes. - A composite index satisfies a foreign key only if the FK columns are a leftmost prefix of the index.
SET FOREIGN_KEY_CHECKS = 0is a last resort for controlled migrations, not regular application use.- MySQL does not auto-drop indexes when you drop foreign keys. Clean up orphaned indexes manually.
Related reading
- MySQL Can't create table errno 150
- MySQL case sensitive query
- mysql CHANGE MASTER TO command's MASTER_HOST's length limitation
- mysql check collation of a table
- MySQL Cloning a MySQL database on the same MySql instance
- MySQL combine two columns into one column
- mysql command for showing current configuration variables
- Mysql command not found in OS X 10.7

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.