Skip to content
EL
Contents

SQL types and the type system

Review a table's types

DecisionFoundation3 min read15 min hands-on#fundamentals#sql#data-modeling

The idea in 30 seconds

Review column types by asking what each value means, not what it looks like. Money is an exact decimal; a moment in time is a timestamptz; a yes/no answer is a boolean; a phone number is text; an id that may grow is a bigint. Every mismatch is a bug waiting for the right data.

Helps to know first: SQL data modification

On this page

The situation

A payments feature needs a table. An AI assistant proposes one.

Review taskPayments table

Pull request: “Add payments table.”

CREATE TABLE payments (
  id              serial PRIMARY KEY,
  amount          float,
  paid_at         timestamp,
  customer_phone  integer,
  refunded        varchar(5)     -- 'true' or 'false'
);

Would you approve it? Write the review you would leave.

Your call

How to review a column’s type

For each column, ask what the value means:

If the value is…UseNot
An amount of moneynumeric(p, s)real, double precision
A moment something happenedtimestamptztimestamp, text
A calendar daydatetimestamptz at midnight
Yes or nobooleantext, 0/1 integers
An identifier made of digits (phone, postcode, account number)textAny number type
A row id that might grow largebigintinteger, serial
Text with a real business length limitvarchar(n)text with checks scattered in code

Check yourself

  1. Which type would you use for a payment amount, and why?

    Show answer

    numeric with a fixed scale, such as numeric(12, 2). It stores decimal values exactly, so totals and comparisons are correct. Floating-point types are approximate.

  2. What is lost when '2025-03-02 10:15+05:30' is stored in a timestamp column?

    Show answer

    The +05:30 offset. The column keeps only the clock reading 10:15, so the exact instant can no longer be known.

  3. Why is boolean better than varchar(5) for a refunded flag?

    Show answer

    A boolean holds only true, false or null, so every row means the same thing. Free text accepts many spellings, and every query has to guess which ones mean yes.

Finished reading?

Saved in this browser only.

Sources

Where each claim on this page comes from.

  1. 1.
    PostgreSQL documentation: 8.1 Numeric Types
    Primary· §8.1 Numeric Types

    Integer ranges; numeric is exact and recommended for money; floating-point types are inexact.

  2. 2.
    PostgreSQL documentation: 8.5 Date/Time Types
    Primary· §8.5.1.3 Time Stamps

    timestamp without time zone silently ignores a time zone given in the input.

  3. 3.
    PostgreSQL documentation: 8.6 Boolean Type
    Primary· §8.6 Boolean Type

    The boolean type stores true, false or null, and accepts several input spellings for each.

  4. 4.
    PostgreSQL documentation: ALTER TABLE
    Primary· Notes

    Changing the type of an existing column normally rewrites the entire table and its indexes.

Go further

What to read next, and why it is worth the time.

  1. 1.
    PostgreSQL documentation: 8.1 Numeric Types
    Primary· §8.1

    Worth knowing the full table of numeric types and their exact limits before you design your next table.

Applies to PostgreSQL 18. Draft: not yet fact-checked end to end.