DEV Community

Cover image for πŸŒ™ Late Night Chronicles: #4 Understanding MySQL Data Types
Shreyash Ogale
Shreyash Ogale

Posted on

πŸŒ™ Late Night Chronicles: #4 Understanding MySQL Data Types

In the previous post, we explored SQL basics and how we interact with databases.

But here’s πŸ‘‡

πŸ‘‰ What kind of data can we actually store in MySQL?

That’s where Data Types come in.


πŸ”Ή What are Data Types?

Data types define what kind of value a column can store.

Think of it like rules:

  • Name β†’ text
  • Age β†’ number
  • Date β†’ date

Without data types, data would become messy and inconsistent.


πŸ”Ή Types of Data in MySQL

MySQL mainly supports:

  • πŸ“ Character (String) types
  • πŸ”’ Numeric types
  • πŸ“… Date & Time types
  • πŸ–ΌοΈ Binary / Large Object types

πŸ”Ή 1. Character (String) Data Types

Used to store text values πŸ‘‡

  • CHAR(n)

    • Fixed length
    • Faster retrieval
    • Wastes space
  • VARCHAR(n)

    • Variable length
    • Saves space
    • Slightly slower than CHAR

πŸ“Œ Example use cases:

  • Name
  • Address
  • City

πŸ”Ή 2. TEXT Data Types

Used for large text data

  • TINYTEXT β†’ up to 255 characters
  • TEXT β†’ up to 65,535 characters
  • MEDIUMTEXT β†’ up to 16 MB
  • LONGTEXT β†’ up to 4 GB

πŸ’‘ Stored outside the table (uses pointer internally)

πŸ‘‰ Used for:

  • Comments
  • Reviews
  • Resume
  • Feedback

πŸ”Ή 3. Numeric Data Types

Used for numbers πŸ‘‡

Integer Types

  • TINYINT (1 byte)
  • SMALLINT (2 bytes)
  • MEDIUMINT (3 bytes)
  • INT (4 bytes)
  • BIGINT (8 bytes)

πŸ‘‰ Used for: age, count, IDs


Floating Point Types

  • FLOAT β†’ up to 7 decimal precision
  • DOUBLE β†’ up to 15 decimal precision
  • DECIMAL β†’ exact precision (used for money πŸ’°)

Boolean

  • TRUE β†’ 1
  • FALSE β†’ 0

πŸ”Ή 4. Date and Time Data Types

Used for storing date & time πŸ‘‡

  • DATE β†’ 'YYYY-MM-DD'
  • TIME β†’ 'HH:MM:SS'
  • DATETIME β†’ date + time
  • YEAR β†’ year only

πŸ‘‰ Used for:

  • DOB
  • Login time
  • Transactions

πŸ”Ή 5. Binary & Large Objects (BLOB)

Used for storing multimedia data πŸ‘‡

  • TINYBLOB
  • BLOB
  • MEDIUMBLOB
  • LONGBLOB

πŸ‘‰ Used for:

  • Images
  • Videos
  • Audio
  • Files

πŸ’‘ Stored outside the table (like TEXT)


πŸ”Ή Important Concept

πŸ‘‰ Large data types (TEXT, BLOB) are:

  • Stored outside the table
  • Accessed using pointers
  • Not ideal for searching

πŸ’‘Learning

  • Use CHAR β†’ when length is fixed
  • Use VARCHAR β†’ when length varies
  • Use DECIMAL β†’ for money
  • Use TEXT/BLOB β†’ for large data

πŸ‘‰ In the next post, we’ll start writing real SQL queries (hands-on πŸš€)

Top comments (0)