Menu

MySQL VARCHAR: Length Limits and Character Sets

Updated on

MySQL VARCHAR(M) stores a variable-length nonbinary string of up to M characters. Although M can be specified up to 65,535, the effective limit for a column depends on the table’s 65,535-byte row-size limit and the column’s character set. A multibyte character set uses more bytes per character, so the maximum number of characters can be lower. See MySQL’s CHAR and VARCHAR reference and row-size limits.

Storage and trailing spaces

VARCHAR stores the characters plus a 1-byte or 2-byte length prefix. One length byte is used when values require no more than 255 bytes; otherwise, two are used. Unlike CHAR, VARCHAR values are not padded. Trailing spaces are retained when values are stored and retrieved. Comparisons of trailing spaces can still depend on the column’s collation.

Example: store variable-length names

CREATE TABLE users (
    user_id INT PRIMARY KEY,
    display_name VARCHAR(100) NOT NULL,
    email VARCHAR(254) NOT NULL
);

INSERT INTO users (user_id, display_name, email)
VALUES (1, 'Ada', '[email protected]');

Choose the declared length based on the application’s maximum valid value, remembering that it is expressed in characters while the row-size limit is measured in bytes. If the value has a fixed width, compare CHAR; if it may exceed the row-size constraints for a column, consider an appropriate TEXT type.

In strict SQL mode, inserting a value that exceeds the declared length raises an error. Without strict mode, MySQL can truncate the value and issue a warning. See the official MySQL SQL mode reference.