Pick the smallest type that correctly holds every value you will ever store. The right type saves disk, shrinks indexes, and prevents silent data corruption.
Pick the smallest type that correctly holds every value you will ever store. The right type saves disk, shrinks indexes, and prevents silent data corruption.
INT (4 bytes) for most ids; BIGINT (8 bytes) only when you truly outgrow ~2.1 billion.DECIMAL(10,2) for money — never FLOAT/DOUBLE, which round in binary and lose cents.VARCHAR(n) for bounded text (names, emails); the n is a cap, storage is only what you use.TEXT for large blobs (articles) — it is stored off-page and cannot have a default; don't use it for short values you filter on.DATETIME (no timezone, 8 bytes) vs TIMESTAMP (UTC-converted, 4 bytes, ~2038 limit).CREATE TABLE payments (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
amount DECIMAL(10,2) NOT NULL, -- exact money, not FLOAT
currency CHAR(3) NOT NULL, -- always 3 chars -> fixed CHAR
note VARCHAR(255), -- bounded, indexable
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Pitfall: the default SQL mode rejects bad values (good) --
INSERT INTO payments (amount) VALUES ('abc'); -- ERROR 1366, not silently 0
FLOAT for money.VARCHAR(255) everywhere out of habit — oversized keys bloat indexes.UNSIGNED on ids/counts that are never negative (doubles the positive range).Data types are the cheapest correctness and performance decision you make, and the hardest to change later — narrow, exact types keep indexes small and stop bad data at the door before it becomes a production bug.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate