Error 1062: Duplicate entry for key
Error Code | SQLSTATE | Error | Description |
---|---|---|---|
1062 | 23000 | ER_DUP_ENTRY | Duplicate entry '%s' for key %d |
Contents
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'); SELECT * FROM t1; +----+------+ | id | f | +----+------+ | 1 | a | | 2 | b | +----+------+ INSERT INTO t1 (f) VALUES ('b'),('c'); ERROR 1062 (23000): Duplicate entry 'b' for key 'f'
Solve the error by either not attempting to insert a duplicate value, or not requiring the key to be unique. For example, the below replaces the unique index with an index permitting duplicates:
ALTER TABLE t1 DROP INDEX f, ADD INDEX (f); INSERT INTO t1 (f) VALUES ('b'),('c'); SELECT * FROM t1; +----+------+ | id | f | +----+------+ | 1 | a | | 2 | b | | 3 | b | | 4 | c | +----+------+
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.