Skip to content
Commitpedia
Log in Join & edit

What is a primary key?

A primary key is the column that uniquely identifies each row in a table, like an ID number nobody else can have.

Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?

Edit history of What is a primary key?: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
1 min

A primary key is one column (or a small set of columns) whose value is different for every row and never empty. It is the national ID number of a table row: two people may share a name, but not an ID. The database enforces this rule and builds an index on it automatically, so finding a row by its key is instant.

Most tables use an auto-incrementing integer called id or a random UUID. Natural values like email addresses can work but change over time, so a surrogate key is safer. Other tables refer to this key through foreign keys, which is how a relational database links orders to customers or comments to posts.

CREATE TABLE customers (
  id SERIAL PRIMARY KEY,
  email TEXT UNIQUE NOT NULL
);

See also: What is a foreign key? and What is an index (database)?

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

Repos in the wild

  • DuckDB duckdb/duckdb An in-process analytical SQL database. Run fast SQL directly on CSV, Parquet and JSON files, from the command line or inside Python and other languages. GitHub
  • SQLite (official Git mirror) sqlite/sqlite The source tree of SQLite, the self-contained SQL database engine embedded in phones, browsers and countless applications, mirrored to GitHub by its developers. GitHub

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Dominik +775 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?