Luôn dùng surrogate key (BIGSERIAL/UUID) làm PK và đặt UNIQUE constraint trên natural key — natural key có thể thay đổi hoặc duplicate trong thực tế; UUID v7 là lựa chọn tốt nhất: globally unique và B-tree friendly.
- Natural key: dữ liệu thực có business meaning — email, SSN, ISBN, phone.
- Ưu: không cần join thêm để lấy giá trị, tự nhiên unique.
- Nhược: có thể thay đổi (user đổi email → phải update tất cả FKs), có thể không unique thực tế (SSN có thể duplicate do lỗi), lộ business data trong URLs.
- Surrogate key: ID nhân tạo không có business meaning — SERIAL/BIGSERIAL (auto-increment integer), UUID.
- Ưu: immutable (không bao giờ thay đổi), stable FK references, không lộ business info, đơn giản.
- Nhược: cần thêm UNIQUE constraint trên natural key nếu cần enforce uniqueness.
- Best practice: luôn dùng surrogate PK + UNIQUE constraint trên natural key nếu cần —
id BIGSERIAL PRIMARY KEY, email TEXT UNIQUE NOT NULL. - Candidate key: bất kỳ column/set nào có thể làm PK (unique + not null) — chọn một làm PK, còn lại add UNIQUE constraint.