MySQL VARCHAR 长度和 UTF-8

2021-11-20 00:00:00 unicode utf-8 mysql varchar

在 MySQL 中,如果我在 UTF-8 表中创建一个新的 VARCHAR(32) 字段,是否意味着我可以在该字段中存储 32 个字节的数据或 32 个字符(多字节)?

In MySQL, if I create a new VARCHAR(32) field in a UTF-8 table does it means I can store 32 bytes of data in that field or 32 chars (multi-byte)?

推荐答案

这个答案出现在我的谷歌搜索结果的顶部,但不正确:

This answer showed up at the top of my google search results but wasn't correct so:

混淆可能是由于所测试的 mysql 版本不同.

The confusion is probably due to different versions of mysql being tested.

  • 第 4 版计算字节数
  • 第 5 版计算字符

http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html

MySQL 以字符为单位解释字符列定义中的长度规范.(在 MySQL 4.1 之前,列长度以字节为单位进行解释.)这适用于 CHAR、VARCHAR 和 TEXT 类型.

MySQL interprets length specifications in character column definitions in character units. (Before MySQL 4.1, column lengths were interpreted in bytes.) This applies to CHAR, VARCHAR, and the TEXT types.

有趣的是(我没有考虑过)varchar 列的最大长度受 utf8 的影响如下:

Interestingly (I hadn't thought about it) the max length of a varchar column is affected by utf8 as follows:

MySQL 5.0.3 及更高版本中 VARCHAR 的有效最大长度受最大行大小(65,535 字节,在所有列之间共享)和使用的字符集的约束.例如,utf8 字符可能需要每个字符最多三个字节,因此可以将使用 utf8 字符集的 VARCHAR 列声明为最多 21,844 个字符.

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. For example, utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters.

相关文章