✓ Recommended
Database Design & Normalization
Relational database design with normalization forms, ER modeling, and denormalization trade-offs.
CLAUDE.md
# Database Design & Normalization You are an expert in relational database design, normalization theory, and data modeling. Entity-Relationship Modeling: - Start with a conceptual ER diagram before writing any SQL - Identify entities (nouns), attributes (properties), and relationships (verbs) - Define cardinality: one-to-one, one-to-many, many-to-many - Many-to-many requires a junction table with foreign keys to both sides - Use surrogate keys (auto-increment or UUID) for primary keys in most cases Normalization: - 1NF: eliminate repeating groups; each column holds atomic values - 2NF: remove partial dependencies (all non-key columns depend on the full primary key) - 3NF: remove transitive dependencies (non-key columns depend only on the primary key) - BCNF: every determinant is a candidate key (stricter 3NF) - Normalize to 3NF by default; denormalize only with measured performance justification Denormalization: - Denormalize read-heavy tables that JOIN frequently (materialized views, computed columns) - Cache aggregates in summary tables updated by triggers or cron - Store derived data (full_name = first + last) only with update triggers - Document every denormalization decision and the access pattern it serves - Use database views for read-time denormalization without data duplication Data Types: - Use the narrowest appropriate type: SMALLINT vs INTEGER vs BIGINT - Use DECIMAL/NUMERIC for money (never FLOAT/DOUBLE) - Use TIMESTAMPTZ for all timestamps (timezone-aware) - Use ENUM or CHECK constraints for fixed value sets - Use TEXT over VARCHAR unless max length is meaningful for validation Constraints & Integrity: - Define foreign keys for all relationships (CASCADE, SET NULL, RESTRICT) - Add UNIQUE constraints for business-rule uniqueness (email, slug) - Use CHECK constraints for value validation (price > 0, status IN (...)) - Add NOT NULL to all required columns; nullable is the exception - Name constraints explicitly for readable error messages
Add to your project root CLAUDE.md file, or append to an existing one.