SQL Data Types
Picking the right type makes queries faster, indexes smaller, and bugs less likely. The big families exist in every major engine — only the names differ.
Integers
| Type | Range | Bytes |
|---|---|---|
TINYINT | 0–255 / -128–127 | 1 |
SMALLINT | ±32k | 2 |
INT | ±2.1B | 4 |
BIGINT | ±9 quintillion | 8 |
Decimals & floats
DECIMAL(p, s)— exact, base-10. Use for money:DECIMAL(10, 2).FLOAT/DOUBLE— binary IEEE-754. Fast but lossy — never for money.
Strings
| Type | Use |
|---|---|
CHAR(n) | Fixed-length. Padded with spaces. |
VARCHAR(n) | Variable-length up to n. The default choice. |
TEXT | Effectively unbounded. Stored separately on some engines. |
Dates & times
DATE— Y-M-D only.TIME— H:M:S only.DATETIME/TIMESTAMP— both. PG / SQL Server:TIMESTAMPTZstores zone.
Binary & JSON
BLOB/BYTEA— raw bytes.JSON/JSONB— structured data;JSONBin Postgres is indexable.UUID— 16-byte global identifier (PG native; emulate withCHAR(36)elsewhere).
Tip: Pick the smallest type that fits the foreseeable range. Smaller types use less RAM, fit more in cache, and let indexes scan faster.
Example
Example
CREATE TABLE demo ( id INT, name VARCHAR(80), price DECIMAL(10,2), notes TEXT, created_at DATETIME );Try it Yourself »
Exercise
Storing money should use…
price
(10, 2)
Seven letters; the exact base-10 type.
Discussion
Loading…