MYSQL大小写及校验集有关的一些知识整理

来源:互联网 发布:手机任意显示软件 编辑:程序博客网 时间:2024/06/05 16:55

1.测试脚本。

在本次文章前,我们先创建一张表,做为学习。脚本如下:

create table about_sensitivity(c1 varchar(10)); insert into about_sensitivity(c1) select 'aaa';insert into about_sensitivity(c1) select 'AAA';

2.参数lower_case_file_system。

在windows系统,该值为ON,在linux为OFF。通过语句show variables like ‘lower_case_file_system’进行查看。参数为ON时,对表对字段,大小写均不敏感。为OFF时,对表大小写敏感,字段不敏感。

lower_case_file_system 对表影响 对字段影响 ON 不敏感 不敏感 OFF 敏感 不敏感

例,为ON时,表和字段未按建表时来书写,语句不会报错

mysql> select * from aBOUT_sensitivity where C1='12312';Empty set

3.mysql校验集collattion。

mysql对字段值的大小写处理是通过校验集collattion来决定的。我们看下about_sensitivity现在的collattion。

mysql> select t.COLLATION_NAME from information_schema.`COLUMNS` t where t.TABLE_NAME='about_sensitivity';+-----------------+| COLLATION_NAME  |+-----------------+| utf8_general_ci |+-----------------+

查询效果

mysql> select * from about_sensitivity where c1='AAA';+-----+| c1  |+-----+| aaa || AAA |+-----+

通过SELECT * FROM information_schema.COLLATIONS可得出所有检验集。_ci是大小写不敏感,_cs和_bin是大小写敏感。但没有发现utf8_general_cs( mysql5.6版本),查了一些资料是说:MySQL doesn’t have case-sensitive Unicode collations, because it’s a difficult problem. Some languages collate lower-case before upper-case, and others collate upper-case before lower-case. SQL Server tries to do case-sensitive Unicode, taking locale into consideration.

4.让查询字符串时大小写敏感的方法。

4.1

mysql> select * from about_sensitivity where c1 ='AAA' collate utf8_bin;+-----+| c1  |+-----+| AAA |+-----+1 row in setmysql> select * from about_sensitivity where binary c1 ='AAA';+-----+| c1  |+-----+| AAA |+-----+1 row in set

4.2修改字段检验集

mysql> ALTER TABLE `about_sensitivity` MODIFY COLUMN `c1`  varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL FIRST ;Query OK, 2 rows affectedRecords: 2  Duplicates: 0  Warnings: 0mysql> select * from about_sensitivity where c1 ='AAA';+-----+| c1  |+-----+| AAA |

5.校验集中的unicodegeneral区别。

主要是在多民族语言比较字符串时有影响,这里不做深究,转了些文字如下:
utf8_unicode_ci和utf8_general_ci对中、英文来说没有实质的差别。
utf8_general_ci校对速度快,但准确度稍差。
utf8_unicode_ci准确度高,但校对速度稍慢。
如果你的应用有德语、法语或者俄语,请一定使用utf8_unicode_ci。一般用utf8_general_ci就够了,到现在也没发现问题