MySQL Data Types

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

TypeUseExample
INTWhole numbers (about -2 billion to 2 billion)age INT
BIGINTVery big whole numbersviews BIGINT
TINYINTSmall numbers (-128 to 127). TINYINT(1) is used as true/falseis_active TINYINT(1)
DECIMAL(p, s)Exact decimals, like money. p = total digits, s = digits after the pointprice DECIMAL(10,2)
FLOAT, DOUBLEApproximate decimalsrating FLOAT

String (text) types

TypeUse
CHAR(n)Fixed length text, like a country code CHAR(2)
VARCHAR(n)Text up to n characters. Most used for names and emails
TEXTLong text, like a blog post or description
ENUM(...)One value from a fixed list, like ENUM('male','female')
JSONJSON data

Date and time types

TypeFormatExample
DATEYYYY-MM-DD2025-01-15
TIMEHH:MM:SS10:30:00
DATETIMEYYYY-MM-DD HH:MM:SS2025-01-15 10:30:00
TIMESTAMPLike DATETIME, stored as UTC and can update by itselfcreated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
YEARYYYY2025
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.