Chapter 1 · Practice 2 of 5
Choose types that match the data: text, numeric, bigint
Why it matters
The right type gives you validation and correct maths for free. The wrong one pushes checks into every piece of code that touches the column.
- Strings: use
text. It performs the same asvarchar(n); add aCHECKfor length only when it is a real rule. - Money: use
numericfor prices and anything that must add up exactly. Neverrealordouble precision, which round, so sums drift. - Counters and IDs: use
bigintfor anything that can grow, sinceintegertops out around 2.1 billion. - Everything else: use
boolean,uuid,inetandjsonbinstead of strings that pretend to be them.
Avoid
floatordouble precisionfor prices and balancesintegerprimary keys on tables that can grow fast- Dates or numbers stored in
textcolumns