Working on a simple database.
I'm wondering when should I use both cases?
I know that you use char for characters but what about varchar?
Asked
Active
Viewed 441 times
-1

00100100
- 64
- 4
-
`char` = fixed length. `varchar` = variable length. `varchar` appends a byte, or two, or more at the end of the record (IIRC) that's used to determine how long the record is. For a `varchar(255)` an extra byte is added at (because 1 byte can hold numbers 0 - 255). If your text is 5 chars long, a `varchar` will allocate 6 bytes to hold the data. If store values such as textual representation of a `sha1` hash (binary is better choice, but this is for example's sake) then `char(40)` is suited for that, since `varchar(40)` would require an extra byte. – N.B. Sep 15 '15 at 21:04
1 Answers
1
Generally, you'll want to pick varchar over char in most cases. Varchar is used when you have varying lengths of strings, making it safer, while char is good for similar lengths. Hope that helps

Pensilvester
- 62
- 4
-
-
Outside of space conservation, I am not aware of any benefits of VARCHAR(); though it does have some drawbacks (at least when using the MyISAM storage engine). – Uueerdo Sep 15 '15 at 21:14