agentleFS
WST2002WST2002/claude-code-skills/skills/database-schema-design/SKILL.md

database-schema-design

Skill4 starsChanged 27 days agoMITView on GitHub

Use when designing a database from scratch, reviewing tables, an ERD, a Prisma/TypeORM/JPA/Django/SQLAlchemy schema or SQL migrations, adding indexes, normalizing data, or making a migration safe for production — Postgres, MySQL or SQLite.

6 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 2

    Distinctive

    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
  1. Very specific:

    Access patterns → indexes: Write the top 5-10 queries first.

    line 18
  2. Distinctive:

    Check every table against checklist.md.

    line 24
  3. Very specific:

    Add columns as nullable or with a constant default.

    line 40
  4. Very specific:

    Create indexes concurrently (CREATE INDEX CONCURRENTLY / online DDL).

    line 41
  5. Distinctive:

    Never drop a column or table in the same release that stops writing to it.

    line 43

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
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

  1. Entities and relationships. List nouns, their cardinality (1:1, 1:N, N:M) and ownership. N:M gets a join table.
  2. Keys. Surrogate primary key (bigint identity or UUIDv7). Natural uniqueness becomes a UNIQUE constraint, not the PK.
  3. Columns. Right type, NOT NULL by default, defaults where sensible. See the type table.
  4. Constraints. Foreign keys with explicit ON DELETE, UNIQUE, CHECK, enums or lookup tables for closed sets.
  5. Access patterns → indexes. Write the top 5-10 queries first; index their WHERE, JOIN and ORDER BY columns.
  6. Lifecycle. created_at, updated_at; soft delete only if the product needs restore or audit.
  7. 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.

Type choices

DataUseAvoid
Moneynumeric(12,2) or integer cents + currencyfloat, double, real
Timestampstimestamptz (Postgres) / UTC datetimelocal time without zone
Closed set of valuesenum or FK to lookup table + CHECKfree-text strings
Emailcitext or lower-cased + UNIQUEcase-sensitive unique
Booleansboolean NOT NULL DEFAULT falsenullable tri-state by accident
Texttext with CHECK (length(...)) if limits mattervarchar(255) everywhere by habit
JSONjsonb for truly flexible attributes onlyJSON for data you filter or join on

Safe migrations

  • Add columns as nullable or with a constant default; backfill in batches; then add NOT NULL.
  • Create indexes concurrently (CREATE INDEX CONCURRENTLY / online DDL).
  • Renames and drops: expand (add new) → migrate code and data → contract (drop old) across releases.
  • Never drop a column or table in the same release that stops writing to it.

Output contract

### [Data integrity | Performance | Migration risk | Design] Title
- Where: table.column (file:line)
- Problem: the bad data or slow query this allows
- Fix: exact DDL or schema change

Order by risk: data loss and integrity first, then performance, then design. When designing, deliver the full DDL or ORM schema plus the index list mapped to the queries it serves.

Common mistakes

  • Missing indexes on foreign key columns (Postgres does not create them).
  • Uniqueness enforced only in application code: races create duplicates.
  • Polymorphic owner_type + owner_id with no referential integrity.
  • Adding an index for every column instead of for actual queries.
  • Reviewing the schema without reading the queries that use it.

Made by Wageesha Tripathi · LinkedIn · Email · More skills

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.