mysqldude
Error codes › 1062

MySQL & MariaDB error 1062: Duplicate entry for key

Error messageDuplicate entry '…' for key …
Example from our test runs: 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.

MySQLMariaDB
SymbolER_DUP_ENTRYER_DUP_ENTRY
SQLSTATE2300023000
TypeServer errorServer error
Versionsevery release since 5.0 activeevery 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 … SELECT contains duplicates, or overlaps rows already in the table.
  • ALTER TABLE … ADD UNIQUE or ADD PRIMARY KEY on a column that already holds duplicate values.
  • An UPDATE changes a value to one another row already has.
  • An AUTO_INCREMENT column has reached the maximum of its type (127 for TINYINT, 2,147,483,647 for INT): 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 IGNORE skips 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 syntax INSERT … AS new ON DUPLICATE KEY UPDATE col = new.col replaces the deprecated VALUES() function; MariaDB keeps VALUES().
  • Replace it: REPLACE INTO deletes 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 on ALTER 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.

  1. Connect with mysql -u root -p and 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');
  2. 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

  1. 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;
  2. 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;
  3. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
Insert a second row with the same value in a UNIQUE column repro
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✓ 106210.11✓ 106211.4✓ 106211.8✓ 1062
Skip the duplicate: INSERT IGNORE (the row is not inserted, a warning is raised) fix
MySQL 5.6✓5.7⚠ 10628.0⚠ 10628.4⚠ 10629⚠ 1062
MariaDB 10.6⚠ 106210.11⚠ 106211.4⚠ 106211.8⚠ 1062
Update the existing row instead: INSERT ... ON DUPLICATE KEY UPDATE fix
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Find the existing duplicates before adding a unique key fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
AUTO_INCREMENT at the top of its type also raises 1062 (MySQL) or 167 (MariaDB) version check
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✓ 16710.11✓ 16711.4✓ 16711.8✓ 167
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
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

  • 1022 ER_DUP_KEY
  • 1586 ER_DUP_ENTRY_WITH_KEY_NAME
  • 1169 ER_DUP_UNIQUE

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 fromSymbolMessage
5.5.68ER_DUP_ENTRYDuplicate entry '%-.192s' for key %d
10.2.44ER_DUP_ENTRYDuplicate entry '%-.192T' for key %d
11.8.10ER_DUP_ENTRYDuplicate entry '%-.192sT' for key %d