MySQL VARCHAR 长度和 UTF-8

标签 mysql unicode utf-8 varchar

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

最佳答案

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

困惑可能是由于测试的 MySQL 版本不同。

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

这里引用自官方 MySQL 5 documentation :

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 影响如下:

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.

关于MySQL VARCHAR 长度和 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40935532/

相关文章:

php - mysql_fetch_assoc() 期望参数 1 是资源。特定行

mysql - 如何在 SQL 中完成以下操作?

c++ - 字节顺序标记真的是一个有效的标识符吗?

unicode - 使用 unicode 输入显示 "input too long"错误的最佳方法?

带有utf8字符错误的C++ URL解码

php - TCPDF - 特殊字符(例如 : • ) showing up as diamond question mark

html - UTF8 大写显示不正确

php - 从 2 个查询返回同一正文中的 2 个响应

php - 由于新行添加到数据库而出现 undefined variable 问题

linux - 使用 Git 的 Linux 服务器上的字符集