Skip to main content
skill CX Codex

database-design · moizibnyousaf — Claude Code skill: schemas, migrations, indexes

A skill for schema design, migrations and query optimization on PostgreSQL, MySQL and NoSQL. Useful whenever the agent writes SQL.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add MoizIbnYousaf/ai-agent-skills --skill database-design -a claude-code
Open on GitHub

What database-design does

The skill hands the agent working patterns for databases: normalization up to 3NF, denormalization for read-heavy tables, and index types — B-tree, composite, partial, GIN for JSONB, covering. The examples are plain SQL, so the agent drops them into your project instead of reasoning from scratch.

Migrations get their own section. There is a safe template wrapped in a transaction with CREATE INDEX CONCURRENTLY and batched backfill, plus a five-step zero-downtime path: add a nullable column, write to both, backfill, read from the new one, drop the old. For slow queries the skill points at EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) and what to read in the output.

One recommendation worth taking seriously: test migrations against production-size data before deploying. A locking ALTER TABLE on a large table takes the service down, and there is no rollback for that. The same section covers UUIDs for public IDs, created_at and updated_at, and soft deletes via deleted_at.

What it helps with

  • design users and addresses tables without duplication
  • write a migration that does not lock the table
  • find missing indexes from query statistics
  • switch pagination from OFFSET to keyset
  • add integrity constraints: foreign keys, CHECK, EXCLUDE

Example prompts for the agent

  • Design a schema for orders and order items, show me the indexes
  • Write a safe migration adding a status column to users with batched backfill
  • Run EXPLAIN ANALYZE on this query and tell me what to optimize

How to install database-design

1Skills CLI (Vercel)

npx skills add MoizIbnYousaf/ai-agent-skills --skill database-design -a claude-code

2Manually: put the files in place

~/.claude/skills/database-design/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/database-design/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about database-design

How do I install database-design in Claude?

Run npx skills add MoizIbnYousaf/ai-agent-skills --skill database-design -a claude-code. The skill lands in ~/.claude/skills/database-design/SKILL.md.

What does database-design do?

It gives the agent patterns for schema design, migration writing and query optimization on PostgreSQL, MySQL and NoSQL. The agent uses them when you ask it to change a database structure or speed up a query.

Which databases does the skill cover?

PostgreSQL, MySQL and NoSQL. The examples are mostly SQL, including CREATE INDEX CONCURRENTLY, EXPLAIN ANALYZE and the EXCLUDE USING gist constraint.

Source text

SKILL.md · MIT
---
name: database-design
description: Database schema design, optimization, and migration patterns for PostgreSQL, MySQL, and NoSQL databases. Use for designing schemas, writing migrations, or optimizing queries.
source: wshobson/agents
license: MIT
version: 4.1.0
---

# Database Design

## Schema Design Principles

### Normalization Guidelines
```sql
-- 1NF: Atomic values, no repeating groups
-- 2NF: No partial dependencies on composite keys
-- 3NF: No transitive dependencies

-- Users table (normalized)
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Addresses table (separate entity)
CREATE TABLE addresses (
  id SERIAL PRIMARY KEY,
  user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
  street VARCHAR(255),
  city VARCHAR(100),
  country VARCHAR(100),
  is_primary BOOLEAN DEFAULT false
);
```

### Denormalization for Performance
```sql
-- When read performance matters more than write consistency
CREATE TABLE order_summaries (
  id SERIAL PRIMARY KEY,
  order_id INTEGER REFERENCES orders(id),
  customer_name VARCHAR(255),  -- Denormalized from customers
  total_amount DECIMAL(10,2),
  item_count INTEGER,
  last_updated TIMESTAMPTZ DEFAULT NOW()
);
```

## Index Design

### Common Index Patterns
```sql
-- B-tree (default) for equality and range queries
CREATE INDEX idx_users_email ON users(email);

-- Composite index (order matters!)
CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);

-- Partial index for specific conditions
CREATE INDEX idx_active_users ON users(email) WHERE deleted_at IS NULL;

-- GIN index for array/JSONB columns
CREATE INDEX idx_posts_tags ON posts USING GIN(tags);

-- Covering index (includes additional columns)
CREATE INDEX idx_orders_covering ON orders(user_id) INCLUDE (total, status);
```

### Index Analysis
```sql
-- Check index usage
SELECT
  schemaname, tablename, indexname,
  idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
ORDER BY idx_scan DESC;

-- Find missing indexes
SELECT
  relname, seq_scan, seq_tup_read,
  idx_scan, idx_tup_fetch
FROM pg_stat_user_tables
WHERE seq_scan > idx_scan
ORDER BY seq_tup_read DESC;
```

## Migration Patterns

### Safe Migration Template
```sql
-- Always use transactions
BEGIN;

-- Add column with default (non-blocking in PG 11+)
ALTER TABLE users ADD COLUMN status VARCHAR(20) DEFAULT 'active';

-- Create index concurrently (doesn't lock table)
CREATE INDEX CONCURRENTLY idx_users_status ON users(status);

-- Backfill data in batches
UPDATE users SET status = 'active' WHERE status IS NULL AND id BETWEEN 1 AND 10000;

COMMIT;
```

### Zero-Downtime Migrations
```
1. Add new column (nullable)
2. Deploy code that writes to both columns
3. Backfill old data
4. Deploy code that reads from new column
5. Remove old column
```

## Query Optimization

### EXPLAIN Analysis
```sql
-- Always use EXPLAIN ANALYZE
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT * FROM orders WHERE user_id = 123 AND status = 'pending';

-- Key metrics to watch:
-- - Seq Scan vs Index Scan
-- - Actual rows vs Estimated rows
-- - Buffers: shared hit vs read
```

### Common Optimizations
```sql
-- Use EXISTS instead of IN for large sets
SELECT * FROM users u
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);

-- Pagination with keyset (cursor) instead of OFFSET
SELECT * FROM posts
WHERE created_at < '2024-01-01'
ORDER BY created_at DESC
LIMIT 20;

-- Use CTEs for complex queries
WITH active_users AS (
  SELECT id FROM users WHERE last_login > NOW() - INTERVAL '30 days'
)
SELECT * FROM orders WHERE user_id IN (SELECT id FROM active_users);
```

## Constraints & Data Integrity

```sql
-- Primary key
ALTER TABLE users ADD PRIMARY KEY (id);

-- Foreign key with cascade
ALTER TABLE orders ADD CONSTRAINT fk_orders_user
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;

-- Check constraint
ALTER TABLE products ADD CONSTRAINT chk_price_positive
  CHECK (price >= 0);

-- Unique constraint
ALTER TABLE users ADD CONSTRAINT uniq_users_email UNIQUE (email);

-- Exclusion constraint (no overlapping ranges)
ALTER TABLE reservations ADD CONSTRAINT excl_no_overlap
  EXCLUDE USING gist (room_id WITH =, tsrange(start_time, end_time) WITH &&);
```

## Best Practices

- Use UUIDs for public-facing IDs, SERIAL/BIGSERIAL for internal
- Always add `created_at` and `updated_at` timestamps
- Use soft deletes (`deleted_at`) for important data
- Design for eventual consistency in distributed systems
- Document schema decisions in migration files
- Test migrations on production-size data before deploying

© MoizIbnYousaf · MIT · MoizIbnYousaf/ai-agent-skills

Similar skills

All skills for Data and databases →

database-design by other authors comes first.

From the same set6

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor