Database Design: Tables That Age Well Choosing Column Types on Purpose
1 / 5
Next
Choosing Column Types on Purpose ~15min

The type IS the first validation

A column that cannot hold nonsense never holds nonsense. Type choice is cheaper than every check you would otherwise write in PHP.

Money: never FLOAT

FLOAT and DOUBLE are binary approximations. 0.1 + 0.2 is not 0.3, and after ten thousand transactions your totals drift by real shillings.

price DECIMAL(10,2) NOT NULL DEFAULT 0.00

DECIMAL is exact. Ten digits total, two after the point.

Text

  • VARCHAR(n), variable length, most strings
  • CHAR(n), fixed, only for genuinely fixed things like a 2-letter country code
  • TEXT, long content. It cannot have a default and is awkward to index; do not reach for it by habit

Dates

DATETIME for a moment in time, and store UTC. Convert to Nairobi time when displaying. A site that stores local time breaks the day a server moves or a second country signs up.

NOT NULL and DEFAULT

NULL means "unknown", not "zero" and not "empty". A nullable column you never intended to be null is how WHERE status != 'done' quietly skips rows, because NULL is not equal to anything, including itself.

Tasks
Preview