MySQL VARCHAR长度和UTF-8 [英] MySQL VARCHAR Lengths and UTF-8

查看:120
本文介绍了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)?

推荐答案

此答案显示在我的Google搜索结果的顶部,但不正确,是这样的:

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/zh-CN/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆