MySQL 8.4Lab-verified · 2026-11-30

MySQL ERROR 1062 (23000): Duplicate entry for key

The value shown already exists in the PRIMARY KEY or UNIQUE index named in the message.

MySQL 8.4.13
1062 (23000): Duplicate entry 'kim@example.com' for key 'users.email'

Jump to a cause

  1. 01Inserting a value that already exists in a UNIQUE column
  2. 02Copying rows with their old ids into an AUTO_INCREMENT table
  3. 03Adding a UNIQUE index to a column that already has duplicates
  4. 04AUTO_INCREMENT reached the maximum value of the column type

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4

The message names both the value and the index: Duplicate entry 'kim@example.com' for key 'users.email'. The key is written as table.index; PRIMARY means the primary key. Start by looking up that value in that table.

The same error also appears when you add a UNIQUE index to existing data, and when an AUTO_INCREMENT column has reached the largest value its type can hold. The value in the message tells you which case you are in.

01Inserting a value that already exists in a UNIQUE column

email is UNIQUE and kim@example.com is already there, so a second INSERT with the same email fails. The SELECT shows the existing row (id 1).

Setup
Setuprun as lab_a
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(100) NOT NULL UNIQUE, name VARCHAR(50));
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_a
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu');
✕ ERROR
ERROR 1062 (23000): Duplicate entry 'kim@example.com' for key 'users.email'
Check the causerun as lab_a
SELECT index_name, column_name, non_unique FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = 'users' ORDER BY index_name;
OUTPUT
+------------+-------------+------------+
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
+------------+-------------+------------+
| email      | email       |          0 |
| PRIMARY    | id          |          0 |
+------------+-------------+------------+
2 rows in set (0.00 sec)

Besides PRIMARY, there is a unique index named email (NON_UNIQUE = 0). That is the key name shown in the error, users.email; it was created by the UNIQUE on the column.

Checkrun as lab_a
SELECT id, email, name FROM users WHERE email = 'kim@example.com';
OUTPUT
+----+-----------------+------+
| id | email           | name |
+----+-----------------+------+
|  1 | kim@example.com | Kim  |
+----+-----------------+------+
1 row in set (0.00 sec)

If the intent is "insert, or update if it already exists", use INSERT … AS new ON DUPLICATE KEY UPDATE. MySQL reports 2 rows affected when an existing row was updated (1 means a new row was inserted). If the value really must be new, change the value instead.

After the fixrun as lab_a
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu') AS new
  ON DUPLICATE KEY UPDATE name = new.name;
SELECT id, email, name FROM users;
✓ SUCCESS
Query OK, 2 rows affected (0.00 sec)

+----+-----------------+-----------+
| id | email           | name      |
+----+-----------------+-----------+
|  1 | kim@example.com | Kim Minsu |
+----+-----------------+-----------+
1 row in set (0.00 sec)

02Copying rows with their old ids into an AUTO_INCREMENT table

Data from another table or system is inserted together with its id. Ids 1 and 2 are already used in orders, so the first copied row fails on orders.PRIMARY. Joining on id shows which ids collide.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(50));
INSERT INTO orders (item) VALUES ('pen'), ('book');
CREATE TABLE orders_import (id INT, item VARCHAR(50));
INSERT INTO orders_import VALUES (1, 'cup'), (2, 'bag');
OUTPUT
Query OK, 0 rows affected (0.03 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.03 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
INSERT INTO orders (id, item) SELECT id, item FROM orders_import;
✕ ERROR
ERROR 1062 (23000): Duplicate entry '1' for key 'orders.PRIMARY'
Checkrun as lab_a
SELECT o.id, o.item, i.item AS import_item FROM orders o JOIN orders_import i ON i.id = o.id;
OUTPUT
+----+------+-------------+
| id | item | import_item |
+----+------+-------------+
|  1 | pen  | cup         |
|  2 | book | bag         |
+----+------+-------------+
2 rows in set (0.00 sec)

Leave out the id column and let AUTO_INCREMENT assign new ids (3 and 4 in the output). Do this only if nothing else refers to the old ids.

After the fixrun as lab_a
INSERT INTO orders (item) SELECT item FROM orders_import;
SELECT id, item FROM orders;
✓ SUCCESS
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

+----+------+
| id | item |
+----+------+
|  1 | pen  |
|  2 | book |
|  3 | cup  |
|  4 | bag  |
+----+------+
4 rows in set (0.00 sec)

03Adding a UNIQUE index to a column that already has duplicates

ALTER TABLE … ADD UNIQUE INDEX checks the existing rows. Phone 010-1111-2222 is stored twice (ids 1 and 3), so the index cannot be created. The message shows the first duplicate value and the new index name.

Setup
Setuprun as lab_a
CREATE TABLE members (id INT PRIMARY KEY, phone VARCHAR(20));
INSERT INTO members VALUES (1, '010-1111-2222'), (2, '010-3333-4444'), (3, '010-1111-2222');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE members ADD UNIQUE INDEX uk_members_phone (phone);
✕ ERROR
ERROR 1062 (23000): Duplicate entry '010-1111-2222' for key 'members.uk_members_phone'
Checkrun as lab_a
SELECT phone, COUNT(*), MIN(id), MAX(id) FROM members GROUP BY phone HAVING COUNT(*) > 1;
OUTPUT
+---------------+----------+---------+---------+
| phone         | COUNT(*) | MIN(id) | MAX(id) |
+---------------+----------+---------+---------+
| 010-1111-2222 |        2 |       1 |       3 |
+---------------+----------+---------+---------+
1 row in set (0.00 sec)
Fixrun as lab_a
DELETE FROM members WHERE id = 3;
OUTPUT
Query OK, 1 row affected (0.00 sec)

Find duplicates with GROUP BY … HAVING COUNT(*) > 1, decide which rows to keep, clean them up, then add the index again. Here the duplicate row id 3 was deleted.

After the fixrun as lab_a
ALTER TABLE members ADD UNIQUE INDEX uk_members_phone (phone);
✓ SUCCESS
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

04AUTO_INCREMENT reached the maximum value of the column type

The id column is TINYINT (max 127). After 127 is used, the next AUTO_INCREMENT value cannot grow, so MySQL tries 127 again and reports Duplicate entry '127' for key 'codes.PRIMARY'. A duplicate on exactly 127, 32767, 8388607 or 2147483647 is the sign of this case.

Setup
Setuprun as lab_a
CREATE TABLE codes (id TINYINT AUTO_INCREMENT PRIMARY KEY, label VARCHAR(20));
INSERT INTO codes (id, label) VALUES (126, 'a');
INSERT INTO codes (label) VALUES ('b');
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_a
INSERT INTO codes (label) VALUES ('c');
✕ ERROR
ERROR 1062 (23000): Duplicate entry '127' for key 'codes.PRIMARY'
Checkrun as lab_a
SELECT MAX(id) FROM codes;
SELECT column_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'codes' AND column_name = 'id';
OUTPUT
+---------+
| MAX(id) |
+---------+
|     127 |
+---------+
1 row in set (0.01 sec)

+-------------+
| COLUMN_TYPE |
+-------------+
| tinyint     |
+-------------+
1 row in set (0.00 sec)
Fixrun as lab_a
ALTER TABLE codes MODIFY id INT NOT NULL AUTO_INCREMENT;
OUTPUT
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

Widen the column (INT, or BIGINT for very large tables). The ALTER rebuilt the table, and the next row got id 128.

After the fixrun as lab_a
INSERT INTO codes (label) VALUES ('c');
SELECT id, label FROM codes;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

+-----+-------+
| id  | label |
+-----+-------+
| 126 | a     |
| 127 | b     |
| 128 | c     |
+-----+-------+
3 rows in set (0.00 sec)

FAQ

How do I fix MySQL error 1062 Duplicate entry for key PRIMARY?

Look up the value from the message in that table. If you are inserting explicit ids, leave the id out so AUTO_INCREMENT assigns one. If the value is exactly the maximum of the column type (127, 2147483647…), widen the column.

How do I insert or update when the row already exists in MySQL?

Use INSERT … VALUES (…) AS new ON DUPLICATE KEY UPDATE col = new.col. MySQL returns 2 rows affected when it updated an existing row.

Why do I get Duplicate entry when adding a unique index?

The column already contains duplicate values. Find them with SELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 1, clean them up, and run the ALTER again.

What does 'for key users.email' mean?

It is the index that was violated, written as table.index. PRIMARY is the primary key; other names are UNIQUE indexes (by default named after the column).

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13