design-relational-schema
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Designs a normalized relational schema from requirements — entities, relationships, PK strategy (surrogate bigint vs natural vs UUIDv7/ULID), 1:1/1:N/M:N and inheritance modeling, 3NF/BCNF normalization, invariants encoded as UNIQUE/CHECK/FK/exclusion constraints, and deliberate read-path denormalization with stated consistency tradeoffs.
In this repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-realtime-channelSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- db-migration-safetySkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-multi-tenancySkill
- design-protobuf-grpc-serviceSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- dockerfile-optimizeSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 123
Constraint coverage: Every invariant you stated in step 1 maps to an actual NOT NULL/UNIQUE/CHECK/FK/exclusion/partial-index in the DDL — …
- Very specific: line 124
Referential integrity: Every FK names an explicit ON DELETE action.
- Distinctive: line 125
Key sanity: Every table has a PK; no natural key that can change is used as a PK.
- Very specific: line 126
Type sanity: No money in float; timestamps are timestamptz (UTC).
- Distinctive: line 127
Access-pattern map: Every listed top query is served by an existing index/PK.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
Reach for this skill when you're designing the shape of the data, before any table exists:
- "Model these requirements / this ERD as tables"
- "Should this PK be a UUID or a bigint? natural or surrogate? composite?"
- "How do I model users↔roles (M:N) / orders→items (1:N) / a polymorphic comment?"
- "Normalize this — I've got repeating columns / update anomalies / duplicated data"
- "Where should I denormalize for a read-heavy dashboard, and what breaks?"
- Choosing column types: enum vs lookup table, soft vs hard delete, audit columns, money/time precision
NOT this skill:
- Changing a table that already has rows/traffic (locks, backfills, rollback) → db-migration-safety
- A query against an existing schema is slow → optimize-sql-query
- You need an append-only, replayable, audit-complete domain model → design-event-sourcing-cqrs
- Computing prices/tax/rounding/FX (the math, not the column type) → money-decimal-arithmetic
- Storing/converting/comparing timestamps & DST correctly → datetime-timezone-correctness
- Shaping items/documents for a non-relational store (DynamoDB/Mongo/Cassandra) around access patterns → model-nosql-data
Steps
- Extract entities, attributes, relationships from requirements — nouns→tables, verbs→relationships. List each entity, its attributes, and for every pair the cardinality (1:1 / 1:N / M:N) and optionality (mandatory vs nullable side). Mark each attribute's identity role: is it a candidate key (naturally unique, immutable), or descriptive? Write functional dependencies (
A → B: A determines B) — they drive normalization in step 3. One table = one entity type; if an attribute is itself a list ("tags", "phone numbers"), it's a separate table, not a CSV column orjsonbdumping ground. Pick a PK strategy per table — default to surrogate, choose the integer/UUID flavor deliberately.
Strategy Use when Avoid when bigint GENERATED ALWAYS AS IDENTITYSingle-DB, internal IDs, smallest/fastest index, FK-heavy — default IDs leak to clients/URLs and count/sequence is sensitive; multi-master inserts uuidv7 / ULID (time-ordered)IDs generated client-side or across shards, exposed in URLs, need merge without collision You can use bigint and don't expose the ID — 16B vs 8B and bigger indexes uuidv4 (random)Only if unguessability matters and you accept index-locality cost Hot insert paths — random UUIDs fragment B-tree pages and bloat WAL Natural key (email, ISO code, slug) Truly immutable, single-attribute, externally governed ( country.iso2,currency.code)It can ever change or isn't guaranteed unique — a changing PK cascades through every FK Composite key Junction tables ( (a_id, b_id)); rows identified only by the combinationA tempting single surrogate would be simpler and the combo isn't queried as a unit Rules: use a surrogate
bigintIDENTITY by default; reach for UUIDv7/ULID (not v4) the moment IDs cross a process boundary or are client-generated; never expose a sequential surrogate where the sequence is sensitive (use UUIDv7 instead); a natural key still deserves aUNIQUEconstraint even when you also keep a surrogate PK. Never useserial/SERIAL(legacy, ownership/permission footguns) — useGENERATED ALWAYS AS IDENTITY.Normalize 1NF → 2NF → 3NF/BCNF; stop at BCNF. Eliminate the anomaly classes in order:
- 1NF — atomic columns, no repeating groups, no arrays-as-CSV. Split
phone1, phone2, phone3andtags TEXTinto child rows. - 2NF — no non-key attribute depends on part of a composite key. In
order_item(order_id, product_id, product_name),product_namedepends only onproduct_id→ move it toproduct. - 3NF — no transitive dependency (non-key → non-key).
employee(id, dept_id, dept_name):dept_namedepends ondept_id, notid→ split outdepartment. - BCNF — every determinant is a candidate key. Fixes the rare overlapping-candidate-key case 3NF misses.
Target 3NF as the floor, BCNF where a determinant anomaly exists. Each non-key fact lives in exactly one place; a fact changes via exactly one
UPDATEto one row. Do not model attribute-value pairs generically (EAV:entity/attribute/valuerows) — it destroys typing, FKs, and constraints; make real typed columns instead.- 1NF — atomic columns, no repeating groups, no arrays-as-CSV. Split
- Model cardinalities explicitly — the FK lives on the "many" side.
- 1:N — FK column on the child (many) side pointing at the parent's PK.
order.customer_id → customer.id. The direction is not a choice: the many side carries the FK. - M:N — a junction (associative) table with a composite PK of both FKs:
enrollment(student_id, course_id, PRIMARY KEY(student_id, course_id)). Relationship attributes (enrolled_at,grade) live on the junction. - 1:1 — share a PK: the dependent table's PK is an FK to the parent (
user_profile.user_id PK REFERENCES user(id)). Use only for optional/rarely-loaded columns; otherwise just add the columns to the parent. Inheritance/polymorphism — pick one, don't mix:
Pattern Shape Use when Single-table one table, nullable subtype columns, kinddiscriminatorfew subtypes, mostly shared columns — default Class-table base table + one child table per subtype, shared PK subtypes have many distinct, NOT-NULL-able columns Concrete-table one full table per subtype, no base subtypes never queried together For a polymorphic FK ("comment on a post or a photo"), avoid the nullable-
(target_type, target_id)pair — it can't have a real FK. Prefer separate nullable FK columns each with its own realREFERENCESplus aCHECKthat exactly one is set.
- 1:N — FK column on the child (many) side pointing at the parent's PK.
Encode every invariant as a constraint in the DDL, not in app code. If the database can enforce it, the database enforces it — app checks race and drift.
NOT NULLon every column that is logically required (default to NOT NULL; justify each nullable column).UNIQUEon each natural/candidate key and on business-unique combos.FOREIGN KEY ... ON DELETE— choose the action deliberately:CASCADE(children are parts of the parent),RESTRICT/NO ACTION(default; refuse to orphan),SET NULL(only if the FK is legitimately optional).CHECKfor value rules (amount_minor >= 0,status IN (...),start_at < end_at).- Partial unique index for conditional uniqueness:
CREATE UNIQUE INDEX ON users(email) WHERE deleted_at IS NULL;(one active email, history allowed). - Exclusion constraint for "no overlap" (e.g. no double-booking a room):
EXCLUDE USING gist (room_id WITH =, during WITH &&).
CREATE TABLE booking ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, room_id bigint NOT NULL REFERENCES room(id) ON DELETE RESTRICT, guest_id bigint NOT NULL REFERENCES guest(id) ON DELETE RESTRICT, during tstzrange NOT NULL, status text NOT NULL DEFAULT 'held' CHECK (status IN ('held','confirmed','cancelled')), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT no_double_booking EXCLUDE USING gist (room_id WITH =, during WITH &&) WHERE (status <> 'cancelled') );- Decide the cross-cutting column conventions once, apply everywhere.
- Soft vs hard delete — default hard delete with
ON DELETErules. Use soft delete (deleted_at timestamptz NULL) only when you must retain history or undo; then every uniqueness and FK must account for it (partial indexesWHERE deleted_at IS NULL, filtered FKs) or you reintroduce duplicates and dangling references. - Audit columns —
created_at timestamptz NOT NULL DEFAULT now(),updated_at timestamptz NOT NULL DEFAULT now()(kept current by a trigger), andcreated_by/updated_byFKs where attribution matters. Full row history → separate*_historytable or → design-event-sourcing-cqrs, not bolted onto the live row. - Enum vs lookup table — small, fixed, code-coupled set (
status) →CHECK (x IN (...))or a native enum. Editable-by-users or carrying extra attributes (label, sort order, active flag) → a lookup table with an FK. Don't ship aroleslookup table of three forever-fixed values; don't ship a CHECK list for something product managers edit weekly. - Types — money as
NUMERIC(precision, scale)or integer minor units, neverfloat/real/double(see money-decimal-arithmetic); timestamps astimestamptzstoring UTC instants, never naivetimestamp(see datetime-timezone-correctness); text astext(notvarchar(n)unless a real domain limit exists); booleans asboolean, not0/1ints or'Y'/'N'.
- Soft vs hard delete — default hard delete with
Denormalize only on a measured read hot path, and write down what you traded. Start fully normalized; denormalize a specific column only when a real, frequent read can't be served cheaply by a join/index. Each denormalization is a stated consistency contract:
Technique Buys you Costs you (must be stated) Derived/rollup column ( post.comment_count)O(1) read, no aggregate join Must update on every child write — trigger or app, or it drifts Duplicated parent attribute ( order_item.product_nameat sale time)Stable historical snapshot Diverges from source by design — that's the point; document it Materialized view Precomputed report Staleness window; explicit REFRESH(concurrently) neededPre-joined wide read table Single-table dashboard read Whole second write path to keep in sync Default: keep it normalized and add an index first. A rollup counter maintained by a trigger is acceptable; copying mutable data you then have to keep in sync in two places is a liability — only when the read win is proven. Record for each: what's duplicated, who keeps it consistent, and the acceptable staleness.
- Output the DDL plus an access-pattern → table/index map. Deliver: (a)
CREATE TABLEstatements with all constraints from steps 5–6, (b) the FK graph, (c) a table mapping each top query/access pattern to the table(s) and the index that serves it (so every hot read has a supporting index and no table has unused indexes). This map is the proof the schema serves the real queries, not just an abstract model.
Common Errors
- EAV ("flexible schema") tables.
entity/attribute/valuerows throw away typing, FKs, and constraints and turn every read into a self-join pivot. Use real typed columns; if attributes are genuinely open-ended, a single typedjsonbcolumn beats EAV. - Float money.
price floatloses cents to binary rounding —0.1 + 0.2 ≠ 0.3. UseNUMERICor integer minor units; defer the math rules to money-decimal-arithmetic. - Nullable-FK soup / polymorphic
(type, id). Aparent_type text, parent_id bigintpair can't have a foreign key, so the DB can't stop dangling references. Use separate real FK columns + aCHECKthat exactly one is non-null. - Natural key as PK that later changes. Making
emailor a username the PK means a single edit cascades through every referencing FK. Keep a surrogate PK; putUNIQUEon the natural key. - Random UUID (v4) PK on a hot insert path. Random keys scatter B-tree inserts, bloating the index and WAL. Use UUIDv7/ULID (time-ordered) when you need a UUID, or a
bigintwhen the ID isn't exposed. - Soft delete without filtered constraints.
deleted_atplus a plainUNIQUE(email)blocks a user from re-registering a freed email, and plain FKs still "see" deleted parents. Make uniqueness and lookups partial:WHERE deleted_at IS NULL. - Over-normalizing tiny fixed sets. A 3-value lookup table joined on every query adds a join for no benefit. A
CHECK (x IN (...))enum is fine for small, code-coupled, rarely-changing sets. - Storing lists in a column.
tags TEXTas CSV (or an unindexed array) can't be FK'd, constrained, or joined cleanly. Model it as a child/junction table. varchar(255)cargo-culting and naivetimestamp. Arbitrary length caps cause silent truncation;timestampwithout time zone loses the offset. Usetextandtimestamptz.- Missing
ON DELETEaction. Defaulting blindly leaves you with either accidental orphans or surprise cascade deletes. ChooseCASCADE/RESTRICT/SET NULLper FK on purpose. - Denormalizing speculatively. Duplicating data "for speed" before any query proves slow doubles your write paths and invites drift. Normalize first, index, measure, then denormalize the proven hot path.
Verify
- 3NF check: For each table, every non-key column depends on the key, the whole key, and nothing but the key. Name any transitive (
non-key → non-key) or partial dependency you allowed and justify it as a deliberate denormalization — otherwise split it. - Anomaly probe: Pick one update, one insert, and one delete per core entity. Confirm each touches exactly one row in one place with no way to leave the data inconsistent (no second copy to forget).
- Constraint coverage: Every invariant you stated in step 1 maps to an actual
NOT NULL/UNIQUE/CHECK/FK/exclusion/partial-index in the DDL — not to an app-layer comment. List any invariant not enforced by the DB and why. - Referential integrity: Every FK names an explicit
ON DELETEaction; no polymorphic(type, id)pair lacks a real FK; every junction table has a composite PK of its two FKs. - Key sanity: Every table has a PK; no natural key that can change is used as a PK; sequential surrogates aren't exposed where the sequence is sensitive; UUID columns are v7/ULID unless v4 is justified.
- Type sanity: No money in
float; timestamps aretimestamptz(UTC); no CSV/array masquerading as a relationship; enums vs lookup chosen per the step-6 rule. - Access-pattern map: Every listed top query is served by an existing index/PK; every index supports at least one stated query (no orphan indexes); each denormalized column has a named owner-of-consistency and a stated staleness bound.
Done = the schema is at 3NF (BCNF where a determinant anomaly existed) with every stated invariant enforced by a DB constraint, every PK/FK and ON DELETE chosen deliberately, no float money / naive timestamps / EAV / polymorphic-FK soup, and an access-pattern→table/index map in which every hot read has a supporting index and every denormalization carries a written consistency tradeoff.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

