What are MySQL Data Types?
In MySQL, data types define the type of data a column can hold. They are essential for structuring and optimizing your database. The choice of data type affects performance, storage, and accuracy.
Types of MySQL Data Types
MySQL data types are broadly categorized into:
- String Data Types
- Numeric Data Types
- Date and Time Data Types
1. String Data Types
Data Type | Description | Example |
---|---|---|
CHAR(size) | Fixed-length string. Default size is 1. | 'A' , 'Hello' |
VARCHAR(size) | Variable-length string. Max size: 65535 bytes. | 'Database' , 'MySQL' |
TEXT | Large text, up to 65,535 characters. | 'This is a long text.' |
ENUM(values) | A string object with predefined values. | 'small' , 'medium' |
BLOB | Binary large objects for storing binary data. |
Example
CREATE TABLE users (
id INT,
name VARCHAR(50),
bio TEXT
);
2. Numeric Data Types
Data Type | Description | Example |
---|---|---|
TINYINT(size) | Integer from -128 to 127. | 1 , -50 |
SMALLINT(size) | Integer from -32,768 to 32,767. | 32000 |
INT(size) | Integer from -2,147,483,648 to 2,147,483,647. | 1000000 |
BIGINT(size) | Large integer values. | 9223372036854775807 |
DECIMAL(size, d) | Exact decimal values. | 12.345 |
FLOAT(size, d) | Approximate floating-point numbers. | 12.34 |
Example
CREATE TABLE products (
product_id INT AUTO_INCREMENT,
price DECIMAL(10, 2),
stock TINYINT
);
3. Date and Time Data Types
Data Type | Description | Example |
---|---|---|
DATE | Stores date values. Format: YYYY-MM-DD . | '2024-12-26' |
DATETIME | Stores date and time. Format: YYYY-MM-DD HH:MM:SS . | '2024-12-26 15:30:00' |
TIMESTAMP | Stores timestamp values. | '2024-12-26 15:30:00' |
TIME | Stores time values. Format: HH:MM:SS . | '15:30:00' |
YEAR | Stores year values. Format: YYYY . | '2024' |
Example
CREATE TABLE events (
event_id INT,
event_date DATE,
event_time TIME,
created_at TIMESTAMP
);
Choosing the Right Data Type
- Match Data Type to Data: Use
VARCHAR
for flexible strings,INT
for numbers, andDATE
for dates. - Optimize Storage: Avoid larger data types if smaller ones suffice. For example, use
TINYINT
instead ofINT
for small values. - Plan for Growth: Anticipate future needs. If a
VARCHAR(50)
might exceed its limit, chooseVARCHAR(100)
.
Best Practices
Use ENUM for Limited Choices
CREATE TABLE shirts (
size ENUM('small', 'medium', 'large')
);
Avoid NULL Where Possible
Avoid NULL Where Possible
Use NOT NULL
to enforce data integrity.
Index Numeric and Date Columns
Index Numeric and Date Columns
Indexing improves query performance, especially for numeric and date columns.
Choose Fixed vs. Variable Length Wisely
Choose Fixed vs. Variable Length Wisely
Use CHAR
for fixed-length data (e.g., codes) and VARCHAR
for variable-length data.
Choose Fixed vs. Variable Length Wisely
Minimize Usage of TEXT and BLOB
Use TEXT
and BLOB
only when necessary, as they require special handling.
MySQL Data type
Quiz-summary
0 of 4 questions completed
Questions:
- 1
- 2
- 3
- 4
Information
Quiz: Test Your Knowledge
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 4 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- Answered
- Review
- Question 1 of 4
1. Question
Which data type is best for storing a price value like 99.99?
CorrectIncorrect - Question 2 of 4
2. Question
What is the maximum size of a VARCHAR column?
CorrectIncorrect - Question 3 of 4
3. Question
Which data type is suitable for storing date values only?
CorrectIncorrect - Question 4 of 4
4. Question
Which query creates a column for binary data?
CorrectIncorrect