Why does the following error occur in MySQL: ERROR 1062 (23000): Duplicate entry?


Let’s say you have set column values as unique key and try to insert duplicate values in the table. This will lead to ERROR 1062 (23000): Duplicate entry.

Let us first create a table -

mysql> create table DemoTable776 (
   FirstValue int,
   SecondValue int,
   unique key(FirstValue,SecondValue)
);
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command. While inserting duplicate value, the same error arises as shown below -

mysql> insert into DemoTable776 values(10,20);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable776 values(10,40);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable776 values(40,20);
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable776 values(10,20);
ERROR 1062 (23000): Duplicate entry '10-20' for key 'FirstValue'

Display all records from the table using select statement -

mysql> select *from DemoTable776;

This will produce the following output -

+------------+-------------+
| FirstValue | SecondValue |
+------------+-------------+
| 10         | 20          |
| 10         | 40          |
| 40         | 20          |
+------------+-------------+
3 rows in set (0.00 sec)

Updated on: 09-Sep-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements