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
+ comments + 5 comments
You can watch on YouTube => Click Here
!!!Demo!!! When will you be doing another article on this subject?
Amela
IT support Bedford !!!Demo!!!
You can watch on YouTube => Click Here
!!!Demo!!! Informative things shared about SQL database. !!!Demo!!!
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!!!
You can watch on YouTube => Click Here
!!!Demo!!! Thank you for your nice and helpful post. Quick Tips for Computer Security
!!!Demo!!!
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