If I have a VARCHAR of 200 characters and that I put a string of 100 characters, will it use 200 bytes or it will just use the actual size of the string?
-
Duplicate: stackoverflow.com/questions/1962310/…user529649– user5296492011-08-19 15:45:18 +00:00Commented Aug 19, 2011 at 15:45
-
A detail blog: sforsuresh.in/mysql_varchar_max_lengthSuresh Kamrushi– Suresh Kamrushi2016-01-20 07:31:18 +00:00Commented Jan 20, 2016 at 7:31
-
Does this answer your question? Importance of varchar length in MySQL tablePaul Roub– Paul Roub2021-06-16 19:39:08 +00:00Commented Jun 16, 2021 at 19:39
3 Answers
100 characters.
This is the var (variable) in varchar: you only store what you enter (and an extra 2 bytes to store length upto 65535)
If it was char(200) then you'd always store 200 characters, padded with 100 spaces
See the docs: "The CHAR and VARCHAR Types"
4 Comments
varchar(200) field will take 101 bytes. Storing a string of 100 characters in a varchar(256) field will take 102 bytes. This is why you see varchar(255) so frequently; 255 characters is the longest string you can store in MySQL's varchar type with only one byte of overhead. Anything larger requires two bytes of overhead.varchar(N) N is the number of characters, so varchar(255) charset utf8mb4 would actually use up to 1021 bytes. I'm not sure if it will always use the full number of bytes or what; I guess it depends how it's packed.VARCHAR means that it's a variable-length character, so it's only going to take as much space as is necessary. But if you knew something about the underlying structure, it may make sense to restrict VARCHAR to some maximum amount.
For instance, if you were storing comments from the user, you may limit the comment field to only 4000 characters; if so, it doesn't really make any sense to make the sql table have a field that's larger than VARCHAR(4000).
Comments
Actually, it will takes 101 bytes.