Copyright

What is a Primary Key in SQL?

Instructor: Kaitlin Oglesby

Kaitlin has a BA in political science and experience teaching.

When searching for a specific record within a database, nothing can be as useful as a primary key. Primary keys act as the identity markers within a database. In this lesson, we'll learn how to implement a primary key in SQL.

Using SQL's Primary Keys

When you are using SQL, you are likely managing massive amounts of data. After all, that is what makes SQL so much more robust than an Excel spreadsheet, much less paper records. However, with so much data, there is a high likelihood that some of that data is going to start to look very similar. So how do database managers make sure that they keep each piece of data separate from the rest?

In short, one of the most basic ways to make sure that data is kept separate from other collections of information is through the use of a primary key. A primary key is an identifier, often numeric, that applies to one relevant record in a database. For example, your driver's license number could be the primary key that identifies your driving record, while many organizations use social security numbers to identify other personal records.

Figure 1 shows a snippet of a table to hold artist information; the primary key is indicated with a key symbol (this table was created in Microsoft Access).


Figure 1: Primary Key Example
SQL Primary Key example


How Are They Used?

Two major rules apply before we can look at how a primary key is used. First of all, each primary key must be unique. Second of all, in a given column that is designated as having the primary keys, all of those keys must be assigned a value. Your driver's license does not have a blank next to the spot for license number, and neither can the value in a database be blank. Otherwise, it can't be used to identify the record in question.

So how do we use it?

First, when creating the table, we have to make sure that we acknowledge what data will be used as a primary key. To do this, we type 'PRIMARY KEY' after designating what type of data will be entered. More often than not, we will use an integer as a primary key, so the text will look like this:

id INT PRIMARY KEY

The reason we use an integer is that it is often the case that a primary key is assigned as a specific ID number. This is one of the reasons that banks and health insurance companies like using Social Security Numbers - one is assigned to every citizen at birth.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support