Common SQL Errors

MySQL Error 1061 ER_DUP_KEYNAME: Duplicate key name – Fix & Prevent

Galaxy Team
August 5, 2025

MySQL raises ER_DUP_KEYNAME when a CREATE or ALTER operation tries to add an index or constraint whose name already exists in the same table.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL Error 1061 ER_DUP_KEYNAME?

ER_DUP_KEYNAME (MySQL Error 1061: Duplicate key name) occurs when a CREATE TABLE, ALTER TABLE, or CREATE INDEX statement reuses an index name that already exists. Inspect current indexes with SHOW INDEX, then drop, rename, or guard the new index with IF NOT EXISTS to eliminate the conflict.

Error Highlights

Typical Error Message

Duplicate key name '%s'

Error Type

Schema Definition Error

Language

MySQL

Symbol

ER_DUP_KEYNAME

Error Code

1061

SQL State

Explanation

Table of Contents

What is MySQL Error 1061 (ER_DUP_KEYNAME)?

The error message "Duplicate key name '%s'" means MySQL refused to create or alter an index because another index with the same name already exists in the target table. The conflict can appear during CREATE TABLE, ALTER TABLE, CREATE INDEX, or when adding UNIQUE or PRIMARY KEY constraints. MySQL demands unique index names per table.

Leaving this error unresolved halts migrations, blocks deployments, and risks application downtime.

Understanding the root cause and fixing it quickly keeps CI pipelines green and ensures database consistency.

What Causes This Error?

Most cases stem from running the same migration twice, forcing MySQL to recreate an index that is already present. Parallel feature branches may introduce two indexes with identical names. ORMs can also auto-generate index names that overlap with manually defined ones.

Foreign keys bring implicit indexes that clash with explicit definitions of the same column.

How to Fix MySQL Error 1061

Begin by listing current indexes with SHOW INDEX. Decide whether to drop, rename, or skip creation of the duplicate index. Apply the chosen change using ALTER TABLE. Rerun the migration in a test environment, then deploy to production.

In Galaxy, the sidebar index list lets engineers verify names before committing schema changes.

Common Scenarios and Solutions

Re-running a migration: wrap CREATE INDEX in IF NOT EXISTS to make it idempotent. Two migrations conflict: agree on a single, descriptive index name and adjust one file. Duplicate of an implicit foreign-key index: keep the implicit one or rename the explicit index.

Best Practices to Avoid This Error

Follow a naming convention such as idx_

_.

Automate schema diffs in CI to flag duplicate names pre-merge. Use Galaxy’s search to locate existing indexes fast. Enforce code reviews that check for duplicate index names. Guard new indexes with IF NOT EXISTS when possible.

Related Errors and Solutions

Error 1062 "Duplicate entry" arises from duplicate data, not index names. Error 1005 "Can't create table" may wrap ER_DUP_KEYNAME when foreign keys fail. Error 1022 signals a similar duplicate key problem during ALTER TABLE.

Resolve the index conflict first to clear these errors.

.

Common Causes

Related Errors

FAQs

Can I ignore ER_DUP_KEYNAME and continue the migration?

No. MySQL rolls back the statement when the error occurs, leaving the table unchanged. You must resolve the duplicate before proceeding.

Does MySQL treat index names as case-sensitive?

On case-insensitive file systems, names are treated as not case-sensitive, increasing the chance of duplicates. Always use exact matches.

How do I find all index names in a database?

Query INFORMATION_SCHEMA.STATISTICS or use SHOW INDEX for each table to list index names and columns.

Will IF NOT EXISTS work with CREATE INDEX in MySQL?

Yes, since MySQL 8.0 the syntax CREATE INDEX IF NOT EXISTS prevents the duplicate key name error during idempotent migrations.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo