Design a Database Schema
Purpose
Produce a schema that enforces the domain's invariants in the database, supports the real access patterns efficiently, and can evolve through safe migrations.
When to use
- A feature introduces new entities or relationships that must be stored.
- An existing schema is extended and the change must stay backward compatible.
- Query performance or data integrity problems point to the table design.
When not to use
- Enterprise or cross-system data modeling. Use
logical-data-model or conceptual-data-model.
- Only an index is needed for a slow query. Use
index-recommendation or query-optimization.
- Rolling out a schema change to production. Use
schema-migration-plan.
Inputs
Required:
- The domain description or entities with their rules, and the target database engine (or "relational, engine-neutral").
Optional, improves quality:
- Main read/write access patterns and expected volumes, retention needs, multi-tenancy model.
- Naming conventions, existing schema, ID strategy, audit/soft-delete policy.
If the engine is unknown, write portable SQL and flag engine-specific choices as [ENGINE-SPECIFIC].
Process
- Extract entities, attributes and relationships with cardinalities; list the invariants in plain language ("a room cannot have overlapping bookings").
- Normalize to 3NF by default; denormalize only for a named access pattern and state the consistency cost.
- Choose keys: surrogate primary key strategy (sequence, UUIDv7/ULID for distributed inserts), natural unique keys as
UNIQUE constraints.
- Choose column types precisely: exact decimals for money with separate currency, timestamps with time zone in UTC, bounded text lengths, no free-form strings for enumerations without a check or lookup table.
- Enforce invariants in the database where possible:
NOT NULL, CHECK, UNIQUE, foreign keys with deliberate ON DELETE behavior, exclusion or partial unique constraints where the engine supports them.
- Derive indexes from access patterns: each foreign key used in joins, composite indexes in filter-then-sort order, covering indexes only for hot queries; note write cost.
- Decide cross-cutting columns: tenant id, audit (created/updated by and at), optimistic concurrency version, soft delete (and how uniqueness works with it).
- Mark personal data columns, and state retention and masking needs.
- Write DDL and a migration outline that is backward compatible (expand, backfill, switch, contract).
- List assumptions and open questions, especially on volumes and deletion rules.
- If the goal continues, suggest
schema-migration-plan to roll the schema out safely or index-recommendation once real query patterns are known.
Output format
# Schema Design: <feature>
Engine: <engine or engine-neutral> · Assumptions: <list>
## Entities and Invariants
| Entity | Key attributes | Invariants |
## Tables
### <table>
| Column | Type | Null | Default | Constraint | Notes (PII?) |
## Relationships
| From | To | Cardinality | FK on delete |
## Indexes
| Index | Columns | Serves access pattern | Write cost note |
## DDL
```sql
CREATE TABLE ...
```
## Migration Outline
1. Expand ... 2. Backfill ... 3. Switch ... 4. Contract ...
## Open Questions
Quality checklist
Common pitfalls
- Enforcing uniqueness or non-overlap only in application code; concurrent requests will break it. Use constraints or explicit locking.
- Soft delete that silently breaks unique constraints. Use partial unique indexes or include the deletion marker.
- Storing local time without zone, or floats for money.
- Generic "entity-attribute-value" or JSON columns for data that is queried and validated. Use them only for truly schemaless extensions.
Example
Input: "Rooms, bookings with start/end, attendees, no overlapping bookings per room. Engine: PostgreSQL."
Excerpt of output:
booking(id uuid PK, room_id FK → room ON DELETE RESTRICT, period tstzrange NOT NULL, organizer_id FK, version int NOT NULL DEFAULT 0)
- Invariant enforced:
EXCLUDE USING gist (room_id WITH =, period WITH &&) [ENGINE-SPECIFIC]; portable fallback: serialize inserts per room with SELECT ... FOR UPDATE on the room row.
- Index:
booking(room_id, lower(period)) for "room calendar for a day".
- Open question: Are cancelled bookings kept? If yes, the exclusion must ignore them
[TBD].
1---2name: database-schema-design3description: "Designs a relational database schema from a domain description: tables, columns and types, primary and foreign keys, constraints, indexes driven by access patterns, and a migration script outline. Use when a new feature needs persistent storage, an existing schema must be extended, or someone asks for tables, an ER model, DDL or indexes for a domain."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/database-schema-design7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: design12 title: "Design a database schema"13 related: "logical-data-model, data-requirements, schema-migration-plan, index-recommendation, aggregate-design"14 prompt: "Design the database schema for a meeting room booking feature: rooms, bookings with start/end time, attendees, and no overlapping bookings per room."15---1617# Design a Database Schema1819## Purpose20Produce a schema that enforces the domain's invariants in the database, supports the real access patterns efficiently, and can evolve through safe migrations.2122## When to use23- A feature introduces new entities or relationships that must be stored.24- An existing schema is extended and the change must stay backward compatible.25- Query performance or data integrity problems point to the table design.2627## When not to use28- Enterprise or cross-system data modeling. Use `logical-data-model` or `conceptual-data-model`.29- Only an index is needed for a slow query. Use `index-recommendation` or `query-optimization`.30- Rolling out a schema change to production. Use `schema-migration-plan`.3132## Inputs33Required:34- The domain description or entities with their rules, and the target database engine (or "relational, engine-neutral").3536Optional, improves quality:37- Main read/write access patterns and expected volumes, retention needs, multi-tenancy model.38- Naming conventions, existing schema, ID strategy, audit/soft-delete policy.3940If the engine is unknown, write portable SQL and flag engine-specific choices as `[ENGINE-SPECIFIC]`.4142## Process431. Extract entities, attributes and relationships with cardinalities; list the invariants in plain language ("a room cannot have overlapping bookings").442. Normalize to 3NF by default; denormalize only for a named access pattern and state the consistency cost.453. Choose keys: surrogate primary key strategy (sequence, UUIDv7/ULID for distributed inserts), natural unique keys as `UNIQUE` constraints.464. Choose column types precisely: exact decimals for money with separate currency, timestamps with time zone in UTC, bounded text lengths, no free-form strings for enumerations without a check or lookup table.475. Enforce invariants in the database where possible: `NOT NULL`, `CHECK`, `UNIQUE`, foreign keys with deliberate `ON DELETE` behavior, exclusion or partial unique constraints where the engine supports them.486. Derive indexes from access patterns: each foreign key used in joins, composite indexes in filter-then-sort order, covering indexes only for hot queries; note write cost.497. Decide cross-cutting columns: tenant id, audit (created/updated by and at), optimistic concurrency version, soft delete (and how uniqueness works with it).508. Mark personal data columns, and state retention and masking needs.519. Write DDL and a migration outline that is backward compatible (expand, backfill, switch, contract).5210. List assumptions and open questions, especially on volumes and deletion rules.5311. If the goal continues, suggest `schema-migration-plan` to roll the schema out safely or `index-recommendation` once real query patterns are known.5455## Output format56````markdown57# Schema Design: <feature>58Engine: <engine or engine-neutral> · Assumptions: <list>5960## Entities and Invariants61| Entity | Key attributes | Invariants |6263## Tables64### <table>65| Column | Type | Null | Default | Constraint | Notes (PII?) |6667## Relationships68| From | To | Cardinality | FK on delete |6970## Indexes71| Index | Columns | Serves access pattern | Write cost note |7273## DDL74```sql75CREATE TABLE ...76```7778## Migration Outline791. Expand ... 2. Backfill ... 3. Switch ... 4. Contract ...8081## Open Questions82````8384## Quality checklist85- [ ] Every stated invariant is enforced by a constraint or explicitly assigned to application code with a reason.86- [ ] Every index maps to an access pattern; no speculative indexes.87- [ ] Money, time and identifiers use precise, unambiguous types.88- [ ] Foreign key delete behavior is chosen deliberately, not left to default.89- [ ] Personal data columns are marked and retention is addressed.90- [ ] The migration can run without downtime or states why it cannot.91- [ ] Inferences are labeled `[ASSUMPTION]` and listed as assumptions or open questions; nothing unsupported is stated as fact.92- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.9394## Common pitfalls95- Enforcing uniqueness or non-overlap only in application code; concurrent requests will break it. Use constraints or explicit locking.96- Soft delete that silently breaks unique constraints. Use partial unique indexes or include the deletion marker.97- Storing local time without zone, or floats for money.98- Generic "entity-attribute-value" or JSON columns for data that is queried and validated. Use them only for truly schemaless extensions.99100## Example101Input: "Rooms, bookings with start/end, attendees, no overlapping bookings per room. Engine: PostgreSQL."102103Excerpt of output:104- `booking(id uuid PK, room_id FK → room ON DELETE RESTRICT, period tstzrange NOT NULL, organizer_id FK, version int NOT NULL DEFAULT 0)`105- Invariant enforced: `EXCLUDE USING gist (room_id WITH =, period WITH &&)` `[ENGINE-SPECIFIC]`; portable fallback: serialize inserts per room with `SELECT ... FOR UPDATE` on the room row.106- Index: `booking(room_id, lower(period))` for "room calendar for a day".107- Open question: Are cancelled bookings kept? If yes, the exclusion must ignore them `[TBD]`.