Every column in a table has a data type. It tells MySQL what kind of value the column can store, and how much space to use.
Numeric types
| Type | Use | Example |
|---|
INT | Whole numbers (about -2 billion to 2 billion) | age INT |
BIGINT | Very big whole numbers | views BIGINT |
TINYINT | Small numbers (-128 to 127). TINYINT(1) is used as true/false | is_active TINYINT(1) |
DECIMAL(p, s) | Exact decimals, like money. p = total digits, s = digits after the point | price DECIMAL(10,2) |
FLOAT, DOUBLE | Approximate decimals | rating FLOAT |
String (text) types
| Type | Use |
|---|
CHAR(n) | Fixed length text, like a country code CHAR(2) |
VARCHAR(n) | Text up to n characters. Most used for names and emails |
TEXT | Long text, like a blog post or description |
ENUM(...) | One value from a fixed list, like ENUM('male','female') |
JSON | JSON data |
Date and time types
| Type | Format | Example |
|---|
DATE | YYYY-MM-DD | 2025-01-15 |
TIME | HH:MM:SS | 10:30:00 |
DATETIME | YYYY-MM-DD HH:MM:SS | 2025-01-15 10:30:00 |
TIMESTAMP | Like DATETIME, stored as UTC and can update by itself | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
YEAR | YYYY | 2025 |
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2),
description TEXT,
in_stock TINYINT(1) DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
How to choose
- Use
DECIMAL for money, never FLOAT, because floats can give small errors. - Use
VARCHAR with a sensible limit, like 100 for a name and 255 for an email. - Phone numbers are text (
VARCHAR(15)), not numbers, because they can start with 0 or have a +. - Store dates in a
DATE or DATETIME column, not as text.
Summary
- Pick the smallest type that fits your data.
- Numbers:
INT, DECIMAL. Text: VARCHAR, TEXT. Dates: DATE, DATETIME. - Next: CREATE DATABASE.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
| No Content Found. |
| Interview Questions & Answers |
| No Content Found. |