Blog Archives

Primary Key versus Unique Key

SQL Schoolhouse!

SQL Schoolhouse!

Recently, I was asked the difference between a primary key and a unique key and this seemed like a good topic for a blog post.

First of all, the primary key is the key that will uniquely identify the record and enforce entity integrity.  By default this will create a clustered index on the column or columns selected.  The unique key, like the primary key, will be unique, however it creates a non-clustered index by default.  The unique key will also allow one NULL record where the primary key will not allow any.

In addition, there is only one primary key per table, whereas you can have multiple unique keys.  Enjoy!

 

Clustered Nuts?

While looking at performance in some of my databases, I noticed there was a high number of FreeSpace scans going on.   Aaargh!  They are called clustered indexes, people use them!  Performance, performance, performance!

In most cases it is best practices to start with a clustered index on every table created.  There are rare instances when this is not the optimal setup, but for the large majority of standard databases put in a clustered index on each table.  Your shooting yourself in the foot, people!

Enjoy!

%d bloggers like this: