What is UNIQUE Constraint in SQL Server?
Introduction:
In this article i will explain what is UNIQUE Constraint in SQL Server.
Description:
In previous articles i explained what is Constraint, how many Constraints available in SQL Server, and what is NOT NULL, CHECK Constraint in SQL Server. Now i will explain what is UNIQUE Constraint in SQL Server.
UNIQUE Constraint:
UNIQUE constraint enforce the uniqueness of the values in a set of columns. In a UNIQUE constraint, no two rows in the table can have the same value for the columns.
We can create UNIQUE constraint in two ways:
- Column Level
- Table Level
Column Level:
Now i'm creating Employee table where EmpNo column is UNIQUE.
Create table Employee
(
EmpNo int unique, -- column level
EmpName varchar(50)
)
Table Level:
Now I'm creating Employee table where EmpNo column is UNIQUE.
Create table Employee
(
EmpNo int,
EmpName varchar(50),
constraint UK_EmpNo unique(EmpNo) -- table level
)
Note: UNIQUE allow one null value, but primary keys do not allow for NULL as one of the unique values.
-
CreatedOct 23, 2013
-
UpdatedOct 03, 2020
-
Views1,893