Skip to main content

Primary Key

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/primary-key

In short

A primary key is a column, or set of columns, whose value uniquely identifies each row in a database table and can never be empty or duplicated.

What is a primary key?

A primary key is the unique identifier of each row in a table, like a student ID number that no two students share. The database enforces two rules on it: every value must be unique, and no value can be NULL. Each table can have only one primary key, although that key can be made of several columns.

Primary keys are either natural or surrogate. A natural key uses real-world data that is already unique, such as a book's ISBN, while a surrogate key is an artificial value with no meaning of its own, usually an auto-incrementing integer or a UUID. Most applications prefer surrogate keys, because real-world values like email addresses can change or turn out not to be unique after all.

The database automatically creates an index on the primary key, so looking up a row by its key is very fast. Other tables refer to a row by storing its primary key in a foreign key column, which is how relational databases link, for example, orders to the customers who placed them. When a key spans several columns, such as (order_id, product_id), it is called a composite primary key.

A primary key is often confused with a unique constraint and with a foreign key. A unique constraint also prevents duplicates, but a table can have many of them and, in most databases, they allow NULL values, while a foreign key is a reference from one table to another table's key. In short, a primary key identifies a row, and a foreign key points to one.

Key takeaways

  • A primary key uniquely identifies every row in a table.
  • Primary key values must be unique and cannot be NULL.
  • A table has only one primary key, but it can span several columns.
  • Surrogate keys such as auto-increment IDs or UUIDs are the most common choice.
  • Primary keys are indexed automatically and referenced by foreign keys.

Example

Single-column and composite primary keyssql
-- A surrogate primary key generated by the database
CREATE TABLE customers (
  id    BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email TEXT NOT NULL UNIQUE,
  name  TEXT NOT NULL
);

-- A composite primary key: one row per product in each order
CREATE TABLE order_items (
  order_id   BIGINT,
  product_id BIGINT,
  quantity   INT NOT NULL,
  PRIMARY KEY (order_id, product_id)
);

Readers ask

What is the difference between a primary key and a foreign key?

A primary key uniquely identifies each row in its own table. A foreign key is a column in another table that stores a primary key value to reference that row, creating a link between the two tables.

Can a table have two primary keys?

No. A table can have only one primary key, but that key can combine several columns into a composite primary key, and you can add separate UNIQUE constraints to other columns that must not repeat.

Should I use an auto-increment ID or a UUID as a primary key?

Auto-increment integers are compact and fast to index, but they reveal roughly how many rows exist and are harder to generate across several servers. UUIDs can be created anywhere without conflicts, and time-ordered versions such as UUIDv7 avoid the index slowdowns caused by fully random UUIDs.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings