Modules / Database / Datatypes Lab

Datatypes in SQL

Every column declares what it can hold and how much space that costs. Push values past the edge of a type, watch FLOAT lose money that DECIMAL keeps, and see why the right type is a correctness decision, not a formality.

Datatypes: The Contract Every Column Signs

Choosing a type decides what values are legal, how much disk they consume, and whether your arithmetic is exact.

Quick Context

A datatype declares what a column may contain. The database uses it to reject invalid data, to decide how many bytes each value occupies, and to choose how comparisons and arithmetic behave. Get it wrong and you pay in storage, in speed, or — worst — in silently incorrect numbers.

Integers: Pick the Smallest That Fits

Integer types differ only in width, and width sets the range. TINYINT holds −128 to 127 in one byte; INT covers roughly ±2.1 billion in four; BIGINT uses eight.

The classic production incident is an INT primary key on a fast-growing table. At 2,147,483,647 rows the next insert fails outright — and migrating a hot table's key type is painful. Type 3000000000 into the value box with INT selected and watch the overflow.

Never Store Money in FLOAT

FLOAT and DOUBLE are binary floating point. They cannot represent 0.1 exactly, for the same reason base 10 cannot write 1/3 exactly. Errors are tiny individually and accumulate over millions of rows.

The demo above adds 0.10 + 0.20 in both types. FLOAT does not produce 0.30 — it produces something imperceptibly different, and = 0.30 is then false. DECIMAL(10,2) stores the digits themselves and is exact.

Rule: money, quantities and anything a human will audit go in DECIMAL / NUMERIC. Reserve FLOAT for measurements where a rounding error in the fifteenth digit genuinely does not matter.

CHAR vs VARCHAR vs TEXT

  • CHAR(n) — fixed width. Always consumes n characters, padding with spaces. Only sensible when values really are uniform, like a 2-letter country code.
  • VARCHAR(n) — variable width up to n, storing only what you use plus a small length prefix. The sane default.
  • TEXT — unbounded. Convenient, but some engines store it separately from the row, which can slow reads.

The n in VARCHAR(n) is mostly a constraint, not an allocation — declaring VARCHAR(255) everywhere does not waste space, but it also fails to document what the column really holds.

Dates, Times and the NULL Question

Store dates in DATE / TIMESTAMP, never in a string. Only a real date type gives you correct sorting, date arithmetic, and rejection of 2024-02-31. For anything spanning time zones, prefer TIMESTAMP WITH TIME ZONE.

Finally, every type also permits NULL unless you say NOT NULL. NULL means unknown, which is genuinely different from 0 or an empty string — and it propagates through arithmetic and comparisons in ways that surprise people.

Interactive Exploration Guide

  1. Select TINYINT and enter 200. Rejected — out of range. Now try 100, which fits comfortably.
  2. Select INT and enter 3000000000. Overflow again, at exactly the boundary that has broken many real systems.
  3. Compare the storage figures. Switching a column from BIGINT to INT on a billion-row table saves four gigabytes.
  4. Read the money demo closely. The FLOAT sum is not 0.30, and the equality test fails. That is not a bug in this page — it is how binary floating point works everywhere.
  5. Select CHAR(10) and type a short word to see how much of the fixed width is wasted on padding.

Key Takeaway

Pick the narrowest type that will hold every legitimate value, use DECIMAL for anything monetary, use real date types for dates, and add NOT NULL wherever a missing value would be meaningless. These choices are hard to change later and quietly govern correctness forever.

Cheat sheet

Datatypes in SQL

Every column declares what it can hold and how much space that costs. Push values past the edge of a type, watch FLOAT lose money that DECIMAL keeps, and see why the right type is a correctness decision, not a formality.

DATABASE · vizlearn.in/database/datatypes_in_sql.html