SQL Server: UNIQUE Constraint

 SQL Server     sbo     Updated on Oct 23, 2013     VIKRAM     594   

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:

  1. Column Level
  2. 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.

  Related Articles
SQL Server: DEFAULT Constraint
SQL Server: FOREIGN KEY Constraint
SQL Server: PRIMARY KEY Constraint
SQL Server: CHECK Constraint
SQL Server: Constraints
  Recent Articles
SQL Server: DEFAULT Constraint
SQL Server: FOREIGN KEY Constraint
SQL Server: PRIMARY KEY Constraint
SQL Server: CHECK Constraint
SQL Server: Constraints