What is the difference between CHAR and VARCHAR?
What is the difference between CHAR and VARCHAR?
CHAR(n) is fixed length: shorter values are padded with spaces to n. VARCHAR(n) is variable length: it stores only the characters you give, up to n, plus a small length prefix.
CREATE TABLE countries (
iso_code CHAR(2) NOT NULL, -- always exactly 2 characters: 'US', 'VN'
name VARCHAR(100) NOT NULL -- varies a lot
);
CHAR(n) |
VARCHAR(n) |
|
|---|---|---|
| Storage | always n characters |
actual length + 1-2 bytes |
| Padding | pads with spaces | none |
| Best for | truly fixed-length codes (ISO codes, fixed-format ids) | names, emails, titles |
| Update behaviour | row size stays the same | row can grow |
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.