Primary Key
A primary key is the column or column combination that uniquely identifies each row in a table — non-null and unique; the row identity.
Purpose
To give every row a unique identity, supporting exact lookup and relational references.
Mechanism
Primary key values are unique and non-null (NOT NULL + UNIQUE). Can be single-column or composite. Usually auto-incrementing integers or UUIDs. The clustered index is often built on the primary key (row data stored in primary-key order). Key choice affects insert performance and storage.
Use cases
- Row unique identification
- Foreign key reference target
- Clustered index
- ORM entity identity
Examples
- Input
id BIGSERIAL PRIMARY KEYOutputAuto-incrementing primary keyPostgreSQL
Common errors
- Using a business field as the primary key (it may change)
- Use a meaningless auto-increment ID or UUID
Related terms
Foreign Key
A foreign key is a column referencing the primary key of another table, establishing referential integrity between tables and preventing orphaned references. GLOSSARY
Index
An index is an auxiliary data structure (e.g., B+ tree, hash) maintained to speed up queries, trading space for time at the cost of write overhead. GLOSSARY
JOIN
JOIN is the SQL operation combining rows of multiple tables by a join condition; types include inner, left/right/full outer, and cross joins. GLOSSARY
Normalization
Database normalization is the process of decomposing tables to eliminate redundancy and update anomalies, with normal forms from 1NF to BCNF/5NF growing stricter. GLOSSARY