http://computertriksno1.blogspot.in/

Difference between Primary Key and Unique Clustered Index in SQL Server

11 Nov 2014 5 comments

Difference between Primary Key and Unique Clustered Index in SQL Server


The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.



You cannot create a unique index on a single column if that column contains NULL in more than one row. Similarly, you cannot create a unique index on multiple columns if the combination of columns contains NULL in more than one row. These are treated as duplicate values for indexing purposes.


A primary key must be unique, but that is just one of the its requirements. Another one would be that it cannot be null, which is not required of a unique constraint.

Also, while, in a way, unique constraints can be used as a poor man's primary keys, using them with IGNORE_DUP_KEY = ON is plainly wrong. That setting means that if you try to insert a duplicate, the insertion will fail silently.



They're definitely different. As mentioned in other answers:

  • Unique key is used just to test uniqueness and nothing else
  • Primary key acts as an identifier of the record.

Also, what's important is that the primary key is usually the clustered index. This means that the records are physically stored in the order defined by the primary key. This has a big consequences for performance.

Also, primary key is automatically included in all other indexes, so getting it doesn't require a record lookup, just reading the index is enough.

To sum up, always make sure you have a primary key on your tables. Indexes have a huge impact on performance and you want to make sure you get your indexes right.


Difference between Primary Key and Unique Clustered Index in SQL Server
Share this article :

+ comments + 5 comments

24 December 2014 at 14:10

You can watch on YouTube => Click Here

!!!Demo!!! When will you be doing another article on this subject? 

Amela
IT support Bedford !!!Demo!!!

6 January 2015 at 12:38

You can watch on YouTube => Click Here

!!!Demo!!! Informative things shared about SQL database. !!!Demo!!!

24 February 2015 at 18:05

You can watch on YouTube => Click Here

!!!Demo!!! very good blog for so many computer tips and tricks for more visit http://www.techbeatus.blogspot.in/ !!!Demo!!!

Anonymous
2 April 2015 at 00:13

You can watch on YouTube => Click Here

!!!Demo!!! Thank you for your nice and helpful post. Quick Tips for Computer Security
!!!Demo!!!

25 May 2016 at 13:06

You can watch on YouTube => Click Here

!!!Demo!!! Great Post,thanks for sharing...

https://savnpik.com/UAE-deals/hotel-and-resorts !!!Demo!!!

Post a Comment

 
Support : GDDon | Creating Website | Gddon |
Copyright © 2013. Computer Tricks and Tips for System - All Rights Reserved
Template Created by Creating Website Modify by GDDon.Com
Proudly powered by Blogger