Schema Design
Purpose
Structures data for integrity, performance, and scalability. Responsibility boundary: designs and reviews logical schemas (entities, keys, constraints, normalization); does NOT write migration scripts, tune queries, or modify application code.
When to Use / When NOT to Use
- Use: New features, data modeling, core domain entities.
- NOT: Simple key-value caches or ephemeral logs.
- NOT: Writing or running migration scripts (use
database-migrations or database-postgres-migrations).
- NOT: Indexing and slow-query tuning (use
database-query-optimization).
Prerequisites & Assumptions
- Domain understanding. ACID requirements.
Scope & Context Resolution
- Supported scopes: whole database/schema, single table or entity, relationship between entities, or a schema artifact (DDL file, ERD). Default: the entities or feature the user names; if none, the domain model described in the prompt.
- Rule 1 - Explicit context: user-stated requirements, constraints, versions, targets and scope are authoritative. Apply this skill strictly within them. MUST NOT add assumptions or expand beyond them. If a user instruction conflicts with a skill rule, follow the user and state the deviation in one line, EXCEPT security/data-loss rules, which stay mandatory (flag the conflict instead).
- Rule 2 - No extra context: use this skill's Purpose, workflow and rules as the sole context, at the default scope. MUST NOT import unrelated requirements or assumptions.
- Rule 3 - Explicit scope: operate ONLY inside the named scope. Out-of-scope files MAY be read as evidence but MUST NOT be modified; report out-of-scope observations as notes, never as changes. Widening scope requires explicit user approval.
- Mutation boundary: design output and DDL/ERD files inside the resolved scope only; MUST NOT alter existing tables or data outside it.
Step-by-Step Workflow
- Resolve scope and context: apply the section above; state the resolved scope and mutation boundary in one line at the start of the output.
- Identify entities & relationships within the resolved scope.
- Normalize to 3NF to prevent anomalies.
- Denormalize for read performance if proven bottleneck.
- Enforce constraints (PK, FK, Check, Not Null).
Enterprise Rules & Standards (MUST / SHOULD / MUST NOT)
- MUST: Define Primary and Foreign Keys.
- MUST: Use ENUMs or Check constraints for limited values.
- MUST: Stay within the resolved scope; MUST NOT modify or expand beyond it without explicit user approval.
- SHOULD: Denormalize only with proven read bottlenecks.
- MUST NOT: Store JSON blobs for highly queryable data.
- MUST NOT: Redesign tables or relationships outside the named scope.
Examples (good vs. bad, with realistic code or output)
- Bad: Storing CSV tags in a TEXT column.
- Good: Relational
user_tags table with proper FKs.
- Good (scope): Prompt "design only the
invoices table" -> define invoices with PK, FK to customers, CHECK on status; note any customers issues without changing them.
- Bad (scope): Same prompt -> renormalizing
customers, orders and payments as well.
Checklist (verifiable, copy-pasteable)
Common Pitfalls
- Entity-Attribute-Value (EAV) pattern abuse.
- Missing indexes on Foreign Keys.
- Scope creep: remodeling adjacent tables when one entity was requested.
References (official docs, standards)
1---2name: database-schema-design3description: 'Normalization, denormalization trade-offs, constraint enforcement. Trigger: "design database schema", "model entity relationships", "ERD".'4metadata:5 source: https://skillmd.com/skills/pnguyen215/database-schema-design6 category: database7 tags: "database, schema, normalization, constraints, data-modeling"8---910# Schema Design1112## Purpose1314Structures data for integrity, performance, and scalability. Responsibility boundary: designs and reviews logical schemas (entities, keys, constraints, normalization); does NOT write migration scripts, tune queries, or modify application code.1516## When to Use / When NOT to Use1718- **Use**: New features, data modeling, core domain entities.19- **NOT**: Simple key-value caches or ephemeral logs.20- **NOT**: Writing or running migration scripts (use `database-migrations` or `database-postgres-migrations`).21- **NOT**: Indexing and slow-query tuning (use `database-query-optimization`).2223## Prerequisites & Assumptions2425- Domain understanding. ACID requirements.2627### Scope & Context Resolution2829- **Supported scopes**: whole database/schema, single table or entity, relationship between entities, or a schema artifact (DDL file, ERD). Default: the entities or feature the user names; if none, the domain model described in the prompt.30- **Rule 1 - Explicit context**: user-stated requirements, constraints, versions, targets and scope are authoritative. Apply this skill strictly within them. MUST NOT add assumptions or expand beyond them. If a user instruction conflicts with a skill rule, follow the user and state the deviation in one line, EXCEPT security/data-loss rules, which stay mandatory (flag the conflict instead).31- **Rule 2 - No extra context**: use this skill's Purpose, workflow and rules as the sole context, at the default scope. MUST NOT import unrelated requirements or assumptions.32- **Rule 3 - Explicit scope**: operate ONLY inside the named scope. Out-of-scope files MAY be read as evidence but MUST NOT be modified; report out-of-scope observations as notes, never as changes. Widening scope requires explicit user approval.33- **Mutation boundary**: design output and DDL/ERD files inside the resolved scope only; MUST NOT alter existing tables or data outside it.3435## Step-by-Step Workflow36371. **Resolve scope and context**: apply the section above; state the resolved scope and mutation boundary in one line at the start of the output.382. Identify entities & relationships within the resolved scope.393. Normalize to 3NF to prevent anomalies.404. Denormalize for read performance if proven bottleneck.415. Enforce constraints (PK, FK, Check, Not Null).4243## Enterprise Rules & Standards (MUST / SHOULD / MUST NOT)4445- **MUST**: Define Primary and Foreign Keys.46- **MUST**: Use ENUMs or Check constraints for limited values.47- **MUST**: Stay within the resolved scope; MUST NOT modify or expand beyond it without explicit user approval.48- **SHOULD**: Denormalize only with proven read bottlenecks.49- **MUST NOT**: Store JSON blobs for highly queryable data.50- **MUST NOT**: Redesign tables or relationships outside the named scope.5152## Examples (good vs. bad, with realistic code or output)5354- **Bad**: Storing CSV tags in a TEXT column.55- **Good**: Relational `user_tags` table with proper FKs.56- **Good (scope)**: Prompt "design only the `invoices` table" -> define `invoices` with PK, FK to `customers`, CHECK on `status`; note any `customers` issues without changing them.57- **Bad (scope)**: Same prompt -> renormalizing `customers`, `orders` and `payments` as well.5859## Checklist (verifiable, copy-pasteable)6061- [ ] Scope and context resolved and stated.62- [ ] No changes outside the resolved scope.63- [ ] PKs/FKs defined.64- [ ] Constraints applied.65- [ ] Normalization reviewed.6667## Common Pitfalls6869- Entity-Attribute-Value (EAV) pattern abuse.70- Missing indexes on Foreign Keys.71- Scope creep: remodeling adjacent tables when one entity was requested.7273## References (official docs, standards)7475- [Database Design Tutorial](https://www.vertabelo.com/blog/database-design-tutorial/)76- [PostgreSQL Documentation: Constraints](https://www.postgresql.org/docs/current/ddl-constraints.html)