一、关于UTF-8
二、关于GBK
三、关于utf8mb4MySql 5.5 之前,UTF8 编码只支持1-3个字节,只支持BMP这部分的unicode编码区,BMP是从哪到哪? 从MySQL 5.5 开始,可支持4个字节UTF编码utf8mb4,一个字符最多能有4字节,所以能支持更多的字符集。
tf8mb4兼容utf8,且比utf8能表示更多的字符。 四、汉字长度与编码有关MySql 5.0 以上的版本: 1、一个汉字占多少长度与编码有关:
2、varchar(n) 表示n个字符,无论汉字和英文, 3、MySQL检查长度,可用SQL语言
五、实际测试1、首先使用 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) 打开irb >> '我爱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) 3[一个汉字三字节] * 2 1[一个英文一字节] * 4 = 10 mysql> select length(name_en) from str_test;
-----------------
| length(name_en) |
-----------------
| 12 |
-----------------
1 row in set (0.00 sec) 10[一个英文一字节] * 1 2[空格一字节] * whitespace = 12 2、使用 创建表 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> 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) 10[英文一个字节] * 1 2[空格一个字节] * whitespace = 12 六、关于varchar 最多能存多少值
使用 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) 使用 当存储长度为 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 当存储长度为 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 当存储长度为 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) 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 #####七、数值类型所占的字节
官方关于
翻译为中文
那:decimal(10,2)占几个字节? 1、首先 10 指的是整数与小数部分的总长度, 2指的是小数部分的长度。那么整数部分就只有 10 - 2 = 8 位 2、因为整数与小数的存储市各自独立确定的,所以他们各自所占用空间的综合就是所占的总空间了。 3、对表可知,整数部分8位占了4个字节,小数部分2位占了1个字节,所以decimal(10,2)总共占了 4 1 = 5 个字节。 4、decimal(6,2) 整数部分(6 - 2 = 4) 位占2字节,小数部分2位占1字节,总共占3字节。 八、总结varchar 字段是将实际内容单独存储在聚簇索引之外,内容开头用1到2个字节表示实际长度(长度超过255时需要2个字节),因此最大长度不能超过65535。
在 在 参考mysql utf8mb4与emoji表情 |
|