Sql Query To Use a Unique Key


Unique Key Constraint is used, when we want to keep the value of a specific column unique i.e non-repeatable. After creating a table using a unique key constraint, it doesn’t allow to insert of repeated value.

It will give an error (UNIQUE constraint failed: user_detail.email) while inserting the same email "fresherbell@gmail.com" more than one time.

SQL Query
create table user_detail ( id int primary key, email varchar(50) unique ); INSERT INTO user_detail VALUES(1,"fresherbell@gmail.com"); SELECT * FROM user_detail; INSERT INTO user_detail VALUES(2,"fresherbell@gmail.com");
Output