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!

 

Advertisement

About SQLGator

Microsoft Data Platform MVP, Florida Gator, Star Wars fanatic and is there anything else...oh yeah PS4! I am a geek and SQL Server Business Intelligence Consultant, there are other technologies greater than these? Not so fast my friend! I also love to travel to new and exotic places.

Posted on March 11, 2013, in SQL Schoolhouse and tagged . Bookmark the permalink. 1 Comment.

  1. The question I’ve been asked in every single DBA interview I’ve ever, ever had.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: