> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/error-codes/mariadb-error-codes-1000-to-1099/e1062.md).

# Error 1062: Duplicate entry for key

| Error Code | SQLSTATE | Error          | Description                     |
| ---------- | -------- | -------------- | ------------------------------- |
| 1062       | 23000    | ER\_DUP\_ENTRY | Duplicate entry '%s' for key %d |

## Possible Causes and Solutions

This error occurs when a key that requires a unique value ([unique](/docs/server/mariadb-quickstart-guides/mariadb-indexes-guide.md#unique-index) or [primary](/docs/server/mariadb-quickstart-guides/mariadb-indexes-guide.md#primary-key)) instead receives a duplicate. The message names the value and the index, `PRIMARY` for the primary key. 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    |
+----+------+
```

### Values That Look Different

Whether two values are duplicates depends on the column's [collation](/docs/server/reference/data-types/string-data-types/character-sets/character-set-and-collation-overview.md), not on their bytes. With a case-insensitive collation such as the default `utf8mb4_general_ci`, `'B'` is a duplicate of `'b'`. With an accent-insensitive collation, `'ä'` is a duplicate of `'a'`. And with a `PAD SPACE` collation, which most collations are, trailing spaces don't count, so `'b '` is a duplicate of `'b'`:

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

If these values must be distinct, give the column a case- and accent-sensitive collation, such as `utf8mb4_bin`.

### Handling Duplicates in SQL

If duplicates are expected, let the statement handle them instead of failing:

* [INSERT IGNORE](/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-ignore.md) skips the duplicate rows, and reports each one as a warning with code 1062.
* [INSERT ... ON DUPLICATE KEY UPDATE](/docs/server/reference/sql-statements/data-manipulation/inserting-loading-data/insert-on-duplicate-key-update.md) updates the existing row instead.
* [REPLACE](/docs/server/reference/sql-statements/data-manipulation/changing-deleting-data/replace.md) deletes the existing row and inserts the new one.

An `AUTO_INCREMENT` column that has reached the largest value of its data type doesn't give this error, but error 167, `Out of range value for column`, in every SQL mode. Change the column to a larger integer type.

On a replica, this error stops replication when a replicated row already exists, for example because something wrote to the replica directly. Find out why the row is already there before you skip the event, because skipping hides the difference between the primary and the replica.

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}
