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

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 set
mysql> 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 affected
Records: 2  Duplicates: 0  Warnings: 0
mysql> 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就够了,到现在也没发现问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值