MySQL VARCHAR 大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7124029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL VARCHAR size?
提问by SBSTP
I'm wondering, 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?
我想知道,如果我有一个 200 个字符的 VARCHAR 并且我放了一个 100 个字符的字符串,它会使用 200 个字节还是只使用字符串的实际大小?
回答by gbn
100 characters.
100 个字符。
This is the var(variable) in varchar
: you only store what you enter (and an extra 2 bytes to store length upto 65535)
这是var(变量)中的varchar
:您只存储您输入的内容(还有额外的 2 个字节来存储长度高达 65535)
If it was char(200)
then you'd always store 200 characters, padded with 100 spaces
如果是,char(200)
那么你总是存储 200 个字符,用 100 个空格填充
See the docs: "The CHAR and VARCHAR Types"
请参阅文档:“CHAR 和 VARCHAR 类型”
回答by dwmcc
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).
VARCHAR 意味着它是一个可变长度的字符,因此它只会占用必要的空间。但是,如果您对底层结构有所了解,那么将 VARCHAR 限制为某个最大数量可能是有意义的。
例如,如果您要存储来自用户的评论,您可以将评论字段限制为仅 4000 个字符;如果是这样,那么使 sql 表具有大于 VARCHAR(4000) 的字段实际上没有任何意义。