Choosing the right data type in MySQL helps you store data efficiently, avoid errors, and improve performance.
MySQL Basics Data Modeling Table Design
MySQL provides different categories of data types so that each column can store the right kind of value, such as numbers, text, or dates.
BOOLEAN, ENUM, and SET.When creating a table, you specify the data type directly after the column name.
-- Defining columns with different MySQL data types
CREATE TABLE products (
id INT UNSIGNED PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) NOT NULL,
in_stock BOOLEAN DEFAULT TRUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
In the example above:
INT UNSIGNED – integer ≥ 0, good for IDs.VARCHAR(100) – variable-length string up to 100 characters.DECIMAL(10,2) – up to 10 digits total, with 2 digits after the decimal point.BOOLEAN – stored internally as TINYINT(1) (0 or 1).DATETIME – stores both date and time.Use numeric types when you want to store numbers that can be used in calculations.
-- Example: numeric data types for an orders table
CREATE TABLE orders (
order_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_id INT UNSIGNED NOT NULL,
total_amount DECIMAL(8,2) NOT NULL,
discount_percent TINYINT UNSIGNED DEFAULT 0
);
String types are used to store text, from short labels to long articles.
-- Example: string data types for a users table
CREATE TABLE users (
user_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
bio TEXT NULL
);
Date and time types help you track when things happened.
YYYY-MM-DD format.HH:MM:SS format.
-- Example: date & time columns for an events table
CREATE TABLE events (
event_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
starts_at DATETIME NOT NULL,
ends_at DATETIME NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
MySQL also provides special-purpose types that limit values to a predefined set.
TINYINT(1), usually 0 or 1.'pending', 'paid', 'cancelled'.
-- Example: ENUM and BOOLEAN in an invoice table
CREATE TABLE invoices (
invoice_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
status ENUM('PENDING', 'PAID', 'CANCELLED') NOT NULL DEFAULT 'PENDING',
is_recurring BOOLEAN DEFAULT FALSE
);
Let us insert some sample data and see how MySQL stores it according to the data types.
-- Insert sample rows to observe data types in action
INSERT INTO products (id, name, price, in_stock)
VALUES
(1, 'Keyboard', 999.50, TRUE),
(2, 'Mouse', 499.00, TRUE),
(3, 'Monitor', 8999.99, FALSE);
id uses INT UNSIGNED, so negative IDs would be rejected.price uses DECIMAL(10,2), so every value will be stored with exactly 2 decimal places.in_stock uses BOOLEAN, so TRUE is stored internally as 1 and FALSE as 0.'abc' into price, MySQL would either convert or reject the value depending on SQL mode.BIGINT if INT is enough.DATE for birth dates, not VARCHAR).UNSIGNED for IDs and counts that can never be negative.VARCHAR over TEXT when the maximum length is known and small.DECIMAL for prices and financial information.students table with:
student_id as INT UNSIGNED primary keyfull_name as VARCHAR(80)email as VARCHAR(120)dob as DATEgpa as DECIMAL(3,2)blog_posts table that uses both VARCHAR and TEXT types.VARCHAR to INT and see what happens if the data cannot be converted.ENUM column called priority to a tasks table with values 'LOW', 'MEDIUM', 'HIGH'.
-- Practice: create a students table with appropriate data types
CREATE TABLE students (
student_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(80) NOT NULL,
email VARCHAR(120) NOT NULL,
dob DATE NOT NULL,
gpa DECIMAL(3,2) NULL
);