Database Schema Skill
Design scalable database schemas with proper relationships, constraints, and indexing strategies.
When to Use
Use this skill when the user wants to:
- Design database tables and relationships
- Create schema migrations
- Define indexes and constraints
- Work with SQL or NoSQL databases
- Normalize or denormalize data models
- Design database migrations
Schema Design Principles
- Normalization: Balance between normalization (reducing redundancy) and query performance (denormalization).
- Naming conventions: Use consistent, descriptive names (e.g., snake_case for PostgreSQL).
- Primary keys: Use natural or surrogate keys appropriately (UUID vs. Auto-incrementing Integer).
- Foreign keys: Define relationships clearly to enforce referential integrity.
- Indexes: Index columns used in
WHERE, JOIN, ORDER BY, and GROUP BY clauses.
- Constraints: Enforce data integrity with
NOT NULL, UNIQUE, CHECK, and DEFAULT.
Database Types
- SQL (Relational): PostgreSQL, MySQL, SQLite, SQL Server (Structured, ACID compliant).
- NoSQL (Document/Key-Value): MongoDB, CouchDB, Redis (Flexible schema, high scalability).
- NoSQL (Wide-column): Cassandra, ScyllaDB.
- Time-series: InfluxDB, TimescaleDB (Optimized for time-stamped data).
- Search: Elasticsearch, Meilisearch, PostgreSQL Full-Text Search.
Implementation Examples
SQL (PostgreSQL) - Relational Schema
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
content TEXT,
published_at TIMESTAMP WITH TIME ZONE
);
-- Indexing for performance
CREATE INDEX idx_posts_user_id ON posts(user_id);
NoSQL (MongoDB) - Document Schema
// Users Collection
{
"_id": "60d5ec...",
"email": "user@example.com",
"password_hash": "$2b$12$...",
"profile": {
"first_name": "John",
"last_name": "Doe"
},
"created_at": "2023-01-01T00:00:00Z"
}
// Posts Collection (Denormalized for read performance)
{
"_id": "70e6fd...",
"user_id": "60d5ec...",
"user_name": "John Doe",
"title": "My First Post",
"content": "Hello world!",
"tags": ["tech", "mongodb"],
"created_at": "2023-01-05T10:00:00Z"
}
Common Pitfalls
- Over-indexing: Too many indexes can slow down
INSERT, UPDATE, and DELETE operations.
- Ignoring Data Types: Using
TEXT when VARCHAR(N) or specific types (like UUID or JSONB) are more efficient.
- Lack of Constraints: Relying solely on application logic for data integrity instead of database constraints.
- Not handling Migrations: Changing schema manually in production without versioned migration scripts.
- N+1 Query Problem: Designing schemas/queries that require many subsequent round-trips to the DB.
Deliverables
- Complete database schema design (ERD or text-based).
- Migration scripts (SQL, Alembic, Flyway, etc.).
- Indexing strategy and optimization plan.
- Data types and constraints definition.
- Query examples for complex joins or aggregations.
Quality Checklist
1---2name: database-schema3description: Design and document database schemas, migrations, relationships, and constraints. Use when creating database models, designing tables, defining relationships, or working with SQL/NoSQL databases.4---56# Database Schema Skill78Design scalable database schemas with proper relationships, constraints, and indexing strategies.910## When to Use1112Use this skill when the user wants to:13- Design database tables and relationships14- Create schema migrations15- Define indexes and constraints16- Work with SQL or NoSQL databases17- Normalize or denormalize data models18- Design database migrations1920## Schema Design Principles2122- **Normalization**: Balance between normalization (reducing redundancy) and query performance (denormalization).23- **Naming conventions**: Use consistent, descriptive names (e.g., snake_case for PostgreSQL).24- **Primary keys**: Use natural or surrogate keys appropriately (UUID vs. Auto-incrementing Integer).25- **Foreign keys**: Define relationships clearly to enforce referential integrity.26- **Indexes**: Index columns used in `WHERE`, `JOIN`, `ORDER BY`, and `GROUP BY` clauses.27- **Constraints**: Enforce data integrity with `NOT NULL`, `UNIQUE`, `CHECK`, and `DEFAULT`.2829## Database Types3031- **SQL (Relational)**: PostgreSQL, MySQL, SQLite, SQL Server (Structured, ACID compliant).32- **NoSQL (Document/Key-Value)**: MongoDB, CouchDB, Redis (Flexible schema, high scalability).33- **NoSQL (Wide-column)**: Cassandra, ScyllaDB.34- **Time-series**: InfluxDB, TimescaleDB (Optimized for time-stamped data).35- **Search**: Elasticsearch, Meilisearch, PostgreSQL Full-Text Search.3637## Implementation Examples3839### SQL (PostgreSQL) - Relational Schema40```sql41CREATE TABLE users (42 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),43 email VARCHAR(255) UNIQUE NOT NULL,44 password_hash TEXT NOT NULL,45 created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP46);4748CREATE TABLE posts (49 id SERIAL PRIMARY KEY,50 user_id UUID REFERENCES users(id) ON DELETE CASCADE,51 title VARCHAR(255) NOT NULL,52 content TEXT,53 published_at TIMESTAMP WITH TIME ZONE54);5556-- Indexing for performance57CREATE INDEX idx_posts_user_id ON posts(user_id);58```5960### NoSQL (MongoDB) - Document Schema61```json62// Users Collection63{64 "_id": "60d5ec...",65 "email": "user@example.com",66 "password_hash": "$2b$12$...",67 "profile": {68 "first_name": "John",69 "last_name": "Doe"70 },71 "created_at": "2023-01-01T00:00:00Z"72}7374// Posts Collection (Denormalized for read performance)75{76 "_id": "70e6fd...",77 "user_id": "60d5ec...",78 "user_name": "John Doe", 79 "title": "My First Post",80 "content": "Hello world!",81 "tags": ["tech", "mongodb"],82 "created_at": "2023-01-05T10:00:00Z"83}84```8586## Common Pitfalls8788- **Over-indexing**: Too many indexes can slow down `INSERT`, `UPDATE`, and `DELETE` operations.89- **Ignoring Data Types**: Using `TEXT` when `VARCHAR(N)` or specific types (like `UUID` or `JSONB`) are more efficient.90- **Lack of Constraints**: Relying solely on application logic for data integrity instead of database constraints.91- **Not handling Migrations**: Changing schema manually in production without versioned migration scripts.92- **N+1 Query Problem**: Designing schemas/queries that require many subsequent round-trips to the DB.9394## Deliverables9596- Complete database schema design (ERD or text-based).97- Migration scripts (SQL, Alembic, Flyway, etc.).98- Indexing strategy and optimization plan.99- Data types and constraints definition.100- Query examples for complex joins or aggregations.101102## Quality Checklist103104- [ ] Primary keys are unique and efficient.105- [ ] Foreign keys enforce referential integrity.106- [ ] Indexes cover high-frequency query filters.107- [ ] Data types are optimal for storage and speed.108- [ ] Constraints (NOT NULL, UNIQUE) prevent invalid data.109- [ ] Migration scripts are versioned and reversible.110- [ ] Schema design is documented and easy to understand.