Difference Between Primary Key and Unique Key in SQL


Difference Between Primary Key and Unique Key 



       Primary Key        Unique Key    
1
Primary Key Can't Accept Null Values.
Unique Key Can Accept Only One Null Value
2
Creates Clustered Index
Creates Non-Clustered Index
3
Only One Primary key in a Table
More than One Unique Key in a Table. 
4
Primary Key Can be Made Foreign Key Into Another Table.
Ex:
CREATE TABLE [country] (
       [id]    VARCHAR (50) NOT NULL,
    [country] VARCHAR (50) NOT NULL,     
    CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED ([id]));
SQL Server, Unique Key Can be Made Foreign Key Into Another Table.
Ex:
CREATE TABLE [country] (    
    [name]    VARCHAR (50) NOT NULL,
    [country] VARCHAR (50) NOT NULL,  
    UNIQUE NONCLUSTERED ([name]));

0 comments:

Post a Comment