Click here to Skip to main content
15,886,724 members
Articles / Productivity Apps and Services / Sharepoint

What is the Difference Between a Primary and Unique Key?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
30 Dec 2017MIT4 min read 11.1K   2   3
Difference between a primary and unique key and why both are important to maintaining a relational database structure

In this article, we learn the difference between a primary and unique key, and why both are important to maintaining a relational database structure.

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database. You can get started using these free tools using my Guide Getting Started Using SQL Server.

Before we get into the difference, let’s make sure we understand the definitions of primary and unique keys. We’ll tackle the primary key first.

Primary Keys

The main purpose of the primary key is to provide a means to identify each record in the table.

The primary key provides a means to identity the row, using data within the row. A primary key can be based on one or more columns, such as first and last name; however, in many designs, the primary key is an auto-generated number from an identity column.

All relational database management systems support primary keys. In fact, for a table to be considered a relational table, and in the first normal form, it must have a primary key.

In SQLServer, the primary key, is referred to as a PRIMARY KEY constraint. A primary key has the following characteristics:

  • There can only be one primary key for a table.
  • The primary key consists of one or more columns.
  • The primary key enforces the entity integrity of the table.
  • All columns defined must be defined as NOT NULL.
  • The primary key uniquely identifies a row.
  • Primary keys result in CLUSTERED unique indexes by default.

As you can see, the primary key is the main key of the table. Its main purpose is to identify each row.

What about unique keys?

Unique Keys

A unique key is also called a unique constraint. A unique constraint can be used to ensure rows are unique within the database. Makes sense, but wait!

Don’t we already do that with the primary key? Yep, we do, but a table may have several sets of columns which you want unique.

The Employee table below:

Table with primary and unique key

Assuming that EmployeeID is the primary key, then we may want to place a unique constraint on GovernmentNumber to ensure each employee has their own number.

In SQL Server, the unique key has the following characteristics:

  • There can be multiple unique keys defined on a table.
  • Unique Keys result in NONCLUSTERED Unique Indexes by default.
  • One or more columns make up a unique key.
  • Column may be NULL, but on one NULL per column is allowed.
  • A unique constraint can be referenced by a Foreign Key Constraint.

Primary and Unique Key Comparison

The main difference between a primary and unique key comes from its intended use. A primary key main job is to uniquely identify a row within a table. The fancy name for this is entity integrity. It an important job. For a table to be truly relational, it must have a primary key defined.

So, if the primary key uniquely identified rows, then why bother with unique keys?

Well, the main job of the unique key allows you to place additional unique conditions on your columns. In our example above, we see that though the employee table has a primary key defined as EmployeeID, there’s an opportunity to enforce uniqueness on GovernmentNumber.

So Why Not Just Use GovernmentNumber as the Primary Key?

Good question! The reason I wouldn’t is that the GovernmentNumber is generated by another organization outside your database. What would happen if the number wasn’t unique? ID analytics maintains in 2010 that 20 million Americans have multiple social security number associated with their name. Do you want your primary key to rely on government data? ??

At this point, you have a sense of the difference between a primary and unique key, but let’s summarize the differences in the following table:

primary and unique key comparison

Now that you know the differences, and see there are also a lot of similarities between primary and unique key, you may have a question when to use each. Let me answer those:

So When Should You Use a Primary Key?

Always! In my book, every table should have a primary key. It provides the main way (primary) to identify each row.

When Should You Use a Unique Key?

I would use a unique key when you have columns you know shouldn’t contain duplicates. This becomes a great way to ensure data validation. In our example, we used the GovernmentNumber, but many others occur.

In fact, you often see a pattern with the primary key is a synthetic value such as an identity value (sequence), therefore, naturally occurring unique values, such as Account Numbers become candidates for unique keys.

The post What is the difference between a primary and unique key? appeared first on Essential SQL.

This article was originally posted at https://www.essentialsql.com/primary-and-unique-key

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
PraiseThanks Pin
aaron218-Jan-18 19:17
aaron218-Jan-18 19:17 
GeneralComment Pin
Member 113888532-Jan-18 2:53
Member 113888532-Jan-18 2:53 
QuestionAnother value in using Uique Key Pin
vcj#B4kT1-Jan-18 7:33
vcj#B4kT1-Jan-18 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.