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 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01