MySQL VARCHAR size?

MysqlVarchar

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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"

Solution 2 - Mysql

> 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).

http://dev.mysql.com/doc/refman/5.0/en/char.html

Solution 3 - Mysql

Actually, it will takes 101 bytes.

MySQL Reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSBSTPView Question on Stackoverflow
Solution 1 - MysqlgbnView Answer on Stackoverflow
Solution 2 - MysqldwmccView Answer on Stackoverflow
Solution 3 - MysqlpanoetView Answer on Stackoverflow