What the message looks like
ERROR 1062 (23000): Duplicate entry '42' for key 'orders.PRIMARY'
ERROR 1062 (23000): Duplicate entry 'anna@example.com' for key 'users.email'Common causes
- A row with the same value already exists in a primary key or
UNIQUEcolumn, for example the same email address or order number. - The
AUTO_INCREMENTcounter is behind the highest existing id, typically after rows were inserted with explicit ids or after an import. - The same insert runs twice: a double click, a retry after a timeout, or two requests that both check first and insert afterwards (race condition).
- The collation treats values as equal that look different. Case-insensitive collations see
Annaandannaas the same value, and many collations ignore trailing spaces. ALTER TABLE ... ADD UNIQUEfails because the existing data already contains duplicates.
Check
- Index definitions and AUTO_INCREMENT counter
mysql -e "SHOW CREATE TABLE appdb.orders\G" - Which row already holds the value?
mysql -e "SELECT * FROM appdb.orders WHERE id = 42" - Highest id in the table
mysql -e "SELECT MAX(id) FROM appdb.orders" - Duplicates before adding a unique index
mysql -e "SELECT email, COUNT(*) FROM appdb.users GROUP BY email HAVING COUNT(*) > 1"
Fix
- Decide what should happen with the existing row. If the new data should update it, use
INSERT ... ON DUPLICATE KEY UPDATE. - To skip duplicates,
INSERT IGNOREworks, but it also turns other errors into warnings, so use it only where that is acceptable.REPLACEdeletes and reinserts the row, which triggers delete side effects and can fail on foreign keys. - If the counter is behind, set it above the maximum:
ALTER TABLE orders AUTO_INCREMENT = 1001;with a value higher thanMAX(id). Avoid inserting explicit ids into auto-increment columns. - Prevent races by letting the database decide: insert directly and handle error 1062 in the code, instead of
SELECTfirst andINSERTafterwards. - Before adding a unique index, find the duplicates with
GROUP BY ... HAVING COUNT(*) > 1, merge or delete them and then create the index. - If values must differ in case, check the column collation with
SHOW CREATE TABLEand use a binary or case-sensitive collation. Changing it affects existing data and comparisons, so test first.
Typical scenario
A shop imports orders from a CSV file every night, and the file brings its own ids. In the morning every new order in the web shop fails with Duplicate entry '10450' for key 'orders.PRIMARY', because the AUTO_INCREMENT counter is still at 10000 while the table reaches 10450. ALTER TABLE orders AUTO_INCREMENT = 10451 fixes it, and the import should work without its own ids from now on.
FAQ
Which column does the key name in the message refer to?
PRIMARY means the primary key. Any other name is the name of a unique index, and SHOW CREATE TABLE or SHOW INDEX FROM tablename shows which columns belong to it. MySQL 8.0 writes it as table.index.
Is INSERT IGNORE a good way to make the error go away?
Only if skipping the row is really what you want. It hides the duplicate silently, and it also downgrades other errors such as invalid values to warnings. For updating existing rows ON DUPLICATE KEY UPDATE is clearer.
Why do I get the error for an address that is only written differently?
Because the column collation treats the values as equal, for example Anna@Example.com and anna@example.com. For email addresses that is usually wanted. SHOW CREATE TABLE shows whether it applies.
How do I decide per row what happens on a duplicate?
INSERT ... ON DUPLICATE KEY UPDATE column = new_value updates the existing row instead of raising an error. You choose in the statement which columns get updated.
The error only happens on the replica or after a failover. Why?
Then data was changed there that did not come from the source, or replication is running from a different position. First find out whether someone wrote to the replica directly (check read_only) before skipping individual rows.