Thursday, August 4, 2016

why we use unique key ?

Primary key don’t allowed null value but unique key allow null value. When we use primary key  and when we use unique key.Primary key is on type of unique key but in a table primary key can use only one time but unique key can use as much as database programmer needed.

Example:

Primary key :

Declaring id column as a primary key . IDENTITY(1,1) use for increment the value by adding one. Primary key always unique ,So It  faster the searching and filtering  .It’s reduce the where query execution time.

CREATE TABLE Employee
(
Id int  IDENTITY(1,1) PRIMARY KEY,
Name varchar(200),
Address varchar(300),
)

Unique Key:

Declaring id and Name column as a Unique key .

CREATE TABLE Employee
(

Id int(12),
Name varchar(200) ,Address varchar(300),
CONSTRAINT ID UNIQUE (Id,Name)

)

No comments:

Post a Comment