Primary Key V/S Unique key

Difference between primary key and unique key are as follows: -

  • We cannot enter null in primary key but we can have one null value in unique key column.
  • When we create primary key cluster index created automatically but unique key non clustered index created automatically.
  • Searching will be fast with the help of primary key as compare to unique key.
  • A table can have only one primary key but we can have more than one unique key.

How to Create Primary Key


create table Course(Course_Id int primary key, Course_Name varchar(100));
or

create table Course(Course_Id int Constraint CPk primary key(Course_Id), Course_Name varchar(100));

or

create table Course(Course_Id int Constraint CPk primary key, Course_Name varchar(100));
or

create table Course(Course_Id int , Course_Name varchar(100) Constraint CPk primary key(Course_Id));

In this table you can see that in enq_type column we have entered null as we don’t have any value for this row.






How to Create Unique Key

 
create table t1(id int primary key, phone varchar(100) unique not null);
or
create table t2(id int primary key, phone varchar(100) constraint unk unique);
or
create table t3(id int primary key, phone varchar(100), 
email varchar(200) constraint unkphone unique(phone), constraint unkemail unique(email));