Wednesday, April 7, 2021

What is the difference between primary key and unique key in SQL Server?

How to answer DBMS interview question about the difference between primary key and unique key

difference between primary key and unique key

This is very commonly asked interview question from SQL Server or database. Following is the answer of this question:

1. By default, Primary key creates clustered index in the table but unique key creates non-clustered index.

2. Primary key column does not accept any null values, where as a unique key column accept only one null value.

3. A table can have only one primary key. On the other hand a table can have more than one unique key.

4. Duplicate values are not allowed in primary key where as duplicate value will be accepted if one or more key parts are null

5. The purpose of implementing primary key is to enforce integrity between entities of database, on the other hand the purpose of unique key is to enforce unique data within the table.



2 comments:

  1. Thanks for sharing this detailed blog! I have just enrolled into SQL Certificationso I am fond of searching about these kinds of blog on internet so that I can explore more into it.

    ReplyDelete
  2. Nice collection. Please check out our website on Microsoft SQL Certification

    ReplyDelete

Please keep your comments relevant.
Comments with external links and adult words will be filtered.