Learn SQL
-- constraints.schema_design_basics

Schema Design from Scratch in SQL

Entities → keys → relations → constraints → indexes

--concept

Identify entities (customers, orders, lines, products), give each a surrogate key, relate via foreign keys, normalize to 3NF, add NOT NULL/CHECK/UNIQUE, index access paths, seed representative data for EXPLAIN testing. Junction tables carry M2M with composite PK plus relationship attributes. Hierarchies default to adjacency (parent_id) + recursive CTE; paths/nested sets trade write cost for subtree speed.

--how it works
1

Entities + surrogates

Customers, orders, lines, products — each with meaningless stable BIGINT IDENTITY.

2

Relate via FKs

Child holds FK; parent referenced. Explicit ON DELETE per ownership.

3

Constrain then index

NOT NULL/CHECK/UNIQUE first (correctness), access-path indexes second (speed).

4

Seed + EXPLAIN

Representative data + plan proof before shipping. M2M via junctions; trees via adjacency+CTE.

--visualization
ENTITIES+KEYSRELATIONSCONSTRAINTSINDEX+SEED
Stage 1 / 4
Entities + surrogate keys — meaningless, stable BIGINT IDENTITY.
--example
query.sql
1CREATE TABLE orders (
2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3 customer_id BIGINT NOT NULL REFERENCES customers(id),
4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
5);
Result
elementrole
ordersentity + surrogate key
customer_idFK relation
created_atconstrained + indexed

March in order: entities, keys, relations, constraints, indexes. Skipping constraints for app-only validation leaves every job, console and bug as an unguarded writer.

--variants

Orders shape

March in order, no shortcuts.

query.sql
1CREATE TABLE orders (
2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3 customer_id BIGINT NOT NULL REFERENCES customers(id),
4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
5);
Result
elementrole
ordersentity + key
customer_idFK relation
created_atconstrained
1Surrogate key never mutates with business.
2FK blocks orphans on insert.
3Seed + EXPLAIN proves customer + date paths.

M2M + trees default

Junctions for many-many; adjacency for hierarchies.

query.sql
1CREATE TABLE enrollments (student_id BIGINT REFERENCES students(id), course_id BIGINT REFERENCES courses(id), PRIMARY KEY (student_id, course_id));
2-- trees: id, parent_id + WITH RECURSIVE
Result
modelshape
M2Mjunction + composite PK
treesadjacency + recursive CTE
1Junction carries enrolled_at when relation matters.
2Adjacency writes cheap, reads recursive.
3Path/nested sets only for proven read-heavy statics.
--key facts
Orderentities → keys → relations → constraints → indexes
M2Mjunction with composite PK + attributes
Treesadjacency default; nested/path for read-heavy
--use cases
Greenfield walkthrough: entities-to-indexes in order
Interview design: orders example end-to-end
--under the hood
FKs are planner hints too — join selectivity improves with declared relations.
Seeded EXPLAIN catches missing FK indexes before parent deletes stall.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.