1、 binary和char比较:mysql
相同特性,摘自官方文档:git
Specifying the CHARACTER SET binary attribute for a character data type causes the columnsql
to be created as the corresponding binary data type: CHAR becomes BINARY, VARCHAR becomes VARBINARY , and TEXT becomes BLOB . For the ENUM and SET data types, this does not occur;函数
如下两种表定义是等义的:sqlserver
CREATE TABLE t测试
(ui
c1 VARCHAR(10) CHARACTER SET binary,this
c2 TEXT CHARACTER SET binary,编码
c3 ENUM('a','b','c') CHARACTER SET binaryspa
);
CREATE TABLE t
(
c1 VARBINARY(10),
c2 BLOB,
c3 ENUM('a','b','c') CHARACTER SET binary
);
占用空间比较,测试uuid在不一样字符集下的占用空间,主要是考虑到uuid是否适合业务主键的问题
建立4个表,第一个表是utf8字符集比较规则是utf8_bin
mysql> create table tc1(synid char(36) character set utf8 collate utf8_bin);
Query OK, 0 rows affected (0.07 sec)
mysql> create table tc2(synid char(36) character set utf8 collate utf8_general_ci);
Query OK, 0 rows affected (0.08 sec)
mysql> create table tc3(synid char(36) character set binary);
Query OK, 0 rows affected (0.06 sec)
mysql> create table tc4(synid binary(36));
Query OK, 0 rows affected (0.11 sec)
插入相同的数据1000条数据,表大小相同:
mysql> SELECT table_name,SUM(data_length) AS data_length
-> FROM information_schema.TABLES WHERE
-> table_name IN ('tc1','tc2','tc3','tc4')
-> GROUP BY table_name;
+------------+----------------+
| table_name | data_length |
+------------+----------------+
| tc1 | 1589248 |
| tc2 | 1589248 |
| tc3 | 1589248 |
| tc4 | 1589248 |
+------------+----------------+
4 rows in set (0.00 sec)
对于相同的缘由主要有两方面:
当时测试以上问题主要是为了讨论uuid适不适合作业务主键,如下是个人一些总结:
2、 char和varchar
注意:须要注意的是,varchar显示是不截断尾部空格的,但在比较的时候忽略空格的,此外varchar存储时会去掉尾部空格,若是该字段被定义成惟一建或主键,去除结尾空格后相同的字符串会违反惟一性约束
3、 binary和vbinary
注意:一样,vbinary在须要注意在存储时去掉尾部\0,若是该字段被定义成惟一建或主键,去除结尾\0后相同的字符串会违反惟一性约束