Unique key constraint

 


We use the UNIQUE constraint to enforce the uniqueness of a column i.e. the column should not allow any duplicate values. We can add a unique constraint through the designer or using a query.

To add a unique constraint using MS SQL Server Management Studio Designer:

1. Right click on the table and select Design option.

2. Right click on the column and select Indexes/Keys option..

3. Click Add option

4. For Columns, select the column name you want to be unique.

5. For Type, choose Unique Key.

6. Click Close, Save the table.

To create the unique key using a query:
Alter Table Table_Name
Add Constraint Constraint_Name Unique(Column_Name)

Used to implement both primary keys and unique keys, a column's uniqueness. So, when do you choose each other? A table can have only one primary key. If you want to apply exclusivity to 2 or more columns, we use the unique key constraint

What is the difference between a primary key constraint and a specific key constraint? This question is mostly asked in interviews.

1. One table can have only one primary key, but more than one unique key.

2. The primary key does not allow taps, where the unique key allows a tap


Hitch up

1. Right click on the constraint and delete.

or

2. Using a Query

Alter table tblPerson

Drop Constant UQ_tblPerson_Email