database-schema-design
Installation
SKILL.md
Database Schema Design
Overview
A good schema makes invalid data impossible and common queries fast. Push rules into constraints, derive indexes from real access patterns, and never ship a migration that locks or loses data.
Designing from scratch
- Entities and relationships. List nouns, their cardinality (1:1, 1:N, N:M) and ownership. N:M gets a join table.
- Keys. Surrogate primary key (bigint identity or UUIDv7). Natural uniqueness becomes a
UNIQUEconstraint, not the PK. - Columns. Right type,
NOT NULLby default, defaults where sensible. See the type table. - Constraints. Foreign keys with explicit
ON DELETE,UNIQUE,CHECK, enums or lookup tables for closed sets. - Access patterns → indexes. Write the top 5-10 queries first; index their
WHERE,JOINandORDER BYcolumns. - Lifecycle.
created_at,updated_at; soft delete only if the product needs restore or audit. - Migration plan (below).
Reviewing an existing schema
Check every table against checklist.md. Read the queries in the code too: a schema is only "wrong" relative to how it is used.