Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

serial-uuid-keys

SERIAL and UUID keys

Last reviewed Jun 1, 2026 Content v20260601
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches SERIAL and UUID keys: the SQL patterns, schema habits, and query reasoning you need before advancing in PostgreSQL.

ID strategy affects replication and ORM defaults—SERIAL vs UUID vs IDENTITY shows up in schema reviews.

You will apply SERIAL and UUID keys in contexts like: Modern startups, geospatial apps, and analytics-friendly OLTP systems.

Copy Postgres SQL into psql, local PostgreSQL, or DB Fiddle (PostgreSQL dialect)—use \d and EXPLAIN ANALYZE where lessons show them. The in-browser lab ships later; psql is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Surrogate primary keys identify rows independently of business data. Postgres traditionally used SERIAL; modern apps often prefer UUID for distributed ID generation.

SERIAL and IDENTITY

CREATE TABLE orders_serial (
  id SERIAL PRIMARY KEY,
  customer_id INT NOT NULL
);

CREATE TABLE orders_identity (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  customer_id INT NOT NULL
);

SERIAL is shorthand creating a sequence; IDENTITY is SQL-standard and preferred in new schemas.

Practice: Run in psql or DB Fiddle (PostgreSQL). Use your practice database from the workflow lesson.

UUID keys

CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE TABLE orders_uuid (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  customer_id INT NOT NULL
);

UUIDs avoid coordination across services; index size is larger than integers—acceptable for many OLTP tables.

Choosing a strategy

Use bigint identity for single-database monoliths; UUID when exposing IDs publicly or sharding writes. Natural keys (email) still get UNIQUE constraints.

Important interview questions and answers

  1. Q: SERIAL implementation?
    A: Creates an integer column plus a sequence default—not a true type.
  2. Q: UUID index size?
    A: 16-byte keys vs 8-byte bigint—more index pages but better for distributed systems.

Self-check

  1. What does SERIAL create behind the scenes?
  2. When might UUID beat SERIAL?

Tip: Prefer GENERATED AS IDENTITY over SERIAL in new schemas.

Interview prep

SERIAL caveat?

Shorthand for integer column plus sequence—not a true type.

UUID trade-off?

Larger indexes but good for distributed ID generation.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • UUID v4 default?
  • BIGSERIAL when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump