Skip to content
Databasesbeginner

What is a primary key?

Updated Nov 30, 20263 min readBased on official documentation
What is a primary key?

In short

A primary key is a column, or a group of columns, that uniquely identifies each row in a database table. No two rows can share the same primary key value, which lets you find, update and link a single row reliably.

A database table stores rows, and each row describes one thing, such as one product or one customer. A primary key is the rule that tells the database which column identifies each row. You pick one column, or a small group of columns, and the database makes sure that value is never repeated across the table.

How it works

A primary key combines two rules. The values must be unique, so no two rows can have the same key. The key also must not be empty, which means it cannot hold a null value. Null is the SQL word for a missing value. Because of these rules, the key gives you a dependable way to point at one row.

The PostgreSQL documentation describes constraints, which are rules that limit what can be stored in a table. A unique constraint makes sure a column, or a group of columns, has no duplicate values across all rows. It also explains that a unique constraint automatically creates an index, a structure that helps the database find rows quickly. A primary key builds on the same idea and adds the no-empty-value rule. If a user tries to store data that breaks a rule, the database raises an error and rejects the change.

When to use it

Every table should have a primary key. Without one, you cannot easily tell two identical-looking rows apart, and it becomes hard to update or delete one specific row safely. Good candidates are an ID number that the table generates for each new row, or a code that is already guaranteed to be unique, such as a product number that the business assigns.

Use a single column when you can. A group of columns can work as a key too, but it adds complexity. Keep the key short and stable, so its value does not need to change after the row is created.

A simple example

Here is a small table of products where the product number is the primary key. The documentation shows a similar style, using the UNIQUE and NOT NULL rules that a primary key relies on:

CREATE TABLE products ( product_no integer PRIMARY KEY, name text NOT NULL, price numeric CHECK (price > 0) );

With this table, inserting two rows with product_no 101 fails. The database also refuses a row where product_no is missing. The price check rule rejects zero or negative prices, which shows how several constraints can work together in one table.

Common mistakes

A common mistake is to treat a column as unique just because it looks unique today. Names, email addresses and other real-world values can change or repeat, so they make weak keys. Another mistake is to forget that a unique rule alone allows nulls in some cases. The documentation notes that by default two null values are not considered equal in a unique constraint, so a primary key is the stronger choice when you need every row to be identifiable.

Avoid changing primary key values after they are set. Other tables often refer to them, and changing them can break those links.

Alternatives

A unique constraint is a close relative. It also prevents duplicates, but a table can have several unique constraints while only one primary key. A unique constraint can also allow missing values, while a primary key does not. For a rule that applies only to some rows, the documentation suggests a unique partial index instead.

If you want to learn more about the rules that shape a table, read about the other constraint types, such as check constraints and not-null constraints. They give you finer control over the data each column accepts.

FAQ

Can a table have more than one primary key?

No. A table can have only one primary key, but that key can be made from several columns. Other rules, such as unique constraints, can cover additional columns.

Do I need a primary key on every table?

It is strongly recommended. Without one, it is hard to identify a single row reliably, and many tools and relationships between tables expect a primary key to exist.

Should I use a name or an email as the primary key?

Usually no. Names and emails can change or be reused, which makes changing links painful. A generated number that never changes is a safer choice for most tables.

What happens if I insert a duplicate primary key?

The database raises an error and does not save the new row. The existing row stays unchanged, so your data remains consistent.

Sources

This page is based on the official documentation below. Read it for full detail.

Primary key — official documentation ↗

Primary key in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.