Lesson

MySQL Data Types

10 min reading
Free Course

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 FLOAT or DOUBLE for Money: Binary floating-point types approximate decimals (0.1 + 0.2 = 0.30000000000000004), causing penny rounding errors in invoices. Always use DECIMAL(M, D) or store integer cents in BIGINT.
  • Use TINYINT Instead of ENUM for Statuses: Adding or reordering ENUM values later requires an ALTER TABLE lock. Storing TINYINT UNSIGNED status 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 declared VARCHAR length, so size VARCHAR(100) or VARCHAR(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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum