Data Types — Choosing Correctly in SQL
Money is NUMERIC, moments are TIMESTAMPTZ, never FLOAT for cash
Types fix what a column holds and how it compares, sorts and stores. Money is NUMERIC(p,s) — never FLOAT (0.1+0.2 ≠ 0.3 in binary). Moments are TIMESTAMPTZ (UTC instant, displayed per zone); plain dates are DATE. Identifiers are BIGINT/UUID; free text is TEXT with CHECK limits, not arbitrary VARCHAR(255).
Money is exact
NUMERIC(p,s) for cash. FLOAT rounds — 0.1+0.2 ≠ 0.3 in binary.
Moments are zoned
TIMESTAMPTZ stores UTC instants; display converts per zone. Naive TIMESTAMP breaks across DST.
Text + CHECK over magic lengths
TEXT plus CHECK (length ≤ n) enforces real limits; VARCHAR(255) is folklore.
Ids are meaningless + stable
BIGINT IDENTITY or UUID. Business values (emails) change — ids must not. JSONB for indexable payloads.
1CREATE TABLE orders (2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,3 amount NUMERIC(12,2) NOT NULL CHECK (amount >= 0),4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()5);
| column | why |
|---|---|
| amount | NUMERIC — exact |
| created_at | TIMESTAMPTZ — zoned |
| id | BIGINT — surrogate key |
Each column gets the narrowest correct type plus a constraint. FLOAT would round money; naive TIMESTAMP would break across daylight-saving changes.
Correct order table
Exact money, zoned time, guarded amount.
1CREATE TABLE orders (2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,3 amount NUMERIC(12,2) NOT NULL CHECK (amount >= 0),4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()5);
| column | choice |
|---|---|
| amount | NUMERIC — exact |
| created_at | TIMESTAMPTZ — zoned |
Text and id policy
Limits where they matter, stability where it counts.
1name TEXT CHECK (length(name) BETWEEN 1 AND 100),2uid UUID PRIMARY KEY DEFAULT gen_random_uuid()
| column | pattern |
|---|---|
| name | TEXT + CHECK |
| uid | UUID — merge-safe |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.