Stop 10 · Schema Design & ConstraintsTopics · 3/11

Choosing Column Data Types

Part of the SQL Roadmap

In short

A wrong data type is a quiet bug. Money that's off by a paisa, dates that sort in the wrong order, and phone numbers that lose their leading zero all come from one choice made in CREATE TABLE.

Picking the right data type for each column keeps data correct and compact: integers for counts, DECIMAL for money, DATE or TIMESTAMP for time, and VARCHAR or TEXT for strings. Wrong types cause bad sorting and silent rounding.

The jaadu places a tiny gold ring on a delicate brass jeweller's scale while a heavy rice sack sits on a large floor scale beside it.

Think of it like this

The jeweller's scales

A jeweller in Thrissur weighs gold on a precise scale that reads to the milligram, but weighs a sack of rice on a big floor scale. Using the rice scale for gold would lose money on every sale. Data types are your scales: NUMERIC for money, INTEGER for counts, DATE for dates.

Watch it happen

Storing money: FLOAT vs NUMERIC

FLOAT / REAL

  • Stored as binary approximations
  • 0.1 + 0.2 is not exactly 0.3
  • Tiny errors grow across lakhs of rows

NUMERIC(12,2)

  • Stores exact decimal values
  • 0.1 + 0.2 = 0.3, always
  • Totals match the bank statement

Use NUMERIC or DECIMAL for money. Keep FLOAT for measurements like sensor readings where tiny errors don't matter.

The big ideas

What you need to know

Numbers you count

INTEGER or BIGINT for counts and IDs. SMALLINT is fine for small ranges like a semester number.

Money is exact

NUMERIC(12,2) holds up to ten digits before the decimal point and exactly two after, perfect for rupees and paise.

Real dates

DATE for days and TIMESTAMPTZ for moments. Dates stored as text sort as '10-01-2026' before '9-12-2025'.

Codes are text

Phone numbers, pin codes and Aadhaar-style IDs aren't for maths. Store them as text so leading zeros and +91 survive.

See it in code

Walk through the code

sql
1CREATE TABLE products (2  product_id   BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,3  name         VARCHAR(120)  NOT NULL,4  price        NUMERIC(10,2) NOT NULL,5  stock        INTEGER       NOT NULL DEFAULT 0,6  weight_kg    REAL,7  is_active    BOOLEAN       NOT NULL DEFAULT TRUE,8  launched_on  DATE,9  updated_at   TIMESTAMPTZ   NOT NULL DEFAULT NOW(),10  hsn_code     VARCHAR(8)11);12 13SELECT 0.1::float8  + 0.2::float8  = 0.3 AS float_equal,14       0.1::numeric + 0.2::numeric = 0.3 AS numeric_equal;

1/5BIGINT IDs never run out, even for a huge product catalogue.

Sensible types for a products table, then proof that FLOAT can't add 0.1 and 0.2 exactly. The :: casts are PostgreSQL. In MySQL and SQL Server, use DECIMAL(10,2) and DATETIME or DATETIME2 instead of TIMESTAMPTZ.

Save yourself the pain

Common mistakes

Oops

Storing phone numbers in a BIGINT column.

Do this instead

Leading zeros and +91 get lost, and nobody adds phone numbers. Use VARCHAR(15).

Oops

Using FLOAT for prices and invoice totals.

Do this instead

Use NUMERIC or DECIMAL so totals match to the paisa.

Oops

Saving dates as text like '05/03/2026'.

Do this instead

Use DATE. Text dates sort alphabetically, can't use date functions, and nobody knows if it's 5 March or 3 May.

Your mission

Fix a badly typed table

~20 min

Quick check

Which type should you use for an invoice amount in rupees and paise?

Go deeper

  1. 1Learn why DECIMAL is preferred over FLOAT for currency amounts.
  2. 2Store dates in date types, never as text, and see how sorting differs.
  3. 3Pick types for a small orders table and justify each choice.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge