MySQL VARCHAR 大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1592702/
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 limit
提问by MySQL DBA
If I have a column in table with field of type VARCHAR(15)
and if I try to insert data of length 16, MySQL gives an error stating
如果我在表中有一个字段类型为字段的列,VARCHAR(15)
并且如果我尝试插入长度为 16 的数据,MySQL 会给出一个错误说明
Data too long for column 'testname' at row 1
Does anyone know why VARCHAR fields in MySQL take fixed length? Also how many bytes does a VARCHAR field take per record based on the size given?
有谁知道为什么 MySQL 中的 VARCHAR 字段采用固定长度?此外,根据给定的大小,VARCHAR 字段每条记录占用多少字节?
采纳答案by meder omuraliev
If you set a column to be varchar(15)
the maximum bytes allowed is 15. Thus you can't pass it more than 15 characters without modifying the column to support more than 15. If you store a 4 character string it should only use around 4 bytes out of a possible 15, whereas if you used char(15)
it would have filled in the other 11 with empty bytes.
如果将列设置为varchar(15)
允许的最大字节数为 15。因此,如果不修改列以支持超过 15 个字符,则不能传递超过 15 个字符。如果存储 4 个字符的字符串,则它应该只使用大约 4 个字节一个可能的 15 个,而如果你使用char(15)
它,它会用空字节填充其他 11 个。
http://dev.mysql.com/doc/refman/5.0/en/char.html
http://dev.mysql.com/doc/refman/5.0/en/char.html
( My byte calculation was probably off since it's always -1/+1 or something like that ).
(我的字节计算可能关闭,因为它总是 -1/+1 或类似的东西)。
回答by mlambie
From the MySQL 5.0 Manual:
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
VARCHAR 列中的值是可变长度的字符串。在 MySQL 5.0.3 之前,长度可以指定为 0 到 255 之间的值,在 5.0.3 及更高版本中可以指定为 0 到 65,535。MySQL 5.0.3 及更高版本中 VARCHAR 的有效最大长度受最大行大小(65,535 字节,在所有列之间共享)和使用的字符集的约束。
I only use VARCHAR when I'm certainthat the data the column needs to hold will never exceed a certain length, and even then I'm cautious. If I'm storing a text string I tend to use one of the TEXT types.
我只在我确定列需要保存的数据永远不会超过特定长度时才使用 VARCHAR ,即使那样我也很谨慎。如果我要存储一个文本字符串,我倾向于使用其中一种 TEXT 类型。
Check out the MySQL Storage Requirementsfor more information on how the bytes are used.
查看MySQL 存储要求以获取有关如何使用字节的更多信息。
回答by martin clayton
Small extra local note. The number of bytes used will depend on the encoding scheme in use. 1 byte per character in latin1 encoding, but up to 3 in UTF8. See link in mlambie's answer for details.
小的额外本地注释。使用的字节数取决于使用的编码方案。在 latin1 编码中每个字符 1 个字节,但在 UTF8 中最多 3 个。有关详细信息,请参阅 mlambie 答案中的链接。
回答by James Black
If you look here it should tell you everything about varchar you want to know: http://dev.mysql.com/doc/refman/5.0/en/char.html
如果你看这里,它应该告诉你你想知道的关于 varchar 的一切:http: //dev.mysql.com/doc/refman/5.0/en/char.html
Basically, depending on the length you chose it will use 1 or two bytes to track the length of the current string in that column, so it will store the number of bytes for the data you put in, plus one or two bytes.
基本上,根据您选择的长度,它将使用 1 或 2 个字节来跟踪该列中当前字符串的长度,因此它将存储您输入的数据的字节数,加上一或两个字节。
So, if you put in 'abc' then it will be 4 or 5 bytes used for that column in that row.
因此,如果您输入 'abc',则该行中的该列将使用 4 或 5 个字节。
If you used char(15)
then even 'abc' would take up 15 bytes, as the data is the right-padded to use up the full 15 bytes.
如果您使用,char(15)
那么即使是 'abc' 也将占用 15 个字节,因为数据被右填充以使用完整的 15 个字节。