MySQL VARCHAR Maximum Length Explained: Limits, Storage, utf8mb4 & Best Practices

目次

1. Introduction

When designing a database in MySQL, it is extremely important to accurately understand the maximum length and specifications of the VARCHAR data type. Since it directly affects database storage efficiency and performance, choosing the optimal configuration is essential.

In this article, centered around the theme “MySQL VARCHAR maximum length,” we will provide a comprehensive explanation—from the basic characteristics of the VARCHAR type to its maximum size, storage efficiency details, and practical usage examples. By reading this article, you will learn the following:

  • Basic specifications and use cases of the VARCHAR type
  • Technical details about the maximum length of VARCHAR
  • Best practices for efficient database design

This content is intended for beginner to intermediate database engineers and programmers, so please read through to the end.

2. Basics of the VARCHAR Type

What Is the VARCHAR Type?

The VARCHAR type is a data type in MySQL used to store variable-length string data. Because it is variable-length, the required storage capacity changes depending on the length of the stored string. Thanks to this flexibility, it offers higher storage efficiency than the CHAR type and is widely used in database design.

Difference Between CHAR and VARCHAR

The CHAR type is used to store fixed-length strings. Even if the string data is shorter, spaces are added to meet the specified length. In contrast, the VARCHAR type determines storage usage based on the actual length of the stored string, eliminating unnecessary space usage.

Data TypeCharacteristicsExample Use Cases
CHARFixed length, suitable for short dataZIP code, country code
VARCHARVariable length, suitable for longer stringsName, email address

For example, consider the following SQL:

CREATE TABLE example (
    char_column CHAR(10),
    varchar_column VARCHAR(10)
);

In this case, char_column always consumes storage for 10 characters, whereas varchar_column consumes only the actual data length plus a 1–2 byte length prefix.

Use Cases and Proper Selection

  • CHAR type: Data with fixed or nearly constant length (e.g., country codes or ZIP codes).
  • VARCHAR type: Data with variable length where storage efficiency is important (e.g., usernames or email addresses).

Because of its flexibility and efficiency, VARCHAR is often used as the default string type in general database design.

3. Maximum Length of MySQL VARCHAR

What Is the Maximum Length of VARCHAR?

In MySQL, the maximum length that can be defined for a VARCHAR column depends on database specifications and the character set used. The maximum length can be set within the range of 1 to 65,535 bytes. However, this limit is constrained not only by the actual data length but also by the table structure and character set.

Specific Constraints

  1. Impact of Character Set
  • In MySQL, the number of bytes per character depends on the character set.
  • Examples:
    • utf8 (1 character = up to 3 bytes)
    • utf8mb4 (1 character = up to 4 bytes)

Therefore, when using utf8mb4, the maximum length of a VARCHAR column is limited to 16,383 characters (4 bytes × 16,383 = 65,532 bytes).

  1. Total Row Size Limit
  • In MySQL’s InnoDB storage engine, the maximum data size per row is 65,535 bytes. Since this includes all columns in the table, the maximum length of VARCHAR columns is affected accordingly.

Calculation Example: VARCHAR(255)

Next, consider VARCHAR(255) as a concrete example.

  • If the character set is utf8mb4:
  • 1 character = up to 4 bytes
  • Maximum size of VARCHAR(255) = 255 × 4 bytes = 1,020 bytes + length prefix (2 bytes)
  • Total required storage = 1,022 bytes

Taking this into account, you must carefully calculate data sizes during table design.

SQL Query Example: Setting Maximum Length

The following example creates a VARCHAR column capable of storing up to 16,383 characters using the utf8mb4 character set.

CREATE TABLE example (
    large_text VARCHAR(16383)
) CHARACTER SET utf8mb4;

In this query, the large_text column consumes up to 65,532 bytes depending on the character set.

Practical Considerations

  • Optimize VARCHAR Length: Setting a VARCHAR length unnecessarily large can waste storage and degrade performance. Choosing an appropriate length is crucial.
  • Be Aware of the Character Set: When using utf8mb4, storing emojis and special characters is possible, but it affects storage efficiency.

4. Storage Efficiency and Considerations

How VARCHAR Storage Efficiency Works

VARCHAR is a data type designed to store variable-length strings efficiently. However, its efficiency depends on configuration and design choices, so understanding the following points is important.

  1. Storage Based on Actual Data Length
  • VARCHAR consumes storage based on the actual length of the stored data.
  • Example: If you store “Hello” (5 characters) in VARCHAR(100), the required storage is 5 bytes plus the length prefix (1–2 bytes).
  1. Length Prefix
  • VARCHAR data includes a prefix that indicates its length.
    • If the data length is 255 bytes or less: prefix is 1 byte.
    • If the data length is 256 bytes or more: prefix is 2 bytes.
  • Example: If 200 characters are stored in VARCHAR(255), 200 bytes + 1 byte (prefix) are used.

Relationship with Row Size Limits

In MySQL’s InnoDB storage engine, the maximum row size is limited to 65,535 bytes. If multiple VARCHAR columns exist in a table, their combined size must fit within this limit.

  • Example Consideration:
    The following SQL may violate the row size limit:
CREATE TABLE example (
    column1 VARCHAR(32767),
    column2 VARCHAR(32767)
) CHARACTER SET utf8mb4;
  • With utf8mb4, 1 character may require up to 4 bytes. Therefore: 32767 × 4 bytes (column1) + 32767 × 4 bytes (column2) = 131,068 bytes, which exceeds the limit.
  • Solution: Use the TEXT type or reduce the length of VARCHAR columns as needed.

5. Why VARCHAR(255) Is Commonly Chosen

Why Is VARCHAR(255) Used So Often?

In MySQL database design, VARCHAR(255) is considered a default choice for many developers. The reasons are related to historical background, technical constraints, and compatibility concerns. Below, we explain in detail why VARCHAR(255) is commonly selected.

1. Historical Background

In older versions of MySQL, the maximum length that could be used for an index was limited to 255 bytes. Although this limitation has been relaxed today, many developers have continued following the old convention, which is why the number 255 remains widely used.

2. Relationship to Index Limits

When you create an index on a VARCHAR column, an overly large index size can reduce performance. VARCHAR(255) is a moderate length that typically does not cause indexing issues in many use cases.

  • Example:
    When creating a table with an indexed VARCHAR column:
  CREATE TABLE users (
      username VARCHAR(255),
      PRIMARY KEY(username)
  );

While it depends on the character set, 255 bytes is generally enough to cover many types of string data.

3. Compatibility Considerations

Many other database engines and frameworks also use VARCHAR(255) as a standard setting. This helps preserve compatibility when migrating from MySQL to another database.

  • Example: In CMS platforms such as WordPress, many tables adopt VARCHAR(255). This is to maintain compatibility across a wide variety of server environments and configurations.

4. Practical Flexibility

VARCHAR(255) is long enough to store many types of string data (e.g., names, email addresses, short descriptions).

  • Examples:
  • Usernames: 50–100 characters are common.
  • Email addresses: Up to 320 characters (per the specification), but 255 characters covers almost all real-world cases.

If you set the length too short, you may not be able to support future data expansion. In that sense, 255 provides a reasonable balance.

5. Relationship with utf8mb4

When using the utf8mb4 character set, each character can require up to 4 bytes. Therefore, VARCHAR(255) may require up to 255 × 4 = 1,020 bytes (+ 2 bytes for the length prefix). Even when considering the row size limit (65,535 bytes), this easily fits.

Notes When Choosing VARCHAR(255)

  • Avoid Overprovisioning:
    VARCHAR(255) is convenient, but it is not always the best choice. Selecting an appropriate length based on the characteristics of your data is important.
  • Example: For fixed-length data such as country codes or ZIP codes, using CHAR is more efficient.
  • Consider the Entire Database Design:
    If you set every column in a table to VARCHAR(255), storage efficiency can drop and you may risk exceeding the row size limit.

6. Practical Examples and Best Practices

Real-World Examples: Configuring VARCHAR Columns

VARCHAR is a highly flexible data type, but in real-world usage you need to keep several considerations and best practices in mind. Here, we explain concrete examples and tips for using it efficiently.

1. Design Based on Use Cases

For Short Strings

When storing short strings (e.g., usernames or ZIP codes), using VARCHAR appropriately can improve storage efficiency.

  • Example:
    Designing a table to store usernames:
  CREATE TABLE users (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(50) NOT NULL
  );
  • VARCHAR(50) is sufficient to cover most usernames.

For Long Strings

VARCHAR can also be useful for longer strings (e.g., comments or reviews). However, when the maximum length is large, you must consider storage constraints.

  • Example:
    Designing a table to store reviews:
  CREATE TABLE reviews (
      id INT AUTO_INCREMENT PRIMARY KEY,
      review_text VARCHAR(1000)
  );
  • Since overly long data may be truncated, set the length according to your data requirements.

2. Settings with Storage Efficiency in Mind

The length you assign to VARCHAR directly affects storage usage. By choosing a suitable length, you can reduce unnecessary storage consumption.

  • Notes:
  • Do not specify an excessively large length such as VARCHAR(255) unless necessary.
  • Consider using the TEXT type when appropriate.

Using Prefix Indexes

When indexing long strings, using a prefix index can improve efficiency.

  • Example:
  CREATE TABLE articles (
      id INT AUTO_INCREMENT PRIMARY KEY,
      title VARCHAR(500),
      INDEX (title(100))
  );
  • By limiting the indexed length, you can improve storage efficiency and performance.

3. Error Handling

If you attempt to insert data that exceeds the maximum length of a VARCHAR column, MySQL will raise an error or warning depending on your configuration.

  • Error Example:
  INSERT INTO users (username) VALUES ('a'.repeat(100)); -- Error occurs
  • Countermeasures:
  • Perform appropriate data validation on the application side.
  • Enable STRICT mode to maintain data integrity.

4. Best Practices

Optimize Length

  • Analyze the maximum length of the data you plan to store, and set the column length with a small margin.
  • Example: For email addresses, VARCHAR(320) can cover the standard specification.

Choose Between CHAR and VARCHAR

  • Use CHAR for fixed-length data, and limit VARCHAR to variable-length data.

Consider the Overall Table Design

  • If a table contains many VARCHAR columns, be careful not to let the row size grow too large.
  • If needed, split data into separate tables to reduce row size.

Summary

VARCHAR is one of the most flexible string data types in MySQL. By setting appropriate lengths and designing efficient indexes, you can maximize performance and storage efficiency. Use these practical approaches as a reference to achieve optimal database design.

7. FAQ (Frequently Asked Questions)

Q1. What Is the Difference Between VARCHAR and TEXT?

A: Both VARCHAR and TEXT can store string data, but the key differences are as follows.

ItemVARCHARTEXT
StorageStored directly within the tableStored in external storage
Maximum LengthUp to 65,535 bytesUp to 65,535 bytes (for TEXT types in general)
IndexingCan index the entire valueOnly prefix indexing is possible
Use CasesShort string data (e.g., names)Long text data (e.g., article content)

How to Choose:

  • VARCHAR is suitable for short variable-length strings.
  • TEXT is used for very long strings (e.g., blog posts or comments).

Q2. What Happens If You Insert Data Longer Than the VARCHAR Length?

A: MySQL behavior depends on your SQL mode settings.

  1. When STRICT Mode Is Enabled (Recommended)
  • An error occurs and the data is not inserted.
  • Example:
    sql SET sql_mode = 'STRICT_ALL_TABLES'; INSERT INTO users (username) VALUES ('a'.repeat(300)); -- Error occurs
  1. When STRICT Mode Is Disabled
  • Excess data is automatically truncated and a warning message is generated.
  • Because this can affect data integrity, enabling STRICT mode is recommended.

Q3. What Is the Difference Between utf8 and utf8mb4?

A: utf8mb4 is an extended version of utf8 that supports emojis and special Unicode characters.

Itemutf8utf8mb4
Max bytes per character3 bytes4 bytes
Supported charactersBasic Unicode charactersAll Unicode characters (including emojis)

How to Choose:

  • Choose utf8mb4 for applications that use emojis or special characters.
  • Consider utf8 if you prioritize storage efficiency.

Q4. How Do You Set the Optimal Length for VARCHAR?

A: It’s important to set the length based on the characteristics and usage of your data.

  • Short strings: For usernames or ZIP codes, VARCHAR(50) or VARCHAR(10) is typically sufficient.
  • Long strings: For email addresses, use VARCHAR(320); for short descriptions, use VARCHAR(1000).
  • Data analysis: Identify the maximum length in real data and set the column with a small margin.

Q5. What Factors Affect VARCHAR Performance?

A: The following factors affect VARCHAR performance.

  1. Overly long column length:
  • Unnecessarily long columns reduce storage efficiency and can also affect query performance.
  1. Character set:
  • When using utf8mb4, storage usage increases, so be careful if you store many long strings.
  1. Index design:
  • When indexing long VARCHAR columns, you can optimize performance by using prefix indexes.

Q6. What Should You Do If VARCHAR Data Hits Storage Limits?

A: Consider the following options.

  1. Review VARCHAR lengths:
  • If you have set an overly large length, reduce it to a realistic value.
  1. Switch to TEXT:
  • If you need to store very long data, consider changing from VARCHAR to TEXT.
  1. Normalize data:
  • Split large data into separate tables to reduce row size.

Q7. What Should You Consider When Using VARCHAR in Indexes?

A: When using indexes on VARCHAR columns, consider the following:

  • Use prefix indexes:
    For long string data, set a prefix index to improve efficiency.
  CREATE TABLE articles (
      id INT AUTO_INCREMENT PRIMARY KEY,
      title VARCHAR(500),
      INDEX (title(100))
  );
  • Set an appropriate length:
    If the indexed length is too large, query performance may degrade.

Summary

In the FAQ section, we covered common questions developers face and their solutions. By using these as a reference, you can leverage VARCHAR effectively and improve MySQL database design and performance.

8. Summary

How to Use MySQL VARCHAR Effectively

In this article, centered on the theme “MySQL VARCHAR maximum length,” we covered a wide range of topics—from the basics of VARCHAR to maximum size limits, storage efficiency, practical examples, and best practices. Let’s review the key takeaways.

What You Learned in This Article

  1. Basic specifications of VARCHAR
  • A flexible data type for storing variable-length strings with excellent storage efficiency.
  • Understanding the difference from CHAR and choosing appropriately based on use cases is important.
  1. Maximum length of VARCHAR
  • Can be set up to 65,535 bytes depending on the MySQL version and character set.
  • When using utf8mb4, the maximum length is 16,383 characters (4 bytes × number of characters).
  1. Storage efficiency and design considerations
  • It’s important to consider the length prefix and row size limits to design an efficient database.
  • Avoid unnecessary large column lengths and optimize the balance between storage and performance.
  1. Why VARCHAR(255) is commonly chosen
  • Influence of historical conventions and relaxed index limits.
  • High compatibility and practical flexibility.
  • Versatility across many character sets and data patterns.
  1. Practical examples and best practices
  • Includes rich use cases and examples that you can apply immediately after reading.
  • Provides detailed advice useful in real work, such as using prefix indexes.
  1. Resolving common questions in the FAQ
  • Covered differences between VARCHAR and TEXT, indexing considerations, and how to handle values that exceed column length.

Aiming for Efficient Database Design

Using VARCHAR effectively in MySQL is a key foundation of database design. Setting appropriate lengths and designing with storage efficiency in mind directly improves performance and scalability.

  • Understand your data characteristics and set the minimum necessary length.
  • Review the overall table structure and watch out for row size limits.
  • Take advantage of VARCHAR flexibility while selecting the right data types.

Next Steps

By applying what you learned here to real projects, you can achieve more efficient database design. We also recommend deepening your knowledge by reviewing related resources and best practices.

Use this information to help you build efficient, high-performance databases!