VARCHAR(20000) 在 MySQL 中有效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1303476/
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
Is a VARCHAR(20000) valid in MySQL?
提问by Zoe
I'm in need of some clarification of the maximum length of a varchar field in MySQL.
我需要澄清 MySQL 中 varchar 字段的最大长度。
I've always thought the max length was 255 (255 what? Characters I've assumed, but this might be a source of my confusion). Taking a look at the tables of a database set up by an external company we're working with, I see a field set-up as varchar(20000), holding chunks of xml longer than 255 characters. Why does this work? Is 20000 a valid value?
我一直认为最大长度是 255(255 是什么?我假设的字符,但这可能是我困惑的根源)。查看我们正在合作的外部公司设置的数据库表,我看到一个字段设置为 varchar(20000),保存了超过 255 个字符的 xml 块。为什么这样做?20000 是有效值吗?
A bit of googling has revealed that in mysql varchar has a limit of 65,535 bytes, and I see varchar(65535) in use, so how does the 255 limit relate to this?
谷歌搜索显示,在 mysql 中 varchar 的限制为 65,535 字节,我看到 varchar(65535) in use,那么 255 限制与此有何关系?
回答by Sampson
Note the MySQL Versions.
注意 MySQL 版本。
http://dev.mysql.com/doc/refman/5.0/en/char.html
http://dev.mysql.com/doc/refman/5.0/en/char.html
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 字节,在所有列之间共享)和使用的字符集的约束。
What version of MySQL do I have?
我有什么版本的 MySQL?
Run the following query...
运行以下查询...
select version() as myVersion
回答by Wouter van Nifterick
It is valid since version 5.0.3, when they changed the maximum length from 255 to 65535.
从 5.0.3 版本开始,当他们将最大长度从 255 更改为 65535 时,它是有效的。
CHAR
has always been 255 max.
CHAR
一直是最大 255。
回答by Havenard
If you are expecting too big inputs, its a better idea to use TEXT, MEDIUMTEXT or LONGTEXT instead.
如果您期望输入太大,最好改用 TEXT、MEDIUMTEXT 或 LONGTEXT。
回答by shantanuo
Though the index can not be built on varchar(2000)
虽然索引不能建立在 varchar(2000) 上
回答by rajukoyilandy
This question already has an answer at What is the MySQL VARCHAR max size?
这个问题已经在 MySQL VARCHAR 最大大小是多少?
Keep in mind that MySQL has a maximum row size limit
请记住,MySQL 有最大行大小限制
The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, not counting BLOB and TEXT types. BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row. Read more about Limits on Table Column Count and Row Size.
MySQL 表的内部表示的最大行大小限制为 65,535 字节,不包括 BLOB 和 TEXT 类型。BLOB 和 TEXT 列仅对行大小限制贡献 9 到 12 个字节,因为它们的内容与行的其余部分分开存储。阅读有关表列数和行大小限制的更多信息。
Maximum size a single column can occupy, is different before and after MySQL 5.0.3
单个列可占用的最大大小,MySQL 5.0.3前后不同
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,535in 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 字节,在所有列之间共享)和使用的字符集的约束。
However, note that the limit is lower if you use a multi-byte character set like utf8 or utf8mb4.
但是,请注意,如果您使用像 utf8 或 utf8mb4 这样的多字节字符集,则限制会更低。
Use TEXT
types inorder to overcome row size limit.
使用TEXT
类型来克服行大小限制。
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.
四种文本类型是 TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。它们对应于四种 BLOB 类型,并且具有相同的最大长度和存储要求。
回答by pavium
The MySQL manual states
MySQL 手册指出
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.
VARCHAR 列中的值是可变长度的字符串。在 MySQL 5.0.3 之前,长度可以指定为 0 到 255 之间的值,在 5.0.3 及更高版本中可以指定为 0 到 65,535。
So it depends on the version you're using.
所以这取决于你使用的版本。