Data types specify how MySQL stores, validates, and manipulates column values during table creation and updates.
Picking the right data type optimizes storage, speeds up queries, and prevents invalid data from entering your tables. Wrong choices lead to wasted space and casting overhead.
Use TINYINT
, SMALLINT
, INT
, or BIGINT
for whole numbers depending on range.Choose DECIMAL(p,s)
for money to avoid rounding, and FLOAT/DOUBLE
only when minor precision loss is acceptable.
total_amount DECIMAL(10,2)
stores up to 99999999.99 without floating-point errors.
Use DATE
for calendar dates, DATETIME
for timestamps independent of time zone, and TIMESTAMP
when you need automatic UTC conversion.
order_date DATETIME DEFAULT CURRENT_TIMESTAMP
captures order time in local server time.
Use CHAR(n)
for fixed-length codes (e.g., country ISO), VARCHAR(n)
for variable text, and TEXT
for long descriptions.Keep VARCHAR
length close to expected max to save memory.
email VARCHAR(255) UNIQUE
handles all RFC-valid emails.
JSON
columns let you store flexible attributes with built-in validation. ENUM
enforces a controlled list of values while remaining space-efficient.
status ENUM('pending','paid','shipped','cancelled')
restricts order status to valid states.
1. Match the smallest range that fits. 2. Prefer UNSIGNED
when negatives are impossible. 3. Index only columns with selective values. 4.Use NOT NULL
plus sensible defaults to avoid NULL handling overhead.
Storing currency in FLOAT
, using overly large VARCHAR
limits, and forgetting time-zone effects with TIMESTAMP
lead to subtle bugs.
.
Yes, use ALTER TABLE table_name MODIFY column_name new_type
, but large tables may lock. Schedule during low-traffic windows.
Prefer VARCHAR(1000)
unless you exceed 64 KB. TEXT breaks index length limits and can slow queries.
ENUM is fine for short, stable lists. For frequently changing sets, create a lookup table with foreign keys instead.