MySQL organizes its column data types into a few main categories: numeric types for storing numbers, string types for text, and date/time types for temporal values, each with variants suited to different precision and storage needs.
Key Points: • Numeric types include INT for whole numbers, FLOAT/DOUBLE for approximate decimals, and DECIMAL for exact fixed-point values like currency. • String types include VARCHAR for variable-length text, CHAR for fixed-length text, and TEXT for large blocks of text. • Date and time types include DATE for a calendar date and DATETIME for a combined date and time. • Choosing DECIMAL over FLOAT/DOUBLE for money avoids floating-point rounding errors in financial calculations. • Picking the smallest type that safely fits your data (e.g. VARCHAR(50) instead of TEXT for a name) keeps storage and indexing efficient.
Example: A products table would use DECIMAL(10,2) for price to avoid rounding errors, VARCHAR(100) for the product name since lengths vary, and DATETIME for created_at to record exactly when the row was inserted.
Code Example:
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
price DECIMAL(10, 2),
description TEXT,
created_at DATETIME
);Interview Tip: A concise interview answer is:
"MySQL's data types fall into numeric types like INT, FLOAT, and DECIMAL, string types like VARCHAR, CHAR, and TEXT, and date/time types like DATE and DATETIME. The main judgment call is picking DECIMAL over FLOAT for anything like money, since DECIMAL avoids floating-point rounding errors that FLOAT and DOUBLE are prone to."