Data Types, CHECK Constraints & Enums
Pick precise PostgreSQL data types (NUMERIC for money, TIMESTAMPTZ for timestamps) and guard data quality with CHECK constraints.
What you'll learn
- Avoid floating-point rounding bugs by using NUMERIC or integer cents
- Store UTC instants with TIMESTAMPTZ
- Enforce domain invariants with NOT NULL, UNIQUE, and CHECK
Overview
Pick precise PostgreSQL data types (NUMERIC for money, TIMESTAMPTZ for timestamps) and guard data quality with CHECK constraints.
Strong database design pays dividends for years: clean constraints catch bugs before application code does, and well-chosen indexes keep response times predictable as tables grow from thousands to millions of rows.
Try It Live (Interactive SQL Sandbox)
Click Run SQL to execute this query against a live in-browser SQLite database.
Key Takeaways
- Avoid floating-point rounding bugs by using NUMERIC or integer cents
- Store UTC instants with TIMESTAMPTZ
- Enforce domain invariants with NOT NULL, UNIQUE, and CHECK
Quick Check
Check Your Understanding
1. Why should monetary values be stored as `NUMERIC(12,2)` or integer cents instead of `FLOAT` / `REAL`?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.