// MySQL error reference

MySQL error 1061: Duplicate key name

CREATE INDEX or ALTER TABLE ADD INDEX uses an index name that already exists on that table.

Error number
1061
Name
ER_DUP_KEYNAME
SQLSTATE
42000
Category
Keys and constraints
Error message
Duplicate key name '%s'

What it means

Index names must be unique per table. Creating an index whose name is taken fails with 1061, even when the columns are different.

Common causes

  • Re-running a migration that creates an index.
  • Two migrations that create indexes with the same name.

How to fix it

  1. List the table's indexes.

    SHOW INDEX FROM orders;
  2. Drop and recreate the index if the definition should change, or choose another name.

    ALTER TABLE orders DROP INDEX idx_customer, ADD INDEX idx_customer (customer_id, created_at);

Related errors

More keys and constraints errors

The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.

// fewer errors, faster fixes

Work on MySQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.