What’s a Primary Key?

Print anything with Printful



A primary key is a unique identifier in a database, either from outside or within the system, with restrictions of existence, uniqueness, and immutability. Numeric keys are used to ensure infinite keys. Assigning a meaningful key is better than automatic generation for large databases.

A primary key is an entry in a database that is unique to a single record. This key is usually derived in one of two ways: a unique identifier from outside the database or a number generated from within the database. Where the database will contain information that is always unique to the entry, such as a social security number or party identification number, this is typically used as the 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 major restrictions on a primary key: existence, uniqueness, and immutability. A key must exist when the record is created, it cannot be added later. Each key must be completely different from any other key. This means that common identifiers, such as name or date of birth, cannot be used because it is possible for two people to be born on the same day with the same name. Finally, a primary key can never be changed once it’s created.

Since a database can potentially have an infinite number of entries, a primary key must also be infinite. To ensure that a database never runs out of keys, most records use a numeric key code. Because numbers can always grow, and computers can simply add placeholder zeros 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 ensure that the key is valid.

Databases will use a primary key to organize the data. Since the key is never repeated, that information will allow the database to keep each record separate from the other. Every piece of information in a record is linked back to the key; that way, no matter what happens to the system, the database can reconstruct the records from loose information.

Assigning a meaningful primary key is often considered a better practice than automatically generating a value. This will give the record an identifier that serves as both a key and data. In small databases, this distinction is rarely needed, but in large systems the extra space used by a generated key can lead to serious database footprint. This will slow down the system and require much more database storage space.




Protect your devices with Threat Protection by NordVPN


Skip to content