Data Modeling
Overview
Data modeling is the design of how data is structured, related, and constrained so it stays correct and queryable as the system grows. Relational modeling emphasizes normalization and referential integrity enforced by the database; document modeling embeds related data for read locality. Read this before creating a schema, adding a table, or choosing between relational and document storage.
Official sources
Core concepts
- Tables, columns, and types: a relational model defines entities as tables with typed columns; choosing correct types and defaults is the first integrity control.
- Constraints: PostgreSQL supports NOT NULL, UNIQUE, CHECK, PRIMARY KEY, FOREIGN KEY, and EXCLUSION constraints — push invariants into the database so invalid data cannot be stored.
- Primary and foreign keys: primary keys uniquely identify rows; foreign keys enforce referential integrity between related tables.
- Normalization: organize data to eliminate redundancy and update anomalies (1NF/2NF/3NF) so each fact lives in exactly one place.
- Schemas and namespacing: schemas group related objects and provide logical organization and access boundaries within a database.
- Document vs relational: document models embed/denormalize related data for single-read access and flexible shape, trading integrity guarantees and join flexibility for read performance.
Best practices
- Enforce invariants with database constraints (NOT NULL, UNIQUE, FOREIGN KEY, CHECK) rather than relying only on application code — the DB is the last line of defense.
- Normalize to remove redundancy first; denormalize deliberately and only when measured read patterns justify it.
- Always define primary keys and back foreign-key columns with indexes to keep referential checks and joins fast.
- Choose data types tightly (e.g. proper numeric/timestamp types, generated/identity columns where appropriate) so the database validates and stores values efficiently.
Common pitfalls
- Modeling everything as nullable text → loses validation and indexing benefits; use precise types and NOT NULL where the value is required.
- Skipping foreign keys "for speed" → orphaned and inconsistent rows accumulate; let referential integrity be enforced by the engine.
- Premature denormalization → duplicated facts drift out of sync; normalize first, denormalize only with evidence.
Examples
-- Relational schema with keys and constraints (PostgreSQL DDL)
CREATE TABLE author (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE
);
CREATE TABLE post (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
author_id bigint NOT NULL REFERENCES author(id),
title text NOT NULL CHECK (length(title) > 0),
published boolean NOT NULL DEFAULT false
);
CREATE INDEX ON post (author_id);
Further reading
Related skills
- ../system-design-fundamentals — partitioning and sharding at scale
- ../caching-strategies — caching query results
- ../api-design-rest-graphql — mapping the model to resources/types
1---2name: data-modeling3description: Relational and document data modeling — tables, keys, constraints, normalization, and when to denormalize; read before designing a schema.4domain: engineering5category: architecture6tags: [data-modeling, relational, normalization, constraints, schema-design]7official_sources:8 - https://www.postgresql.org/docs/current/ddl.html9verified: 2026-06-1610metadata:11 source: https://skillmd.com/skills/gabriel7silva/data-modeling12---1314# Data Modeling1516## Overview17Data modeling is the design of how data is structured, related, and constrained so it stays correct and queryable as the system grows. Relational modeling emphasizes normalization and referential integrity enforced by the database; document modeling embeds related data for read locality. Read this before creating a schema, adding a table, or choosing between relational and document storage.1819## Official sources20- PostgreSQL — Data Definition (tables, constraints, keys, schemas, partitioning): https://www.postgresql.org/docs/current/ddl.html2122## Core concepts23- **Tables, columns, and types**: a relational model defines entities as tables with typed columns; choosing correct types and defaults is the first integrity control.24- **Constraints**: PostgreSQL supports NOT NULL, UNIQUE, CHECK, PRIMARY KEY, FOREIGN KEY, and EXCLUSION constraints — push invariants into the database so invalid data cannot be stored.25- **Primary and foreign keys**: primary keys uniquely identify rows; foreign keys enforce referential integrity between related tables.26- **Normalization**: organize data to eliminate redundancy and update anomalies (1NF/2NF/3NF) so each fact lives in exactly one place.27- **Schemas and namespacing**: schemas group related objects and provide logical organization and access boundaries within a database.28- **Document vs relational**: document models embed/denormalize related data for single-read access and flexible shape, trading integrity guarantees and join flexibility for read performance.2930## Best practices31- Enforce invariants with database constraints (NOT NULL, UNIQUE, FOREIGN KEY, CHECK) rather than relying only on application code — the DB is the last line of defense.32- Normalize to remove redundancy first; denormalize deliberately and only when measured read patterns justify it.33- Always define primary keys and back foreign-key columns with indexes to keep referential checks and joins fast.34- Choose data types tightly (e.g. proper numeric/timestamp types, generated/identity columns where appropriate) so the database validates and stores values efficiently.3536## Common pitfalls37- Modeling everything as nullable text → loses validation and indexing benefits; use precise types and NOT NULL where the value is required.38- Skipping foreign keys "for speed" → orphaned and inconsistent rows accumulate; let referential integrity be enforced by the engine.39- Premature denormalization → duplicated facts drift out of sync; normalize first, denormalize only with evidence.4041## Examples42```sql43-- Relational schema with keys and constraints (PostgreSQL DDL)44CREATE TABLE author (45 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,46 email text NOT NULL UNIQUE47);4849CREATE TABLE post (50 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,51 author_id bigint NOT NULL REFERENCES author(id),52 title text NOT NULL CHECK (length(title) > 0),53 published boolean NOT NULL DEFAULT false54);5556CREATE INDEX ON post (author_id);57```5859## Further reading60- PostgreSQL constraints reference: https://www.postgresql.org/docs/current/ddl-constraints.html61- PostgreSQL table partitioning: https://www.postgresql.org/docs/current/ddl-partitioning.html6263## Related skills64- ../system-design-fundamentals — partitioning and sharding at scale65- ../caching-strategies — caching query results66- ../api-design-rest-graphql — mapping the model to resources/types