Purpose
Design efficient, scalable database schemas optimized for application access patterns with proper indexing and migration strategies.
When to Use
Use this skill when the task involves:
- Designing new database tables, collections, or key structures
- Creating or modifying indexes for query performance
- Writing database migrations (Prisma, TypeORM, Drizzle, raw SQL)
- Optimizing slow queries or analyzing execution plans
- Planning schema changes with backward compatibility
Constraints
- Always consider query patterns first before designing schema
- Include migration rollback scripts for every schema change
- Apply least-privilege for database roles and permissions
- Use explicit constraints (NOT NULL, UNIQUE, CHECK) at database level
- Prefer UUID or ULID over auto-increment for distributed systems
- Document breaking changes and data migration requirements
Execution Steps
- Identify entities — List domain entities and their attributes
- Map relationships — Define 1:1, 1:N, M:N relationships with cardinality
- Design schema — Create tables/collections with appropriate types
- Add indexes — Create indexes based on WHERE, JOIN, and ORDER BY patterns
- Write migrations — Generate up/down migration scripts
- Validate performance — Check EXPLAIN plans for critical queries
- Document changes — Note breaking changes and required data backfills
Examples
Input
Design a schema for a blog with posts, authors, and tags (many-to-many)
Output
-- PostgreSQL schema
CREATE TABLE authors (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE posts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
author_id UUID NOT NULL REFERENCES authors(id) ON DELETE CASCADE,
title VARCHAR(500) NOT NULL,
slug VARCHAR(500) NOT NULL UNIQUE,
content TEXT,
published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE tags (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE post_tags (
post_id UUID REFERENCES posts(id) ON DELETE CASCADE,
tag_id UUID REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (post_id, tag_id)
);
-- Indexes for common queries
CREATE INDEX idx_posts_author ON posts(author_id);
CREATE INDEX idx_posts_published ON posts(published_at) WHERE published_at IS NOT NULL;
CREATE INDEX idx_posts_slug ON posts(slug);
Related Skills
nest-backend-service-builder — Implement repository layer
system-planning-assistant — High-level data architecture decisions
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: database-schema-designer-33description: Design PostgreSQL, MongoDB, or Redis schemas with entities, relationships, indexes, constraints, migrations, query optimization, and backward-compatible schema evolution Use when this capability is needed.4---56# Purpose78Design efficient, scalable database schemas optimized for application access patterns with proper indexing and migration strategies.910## When to Use1112Use this skill when the task involves:1314- Designing new database tables, collections, or key structures15- Creating or modifying indexes for query performance16- Writing database migrations (Prisma, TypeORM, Drizzle, raw SQL)17- Optimizing slow queries or analyzing execution plans18- Planning schema changes with backward compatibility1920## Constraints2122- Always consider **query patterns first** before designing schema23- Include **migration rollback scripts** for every schema change24- Apply **least-privilege** for database roles and permissions25- Use **explicit constraints** (NOT NULL, UNIQUE, CHECK) at database level26- Prefer **UUID or ULID** over auto-increment for distributed systems27- Document **breaking changes** and data migration requirements2829## Execution Steps30311. **Identify entities** — List domain entities and their attributes322. **Map relationships** — Define 1:1, 1:N, M:N relationships with cardinality333. **Design schema** — Create tables/collections with appropriate types344. **Add indexes** — Create indexes based on WHERE, JOIN, and ORDER BY patterns355. **Write migrations** — Generate up/down migration scripts366. **Validate performance** — Check EXPLAIN plans for critical queries377. **Document changes** — Note breaking changes and required data backfills3839## Examples4041### Input4243> Design a schema for a blog with posts, authors, and tags (many-to-many)4445### Output4647```sql48-- PostgreSQL schema49CREATE TABLE authors (50 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),51 email VARCHAR(255) NOT NULL UNIQUE,52 name VARCHAR(255) NOT NULL,53 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()54);5556CREATE TABLE posts (57 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),58 author_id UUID NOT NULL REFERENCES authors(id) ON DELETE CASCADE,59 title VARCHAR(500) NOT NULL,60 slug VARCHAR(500) NOT NULL UNIQUE,61 content TEXT,62 published_at TIMESTAMPTZ,63 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()64);6566CREATE TABLE tags (67 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),68 name VARCHAR(100) NOT NULL UNIQUE69);7071CREATE TABLE post_tags (72 post_id UUID REFERENCES posts(id) ON DELETE CASCADE,73 tag_id UUID REFERENCES tags(id) ON DELETE CASCADE,74 PRIMARY KEY (post_id, tag_id)75);7677-- Indexes for common queries78CREATE INDEX idx_posts_author ON posts(author_id);79CREATE INDEX idx_posts_published ON posts(published_at) WHERE published_at IS NOT NULL;80CREATE INDEX idx_posts_slug ON posts(slug);81```8283## Related Skills8485- `nest-backend-service-builder` — Implement repository layer86- `system-planning-assistant` — High-level data architecture decisions8788---89> Converted and distributed by [TomeVault](https://tomevault.io/claim/phatpham9) — claim your Tome and manage your conversions.90<!-- tomevault:4.0:skill_md:2026-04-11 -->