We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is a Primary Key?

M. McGee
By
Updated: May 16, 2024

A primary key is an entry in a database that is unique to a single record. This key is generally derived in one of two ways: a unique identification code from outside the database or a generated number from within the database. When the database will contain information that is always unique to the entry, such as a social security number or part identification number, then those are typically used as a primary key. When the data won’t have such an identifier, the database will often create numbers based on its internal systems to give each record a unique code.

There are three main restrictions on a primary key: existence, uniqueness and immutability. A key must exist at the time the record is made — it can’t be added in later. Each key has to be completely different from any other key. This means that common identifiers, such as name or birth date, can’t be used because it is possible that two people are born on the same day with the same name. Lastly, a primary key can never be altered once created.

Since a database can potentially have an infinite number of entries, a primary key must be infinite as well. To make sure that a database will never run out of keys, most records use a numerical code for the key. Since numbers can always get bigger and computers can simply add place-holding zeroes to older entries, a system will never run out of keys. Sometimes these numbers are based on non-unique information, but a unique identifier is added to make sure the key is viable.

Databases will use a primary key as a way of organizing data. Since the key is never repeated, that piece of information will allow the database to keep every record separate from every other. Each piece of information in a record is connected back to the key; that way, no matter what happens to the system, the database can rebuild the records from loose information.

Assigning a meaningful primary key is often seen as a better practice than auto-generating a value. This will give the record an identifier that both works as a key and provides data. In small databases, this distinction is rarely necessary, but in large systems, the extra space used by a generated key can result is serious database bloat. This will both slow the system down and make the database require significantly more storage space.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
M. McGee
By M. McGee
Mark McGee is a skilled writer and communicator who excels in crafting content that resonates with diverse audiences. With a background in communication-related fields, he brings strong organizational and interpersonal skills to his writing, ensuring that his work is both informative and engaging.
Discussion Comments
By anon316971 — On Jan 31, 2013

I can't agree with what author said about the "meaningful primary key". The primary key should be some unique meaningless identifier. Whenever you assign a meaningful primary key, I guarantee that exception will occur. If you didn't see one, you project is not big enough. For example, you would assume a SSN can be used as unique identifier, but what if the person is an undocumented, new born baby? They don't have a SSN!

By miriam98 — On Aug 29, 2011

@allenJo - One of the other purposes of primary keys is that they enable you to relate tables to each other. Table relationships are another aspect of good database design.

You may be able to relate tables without primary keys, but you will have problems later on if you try to do batch updates of the data and stuff. I also think that in this regard, numbers are indeed better candidates as primary keys.

By allenJo — On Aug 28, 2011

@hamje32 - You can certainly do that, but in my opinion there is no better primary key than a single, unique number. If you use something else – or even a composite of fields like you said – it slows down the system.

As much as some people bewail Social Security numbers as being the mark of Big Brother, there would no other practical way to uniquely track individuals in our country in my opinion.

What else would you use – a combination of last name, first name, date and location of birth? I guarantee you that you will hit duplicates using that approach eventually.

You can’t go wrong with a number that never repeats. Databases are far more efficient processing numbers than they are with text anyway.

By hamje32 — On Aug 27, 2011

@Mammmood - Yeah, another thing to point out is that you can have multiple primary keys in a table. In this case it would be a composite primary key.

If it takes, for example, three pieces of information to uniquely identify a record, then you can set primary keys on all of them. The combination of those three fields would need to be unique, otherwise the database would throw up an error and prevent you from adding a new record.

Here I am not referring to an auto number field of course, which would be unique in itself. I am referring to other fields you create which you decide to use as primary keys.

By Mammmood — On Aug 27, 2011

Primary keys are one of the first things that you learn about in good database design. To create tables that have no primary keys is to invite a nightmare.

If you don’t want to create a unique primary key on your own, some database systems have what’s called an auto number field. Basically this field starts with the number one and increments with each new record that you add. It’s a long integer field, and it can increment until you reach a certain number into the billions.

Most tables don’t have billions of records so you don’t need to worry there.

By jennythelib — On Aug 27, 2011

@robbie21 - Well, with ISBNs, you see that they had to add three more digits because 10 digits were no longer going to be enough! And I guess with social security numbers, if there were ever more than a billion Americans, they could add digits to that, too!

VINs are so long and remember they also have letters in them, which means there are more possibilities (because a letter space has 26 rather than 10 possible place fillers) so I suppose they have plenty of room for now. After all, there isn't really an infinite number of cars - there are only so many people, and only so many cars that it's practical for one person to own!

But yes, I guess those would be really good examples of primary keys. The difference I see is that the ISBN refers to a whole edition of a book - all that copies would have the same number - while a VIN is unique to one specific vehicle. (I'm a librarian and one memorable summer, I had a job in a body shop!)

By robbie21 — On Aug 26, 2011

Would ISBN (the 10- or 13-digit number you find in a book that uniquely identifies it) and VIN (vehicle identification number, that super-long number your car has) be considered primary keys? They are not infinite; they have set lengths. So do social security numbers, come to think of it.

M. McGee
M. McGee
Mark McGee is a skilled writer and communicator who excels in crafting content that resonates with diverse audiences....
Learn more
Share
https://www.easytechjunkie.com/what-is-a-primary-key.htm
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.