Understanding MySQL VARCHAR vs CHAR: Storage, Performance, and Best Use Cases
This article explains the fundamental differences between MySQL VARCHAR and CHAR types, covering their definitions, length limits, storage overhead, character‑set effects, practical examples, and guidelines for choosing the appropriate column type in database design.
When designing database columns, string types are common; this article examines MySQL VARCHAR and CHAR, their characteristics, differences, and how character sets affect storage.
VARCHAR type
VARCHAR (Variable Character) stores variable‑length strings. Defined as VARCHAR(n) where n is the maximum number of characters. MySQL allows lengths from 1 to 65535, limited by row size and character‑set byte size. For strings ≤255 bytes MySQL stores length in 1 byte; longer strings use 2 bytes.
CHAR type
CHAR stores fixed‑length strings, maximum 255 characters. MySQL pads shorter values with spaces to the defined length. When reading, trailing spaces are trimmed. Fixed length gives slightly better performance but can waste space, especially with multibyte character sets.
Impact of character set
The character set (charset) determines how many bytes each character occupies. latin1 uses 1 byte per character, utf8 uses 1‑3 bytes, and utf8mb4 uses 1‑4 bytes. Because MySQL rows are limited to 65,535 bytes, the chosen charset influences the maximum column length that fits in a row.
Data‑visualization differences
When inserting a string with trailing spaces into VARCHAR and CHAR columns, MySQL stores the spaces for VARCHAR but treats them as padding for CHAR. A SELECT shows identical textual output, but CHAR_LENGTH reveals that VARCHAR retains the spaces while CHAR does not.
CREATE TABLE strings(
id INT PRIMARY KEY AUTO_INCREMENT,
variable VARCHAR(20),
fixed CHAR(20)
);
INSERT INTO strings (variable, fixed) VALUES ("Drifter ", "Drifter ");
SELECT * FROM strings;
SELECT CHAR_LENGTH(variable) AS varchar_len,
CHAR_LENGTH(fixed) AS char_len
FROM strings;Running CHAR_LENGTH shows varchar_len = 12 (including 5 trailing spaces) and char_len = 7 (spaces trimmed).
Storage space examples
With charset utf8mb4, storing "Spider" (6 characters) consumes 25 bytes in VARCHAR(6) (4 bytes per character + 1 byte length) and 24 bytes in CHAR(6). Storing "Eido" (4 characters) uses 17 bytes in VARCHAR(6) (4 bytes per character + 1 byte length) but still 24 bytes in CHAR(6) because the column is fixed‑length.
Key takeaways
VARCHAR is suitable for variable‑length strings and saves space, but adds a length byte.
CHAR provides fixed‑length storage with potential space waste; it can be faster for very short, constant‑size data.
Choose the column type based on expected data variability, performance needs, and the character set’s byte size.
Consider row‑size limits (65 535 bytes) when defining large VARCHAR columns with multibyte charsets.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
