SQL Server interview question: - What is the difference between unique key and primary key?

 Posted by articlesmaint on 3/9/2011 | Category: Sql Server Interview questions | Views: 5715


This is a typical SQL Server interview question and below is the comparison sheet.



















Unique Key


Primary Key


Unique key can have nulls


Primary key cannot have nulls.


In a single table we can create multiple unique keys.


In a single table we can have only one primary key.


It creates a non-clustered index by default.


It created a clustered index by default.


Both unique keys and primary keys can be referenced by foreign key.


Below is a nice video which demonstrates the same in a much precise manner...Enjoy.





Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Akiii on: 3/9/2011
i have read somewhere that we can have composite primary key ?
is this true ?

Thanks and Regards
Akiii
Posted by: Kishork80 on: 3/10/2011
Combination of more than 2 columns in a table is called composite key .If more than one column is considered as primary key ,combined together, it is call composite primay key :)
Posted by: Kishork80 on: 3/10/2011
UPDATED: Combination of more than 1 columns in a table is called composite key .If more than one column is considered as primary key ,combined together, it is call composite primay key :)

Posted by: Akiii on: 3/10/2011
@Kishork80

Thank you for your answer, but i have one question, if both are primary keys then why do we need a composite key. We all know that one primary key is enough for checking ambiguity.....?
Please explain with examples if you have....
Thank you for your time....

Thanks and Regards
Akiii

Login to post response