UNIQUE Constraint In SQL
UNIQUE Constraint In SQL:-
  1. The UNIQUE constraint uniquely identifies each record in a database table.
  2. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.
  3. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note:-In a table we can defined many UNIQUE constraints, but only one PRIMARY KEY constraint per table.

How to create UNIQUE Constraint on CREATE TABLE in SQL:-

  

CREATE TABLE tblEmpRecord
(
EmpId int NOT NULL UNIQUE,
FirstName varchar(200),
LastName varchar(100) NOT NULL,
Email varchar(MAX),
Address nvarchar(200),
City varchar(200)
)



Alter UNIQUE Constraint on the table in SQL:-

  

------------For single column-------------------
ALTER TABLE tblEmpRecord ADD UNIQUE(City)


------------For multiple columns----------------
ALTER TABLE tblEmpRecord
ADD CONSTRAINT uc_EmpId UNIQUE (FirstName,LastName)



How To DROP a UNIQUE Constraint in SQL:-
  

ALTER TABLE tblEmpRecord DROP CONSTRAINT uc_EmpId