Data Types
3. Data Types
Section titled “3. Data Types”Numeric Types
Section titled “Numeric Types”TINYINT -- 1 byte, range: -128 to 127SMALLINT -- 2 bytes, range: -32,768 to 32,767MEDIUMINT -- 3 bytesINT -- 4 bytes, range: ~-2B to 2BBIGINT -- 8 bytesFLOAT(p) -- Approximate decimal (4 or 8 bytes)DOUBLE -- Double precision floatDECIMAL(M,D) -- Exact decimal → use for money! (e.g., DECIMAL(10,2))String Types
Section titled “String Types”CHAR(n) -- Fixed length, max 255 → fast for fixed-size data (e.g., country code)VARCHAR(n) -- Variable length, max 65,535 → most commonTEXT -- Up to 65,535 chars (no default, not indexable fully)MEDIUMTEXT -- Up to 16 MBLONGTEXT -- Up to 4 GBENUM('a','b')-- One value from a list → stored as integer internallySET('a','b') -- Multiple values from a listDate & Time Types
Section titled “Date & Time Types”DATE -- YYYY-MM-DDTIME -- HH:MM:SSDATETIME -- YYYY-MM-DD HH:MM:SS (no timezone)TIMESTAMP -- YYYY-MM-DD HH:MM:SS (stores UTC, auto-converts timezone)YEAR -- YYYYInterview Tip: Use
TIMESTAMPfor audit fields (created_at,updated_at) — it auto-converts to server timezone. UseDATETIMEwhen timezone-independence is needed.
Binary Types
Section titled “Binary Types”BINARY(n) -- Fixed-length binaryVARBINARY(n) -- Variable-length binaryBLOB -- Binary large object (images, files)