15 May 2013

Difference between Primary Key & Unique Key



Primary Key:

i) Can be only one in a table
ii) It never allows null values
iii) Primary Key is unique key identifier and can not be null and must be unique.
iV) Primary key can be made foreign key into another table.
v) By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.

Unique Key:

i) Can be more than one unique key in one table.
ii) Unique key can have null values, accept only one null value.
iii) Unique key can be null and may not be unique.
iv) In SQL Server, Unique key can be made foreign key into another table.
v) By default, Unique key is a unique non-clustered index.


Define Primary key and Unique key

 CREATE TABLE Employee 
 (
 EmpID int PRIMARY KEY, --define primary key
 Name varchar (50) NOT NULL,
 MobileNo int UNIQUE, --define unique key
 Salary int NULL
 ) 


No comments:

Post a Comment