MySQL VARCHAR Lengths and UTF-8(MySQL VARCHAR 长度和 UTF-8)
问题描述
在 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.
这篇关于MySQL VARCHAR 长度和 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL VARCHAR 长度和 UTF-8
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
