Error 1062: Duplicate entry for key

You are viewing an old version of this article. View the current version here.
Error CodeSQLSTATEErrorDescription
106223000ER_DUP_ENTRYDuplicate entry '%s' for key %d

Possible Causes and Solutions

This error occurs when a key that requires a unique value (unique or primary) instead receives a duplicate. For example:

CREATE TABLE t1 (
  id INT AUTO_INCREMENT PRIMARY KEY,
  f VARCHAR(10) UNIQUE
);


INSERT INTO t1 (f) VALUES ('a'),('b');
Query OK, 2 rows affected (0.027 sec)
Records: 2  Duplicates: 0  Warnings: 0

SELECT * FROM t1;
+----+------+
| id | f    |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+
2 rows in set (0.000 sec)

INSERT INTO t1 (f) VALUES ('b'),('c');
ERROR 1062 (23000): Duplicate entry 'b' for key 'f'

Solve the error by either not requiring the key to be unique, or by not attempting to insert a duplicate value.

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.