Claude Tips mascot
Claude Tips & Tricks
Workflows advanced

Use Claude for Database Schema Design and Migration

Have Claude analyze your existing schema, suggest optimizations, generate migrations, and produce ERD diagrams from your database code.

Claude can read your schema files, understand relationships, and generate migrations that actually work.

Schema Review

Read all files in src/db/schema/ and:
1. Identify missing indexes on frequently queried columns
2. Find N+1 query risks based on the relations
3. Suggest any normalization improvements
4. Check for columns that should have NOT NULL but don't

Generate Migrations

I need to add a "teams" feature. Users can belong to multiple teams.
Read the current schema in src/db/schema.ts and generate:
1. A migration that adds teams, team_members tables
2. Proper foreign keys and indexes
3. A rollback migration

Use our existing migration format (check src/db/migrations/ for examples).

ERD from Code

Read all our Prisma/Drizzle/TypeORM schema files and generate
a Mermaid ERD diagram showing all tables and relationships.

Claude outputs something like:

erDiagram
    USERS ||--o{ TEAM_MEMBERS : belongs_to
    TEAMS ||--o{ TEAM_MEMBERS : has
    USERS ||--o{ POSTS : creates

Index Optimization

Run `EXPLAIN ANALYZE` on our slowest queries (check src/db/queries/)
and suggest index additions. Show the expected improvement.

Tip

Always review generated migrations against your ORM’s migration format. Run them on a dev database before staging.