HomeMYSQLDifference Between CHAR, VARCHAR, and TEXT in MySQL

Difference Between CHAR, VARCHAR, and TEXT in MySQL

When designing a database, one of the most important tasks is choosing the right data type for storing information. In MySQL, string data types such as CHAR, VARCHAR, and TEXT are commonly used. While all three can store character-based data, they have key differences in storage, performance, and usage scenarios. Choosing the wrong one can impact both performance and scalability.

In this blog, we’ll explore each type in detail and then compare them side by side.


1. CHAR in MySQL

The CHAR data type is a fixed-length string type. This means that the storage size is always the same, regardless of the actual string length.

  • Syntax: CHAR(M) where M defines the length (1 to 255).
  • Storage:
    Always uses the full defined length. For example, CHAR(10) will always use 10 bytes (plus potential character set overhead), even if you store only 3 characters. The remaining space is padded with spaces.
  • Performance:
    CHAR is faster for fixed-length data because MySQL doesn’t need to check the actual length; it knows every row takes the same space.
  • Use Cases:
    • Storing fixed-size codes (e.g., country codes, gender: “M”/”F”).
    • Storing hashes of fixed size (e.g., MD5 or SHA1).
    • Best for short and fixed-length fields.

2. VARCHAR in MySQL

The VARCHAR data type is a variable-length string type. Unlike CHAR, it only uses the space needed to store the actual string plus an extra byte (or two bytes if the column length is greater than 255) to record the length.

  • Syntax: VARCHAR(M) where M defines the maximum length (1 to 65535 depending on row size and storage engine).
  • Storage:
    • Uses actual string length + 1 byte (or +2 if M > 255).
    • Example: VARCHAR(10) storing “Hi” will only use 2 + 1 = 3 bytes.
  • Performance:
    Slower than CHAR when rows need frequent updates, because varying lengths can cause row fragmentation and reallocation. However, it is space efficient for varying-length data.
  • Use Cases:
    • Names, email addresses, or fields where length varies.
    • User input where size is unpredictable.
    • Best for flexible string lengths that are not extremely large.

3. TEXT in MySQL

The TEXT data type is used to store large variable-length text. Unlike CHAR and VARCHAR, TEXT is designed for long-form content.

  • Types of TEXT:
    MySQL provides different TEXT types based on maximum length:
    • TINYTEXT (255 characters)
    • TEXT (65,535 characters ≈ 64 KB)
    • MEDIUMTEXT (16,777,215 characters ≈ 16 MB)
    • LONGTEXT (4,294,967,295 characters ≈ 4 GB)
  • Storage:
    Stored separately from the table’s row (only a pointer is stored in the row).
  • Performance:
    Slower compared to CHAR and VARCHAR because of separate storage and more complex indexing. Not suitable for heavy searching unless full-text indexing is used.
  • Use Cases:
    • Articles, blog posts, product descriptions.
    • JSON or XML storage.
    • Large text fields where length varies drastically.

4. Comparison Table: CHAR vs VARCHAR vs TEXT

FeatureCHARVARCHARTEXT
Length TypeFixed-lengthVariable-lengthVariable-length (large)
Max Size255 characters65535 characters (depending on row)Up to 4GB (depends on type)
StorageAlways uses defined lengthActual length + 1/2 bytesStored off-table with pointer
PerformanceFast, predictableEfficient for varying size, but slowerSlowest (large object handling)
PaddingPads with spacesNo paddingNo padding
Best Use CaseFixed-size short stringsVariable short/medium stringsLarge text, articles, documents

5. When to Use Which?

  • Use CHAR when:
    • Data has a fixed length (e.g., CHAR(3) for country codes like “USA”, “IND”).
    • You need faster access at the cost of storage.
  • Use VARCHAR when:
    • Data varies in size (e.g., names, emails, addresses).
    • You want storage efficiency.
  • Use TEXT when:
    • You need to store long-form or large text (e.g., blogs, product descriptions).
    • You don’t need frequent searching/filtering (or you can use full-text indexing).

Conclusion

Choosing the right data type in MySQL is about balancing storage, performance, and use case requirements.

  • CHAR is best for fixed-length, short data.
  • VARCHAR is ideal for variable-length strings.
  • TEXT should be used for large content.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

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