Menu

MySQL VARCHAR: Character Limits and Examples

Understand how MySQL VARCHAR(M) counts characters, how the character set affects storage, and what happens when a value exceeds the limit.

MySQL VARCHAR(M) stores variable-length text with a maximum length of M characters. It differs from CHAR(M), which stores fixed-length text. M counts characters; the number of bytes used depends on the column’s character set.

Syntax

VARCHAR(M)

Specify a maximum length for each VARCHAR column. MySQL does not assign a default length when M is omitted.

The stored value uses a one-byte or two-byte length prefix, followed by the encoded text. The effective maximum length is also limited by the table’s row-size limit and the character set, so a declared character limit does not mean the column always uses that many bytes.

Use CHAR_LENGTH() to count characters and LENGTH() to count bytes in a string:

SELECT
  CHAR_LENGTH('海豚') AS characters,
  LENGTH('海豚') AS bytes;

For non-ASCII text, the byte count can be greater than the character count.

Create a VARCHAR column

CREATE TABLE users (
  user_id INT PRIMARY KEY,
  display_name VARCHAR(40) NOT NULL
) CHARACTER SET utf8mb4;

This column accepts up to 40 characters. utf8mb4 determines how many bytes those characters can occupy, while the table’s row-size limit also constrains the full schema.

Values longer than the column limit

For example, a non-space value with more than two characters does not fit in VARCHAR(2):

CREATE TABLE labels (
  label VARCHAR(2) NOT NULL
);

INSERT INTO labels (label)
VALUES ('ABC');

With strict SQL mode enabled, MySQL raises Error 1406 and rejects the value. Without strict mode, MySQL truncates an overlength value and reports a warning. For diagnosis and safe fixes, see MySQL Error 1406: Data Too Long for Column.

VARCHAR has a special rule for excess trailing spaces: MySQL truncates them and reports a warning regardless of SQL mode. Do not rely on truncation to clean input; validate the value or adjust the column only when the application needs a longer string.

For fixed-length text and its padding behavior, see MySQL CHAR. For detailed storage and trailing-space rules, consult the MySQL 8.4 CHAR and VARCHAR reference.