MySQL VARCHAR Max Length: Characters, Bytes, and Examples
MySQL VARCHAR(M) stores a variable-length string of up to M characters, not M bytes. VARCHAR(255) is a common choice, but 255 is not MySQL’s maximum. The declared length can be as high as 65,535 characters in syntax; the actual allowed length is constrained by the character set, the 65,535-byte maximum row size shared by all columns, and storage-engine limits. See the official MySQL string type syntax and row-size limits.
VARCHAR maximum length with utf8mb4
The declared length counts characters, while MySQL’s row-size limit counts bytes. With utf8mb4, a character can require up to four bytes. These examples show the maximum encoded data size for each declaration, before the length prefix and other row overhead:
| Declaration | Maximum data bytes | Length prefix |
|---|---|---|
VARCHAR(100) CHARACTER SET latin1 |
100 | 1 byte |
VARCHAR(100) CHARACTER SET utf8mb4 |
400 | 2 bytes |
VARCHAR(255) CHARACTER SET utf8mb4 |
1,020 | 2 bytes |
VARCHAR(1000) CHARACTER SET utf8mb4 |
4,000 | 2 bytes |
A VARCHAR(255) column using utf8mb4 may therefore require more than 255 bytes. Conversely, a VARCHAR(65535) declaration is not guaranteed to work: even a single column must leave room for its length prefix and row overhead, and other columns share the same limit. For long content, consider TEXT rather than forcing an oversized VARCHAR.
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, not a blanket 255 rule. To inspect characters versus bytes, run this query with an utf8mb4 connection:
SELECT CHAR_LENGTH('café') AS characters,
LENGTH('café') AS bytes;
characters bytes
---------- -----
4 5CHAR_LENGTH() counts characters; LENGTH() counts encoded bytes. If the value has a fixed width, compare CHAR.
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.