SQL · Module 10 · Lesson 21 min read

Choosing Keys: Natural, Surrogate, UUID

Natural key

A real-world value that's already unique: email, phone number, national ID, ISBN, country code.

  • ✅ Meaningful, no extra column
  • ❌ Real-world values change (people change email and phone) → every FK that references it must change too
  • ❌ Sometimes not as unique as you think, and may be sensitive (national ID)

A meaningless, generated identifier: id INT GENERATED ALWAYS AS IDENTITY or a UUID.

  • ✅ Never changes, small, simple joins
  • Keep natural keys unique with a UNIQUE constraint: email TEXT NOT NULL UNIQUE

Integer IDs vs UUIDs

INT / BIGINT identityUUID
Size4 / 8 bytes16 bytes
Index speedfastest (sequential)random v4 UUIDs scatter inserts
Guessable?yes (/users/42 → try 43)no
Created where?by the databaseanywhere (app, offline device)
Merging data from systemscollisionsno collisions
  • INT – up to 2.1 billion rows; BIGINT for big or fast-growing tables (logs, events)
  • UUIDv7 (time-ordered) combines UUID uniqueness with index-friendly ordering — a great modern option (built into PostgreSQL 18 as uuidv7())
  • Supabase uses UUIDs for users (auth.users.id)

A common combo

Internal BIGINT id for joins + a public UUID or short code for URLs and APIs.

Composite keys

Several columns together: PRIMARY KEY (trip_id, user_id). Natural for junction tables. For other tables, a surrogate id + UNIQUE on the combination is often easier to work with.

Rules for good keys

Unique, never NULL, stable (never changes), simple, and not carrying meaning you might need to change later.

Example

SQL
-- Surrogate key + natural key kept unique
CREATE TABLE merchants (
  id         BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,   -- internal, for joins
  public_id  UUID NOT NULL UNIQUE DEFAULT gen_random_uuid(),    -- safe to show in URLs
  pan_number TEXT NOT NULL UNIQUE,                              -- natural key, still unique
  name       TEXT NOT NULL
);

INSERT INTO merchants (pan_number, name) VALUES
  ('301234567', 'Bhatbhateni Supermarket'),
  ('302345678', 'Daraz Nepal')
RETURNING id, public_id, name;
--  1 | 6f1d…-… (random) | Bhatbhateni Supermarket
--  2 | a93c…-… (random) | Daraz Nepal

-- The natural key can change without touching foreign keys
UPDATE merchants SET pan_number = '309999999' WHERE id = 2;

-- Why not email as the primary key? It changes!
-- If expenses referenced users(email), changing Asha's email would require
-- ON UPDATE CASCADE through every related table.

-- Size comparison
SELECT pg_column_size(1::int)               AS int_bytes,     -- 4
       pg_column_size(1::bigint)            AS bigint_bytes,  -- 8
       pg_column_size(gen_random_uuid())    AS uuid_bytes;    -- 16

-- Composite primary key on a junction table
CREATE TABLE merchant_categories (
  merchant_id BIGINT REFERENCES merchants(id) ON DELETE CASCADE,
  category_id INT    REFERENCES categories(id) ON DELETE CASCADE,
  PRIMARY KEY (merchant_id, category_id)
);