Showing posts with label What is Unique Key?. Show all posts
Showing posts with label What is Unique Key?. Show all posts

Friday, 12 May 2017

What is Unique Key?

In this post, I will explain What is Unique Key?

Unique Key provides uniqueness for a column or set of columns in a table like a primary key. It is used to prevent duplicate values in a column. A database table can have more than one Unique Key and it allows at least one NULL value in a column( Nullable). Unique Key generates a Unique Non-Clustered Index

Example of Unique Key-

Create a Unique Key while creating a table

CREATE TABLE tbl_Employee
(
    EmpId INT NOT NULL,
    EmpName VARCHAR(100) NULL,
    CONSTRAINT UC_EmpId UNIQUE (EmpId)
)


Create Unique Key using alter table statement
ALTER TABLE tbl_Employee
ADD CONSTRAINT UC_EmpId UNIQUE (EmpId)

Drop Unique Key using alter table statement

ALTER TABLE tbl_Employee
DROP CONSTRAINT  UC_EmpId