What is a PRIMARY KEY?
SQLLevel: FoundationFundamentals (Easy)
What is a PRIMARY KEY?
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
A primary key is a column (or combination of columns) that uniquely identifies each row in a table. It implies NOT NULL and UNIQUE. A table can have only one primary key.
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);
Internally, the primary key usually creates a clustered index (the physical sort order of the table on disk). In PostgreSQL it creates a unique B-tree index but doesn't physically cluster rows unless you run CLUSTER.
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT