What will happen if we have set UNIQUE and multiple insertion with duplicate values

MySQLMySQLi Database

An error will arise and nothing will get inserted in the table Let us see an example and create a table −

mysql> create table DemoTable1585
   -> (
   -> StudentId int,
   -> StudentMarks int,
   -> UNIQUE(StudentId)
   -> );
Query OK, 0 rows affected (1.02 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1585 values(1,87),(2,98),(3,91),(3,48);
ERROR 1062 (23000): Duplicate entry '3' for key 'StudentId'

Display all records from the table using select statement −

mysql> select * from DemoTable1585;

This will produce the following output. Nothing gets inserted:

Empty set (0.00 sec)
raja
Published on 16-Dec-2019 06:30:06
Advertisements