STAGE 04 / LESSON 01
Model data with tables and constraints
A relational database stores related facts in tables with rules that protect their consistency.
THE IDEA
Understand it.
A row represents one record and a column represents one attribute. A primary key uniquely identifies a row. A foreign key can require that a referenced record exists in another table.
Choose constraints for rules that must hold regardless of which application writes the data. For example, a task title can be required and a task can reference an existing owner.
SEE IT IN CONTEXT
A small task table
CREATE TABLE tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
status text NOT NULL CHECK (status IN ('open', 'done'))
);The database rejects a missing title or an unsupported status. Authorization about who may edit a task is still application work.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
GitHub
GitHub engineering, 2021Relational data at scale
GitHub described partitioning its relational databases as the application and its data grew.
- Application data
- Relational database
- Partitions
What to learn: Tables and their relationships remain important even when a system must split data for scale; the article describes GitHub's 2021 work.
Read the source: GitHub relational database partitioningShopify
Shopify engineering, 2018Commerce data in MySQL
Shopify's 2018 stack overview describes MySQL and database sharding in its commerce platform.
- Commerce application
- MySQL databases
- Shards
What to learn: A relational model can support real commerce workloads. The report discusses MySQL, so do not read it as evidence that Shopify used PostgreSQL.
Read the source: Shopify technology stackTRY IT YOURSELF
Practice.
Why is a NOT NULL constraint useful when the form already marks a field as required?
Show one possible answer
Another client or script can bypass the form. A database constraint protects the data across all writers.
GO DEEPER