# 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.\
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    |
+----+------+
```

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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/error-codes/mariadb-error-codes-1000-to-1099/e1062.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
