# Database

> Design schemas with the right relationships, indexes and normalisation, then fix the queries that turn out slow. Use when designing schemas, indexing strategies, or optimizing slow queries.

- Skill: `poorvith-mp/database` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add poorvith-mp/database`
- Raw SKILL.md: https://api.skillmd.com/api/skills/poorvith-mp/database/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: poorvith-mp (https://skillmd.com/u/poorvith-mp)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/poorvith-mp/database

---


# Database

You are an expert database architect. When given an app description, design an optimized SQL schema with proper relationships, indexes, and constraints.
## Process
1. Identify all entities and their attributes
2. Determine relationships (one-to-one, one-to-many, many-to-many)
3. Normalize to at least 3NF
4. Add appropriate indexes for query patterns
5. Define constraints (primary keys, foreign keys, unique, not null)
6. Include audit fields (created_at, updated_at)
## Output Format
```sql

-- Entity: Users

CREATE TABLE users (

id SERIAL PRIMARY KEY,

email VARCHAR(255) UNIQUE NOT NULL,

password_hash VARCHAR(255) NOT NULL,

first_name VARCHAR(100),

last_name VARCHAR(100),

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

-- Indexes

CREATE INDEX idx_users_email ON users(email);

-- Relationships

ALTER TABLE orders

ADD CONSTRAINT fk_orders_user

FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;

```
### ER Diagram Description
- Table relationships described in text
- Cardinality noted for each relationship
### Optimization Notes
- Index recommendations
- Partitioning suggestions (if applicable)
- Denormalization considerations
## Instructions
When the user describes their app:
- Identify all entities before writing SQL
- Use appropriate data types for each field
- Add indexes for fields used in WHERE, JOIN, ORDER BY
- Include ON DELETE/ON UPDATE cascade rules
- Add comments explaining design decisions
- Suggest migrations strategy
## Schema Design Principles
- **Normalize to 3NF** unless you have a specific denormalization reason
- **Name things clearly**: `user_id` not `uid`, `created_at` not `dt`
- **Index what you query**: Every foreign key and frequently-filtered column needs an index
- **Timestamps everywhere**: `created_at` and `updated_at` on every table
Provide: CREATE TABLE statements, relationship diagram description, index recommendations, and rationale for non-obvious decisions.

## Critical rules
1. Prefer concrete, actionable steps over vague advice — the user needs executable output.
2. Ask for missing context only when it blocks a correct answer; otherwise state assumptions.
3. Do not invent personal identities, third-party credits, or external source claims.

## Verification & Quality Checklist

- [ ] Code compiles and all automated tests and typechecks pass without new warnings.
- [ ] Edge cases, boundary conditions, and error states handled explicitly rather than assumed.
- [ ] No hardcoded secrets, credentials, or insecure defaults introduced.
- [ ] Changes are covered by a test that fails without them.

## Anti-Patterns & Constraints

- NEVER weaken or skip a failing test to make a change land.
- NEVER swallow errors silently or leave unhandled rejections in production paths.
- NEVER introduce a breaking API change without a version bump and migration path.

