Skip to content

In MySQL, VARCHAR is a data type used to store variable-length character strings. You would normally use it to store strings (names, addresses, descriptions) where the length might vary.

What is the syntax for MySQL’s VARCHAR data type?

column_name VARCHAR(length)
  • column_name: The name of the column you’re defining.
  • length: The maximum number of characters the VARCHAR column can hold.

Some things to keep in mind:

  • Variable-length storage: VARCHAR stores only the actual length of the string plus one byte to record the length. This makes it more efficient than CHAR for shorter strings.
  • Specifying length: When defining a VARCHAR column, you need to specify the maximum length the column can hold. This can range from 0 to 65,535 characters.
  • Padding: Unlike CHAR, VARCHAR does not pad spaces when storing strings shorter than the defined length.
  • Performance consideration: While VARCHAR is more efficient for variable-length strings, it may incur slight overhead in terms of storage and retrieval compared to fixed-length types like CHAR.
  • Storage efficiency: VARCHAR is a good choice when dealing with variable-length data as it optimizes storage by only using the space necessary to store the actual data.

Example:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100)
);

In this example, we created a table called users with two columns: name and email, both using the VARCHAR data type. The name column can hold up to 50 characters, while the email column can hold up to 100 characters.

Conclusion

MySQL’s VARCHAR data type is ideal for storing variable-length character strings efficiently. By specifying the maximum length required, you can balance storage optimization with data integrity in your database schema.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.