Common SQL Errors

MySQL Error 1022: ER_DUP_KEY - How to Fix Duplicate Key Conflicts

Galaxy Team
August 5, 2025

MySQL error 1022 arises when a write, ALTER, or CREATE operation would generate a duplicate value or index name that violates a UNIQUE or PRIMARY KEY constraint.

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 1022 (ER_DUP_KEY)?

MySQL Error 1022: ER_DUP_KEY signals that an INSERT, UPDATE, CREATE TABLE, or ALTER TABLE statement would produce a duplicate key or index name. Locate the conflicting row or index, remove or change the duplicate value or rename the index, then rerun the statement to fix the problem.

Error Highlights

Typical Error Message

Can't write; duplicate key in table '%s'

Error Type

Constraint Violation

Language

MySQL

Symbol

ER_DUP_KEY

Error Code

1022

SQL State

Explanation

Table of Contents

What does MySQL Error 1022 mean?

Error 1022 appears with the message "Can't write; duplicate key in table 'tbl'" when MySQL blocks a write that would violate a UNIQUE or PRIMARY KEY index or reuse an existing index name.

The server stops the statement to protect data integrity.

When does Error 1022 show up?

The error is common during INSERT or UPDATE operations that insert duplicate values, during ALTER TABLE statements that add or rename indexes, and when restoring dumps that recreate keys already present in the target schema.

Why is resolving ER_DUP_KEY critical?

Leaving duplicate key violations unresolved can corrupt logical data relationships, break foreign key references, cause replication errors, and crash application workflows that expect a successful commit.

What Causes This Error?

Duplicate data in the column set of a UNIQUE or PRIMARY KEY, mismatched index definitions between source and destination tables, or accidentally reusing an index name produce the 1022 error.

Concurrency spikes can also surface race-condition duplicates.

How to Fix MySQL Error 1022

Identify the conflicting key with SHOW INDEX or the error log, locate duplicates with SELECT queries, clean or update offending rows, or rename the index. Re-run the original statement once the conflict disappears.

Common Scenarios and Solutions

During bulk loads, temporary removal of the unique index lets data import, followed by a deduplication pass before recreating the key.

In schema migrations, add IF NOT EXISTS or unique suffixes to index names.

Best Practices to Avoid This Error

Enforce data validation at application level, run pre-migration checks for duplicates, and add monitoring triggers to detect potential conflicting values early.

Related Errors and Solutions

Errors 1062, 121, and 1061 also deal with duplicate entries or names but arise in slightly different contexts. Their root-cause analysis and fixes are similar to 1022.

.

Common Causes

Duplicate Data in Unique Columns

Existing rows already hold the value the new operation is trying to insert or update, breaking uniqueness.

Reused Index Name

An ALTER TABLE attempts to create an index with a name that already exists in the same table.

Inconsistent Dump Restore

Importing a dump that contains both DROP INDEX and CREATE INDEX statements in the wrong order can recreate an index twice.

Concurrent Inserts

High-volume concurrent inserts may race to write identical keys before a transaction commits, triggering the error.

Foreign Key Side Effects

Adding a foreign key implicitly creates an index that might conflict with an existing index of the same columns.

.

Related Errors

FAQs

How can I quickly find the duplicate causing Error 1022?

Use SHOW CREATE TABLE to see key columns, then GROUP BY those columns to locate duplicate values.

Is it safe to drop and recreate the index?

Yes, if you first verify that the table will still meet uniqueness requirements after recreation.

Will IGNORE keyword bypass Error 1022?

INSERT IGNORE skips rows that would create duplicates, but you may lose data unexpectedly, so validate results.

How does Galaxy help prevent this error?

Galaxy's AI copilot flags potential duplicate inserts and highlights index conflicts in real time, letting you fix them before execution.

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