Why Types Matter
A column type decides what values are allowed, how much space they use, and how they sort and compare. The right type keeps data correct and queries fast.
Integer Types
| Type | Bytes | Signed range | Use for |
|---|---|---|---|
TINYINT | 1 | -128 to 127 | Flags, small codes, ratings |
SMALLINT | 2 | -32,768 to 32,767 | Small counts, years |
INT | 4 | about -2.1 billion to 2.1 billion | Normal ids and counts |
BIGINT | 8 | about 9.2 quintillion | Huge tables, large counters |
Add UNSIGNED to allow only zero and positive numbers, which doubles the positive range (an INT UNSIGNED goes up to about 4.29 billion). Ids and quantities are good candidates.
Result:
| id | stars |
|---|---|
| 1 | 5 |
| 2 | 4 |
A value outside the range is rejected in strict mode:
ERROR 1264: Out of range value for column 'stars' at row 1
INT(11) the number is only a display hint that MySQL 8 has deprecated. It does not change the range. Write INT.
DECIMAL, FLOAT and DOUBLE
Use **DECIMAL(p, s)** for money: p total digits, s digits after the decimal point. It is exact. FLOAT and DOUBLE are approximate binary numbers, good for science and measurements but not for money.
Result:
| decimal_sum | double_sum | double_rounded |
|---|---|---|
| 0.30 | 0.3 | 0.3 |
DECIMAL gives exactly 0.30. The double is stored as a binary approximation; MySQL prints a short form, but comparisons can still surprise you:
Result:
| double_equal | decimal_equal |
|---|---|
| 0 | 1 |
double_equal is 0 (false) while decimal_equal is 1 (true). Never compare floating point numbers with =.
Text Types
| Type | Max size | Notes |
|---|---|---|
CHAR(n) | 255 characters | Fixed length, padded. For codes like 'IN' |
VARCHAR(n) | up to 65,535 bytes per row | Variable length. The normal choice for names and emails |
TEXT | 65,535 bytes | Long text. Cannot have a plain default value |
MEDIUMTEXT, LONGTEXT | 16 MB, 4 GB | Articles, documents |
With utf8mb4, each character may take up to 4 bytes, so VARCHAR(255) can use up to 1020 bytes.
Result:
| country | name | characters | bytes |
|---|---|---|---|
| IN | India | 5 | 5 |
| US | United States | 13 | 13 |
ERROR 1406: Data too long for column 'name' at row 1
Strict mode rejects text that is too long instead of cutting it.
Date and Time Types
| Type | Stores | Range | Notes |
|---|---|---|---|
DATE | Year-month-day | 1000-01-01 to 9999-12-31 | Birthdays, order dates |
TIME | Time or duration | -838:59:59 to 838:59:59 | Durations |
DATETIME | Date and time | 1000 to 9999 | Stored as written, no time zone conversion |
TIMESTAMP | Date and time | 1970 to 2038 | Stored as UTC and converted to the session time zone |
YEAR | Year | 1901 to 2155 | Rarely used |
Result:
| id | happened | has_created |
|---|---|---|
| 1 | 2025-03-15 14:30:00 | 1 |
TIMESTAMP stops at the year 2038 and changes with the session time zone, so many teams prefer DATETIME and store UTC in the application. Choose one convention and keep it.
ENUM and SET
ENUM stores one value from a fixed list, using 1 or 2 bytes internally.
Result:
| id | status |
|---|---|
| 1 | open |
| 2 | closed |
ERROR 1265: Data truncated for column 'status' at row 1
ALTER TABLE, and sorting follows the list order, not the alphabet. Many designers prefer a small lookup table plus a foreign key for values that change.
BOOLEAN
BOOLEAN is just a synonym for TINYINT(1). TRUE is 1 and FALSE is 0.
Result:
| id | active |
|---|---|
| 1 | 1 |
| 2 | 0 |
JSON and BLOB (Preview)
JSON stores structured documents with validation and special functions (a later lesson covers it). BLOB stores binary data. For large files such as photos, store the file in object storage and keep only its path in the database.
Choosing Types: A Quick Guide
| Data | Good type |
|---|---|
| Primary key, count | INT UNSIGNED or BIGINT UNSIGNED |
| Price, money | DECIMAL(10,2) |
| Name, email, title | VARCHAR(100) and similar |
| Country or currency code | CHAR(2), CHAR(3) |
| Long description | TEXT |
| Date only | DATE |
| Moment in time | DATETIME (store UTC) or TIMESTAMP |
| Status with few fixed values | ENUM, or a lookup table |
| Yes/no flag | BOOLEAN |
| Phone number | VARCHAR(20), never a number |
Phone numbers, postal codes and ids from other systems are text: they can start with 0 and you never add them up.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Using FLOAT for money | Rounding errors | Use DECIMAL |
| Storing dates as text | Wrong sorting, no date functions | Use DATE or DATETIME |
VARCHAR(255) everywhere | Waste and sloppy design | Pick a sensible limit |
Phone number as INT | Leading zeros and + lost | VARCHAR |
Ignoring UNSIGNED on ids | Half the key range unused | Use UNSIGNED for ids and counts |
Practice
- Create a
productstable with the right types for id, name, price, stock, created and active. - Insert a price with 3 decimals into a
DECIMAL(10,2)column and see what is stored. - Create an
ENUMcolumn and try an invalid value. - Compare
CHAR_LENGTHandLENGTHfor the text'café'.
Conclusion
Pick the smallest sensible type: INT UNSIGNED for ids, DECIMAL for money, VARCHAR for text, DATE or DATETIME for time, ENUM or lookup tables for fixed lists, and keep phone numbers as text.

