Lesson 1.1Free preview

Tables, types, and constraints

Application code is one deploy away from letting bad data through; a constraint is not.

28mIntermediate16.7k students

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

Next Lesson
Tables, types, and constraints — PostgreSQL for Application Developers — Vertex