How to Create UNIQUE Constraint in SQL
In this article I am going to explain about UNIQUE Constraints in SQL.
If you want unique value in any specific column use UNIQUE constraints. UNIQUE constraints does not allow any duplicate value in specific column and provide unique value.
Both Primary key constraint and unique constraint provide unique value. When you want unique value in any specific column but you are not interested to use primary key use Unique.
Difference between Primary key Constraint and Unique Constraint
- You can use multiple unique constraints on a table but primary key only one.
- You can use null value in unique constraint but not in primary key constraint but you can use NULL value only one time in unique constraint.
A UNIQUE constraint can be referenced by a FOREIGN KEY constraint.
Example of Unique Constraint
In this example emp_panno is unique constraint and we know that pan number of every person is unique.
create table empinfo
empid int primary key,
depid int foreign key references depinfo(depid) ,
emp_panno int unique,