MySQL & MariaDB error 1062: Duplicate entry for key
Duplicate entry '3' for key 'emp.PRIMARY' (8.0.46) An INSERT or UPDATE tried to store a value that already exists in a PRIMARY KEY or UNIQUE index. The statement is rejected and nothing is written.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_DUP_ENTRY | ER_DUP_ENTRY |
| SQLSTATE | 23000 | 23000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Every PRIMARY KEY and UNIQUE index guarantees that no two rows share the same value (or combination of values for a multi-column key). When a statement would break that rule, the server stops it with 1062 and names the value and the index:
Duplicate entry 'ann@example.com' for key 'users.uq_email'
Since MySQL 8.0.19 the key name is prefixed with the table name (users.uq_email); older versions and MariaDB print only the index name (uq_email). In a multi-row INSERT, nothing from that statement is kept on InnoDB.
Why it happens
- The application inserts a row that already exists, often a retried request or a double form submit.
- A data import or
INSERT … SELECTcontains duplicates, or overlaps rows already in the table. ALTER TABLE … ADD UNIQUEorADD PRIMARY KEYon a column that already holds duplicate values.- An
UPDATEchanges a value to one another row already has. - An
AUTO_INCREMENTcolumn has reached the maximum of its type (127 forTINYINT, 2,147,483,647 forINT): the next insert reuses the maximum and fails with 1062 on MySQL. MariaDB reports this case as error 167 instead.
How to fix it
Decide what should happen to the duplicate, then pick the statement that says so:
- Ignore it:
INSERT IGNOREskips the row and turns the error into a warning. It also hides other problems (bad dates, truncation), so prefer the next option where you can. - Update the existing row:
INSERT … ON DUPLICATE KEY UPDATE col = VALUES(col). In MySQL 8.0.20+ the newer alias syntaxINSERT … AS new ON DUPLICATE KEY UPDATE col = new.colreplaces the deprecatedVALUES()function; MariaDB keepsVALUES(). - Replace it:
REPLACE INTOdeletes the old row and inserts the new one. It fires DELETE triggers and cascades foreign keys, so use it with care. - Clean the data first when adding a unique key: find duplicates with
GROUP BY … HAVING COUNT(*) > 1, remove the extra rows, then add the index. Don’t rely onALTER IGNORE TABLE: MySQL removed it in 5.7, and MariaDB 10.11.17+ deletes every copy of a duplicated value (MDEV-42382). - Exhausted AUTO_INCREMENT: change the column to a larger type, e.g.
ALTER TABLE t MODIFY id BIGINT UNSIGNED AUTO_INCREMENT.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(100) NOT NULL, UNIQUE KEY uq_email (email)); INSERT INTO users (email) VALUES ('ann@example.com');Run:
INSERT INTO users (email) VALUES ('ann@example.com');
You get: Duplicate entry 'ann@example.com' for key 'uq_email'(11.8.9)
Verified fixes
Skip the duplicate: INSERT IGNORE (the row is not inserted, a warning is raised)
INSERT IGNORE INTO users (email) VALUES ('ann@example.com'); SELECT COUNT(*) FROM users;Update the existing row instead: INSERT ... ON DUPLICATE KEY UPDATE
INSERT INTO users (email) VALUES ('ann@example.com') ON DUPLICATE KEY UPDATE email = VALUES(email); SELECT COUNT(*) FROM users;Find the existing duplicates before adding a unique key
CREATE TABLE contacts (email VARCHAR(100)); INSERT INTO contacts VALUES ('a@x'), ('a@x'), ('b@x'); SELECT email, COUNT(*) FROM contacts GROUP BY email HAVING COUNT(*) > 1;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Insert a second row with the same value in a UNIQUE column repro | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 |
| Skip the duplicate: INSERT IGNORE (the row is not inserted, a warning is raised) fix | ✓ | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 | ⚠ 1062 |
| Update the existing row instead: INSERT ... ON DUPLICATE KEY UPDATE fix | ✓ | ✓ | ⚠ 1287 | ⚠ 1287 | ⚠ 1287 | ✓ | ✓ | ✓ | ✓ |
| Find the existing duplicates before adding a unique key fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| AUTO_INCREMENT at the top of its type also raises 1062 (MySQL) or 167 (MariaDB) version check | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 1062 | ✓ 167 | ✓ 167 | ✓ 167 | ✓ 167 |
What the warnings and failures say (3)
- Skip the duplicate: INSERT IGNORE (the row is not inserted, a warning is raised) (MySQL 5.7, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): warning 1062: Duplicate entry 'ann@example.com' for key 'uq_email'
- Skip the duplicate: INSERT IGNORE (the row is not inserted, a warning is raised) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1062: Duplicate entry 'ann@example.com' for key 'users.uq_email'
- Update the existing row instead: INSERT ... ON DUPLICATE KEY UPDATE (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: 'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and rep
Related errors
Where this error shows up in our guides
- Adding unique constraint · ALTER TABLE ... ADD PRIMARY KEY with duplicates (expect 1062) MySQL 5.6 5.7 8.0 8.4 9MariaDB 10.6 10.11 11.4 11.8
- Removing duplicate records · ALTER TABLE ... ADD UNIQUE INDEX with duplicates (post shows 1062) MySQL 5.6 5.7 8.0 8.4 9MariaDB 10.6 10.11 11.4 11.8
- Triggers for auditing · Post's design with only the column name fixed: audit PK = product_id MySQL 5.6 5.7 8.0 8.4 9MariaDB 10.6 10.11 11.4 11.8
- MySQL reset of auto increment values · Counter at INT max (2147483647): next insert fails (post shows 1062) MySQL 5.6 5.7 8.0 8.4 9
How the message changed between versions
| MariaDB from | Symbol | Message |
|---|---|---|
| 5.5.68 | ER_DUP_ENTRY | Duplicate entry '%-.192s' for key %d |
| 10.2.44 | ER_DUP_ENTRY | Duplicate entry '%-.192T' for key %d |
| 11.8.10 | ER_DUP_ENTRY | Duplicate entry '%-.192sT' for key %d |