MySQL
database management
foreign key constraint
index
SQL error

MySQL "Cannot drop index needed in a foreign key constraint" (error 1553)

Codemia Knowledge Hub ·

MySQL error 1553 ("Cannot drop index 'index_name': needed in a foreign key constraint") means the index you are dropping is the one MySQL uses to enforce a foreign key. MySQL requires an index on the foreign key columns of both parent and child tables, so it blocks the drop to protect referential integrity. Drop the foreign key constraint first, then the index, and recreate the constraint afterward if you still need it.

Why MySQL needs an index for every foreign key

PostgreSQL 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 creates an index on the child table's foreign key columns if none exists, and uses it to check constraint violations efficiently on inserts, updates and deletes.

The parent table's referenced columns must also be indexed. Usually that is the primary key, but any unique index works.

sql
1CREATE TABLE customers (
2    customer_id INT PRIMARY KEY,
3    name VARCHAR(100) NOT NULL,
4    email VARCHAR(255)
5);
6
7CREATE TABLE orders (
8    order_id INT PRIMARY KEY,
9    order_date DATE NOT NULL,
10    customer_id INT,
11    INDEX idx_orders_customer_id (customer_id),
12    CONSTRAINT fk_orders_customer
13        FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
14);

Here idx_orders_customer_id supports the foreign key fk_orders_customer, so dropping it triggers error 1553.

Reproducing the error

sql
-- This fails with ERROR 1553
ALTER TABLE orders DROP INDEX idx_orders_customer_id;
text
ERROR 1553 (HY000): Cannot drop index 'idx_orders_customer_id': needed in a foreign key constraint

Removing the index would leave the constraint without the index it needs.

Fixing it step by step

Step 1: Find the foreign key constraint

Find which foreign keys depend on the index with INFORMATION_SCHEMA:

sql
1SELECT
2    CONSTRAINT_NAME,
3    TABLE_NAME,
4    COLUMN_NAME,
5    REFERENCED_TABLE_NAME,
6    REFERENCED_COLUMN_NAME
7FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
8WHERE TABLE_SCHEMA = 'your_database'
9  AND TABLE_NAME = 'orders'
10  AND REFERENCED_TABLE_NAME IS NOT NULL;

This returns the constraint name (e.g., fk_orders_customer) you need next.

For a quick view, use SHOW CREATE TABLE:

sql
SHOW CREATE TABLE orders;

The output lists all constraints and indexes, so you can see which index belongs to which foreign key.

Step 2: Drop the foreign key constraint

sql
ALTER TABLE orders DROP FOREIGN KEY fk_orders_customer;

This removes the constraint but leaves the index. MySQL does not drop the index automatically.

Step 3: Drop the index

The index is no longer required by any constraint:

sql
ALTER TABLE orders DROP INDEX idx_orders_customer_id;

Step 4: Recreate the foreign key (if needed)

If you still need referential integrity and only wanted to replace the index (for example, with a composite index), recreate the constraint:

sql
1-- Create a new composite index
2ALTER TABLE orders ADD INDEX idx_orders_customer_date (customer_id, order_date);
3
4-- Recreate the foreign key using the new index
5ALTER TABLE orders ADD CONSTRAINT fk_orders_customer
6    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
7    ON DELETE CASCADE
8    ON UPDATE CASCADE;

MySQL uses the new composite index for the foreign key as long as the foreign key columns are a leftmost prefix of the index.

Doing it in a single ALTER TABLE

Combining the operations in one ALTER TABLE lets MySQL process the table in a single operation:

sql
1ALTER TABLE orders
2    DROP FOREIGN KEY fk_orders_customer,
3    DROP INDEX idx_orders_customer_id,
4    ADD INDEX idx_orders_customer_date (customer_id, order_date),
5    ADD CONSTRAINT fk_orders_customer_v2
6        FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
7        ON DELETE CASCADE;

The new constraint needs a different name: reusing fk_orders_customer in the same statement that drops it fails with ERROR 1826 (duplicate foreign key constraint name).

On large tables this reduces downtime, since InnoDB handles one table operation instead of four separate ones. Adding a foreign key with foreign_key_checks on still uses the table-copy algorithm, so expect that part to take time.

FOREIGN_KEY_CHECKS does not get around it

Disabling foreign key checks looks like a shortcut, but MySQL 8.0 still refuses the drop:

sql
1SET FOREIGN_KEY_CHECKS = 0;
2
3-- Still fails with ERROR 1553 on MySQL 8.0
4ALTER TABLE orders DROP INDEX idx_orders_customer_id;
5
6SET FOREIGN_KEY_CHECKS = 1;

FOREIGN_KEY_CHECKS skips row-level checks on inserts, updates and deletes; it does not let an index that backs a constraint disappear. Drop the foreign key first, as shown above.

Listing every foreign key in a database

When refactoring indexes across many tables, see all foreign key relationships at once:

sql
1SELECT
2    kcu.TABLE_NAME AS child_table,
3    kcu.COLUMN_NAME AS child_column,
4    kcu.CONSTRAINT_NAME AS fk_name,
5    kcu.REFERENCED_TABLE_NAME AS parent_table,
6    kcu.REFERENCED_COLUMN_NAME AS parent_column,
7    rc.UPDATE_RULE,
8    rc.DELETE_RULE
9FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
10JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
11    ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
12    AND kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
13WHERE kcu.TABLE_SCHEMA = 'your_database'
14  AND kcu.REFERENCED_TABLE_NAME IS NOT NULL
15ORDER BY kcu.TABLE_NAME, kcu.CONSTRAINT_NAME;

This maps every foreign key, its columns and its cascade rules, which you need before changing indexes.

Which indexes can back a foreign key

RuleExampleSupported?
Exact match on FK columnsINDEX (customer_id) for FK on customer_idYes
Leftmost prefix of composite indexINDEX (customer_id, order_date) for FK on customer_idYes
Non-leftmost column of composite indexINDEX (order_date, customer_id) for FK on customer_idNo
Unique indexUNIQUE (customer_id) for FK on customer_idYes
Primary keyPRIMARY KEY (customer_id) for FK on customer_idYes

A composite index satisfies a foreign key only if the FK columns are a leftmost prefix: INDEX (a, b) works for a foreign key on (a) but not on (b).

Mistakes when dropping foreign key indexes

Dropping the index before the foreign key. MySQL enforces the order strictly. Drop the foreign key first, or combine both in one ALTER TABLE.

Expecting MySQL to drop the index with the foreign key. It doesn't. The auto-created index stays and must be removed separately if you no longer need it.

Leaving FOREIGN_KEY_CHECKS = 0 on. The setting is session-scoped, so other sessions are unaffected, but data inserted during that session is not validated.

Not checking which index the foreign key uses. With several candidate indexes (e.g., INDEX (customer_id) and INDEX (customer_id, status)), MySQL uses one of them. Dropping the other may succeed while dropping the chosen one triggers the error. SHOW CREATE TABLE shows which is associated with the constraint.

Forgetting cascade rules when recreating. Specify ON DELETE and ON UPDATE again. The default is RESTRICT, which may differ from the original constraint.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design