mysql中, varchar(n) n是字符(不区分中文、英文)的个数


mysql中的varchar(n) 中表示n个字符(中文、或者英文),不是字节个数;mysql




参考link:https://ruby-china.org/topics/24920git




1、关于UTF-8

UTF-8 Unicode Transformation Format-8bit。是用以解决国际上字符的一种多字节编码。sql

它对英文使用8位(即一个字节) ,中文使用24位(三个字节)来编码。数据库

UTF-8包含全世界全部国家须要用到的字符,是国际编码,通用性强。浏览器

UTF-8编码的文字能够在各国支持UTF8字符集额的浏览器上显示。 若是是UTF8编码,则在外国人的英文IE也能显示中文,他们无需下载IE的中文语言支持包。ruby

2、关于GBK

GBK 是国家标准GB2312基础上扩容后兼容GB2312的标准。
GBK的文字编码是用双字节来表示的,即不论中、英文字符均使用双字节来表示,为了区分中文,将其最高位都设定成1。
GBK包含所有中文字符,是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBK大。app

3、关于utf8mb4

MySql 5.5 以前,UTF8 编码只支持1-3个字节,只支持BMP这部分的unicode编码区,BMP是从哪到哪? 戳这里 基本就是 0000 ~ FFFF 这一区。测试

从MySQL 5.5 开始,可支持4个字节UTF编码utf8mb4,一个字符最多能有4字节,因此能支持更多的字符集。ui

utf8mb4 is a superset of utf8阿里云

tf8mb4兼容utf8,且比utf8能表示更多的字符。
至于何时用,看你作的什么项目了。。。 在作移动应用时,会遇到IOS用户在文本的区域输入emoji表情,若是不作必定处理,就会致使插入数据库异常。

4、汉字长度与编码有关

MySql 5.0 以上的版本:

一、一个汉字占多少长度与编码有关:

  • UTF-8:一个汉字 = 3个字节,英文是一个字节
  • GBK: 一个汉字 = 2个字节,英文是一个字节

二、varchar(n) 表示n个字符,不管汉字和英文,MySql都能存入 n 个字符,仅实际字节长度有所区别。

三、MySQL检查长度,可用SQL语言

SELECT LENGTH(fieldname) FROM tablename

5、实际测试

一、首先使用utf8 建立 str_test 表。

CREATE TABLE `str_test` ( `name_chn` varchar(20) NOT NULL, `name_en` varchar(20) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8 CREATE TABLE `str_test` ( `name_chn` varchar(20) NOT NULL, `name_en` varchar(20) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8

而后插入值

mysql> insert into str_test values ('我爱Ruby', 'I Love Ruby!'); Query OK, 1 row affected (0.02 sec) mysql> insert into str_test values ('我爱Ruby', 'I Love Ruby!'); Query OK, 1 row affected (0.02 sec)

打开irb

>> "我爱Ruby".size => 6 >> "I Love Ruby!".size => 12 >> >> "我爱Ruby".size => 6 >> "I Love Ruby!".size => 12 >>

从MySQL中查询出来的结果,对比

mysql> select * from str_test; +------------+--------------+ | name_chn | name_en | +------------+--------------+ | 我爱Ruby | I Love Ruby! | +------------+--------------+ 1 row in set (0.02 sec) mysql> select length(name_chn) from str_test; +------------------+ | length(name_chn) | +------------------+ | 10 | +------------------+ 1 row in set (0.01 sec) mysql> select * from str_test; +------------+--------------+ | name_chn | name_en | +------------+--------------+ | 我爱Ruby | I Love Ruby! | +------------+--------------+ 1 row in set (0.02 sec) mysql> select length(name_chn) from str_test; +------------------+ | length(name_chn) | +------------------+ | 10 | +------------------+ 1 row in set (0.01 sec)

3[一个汉字三字节] * 2 + 1[一个英文一字节] * 4 = 10

mysql> select length(name_en) from str_test; +-----------------+ | length(name_en) | +-----------------+ | 12 | +-----------------+ 1 row in set (0.00 sec) mysql> select length(name_en) from str_test; +-----------------+ | length(name_en) | +-----------------+ | 12 | +-----------------+ 1 row in set (0.00 sec)

10[一个英文一字节] * 1 + 2[空格一字节] * whitespace = 12

二、使用 GBK 作测试

建立表

 CREATE TABLE `str_test` ( `name_chn` varchar(20) NOT NULL, `name_en` varchar(20) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk CREATE TABLE `str_test` ( `name_chn` varchar(20) NOT NULL, `name_en` varchar(20) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk

插入数据,而且测试

mysql> insert into str_test values ('我爱Ruby', 'I Love Ruby!'); Query OK, 1 row affected (0.00 sec) mysql> select * from str_test; +------------+--------------+ | name_chn | name_en | +------------+--------------+ | 我爱Ruby | I Love Ruby! | +------------+--------------+ 1 row in set (0.01 sec) mysql> insert into str_test values ('我爱Ruby', 'I Love Ruby!'); Query OK, 1 row affected (0.00 sec) mysql> select * from str_test; +------------+--------------+ | name_chn | name_en | +------------+--------------+ | 我爱Ruby | I Love Ruby! | +------------+--------------+ 1 row in set (0.01 sec)

GBK 中文是两个字节,英文是一个字节。

mysql> select length(name_chn) from str_test; +------------------+ | length(name_chn) | +------------------+ | 8 | +------------------+ 1 row in set (0.00 sec) mysql> select length(name_chn) from str_test; +------------------+ | length(name_chn) | +------------------+ | 8 | +------------------+ 1 row in set (0.00 sec)

2[中文两个字节] * 2 + 4[英文一个字节] * 1 = 8

mysql> select length(name_en) from str_test; +-----------------+ | length(name_en) | +-----------------+ | 12 | +-----------------+ 1 row in set (0.00 sec) mysql> select length(name_en) from str_test; +-----------------+ | length(name_en) | +-----------------+ | 12 | +-----------------+ 1 row in set (0.00 sec)

10[英文一个字节] * 1 + 2[空格一个字节] * whitespace = 12

6、关于varchar 最多能存多少值
  • mysql的记录行长度是有限制的,不是无限长的,这个长度是64K,即65535个字节,对全部的表都是同样的。

  • MySQL对于变长类型的字段会有1-2个字节来保存字符长度。

  • 当字符数小于等于255时,MySQL只用1个字节来记录,由于2的8次方减1只能存到255。

  • 当字符数多余255时,就得用2个字节来存长度了。

  • utf-8状态下的varchar,最大只能到 (65535 - 2) / 3 = 21844 余 1。

  • gbk状态下的varchar, 最大只能到 (65535 - 2) / 2 = 32766 余 1

使用 utf-8 建立

mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(21845) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8 -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(21844) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8 -> -> -> ; Query OK, 0 rows affected (0.06 sec) mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(21845) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8 -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(21844) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=utf8 -> -> -> ; Query OK, 0 rows affected (0.06 sec)

使用gbk建立

当存储长度为 32768 失败~

 mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32768) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1074 (42000): Column length too big for column 'name_chn' (max = 32767); use BLOB or TEXT instead mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32768) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1074 (42000): Column length too big for column 'name_chn' (max = 32767); use BLOB or TEXT instead

当存储长度为 32767 失败~

mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32767) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32767) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

当存储长度为 32766 成功~

mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32766) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; Query OK, 0 rows affected (0.03 sec) mysql> CREATE TABLE `str_test` ( -> `id` tinyint(1) NOT NULL, -> `name_chn` varchar(32766) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; Query OK, 0 rows affected (0.03 sec)

smallint 用两个字节存储,因此

2[smallint] + 32766 * 2[varchar存储长度] + 2[2个字节来存长度] > 65535

因此失败~

mysql> CREATE TABLE `str_test` ( -> `id` smallint(1) NOT NULL, -> `name_chn` varchar(32766) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs mysql> CREATE TABLE `str_test` ( -> `id` smallint(1) NOT NULL, -> `name_chn` varchar(32766) NOT NULL -> ) ENGINE=InnoDB AUTO_INCREMENT=62974 DEFAULT CHARSET=gbk -> ; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

#####7、数值类型所占的字节

类型 所占字节
int 4 字节
smallint 2 字节
tinyint 1 字节
decimal 变长

官方关于decimal 的描述以下

Values for DECIMAL (and NUMERIC) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes.
Storage for the integer and fractional parts of each value are determined separately.
Each multiple of nine digits requires four bytes, and the “leftover” digits require some fraction of four bytes.
The storage required for excess digits is given by the following table.

翻译为中文

使用二进制格式将9个十进制(基于10)数压缩为4个字节来表示DECIMAL列值。
每一个值的整数和分数部分的存储分别肯定。
每一个9位数的倍数须要4个字节,而且“剩余的”位须要4个字节的一部分。
下表给出了超出位数的存储需求:

Leftover Digits Number Of Bytes
0 0
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4

那:decimal(10,2)占几个字节?

一、首先 10 指的是整数与小数部分的总长度, 2指的是小数部分的长度。那么整数部分就只有 10 - 2 = 8 位

二、由于整数与小数的存储市各自独立肯定的,因此他们各自所占用空间的综合就是所占的总空间了。

三、对表可知,整数部分8位占了4个字节,小数部分2位占了1个字节,因此decimal(10,2)总共占了 4 + 1 = 5 个字节。

四、decimal(6,2) 整数部分(6 - 2 = 4) 位占2字节,小数部分2位占1字节,总共占3字节。

8、总结

varchar 字段是将实际内容单独存储在聚簇索引以外,内容开头用1到2个字节表示实际长度(长度超过255时须要2个字节),所以最大长度不能超过65535。

  • UTF-8:一个汉字 = 3个字节,英文是一个字节
  • GBK: 一个汉字 = 2个字节,英文是一个字节

utf-8状态下,汉字最多能够存 21844个字符串, 英文也为 21844个字符串。

gbk状态下,汉字最多能够存 32766个字符串,英文也为 32766个字符串。

参考

mysql utf8mb4与emoji表情
关于GBK、GB23十二、UTF8
阿里云 Rails 项目调整 RDS MySQL 编码为 utf8mb4 的详细步骤
MySQL下varchar类型最大长度是多少