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)whereMdefines 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:
CHARis 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)whereMdefines 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.
- Uses actual string length + 1 byte (or +2 if
- Performance:
Slower thanCHARwhen 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 differentTEXTtypes 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 toCHARandVARCHARbecause 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
| Feature | CHAR | VARCHAR | TEXT |
|---|---|---|---|
| Length Type | Fixed-length | Variable-length | Variable-length (large) |
| Max Size | 255 characters | 65535 characters (depending on row) | Up to 4GB (depends on type) |
| Storage | Always uses defined length | Actual length + 1/2 bytes | Stored off-table with pointer |
| Performance | Fast, predictable | Efficient for varying size, but slower | Slowest (large object handling) |
| Padding | Pads with spaces | No padding | No padding |
| Best Use Case | Fixed-size short strings | Variable short/medium strings | Large 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.
- Data has a fixed length (e.g.,
- 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.
CHARis best for fixed-length, short data.VARCHARis ideal for variable-length strings.TEXTshould be used for large content.
