Data Modeling Patterns
Relational and NoSQL modeling patterns, normalization guides, temporal data handling, and schema versioning.
📁 File Structure 18 files
📖 Documentation Preview README excerpt
Data Modeling Patterns
Runnable, heavily-commented SQL patterns and decision guides for designing
relational schemas that stay correct and fast as they grow. This is not theory —
every pattern is a working .sql file you can run top-to-bottom on PostgreSQL,
paired with a guide that explains when to use it and why.
Built for backend engineers, data engineers, and DBAs who design schemas and want
defensible answers to "normalize or not?", "which SCD type?", "should I
partition?", and "why is this an anti-pattern?".
What's inside
- 9 runnable SQL files — dimensional models, all the SCD types, audit columns,
soft delete, partitioning, a full normalization walk-through, and a complete
worked e-commerce schema.
- 6 decision guides — each with quick-reference tables, an explicit decision
tree, and an FAQ, so you can choose correctly under time pressure.
- A worked example — one coherent e-commerce OLTP schema with an
entity-relationship diagram and design rationale that ties every pattern together.
All SQL is PostgreSQL syntax with notes where other engines differ. No external
dependencies — you need only a database to run it against (or just read it).
Quick start
1. Unzip the product.
2. Skim this README, then open [examples/ecommerce-er-diagram.md](examples/ecommerce-er-diagram.md)
for the big picture.
3. Run a pattern against a scratch database, e.g.:
psql "postgres://localhost/scratch" -f sql/normalization-examples.sql
psql "postgres://localhost/scratch" -f sql/scd-type2.sql
psql "postgres://localhost/scratch" -f examples/ecommerce-model.sql
Each file is self-contained: it creates its own schema, seeds demo data, and
ends with verification queries (commented) you can run to see the result.
4. Read the matching guide in docs/ for the why and the decision rules.
The SQL files create schemas nameddw,app,norm, andshop. Run them on
a throwaway database so they never touch anything real.
File-by-file guide
`sql/` — runnable patterns
| File | Pattern | What it teaches |
|------|---------|-----------------|
| [star-schema.sql](sql/star-schema.sql) | Star dimensional model | Fact + denormalized dimensions, grain, surrogate keys, a date dimension |
| [snowflake-schema.sql](sql/snowflake-schema.sql) | Snowflake dimensional model | Normalized dimension hierarchies and when they earn their keep |
| [scd-type2.sql](sql/scd-type2.sql) | Slowly Changing Dimension, Type 2 | Full history: validity windows, current flag, correct fact joins |
| [scd-type1-3.sql](sql/scd-type1-3.sql) | SCD Types 1 & 3 | Overwrite vs keep-previous-value, chosen per column |
| [audit-columns.sql](sql/audit-columns.sql) | Audit columns | created/updated bookkeeping, a tamper-proof trigger, full audit log |
| [soft-delete.sql](sql/soft-delete.sql) | Soft delete | deleted_at marker, a safety-net view, partial unique indexes, purge job |
| [partitioning.sql](sql/partitioning.sql) | Table partitioning | RANGE / LIST / HASH strategies, pruning, instant retention |
| [normalization-examples.sql](sql/normalization-examples.sql) | Normalization | One messy table walked from unnormalized → 1NF → 2NF → 3NF |
`docs/` — decision guides
... continues with setup instructions, usage examples, and more.