MySQL Tutorials
Estimated reading: 4 minutes 32 views

7️⃣ 🔣 MySQL Data Types – Understand and Choose the Right Column Types

MySQL offers a variety of data types to match real-world data storage needs. Whether you’re storing numbers, dates, text, or geographic coordinates, choosing the right type affects performance, storage efficiency, and data integrity.


🧲 Introduction – Why Data Types Matter in MySQL?

In MySQL, data types define what kind of data each column can hold. The right choice ensures:

  • 🔹 Efficient storage
  • 🔹 Fast queries
  • 🔹 Accurate computation
  • 🔹 Validated input

Using the wrong data type leads to data loss, wasted space, or logic errors. Mastering MySQL’s data types is a key step in designing solid, scalable schemas.


📘 Topics Covered

🔢 Topic📄 Description
🧱 MySQL Numeric TypesStore integers, decimals, floating points, and binary numbers
🔤 MySQL String TypesStore fixed/variable-length text, blobs, and encoded characters
⏱️ MySQL Date & Time TypesStore dates, times, timestamps, and durations
🧩 MySQL JSON & Spatial Data TypesStore structured and geographic/location data
📏 MySQL Data Type LimitsMaximum sizes, byte ranges, and use case boundaries
✅ Best Practices for Data TypesTips for choosing accurate and efficient types
🚀 Real-World Use CasesPractical examples of data type selection in production apps

🔢 1. Numeric Data Types

TypeDescriptionUse Case
TINYINT1 byte integer (–128 to 127)Boolean flags, status
SMALLINT2 byte integerAge, short IDs
INTStandard 4 byte integerIDs, counters
BIGINT8 byte large integerFinancials, timestamps
DECIMALExact decimal valuesCurrency, precision math
FLOATApproximate 4-byte floatScientific use
DOUBLEApproximate 8-byte floatRates, calculations
BIT(n)Bitfield of n bitsFlags, switches

🧪 Example:

CREATE TABLE accounts (
  account_id INT PRIMARY KEY,
  balance DECIMAL(10,2),
  is_active TINYINT(1)
);

🔤 2. String Data Types

TypeDescriptionUse Case
CHAR(n)Fixed-length stringCountry codes, statuses
VARCHAR(n)Variable-length stringNames, emails, titles
TEXTLong string (up to 64KB)Descriptions, comments
TINYTEXTUp to 255 charactersNotes
MEDIUMTEXTUp to 16MBArticles
LONGTEXTUp to 4GBLogs, documents
ENUMOne from defined valuesStatus, category
SETMultiple from defined valuesTags, user roles

🧪 Example:

CREATE TABLE users (
  username VARCHAR(50) NOT NULL,
  bio TEXT,
  status ENUM('active', 'inactive', 'banned') DEFAULT 'active'
);

⏱️ 3. Date & Time Data Types

TypeDescriptionFormat
DATEDate only'YYYY-MM-DD'
DATETIMEDate + Time'YYYY-MM-DD HH:MM:SS'
TIMESTAMPDate + Time with time zone supportAuto UTC conversion
TIMETime only'HH:MM:SS'
YEAR4-digit year'YYYY'

🧪 Example:

CREATE TABLE events (
  event_name VARCHAR(100),
  event_date DATE,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

🧩 4. JSON & Spatial Data Types

🔸 JSON

Used to store structured key-value data.

CREATE TABLE configs (
  id INT,
  settings JSON
);

🔸 Spatial (GIS)

TypeDescription
GEOMETRYBase spatial type
POINTLatitude & longitude
POLYGONRegion areas
LINESTRINGRoutes and paths

📏 Data Type Limits & Sizes

CategoryTypeMax Size / Range
NumericINT±2,147,483,647
NumericBIGINT±9 quintillion
StringVARCHAR(n)Up to 65,535 bytes
TextTEXTUp to 64 KB
Date/TimeDATETIMEYears 1000 to 9999
JSONJSONUp to 1 GB (recommended < 64 MB)

📘 Best Practices for Choosing Data Types

✅ Tip💡 Why It Matters
Use smallest fitting numeric typeSaves storage and boosts performance
Prefer VARCHAR over TEXTIndexing and performance benefits
Use ENUM for small fixed valuesClean data and validation
Use DECIMAL for currencyPrevents float rounding errors
Use TIMESTAMP for audit logsAuto-set/update timestamps
Match charset with expected contentPrevent encoding issues with emojis, etc.

🚀 Real-World Use Cases

Use CaseData Type
UsernameVARCHAR(50)
CurrencyDECIMAL(10,2)
Boolean flagTINYINT(1)
Blog postTEXT, MEDIUMTEXT
Event dateDATE, TIMESTAMP
Country codeCHAR(2)
User preferencesJSON
GPS coordinatesPOINT, GEOMETRY

📌 Summary – Recap & Next Steps

Choosing the correct MySQL data types ensures optimal performance, storage savings, and data integrity. By selecting the right type for each use case—whether it’s for text, time, or numerical values—you prevent issues that often arise from poor database design.

🔍 Key Takeaways:

  • Use INT, DECIMAL, and FLOAT for numbers appropriately
  • Use VARCHAR for strings, TEXT for large content
  • Apply DATE, DATETIME, TIMESTAMP for time tracking
  • Consider JSON and spatial types for modern applications

⚙️ Real-World Relevance:
Right data types power fast, scalable, and accurate systems—from banking and inventory apps to real-time analytics and map-based services.


❓ FAQ – MySQL Data Types

❓ What’s the difference between VARCHAR and TEXT?

VARCHAR(n) is inline-stored, faster, and fully indexable.
⚠️ TEXT is stored off-page and has indexing limitations.


❓ When should I use DECIMAL instead of FLOAT?

✅ Use DECIMAL for currency to avoid rounding issues.
FLOAT/DOUBLE are imprecise and not ideal for financials.


❓ How large can a VARCHAR be?

It can store up to 65,535 bytes, shared across all columns in a row. Encoding (e.g., utf8mb4) affects byte usage.


❓ What’s the difference between DATE, DATETIME, and TIMESTAMP?

  • DATE: Date only
  • DATETIME: Full date & time, no timezone
  • TIMESTAMP: Stores in UTC, converts to local time

❓ Can I index TEXT or BLOB columns?

Partially. You must specify a prefix length:

CREATE INDEX idx_bio ON users (bio(255));

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

7️⃣ 🔣 MySQL Data Types

Or Copy Link

CONTENTS
Scroll to Top