Skip to content
uencoder

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 KEY
    Output
    Auto-incrementing primary key

    PostgreSQL

Common errors

Using a business field as the primary key (it may change)
Use a meaningless auto-increment ID or UUID

Related terms

References