MySQL Data Types
MySQL Data Types: Choosing Accurate, Compact Columns
MySQL data types define what kind of values each column stores, how much memory and disk space each row consumes, and how sorting and comparisons behave. Picking the smallest accurate type keeps InnoDB indexes compact so more hot data fits in RAM.
Essential MySQL Data Type Groups
| Category | Common Types | Best Used For |
|---|---|---|
| Integer | TINYINT, INT, BIGINT UNSIGNED |
IDs, status codes, counters, quantities |
| Exact Decimal | DECIMAL(10, 2) |
Prices, invoices, taxes, account balances |
| String | CHAR(n), VARCHAR(n), TEXT |
Fixed codes (CHAR(2)), names/emails (VARCHAR), articles (TEXT) |
| Date & Time | DATE, DATETIME, TIMESTAMP |
Birthdays, scheduled events, UTC timestamps |
| Structured / Boolean | TINYINT(1) (BOOLEAN), JSON |
Flags (0/1), flexible metadata objects |
Practical SQL Example
This schema demonstrates production-ready column types for a demo_products table:
CREATE TABLE demo_products (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sku CHAR(8) NOT NULL UNIQUE,
product_name VARCHAR(120) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock_qty INT UNSIGNED NOT NULL DEFAULT 0,
is_active TINYINT(1) NOT NULL DEFAULT 1,
attributes JSON NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Common Mistakes and Best Practices
- Never Use
FLOATorDOUBLEfor Money: Binary floating-point types approximate decimals (0.1 + 0.2 = 0.30000000000000004), causing penny rounding errors in invoices. Always useDECIMAL(M, D)or store integer cents inBIGINT. - Use
TINYINTInstead ofENUMfor Statuses: Adding or reorderingENUMvalues later requires anALTER TABLElock. StoringTINYINT UNSIGNEDstatus codes (with clear documentation or a lookup table) keeps migrations zero-downtime. - Avoid Oversizing
VARCHAR(255)Everywhere: Temporary in-memory sort buffers allocate space based on max declaredVARCHARlength, so sizeVARCHAR(100)orVARCHAR(50)realistically.
Frequently Asked Questions
What is the difference between CHAR(10) and VARCHAR(10)?
CHAR(10) always stores fixed 10-character width (ideal for fixed ISO country codes like CHAR(2)), while VARCHAR(10) stores only the actual string length plus a 1–2 byte length prefix.
How does BOOLEAN work in MySQL?
BOOLEAN is an alias for TINYINT(1), where FALSE equals 0 and TRUE equals 1.
When should I use JSON vs. normalized columns?
Use normalized columns for attributes you frequently filter, sort, or join on, and reserve JSON for optional, variable metadata payloads.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.