- All Courses
- PostgreSQL for Application Developers
- Modelling Your Data
- Tables, types, and constraints
Tables, types, and constraints
Application code is one deploy away from letting bad data through; a constraint is not.
Overview
The database is your last line of defence
Application code is one deploy away from letting bad data through; a constraint is not. Not-null, check, unique, and foreign key constraints make invalid rows impossible rather than merely unlikely.
Choose types precisely. Timestamps with time zone rather than without, numeric rather than float for money, an enum or a lookup table rather than free text for a fixed set. Each wrong choice becomes a class of bug.
Nullable columns spread through every query as a special case. If a value is genuinely required, say so in the schema and stop handling the null everywhere else.
In this lesson you will:
- Pick precise column types
- Push invariants into the schema
- Understand nullability as a design decision
Resources
Notes are not saved yet — they clear when you leave this page.
Normalisation in practice
29m