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.
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.
Can't write; duplicate key in table '%s'
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.
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.
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.
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.
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.
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.
Enforce data validation at application level, run pre-migration checks for duplicates, and add monitoring triggers to detect potential conflicting values early.
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.
.
Existing rows already hold the value the new operation is trying to insert or update, breaking uniqueness.
An ALTER TABLE attempts to create an index with a name that already exists in the same table.
Importing a dump that contains both DROP INDEX and CREATE INDEX statements in the wrong order can recreate an index twice.
High-volume concurrent inserts may race to write identical keys before a transaction commits, triggering the error.
Adding a foreign key implicitly creates an index that might conflict with an existing index of the same columns.
.
Use SHOW CREATE TABLE to see key columns, then GROUP BY those columns to locate duplicate values.
Yes, if you first verify that the table will still meet uniqueness requirements after recreation.
INSERT IGNORE skips rows that would create duplicates, but you may lose data unexpectedly, so validate results.
Galaxy's AI copilot flags potential duplicate inserts and highlights index conflicts in real time, letting you fix them before execution.