How to resolve MySQL ERROR 1062 (23000) Duplicate entry for key

When you try to insert the new record into MySQL table, sometimes you might see ERROR 1062 (23000) Duplicate entry ‘X’ for key ‘xxxxxxx’. Here we will see Error 1062 thrown by MySQL.

When MySQL Throw ERROR 1062 (23000)

When there is a primary key in any table, and we try to insert the duplicate entry then MySQL throws ERROR 1062 (23000) Duplicate entry error.

Example:

Suppose we have dept table and dept table contains the following data.

mysql-select-table-query

Here the id column is the primary key, means id column can have only unique and not null values. Now if we try to insert a duplicate value in the id column then it will throw ERROR 1062 (23000) Duplicate entry for key.

insert into dept values (1,'security');
ERROR 1062 (23000): Duplicate entry '1' for key 'dept.PRIMARY'

To resolve ERROR 1062 in MySQL, you have to enter some other value as Primary key can not have duplicate values.

Scroll to Top