✏️ Explanatory Question

What are the different data types in MySQL?

👁 8 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

5

What are the different data types in MySQL?

Level: Basic — Tests whether you can pick the right type for each column, which affects storage and performance.

MySQL data types are grouped into three main categoriesNumeric, String (Character), and Date & Time. Choosing the correct type keeps your database efficient and your data accurate.

1. Numeric Types

  • INT / INTEGER: Whole numbers (also TINYINT, SMALLINT, MEDIUMINT, BIGINT for different ranges).
  • DECIMAL / NUMERIC: Exact fixed-point values — ideal for money.
  • FLOAT / DOUBLE: Approximate floating-point values for scientific data.
  • BIT: Stores bit values.

2. String (Character) Types

  • CHAR: Fixed-length string (0–255).
  • VARCHAR: Variable-length string (up to 65,535).
  • TEXT: Large text (TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT).
  • BLOB: Binary large objects (images, files).
  • ENUM / SET: A value from a predefined list (ENUM = one, SET = many).

3. Date & Time Types

  • DATE: Stores a date (YYYY-MM-DD).
  • DATETIME: Date and time combined.
  • TIMESTAMP: Date and time, auto-updates, timezone-aware.
  • TIME: Stores time only (HH:MM:SS).
  • YEAR: Stores a year value.
Key point: Always use DECIMAL (not FLOAT/DOUBLE) for money — floating-point types are approximate and can cause rounding errors in financial calculations.

Quick Reference — Common Numeric Ranges

Type Storage Signed Range
TINYINT 1 byte -128 to 127
SMALLINT 2 bytes -32,768 to 32,767
INT 4 bytes -2.1B to 2.1B
BIGINT 8 bytes -9.2 quintillion to 9.2 quintillion

Quick Example

CREATE TABLE products (
    id          INT AUTO_INCREMENT PRIMARY KEY,   -- numeric
    name        VARCHAR(100) NOT NULL,            -- string
    price       DECIMAL(10, 2),                   -- exact money value
    category    ENUM('Electronics','Books','Food'), -- one from a list
    description TEXT,                             -- large text
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- date & time
    launch_date DATE                              -- date only
);
Interviewer tip: Mention the three categories (Numeric, String, Date/Time) first, then give 1–2 examples of each. Bonus points for noting DECIMAL over FLOAT for currency.