How Many Characters Can MySQL 5.7 VARCHAR Store? Limits by Charset Explained
This article experimentally determines the maximum number of characters a MySQL 5.7 VARCHAR column can hold under different character sets, explains the underlying row‑size and index‑key restrictions, and provides concrete CREATE TABLE examples for latin1, utf8, and utf8mb4.
Background
MySQL stores VARCHAR values with a 1‑ or 2‑byte length prefix, and the total row size cannot exceed 65,535 bytes. The actual number of characters that fit depends on the character set’s maximum bytes per character and on row‑format overhead such as the NULL‑flag byte.
latin1 Character Set
Attempting to create a table with varchar(65535) under latin1 fails with the error “Row size too large…”. Reducing the column length to 65,532 succeeds, confirming that the 2‑byte length prefix consumes 2 bytes, leaving a practical limit of 65533 characters.
create table tinywan(name varchar(65535)) charset=latin1;Result:
Row size too large create table tinywan(name varchar(65532)) charset=latin1;Result:
Query OKutf8mb4 Character Set
With the default database charset set to utf8mb4, the maximum row size remains 65,535 bytes, but each character can occupy up to 4 bytes. Therefore the theoretical maximum column length is floor(65535/4) = 16383. However, when the column is defined as a primary key, InnoDB’s index‑key limit of 3072 bytes applies.
create table tinywan3(name varchar(16383) primary key);Result: Specified key was too long; max key length is 3072 bytes Since the index key length is measured in bytes, the maximum safe primary‑key length for utf8mb4 is 3072 / 4 = 768 characters.
create table tinywan3(name varchar(768) primary key) charset=utf8mb4;Result:
Query OKutf8 Character Set
For utf8, each character can use up to 3 bytes. The primary‑key limit becomes 3072 / 3 ≈ 1024 characters, while a non‑indexed VARCHAR can theoretically store up to floor(65535/3) = 21845 characters. Because of the 2‑byte length prefix, the actual maximum without an index is 21844 characters.
create table tinywan32(name varchar(21844) not null) charset=utf8;Result:
Query OKConclusions
In latin1, a VARCHAR column can store up to 65533 characters.
In utf8, a non‑indexed VARCHAR can store up to 21844 characters; as a primary key the safe limit is 1024 characters.
In utf8mb4, a non‑indexed VARCHAR can store up to 16383 characters; as a primary key the safe limit is 768 characters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
