SQL databases
easy 25 minA SQL (relational) database keeps data in tables with declared columns, joins those tables by key when you ask a question, and enforces rules such as unique emails and valid references.
Remember it
The few things worth knowing cold, and how to make them stick.
Memory hook
Schema first, Query with SELECT, Links between tables: S, Q, L.
A school register in two linked lists: the teacher is written once, a string joins the lists, and a slip for a class that does not exist is refused.
Cheat sheet
- Model
- tables, rows, columns, keys
- Rules
- PK, FK, unique, not null, check
- Normal forms
- the key, the whole key, nothing but the key
- Default index
- B-tree: equality, ranges, order
- Transaction
- BEGIN, changes, COMMIT or ROLLBACK
- Query order
- from, where, group, having, select, order, limit
- Reads scale by
- replicas
- Writes scale by
- a bigger machine, then shards
Spot it when you see
- Money, stock, seats.
- Questions that cross several kinds of fact.
- Reports nobody has defined yet.
Tricks that stick
- ACID cleans data: after it, only valid states are left.
- Really, SQL stands for Structured Query Language; S, Q, L above is only a hook.
- A foreign key is a string between two lists.
Do not forget
- Uniqueness checked only in code.
- An index on every column.
- A transaction held open while calling another service.
- Reading your own write from a lagging replica.