Understanding MySQL Data Types
In our previous lesson on setting-up-mysql, we learned how to prepare our environment. Now, we dive into one of the most critical aspects of database design: Data Types. Choosing the right data type is like choosing the right container for storage; you wouldn't put water in a cardboard box, and you shouldn't store a birthdate as a simple text string.
MySQL data types define what kind of value a column can hold. This ensures data integrity, optimizes storage space, and improves query performance.
Why Data Types Matter
- Storage Efficiency: Using
TINYINTinstead ofINTfor small numbers saves bytes on every row. - Data Integrity: Prevents users from entering text into a price field.
- Performance: Mathematical operations are faster on numeric types than on strings.
- Sorting: Dates stored as strings sort alphabetically, while proper date types sort chronologically.
Categories of MySQL Data Types
MySQL organizes data types into several broad categories. Understanding these helps you navigate the introduction-to-mysql ecosystem more effectively.
1. Numeric Data Types
Used for storing numbers, whether they are whole integers or decimals.
- INT: A standard integer. Use this for IDs and general counts.
- TINYINT: Very small integers (range -128 to 127). Perfect for boolean flags (0 or 1).
- BIGINT: For very large numbers, such as global transaction IDs.
- DECIMAL(p, s): Fixed-point numbers. Essential for financial data where accuracy is non-negotiable.
- FLOAT/DOUBLE: Floating-point numbers for scientific calculations where slight rounding is acceptable.
2. String (Character) Data Types
Used for storing text, from single characters to entire books.
- CHAR(n): Fixed-length strings. If you define
CHAR(5)and store "AB", it still uses 5 bytes. Use this for codes of fixed length like ISO country codes. - VARCHAR(n): Variable-length strings. If you store "AB" in
VARCHAR(255), it only uses 2 bytes plus a length prefix. This is the most common type for names and emails. - TEXT: Used for long-form content like blog posts or descriptions.
- BLOB: Binary Large Objects, used for storing images or PDF files directly in the database (though often avoided for performance reasons).
3. Date and Time Data Types
Crucial for tracking when events occur.
- DATE: Stores YYYY-MM-DD.
- DATETIME: Stores YYYY-MM-DD HH:MM:SS.
- TIMESTAMP: Similar to DATETIME but converted to UTC for storage and back to local time for retrieval. Great for "Last Updated" fields.
- YEAR: Stores a four-digit year.
Data Type Selection Flowchart
[Start: What are you storing?]
|
|-- [Numbers?] --> [Whole Number?] -- Yes --> [Small?] -> TINYINT
| | |-- No --> [Large?] -> BIGINT
| |-- [Decimals?] -- Yes --> [Money?] -> DECIMAL
| |-- No --> [Scientific?] -> FLOAT
|
|-- [Text?] ----> [Fixed Length?] -- Yes --> CHAR
| |-- [Variable Length?] -- Yes --> [Long?] -> TEXT
| |-- No --> VARCHAR
|
|-- [Temporal?] -> [Date Only?] -> DATE
|-- [Date & Time?] -> DATETIME
Practical Example: Building a User Table
Let's apply our knowledge by creating a table for a web application. Notice how we select types based on the nature of the data.
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
email VARCHAR(100),
age TINYINT UNSIGNED,
account_balance DECIMAL(10, 2),
bio TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Common Mistakes to Avoid
- Using VARCHAR for everything: Storing numbers as strings prevents you from using mathematical functions like
SUM()orAVG()efficiently. - Choosing BIGINT by default: Unless you expect billions of rows, a standard
INTis more memory-efficient. - Ignoring DECIMAL for Money: Never use
FLOATorDOUBLEfor currency. Rounding errors in floating-point math can lead to lost cents over time. - Overestimating VARCHAR length: Don't just set
VARCHAR(255)for every field. Be realistic to help the database engine optimize memory.
Real-World Use Cases
E-commerce Platform: Use DECIMAL for product prices, INT for stock levels, and DATETIME for order placement times.
Social Media App: Use VARCHAR for handles, TEXT for posts, and TINYINT to represent whether a profile is public (1) or private (0).
Interview Notes for Database Developers
- Question: What is the difference between
CHARandVARCHAR? - Answer:
CHARis fixed-length and pads remaining space with blanks, making it faster for fixed-size data.VARCHARis variable-length and saves space for varying data sizes but has a slight overhead for length calculation. - Question: When should you use
TIMESTAMPoverDATETIME? - Answer: Use
TIMESTAMPwhen you need to track changes across different time zones, as it automatically converts to the server's local time zone. UseDATETIMEfor fixed dates like birthdays.
Summary
Understanding MySQL data types is the foundation of efficient database design. By choosing the most specific type possible—such as TINYINT for small numbers or DECIMAL for financial data—you ensure your database is fast, reliable, and scalable. In the next part of our MySQL Mastery series, we will explore how to create and manage tables using these types.