CHAR(n) is a fixed-length string and VARCHAR(n) is a variable-length string. In both, n counts characters, not bytes.
CHARpads values with spaces to the full length when stored, and MySQL strips trailing spaces on read. The length can be 0 to 255. It is a little faster for fixed-size values such as country codes, but wastes space on shorter ones.VARCHARstores only the characters used, plus a length prefix, so it is more storage efficient.
You can declare a VARCHAR length up to 65,535, but the real limit is lower. All columns share a 65,535-byte row size limit, and the character set decides how many bytes each character takes. The prefix is 1 byte when values need at most 255 bytes, and 2 bytes otherwise. With the default utf8mb4, a character can take up to 4 bytes, so a VARCHAR(255) value can need 1,020 bytes and a 2-byte prefix.
-- CHAR(10) always holds 10 characters, padded with spaces
name CHAR(10)
-- VARCHAR(255) stores only the characters used, plus a length prefix
name VARCHAR(255)
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓