MySQL中大小写敏感涉及的属性以及查询如何区分大小写

lower_case_file_system

PropertyValue
System Variablelower_case_file_system
ScopeGlobal
DynamicNo
TypeBoolean

该变量用于设置数据库在检索数据目录时候是否区分大小学,OFF意味着是大小写敏感的;ON意味着大小写不敏感。这是一个只读属性,修改该变量不会对文件系统产生影响。

lower_case_table_names

PropertyValue
Command-Line Format--lower-case-table-names[=#]
System Variablelower_case_table_names
ScopeGlobal
DynamicNo
TypeInteger
Default Value0
Minimum Value0
Maximum Value2

值以及含义:

lower-case-table-names存储大小写比较是否区分大小写
0真实名字存储大小写敏感
1转换小写存储不敏感
2真实名字存储转化为小写比较

操作系统文件是否大小写敏感:

操作系统大小写敏感
Window不敏感
Mac不敏感
Linux敏感

对于Window系统,默认值为0;对于Mac默认值为2,但是对Linux由于不支持2,因此强制设置为0.

对于大小写不敏感的文件系统不应该设置0,这是一个错误的设置,例如Window或者Mac,否则当SQL中表名大小写与真实表名不一致时候报错。

对于MyISAM,使用不同字母大小写会导致索引崩溃。对于InnoDB,在任何平台都应该设置该值为1,强制将名字转化为小写。

 

拓展:

对于MySQL默认的like 以及 = 判等的比较默认是不区分大小写的,如果区分大小写可以使用BINARY关键字,BINARY关键字是将字符串转化为byte,之后根据byte值比较字符串。例如:

join区分大小写:

select *
from tab_a a1
         join tab_a a2 on binary a1.id = a2.id

like 区分大小写:

select *
from tab_a a1
where binary id like 'a';

 

纳尼?!如下查询:

mysql> SELECT  'a' = 'a   ';
+--------------+
| 'a' = 'a   ' |
+--------------+
|            1 |
+--------------+

mysql比较字符串会去空格?!!!是的,MySQL是将尾部空格去掉进行比较。如果想保留空格的话需要使用BINARY关键字。例如:

mysql> SELECT  binary  'a' = 'a   ';
+----------------------+
| binary  'a' = 'a   ' |
+----------------------+
|                    0 |
+----------------------+
1 row in set (0.00 sec)

BINARY会是MySQL字符串比较时候的尾部空格生效!!!!SQL官网原文:

The BINARY operator converts the expression to a binary string (a string that has the binary character set and binary collation). A common use for BINARY is to force a character string comparison to be done byte by byte using numeric byte values rather than character by character. The BINARY operator also causes trailing spaces in comparisons to be significant.

参见:

https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_lower_case_table_names

https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#operator_binary

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值