Lesson 16 of 60 – MySQL Data Types
27%

MySQL Data Types

MySQL data types define the kind of data that can be stored in a table column. Choosing the correct data type helps organize data properly and can improve storage and query efficiency.

Note: Always choose a data type according to the type of information a column needs to store, such as numbers, text, dates, or binary data.

1. What are Data Types?

A data type specifies what kind of value a column can store.

id       INT
name     VARCHAR(100)
price    DECIMAL(10,2)
dob      DATE

Each column has a data type suitable for its purpose.

2. Main Categories of MySQL Data Types

MySQL data types can broadly be grouped into:

  • Numeric data types
  • String data types
  • Date and time data types
  • Spatial data types
  • JSON data type
  • Binary data types

3. Numeric Data Types

Numeric data types are used to store numbers.

Common examples include:

INT
BIGINT
DECIMAL
FLOAT
DOUBLE

4. INT

INT is commonly used for whole numbers.

CREATE TABLE students (
    id INT,
    age INT
);

Examples of integer values are 1, 25, 100, and 500.

5. BIGINT

BIGINT is used for larger integer values than INT.

CREATE TABLE transactions (
    transaction_id BIGINT
);

It can be useful when an application needs to store very large integer values.

6. DECIMAL

DECIMAL is commonly used when exact decimal values are required.

CREATE TABLE fees (
    amount DECIMAL(10,2)
);

It is commonly suitable for financial values such as fees and prices.

7. DECIMAL Precision and Scale

In DECIMAL(10,2):

  • 10 represents the total number of digits.
  • 2 represents the number of digits after the decimal point.
DECIMAL(10,2)

Example:

15000.50

8. FLOAT

FLOAT is used for approximate floating-point numbers.

CREATE TABLE measurements (
    value FLOAT
);

It can be useful for scientific or measurement-related values where approximate representation is acceptable.

9. DOUBLE

DOUBLE is another approximate floating-point numeric type with a larger range and precision than FLOAT.

CREATE TABLE calculations (
    result DOUBLE
);

10. String Data Types

String data types are used to store text and character data.

Common examples include:

CHAR
VARCHAR
TEXT
LONGTEXT

11. CHAR

CHAR is used for fixed-length strings.

CREATE TABLE employees (
    gender CHAR(1)
);

It can be useful when values have a consistent fixed length.

12. VARCHAR

VARCHAR is used for variable-length text.

CREATE TABLE students (
    name VARCHAR(100),
    email VARCHAR(150)
);

VARCHAR is commonly used for names, emails, addresses, and similar text.

13. CHAR vs VARCHAR

CHAR VARCHAR
Fixed-length string Variable-length string
Useful for fixed-size values Useful for variable-size text
Example: CHAR(1) Example: VARCHAR(100)

14. TEXT

TEXT is used for larger amounts of text.

CREATE TABLE posts (
    title VARCHAR(200),
    content TEXT
);

It can be useful for descriptions, articles, comments, and other text content.

15. LONGTEXT

LONGTEXT is designed for very large text values.

CREATE TABLE documents (
    id INT,
    content LONGTEXT
);

It can be useful when an application needs to store very large text content.

16. Date and Time Data Types

MySQL provides data types for storing dates and times.

Common examples include:

DATE
TIME
DATETIME
TIMESTAMP
YEAR

17. DATE

DATE stores a calendar date.

CREATE TABLE students (
    admission_date DATE
);

A DATE value can look like:

2026-09-21

18. TIME

TIME stores a time value.

CREATE TABLE classes (
    start_time TIME
);

Example:

09:00:00

19. DATETIME

DATETIME stores both date and time.

CREATE TABLE attendance (
    login_time DATETIME
);

Example:

2026-09-21 09:30:00

20. TIMESTAMP

TIMESTAMP is another date and time data type and is commonly used for recording timestamps such as when a record was created or updated.

CREATE TABLE users (
    id INT,
    created_at TIMESTAMP
);

21. YEAR

YEAR is used to store year values.

CREATE TABLE employees (
    name VARCHAR(100),
    joining_year YEAR
);

Example:

2026

22. BOOLEAN

MySQL supports BOOLEAN as a synonym for TINYINT(1).

It is commonly used when an application needs a true/false-style value.

CREATE TABLE students (
    id INT,
    active BOOLEAN
);

Applications commonly use values representing true or false.

23. BINARY Data Types

MySQL provides binary string data types for storing binary data.

Common examples include:

BINARY
VARBINARY
BLOB

These types can be useful for binary information such as certain files or raw data.

24. BLOB

BLOB stands for Binary Large Object.

It can be used for storing binary data.

CREATE TABLE files (
    id INT,
    file_data BLOB
);

25. JSON Data Type

MySQL provides a JSON data type for storing JSON documents.

CREATE TABLE users (
    id INT PRIMARY KEY,
    profile JSON
);

This can be useful when applications need to store structured JSON data.

26. Choosing the Correct Data Type

Data Suitable Type
Student ID INT
Student Name VARCHAR
Course Description TEXT
Fee DECIMAL
Birth Date DATE
Login Time DATETIME
Active Status BOOLEAN

27. Example with Different Data Types

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    fee DECIMAL(10,2),
    birth_date DATE,
    login_time DATETIME,
    active BOOLEAN
);

This table demonstrates several commonly used MySQL data types.

28. Common Data Type Mistakes

Some common mistakes include:

  • Using VARCHAR for every type of data
  • Using INT for decimal amounts
  • Using TEXT for short fixed values unnecessarily
  • Using an inappropriate date or time type
  • Choosing an unnecessarily large data type
  • Ignoring the actual range of values required

29. Complete Data Type Example

CREATE DATABASE IF NOT EXISTS training_db;

USE training_db;

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT,
    mobile VARCHAR(15),
    fee DECIMAL(10,2),
    admission_date DATE,
    admission_time TIME,
    created_at DATETIME,
    active BOOLEAN,
    profile JSON
);

This example demonstrates numeric, string, decimal, date, time, Boolean, and JSON data types.

30. Practical Student Table

Here is a practical example of selecting data types for a student management application:

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    student_name VARCHAR(100) NOT NULL,
    father_name VARCHAR(100),
    mobile VARCHAR(15) NOT NULL,
    email VARCHAR(150),
    age INT,
    course VARCHAR(100),
    fee DECIMAL(10,2) DEFAULT 0.00,
    admission_date DATE,
    login_time DATETIME,
    active BOOLEAN DEFAULT TRUE
);

Each column uses a data type according to the kind of information it stores.

📌 Key Points

  • Data types define what kind of values a column can store.
  • INT is commonly used for whole numbers.
  • BIGINT is used for larger integer values.
  • DECIMAL is useful for exact decimal values such as fees and prices.
  • CHAR stores fixed-length strings.
  • VARCHAR stores variable-length text.
  • TEXT and LONGTEXT are used for larger text values.
  • DATE, TIME, DATETIME, and TIMESTAMP store date/time information.
  • BOOLEAN is commonly used for true/false-style values.
  • JSON can store JSON documents.

🧠 Quick Quiz

Question: Which MySQL data type is commonly used to store exact decimal values such as fees and prices?