Unique Key Constraint in SQL Server
UNIQUE Key constraint is used to enforce the uniqueness of a column. i.e. the column should not allow any duplicate value. UNIQUE Key constraint allows only one NULL value.
The primary key and unique key constraint both are used to enforce the uniqueness of a column, But in the case of a primary key we can create only one primary key in a table, So if there is a need for uniqueness of another column then we can use unique key constraint.
Primary key constraint does not allow NULL value, while Unique key constraint allows one NULL value. Another NULL value will be considered as the Violation of UNIQUE KEY constraint (cannot inset the duplicate key in the object).
For example, you have an employee table having an email ID column, and as you know two employees do not have the same email ID then we can create a unique key constraint on the email column to enforce the uniqueness of the column.
UNIQUE KEY Constraint on Create Table

UNIQUE KEY Constraint on ALTER TABLE
ALTER TABLE tblEmployee
ADD UNIQUE(EmpEmail);
This will create a UNIQUE KEY constraint on EmpEmail column, and it will create a name.
But if we want to give name for UNIQUE KEY, then we add the UNIQUE KEY as,
ALTER TABLE tblEmployee
ADD CONSTRAINT UC_tblEmployee UNIQUE (EmpEmail);
This will generate the UNIQUE KEY with the name UC_tblEmployee.
In the above table, you can notice that EmpEmail has a UNIQUE KEY Constraint, Now if we insert some values in this table.
In the below image, I inserted a row in the tblEmployee table, and that succeeded without any issue.

But, When I tried to insert a new row with new EmpID but with the similar EmpEmail, I got Unique Key violation error as below.

DROP a UNIQUE KEY Constraint
ALTER TABLE tblEmployee
DROP CONSTRAINT UC_tblEmployee
Recommended Readings
- Advanced SQL Queries For Practice With Solution
- SQL Queries For Practice With Solution
- SQL Interview Questions and Answers
- STORED PROCEDURE in SQL Server
- How To Join Tables Data in SQL Server
- How to use Transaction in SQL Stored Procedure
- Difference Between IN and NOT IN Operators in SQL
- How To Modify Date in SQL Using DATEADD
- How To Get Year From Date in SQL Server
- How To Get Month From Date in SQL Server
- How To Get Day From Date in SQL Server
- How To Use ROW_NUMBER Function in SQL
- Date and Time Functions in SQL Server
- How To Find Nth Highest Salary in SQL Server
- How to Backup Table Using SELECT INTO Statement
- How To Use HAVING Clause in SQL Server
- Aggregate Functions in SQL Server
- How To Group Data Using Group By in SQL Server
- How To Truncate Table in SQL Server
- How To Delete Data From Table in SQL Server
- How To Update Table Data in SQL Server
- How To Sort Data Using Order By Clause in SQL
- How To Select Distinct Records in SQL Server
- How to Filter Data From Table in SQL Server
- Round Off Values Using Ceiling and Floor in SQL
- How To Find Square Root Of A Number in SQL Server
- How To Select Data From Table in SQL Server
- How To Insert Data in SQL Server Table
- How To Add NOT NULL Constraint in SQL Server
- How To Add Check Constraint on SQL Server Table
- How To Add Default Constraint on SQL Server Table
- Unique Key Constraint in SQL Server
- How to add Foreign Key Constraint in SQL Server
- How To Add Identity To SQL Server Table Column
- How to add Primary Key Constraint in SQL Server
- How To Create Alter and Drop Table in SQL Server
- How To Create Alter and Drop Database in SQL