Skip to content
Back to skills

Database Designer

ASecurity

Analyse an existing SQL schema for normalization and constraint gaps, recommend indexes from query patterns, and generate forward/rollback migrations. Use when reviewing DDL, tuning indexes, or diffing two schema versions.

  • 1,271 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added May 27, 2026
data-aipythongobashsqldebuggingdatabaseperformancedocumentation

Works with

  • cli

Security analysis

A100/100

Pro scans all 14 files and shows the line behind each finding

Scanned December 4, 2026

npx -y skills add borghei/Claude-Skills --skill database-designer --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Database Designer?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Database Designer
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/borghei-database-designer/badge)](https://www.skillsdirectory.com/skills/borghei-database-designer)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: database-designer
description: >
  Analyse an existing SQL schema for normalization and constraint gaps, recommend indexes
  from query patterns, and generate forward/rollback migrations. Use when reviewing DDL,
  tuning indexes, or diffing two schema versions.
license: MIT + Commons Clause
metadata:
  version: 1.1.0
  author: borghei
  category: engineering
  domain: databases
  tier: POWERFUL
  updated: 2026-11-08
  tags:
    - database
    - schema-design
    - indexing
    - migrations
    - postgresql
    - mongodb
---
# Database Designer

The agent analyses an existing relational schema for normalization, constraint, naming and type problems, recommends indexes for the queries that actually run, and generates forward and rollback migrations between two schema versions. To design a new schema from product requirements (ERDs, RLS, ORM models), use `engineering/database-schema-designer` instead.

## When to Use

- Reviewing a schema (DDL or JSON) before or after a change.
- Choosing or pruning indexes for a known set of queries.
- Turning a schema diff into a migration with rollback and validation queries, optionally zero-downtime (expand-contract).
- Choosing between database engines for a workload (reference only).

## Clarify First

Before designing or migrating, settle the inputs below. When one is missing or vague and would change the result, ask about it; when it wouldn't, use a sensible default and say which one you used.

- **Task:** schema analysis/ERD, index optimization, or migration generation (selects `schema_analyzer.py` vs `index_optimizer.py` vs `migration_generator.py`).
- **Engine:** PostgreSQL or MySQL (drives the DDL dialect; for choosing an engine, see the selection reference).
- **Schema input & query patterns:** the DDL/JSON schema and the queries to optimize for (the input the tools analyze; index recommendations depend on the query patterns).

Ask about the two or three inputs that matter most for this request, not the whole list. If the user wants a result straight away, produce it and state your assumptions at the top.

## Workflow

1. **Analyse** the current schema with `schema_analyzer.py` (DDL or JSON). Read the findings as candidates: the transitive-dependency check matches column-name prefixes (`user_id`, `user_email`) and can flag unrelated columns, so confirm each before proposing a split.
2. **Index** with `index_optimizer.py`, which needs the schema as JSON plus the query patterns (`assets/sample_query_patterns.json` shows the shape). Columns already covered by a primary key, a unique constraint or an existing index prefix are not recommended again. Validate each recommendation with `EXPLAIN` on real data before creating it.
3. **Migrate** with `migration_generator.py --current old.json --target new.json`. Review every HIGH-risk step (drops, type changes) and use `--zero-downtime` for large or busy tables. Run the migration on a copy, then run the rollback, before it goes near production.
4. **Verify** that the analyzer's high-severity count went down and the generated rollback restores the original schema.

## Quick Start

```bash
python3 scripts/schema_analyzer.py --input assets/sample_schema.sql                 # findings, exit 1
python3 scripts/schema_analyzer.py --input assets/sample_schema.sql --erd-only      # Mermaid ERD
python3 scripts/index_optimizer.py --schema assets/sample_schema.json --queries assets/sample_query_patterns.json --analyze-existing
python3 scripts/migration_generator.py --current assets/sample_schema.json --target assets/sample_schema_target.json --format sql
```

Real output of the first command (excerpt):

```
SCHEMA OVERVIEW
Total Tables: 11
Total Columns: 136
...
• products: Columns ['category_id', 'category_name'] may have transitive dependency through 'category_id'
  Suggestion: Consider creating separate 'category' table with these columns
• users.email: VARCHAR(255) antipattern
  Current: VARCHAR(255) → Suggested: VARCHAR(n) sized to the real maximum; measure with: SELECT max(length(email)) FROM users;
...
FIX 36 issues
```

A clean schema ends with `PASS: no schema issues found` (try `assets/sound_schema.json`).

## Tools Overview

| Tool | What it does | Key flags |
|------|--------------|-----------|
| `scripts/schema_analyzer.py` | Parse DDL (`CREATE TABLE`, inline and table-level `REFERENCES`, `CREATE INDEX`) or JSON (with `indexes`); report 1NF-BCNF smells, missing PK/FK/NOT NULL/CHECK, VARCHAR(255) defaults, naming, unindexed foreign keys; Mermaid ERD | `--input`, `--format json\|text` (alias `--output-format`), `--generate-erd`, `--erd-only`, `--output` |
| `scripts/index_optimizer.py` | Recommend single, composite and covering indexes from query patterns; order composite columns by selectivity; flag redundant and overlapping indexes; list queries that name tables/columns missing from the schema; `--analyze-existing` adds a KEEP/REVIEW verdict per declared index | `--schema`, `--queries`, `--analyze-existing`, `--min-priority 1-4`, `--format json\|text` |
| `scripts/migration_generator.py` | Diff two JSON schemas into ordered steps with forward and rollback SQL, risk level and validation queries | `--current`, `--target`, `--format json\|text\|sql`, `--zero-downtime`, `--include-validations`, `--validate-only` |

Selectivity and benefit labels in `index_optimizer.py` are heuristics (column-name and type patterns) unless the schema supplies `cardinality_estimate` per column (and optionally `row_count` per table). Risk levels in `migration_generator.py` are rules of thumb (drops and narrowing type changes are HIGH). Neither replaces `EXPLAIN` or a rehearsal on a copy.

## Exit Codes

| Code | Meaning |
|------|---------|
| 0 | `schema_analyzer.py` / `index_optimizer.py`: no findings (`PASS ...`). `migration_generator.py`: plan produced (`PLAN READY ...`) or the two schemas are identical (`PASS ...`) |
| 1 | `schema_analyzer.py` / `index_optimizer.py`: findings to act on (`FIX N issues`). Never returned by `migration_generator.py` |
| 2 | Unusable input: missing or unreadable file, invalid JSON, no tables, a table without columns, a column without a `type`, a query without `id`/`table`. One-line message, no traceback |

Schema JSON shape: see `assets/sound_schema.json`. Query JSON shape: `assets/sound_queries.json`. Malformed examples: `assets/malformed_*`.

## Anti-Patterns

**Mistake:** Creating every index the optimizer suggests.
**Why it happens:** Each recommendation looks reasonable on its own.
**Instead:** Indexes cost write throughput and storage. Create the ones that serve frequent or slow queries, confirm with `EXPLAIN`, and drop the redundant ones `--analyze-existing` finds.

**Mistake:** Running a generated `DROP COLUMN` or type change directly on a large production table.
**Why it happens:** The SQL is ready to paste.
**Instead:** Use expand-contract (`--zero-downtime`): add, backfill, switch reads, then drop in a later release. Rehearse the rollback first.

**Mistake:** Normalising everything the analyzer flags.
**Why it happens:** Normal forms look like correctness.
**Instead:** Denormalised columns can be deliberate (snapshots of a price at order time, read models). Keep them when they are intentional and say why.

## Files

- `scripts/schema_analyzer.py`, `scripts/index_optimizer.py`, `scripts/migration_generator.py` — the tools above.
- `assets/contract.json` — declared runs (sound / flawed / malformed) checked by `scripts/skill_contract_check.py`; `assets/sound_*` exit 0, `assets/malformed_*` exit 2.
- `assets/sample_schema.sql` (DDL, 11 tables), `assets/sample_schema.json` (same schema as JSON), `assets/sample_schema_target.json` (adds `users.phone` and an index, drops `products.brand`), `assets/sample_query_patterns.json`.
- `expected_outputs/` — real outputs of the Quick Start commands; `README.md` — longer walkthrough.

## Reference Documentation

Load the reference that matches the task — keep this file lean and pull detail on demand:

- **[references/workflows-and-tools.md](references/workflows-and-tools.md)** — the Quick Start, three core workflows (analyze/optimize, safe migration, index optimization), and the full CLI reference (flags, usage, output formats) for `schema_analyzer.py`, `index_optimizer.py`, and `migration_generator.py`. Read when running the tools end-to-end.
- **[references/patterns-and-troubleshooting.md](references/patterns-and-troubleshooting.md)** — index-type selection table, anti-patterns, the troubleshooting table for tool output, and the success-criteria quality bar. Read when picking an index type, avoiding mistakes, debugging output, or checking your work.
- **[references/database_selection_decision_tree.md](references/database_selection_decision_tree.md)** — systematic database-technology selection based on requirements, data patterns, and operational constraints. Read when choosing between SQL and NoSQL engines.
- **[references/index_strategy_patterns.md](references/index_strategy_patterns.md)** — proven patterns for index design, optimization strategies, and pitfalls to avoid. Read when designing an indexing strategy in depth.
- **[references/normalization_guide.md](references/normalization_guide.md)** — normal forms (1NF-BCNF), decomposition to eliminate anomalies, and integrity trade-offs. Read when normalizing or deliberately denormalizing a schema.

## Scope & Limitations

Covers PostgreSQL and MySQL schemas: analysis, indexing and migrations. MongoDB, DynamoDB and other NoSQL stores get only the selection guidance in the references. Does not cover live query profiling (`engineering/performance-profiler`), multi-service migration rollout (`engineering/migration-architect`), or database administration (backups, replication, roles).

Files in this skill

  • README.md11.7 KB
  • SKILL.md13.9 KB
  • assets/sample_query_patterns.json9 KB
  • assets/sample_schema.json9.1 KB
  • assets/sample_schema.sql8.1 KB
  • expected_outputs/index_optimization_sample.txt2.6 KB
  • expected_outputs/migration_sample.txt4.4 KB
  • expected_outputs/schema_analysis_sample.txt7.3 KB
  • index_optimizer.py38 KB
  • migration_generator.py49.2 KB
  • references/database_selection_decision_tree.md16 KB
  • references/index_strategy_patterns.md11.6 KB
  • references/normalization_guide.md10.4 KB
  • schema_analyzer.py41 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in data-ai →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.