在linux的环境下,在mysql的配置里会对大小写有一定的要求的。尤其是一些云平台提供的固定环境是配置好这些要求,所以需要对这些有一定的了解。(比如阿里云就会设定lower_case_table_names=1,具体分析在后面)
1、表名大小写
Linux默认情况下区分大小写(另外,MySQL在Windows下数据库名、表名、列名、别名都不区分大小写,即使设置lower_case_table_names=0,在查询时还是不会区分大小写。只是在导入导出时会对大小写有区别。原因是大小写的功能是在操作系统的文件系统对大小对区别的基础上实现的。WINDOWS的文件系统查询时不区分文件大小写。)
(1)变量含义解释
lower_case_file_system:数据库所在的文件系统对文件名大小写敏感度。
ON表示大小写不敏感 OFF表示敏感
lower_case_table_names:表名大小写敏感度
0表示使用Create语句指定的大小写保存文件
1表示大小写敏感,文件系统以小写保存
2表示使用Create语句指定的大小写保存文件,但MySQL会将之转化为小写
(当Linux设置为2时,错误日志显示[Warning]
lower_case_table_names was set to 2, even though your the file system ‘/home/mysql/master_a/data/‘ is case sensitive.
Now setting lower_case_table_names to 0 to avoid future problems.)
(2)变量更新
mysql> show variables like ‘lower%‘;
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
+------------------------+-------+
2 rows in set (0.01 sec)
--修改发现变量为只读变量
mysql> set global lower_case_table_names=1;
ERROR 1238 (HY000): Variable ‘lower_case_table_names‘ is a read only variable
修改my.cnf后重启数据库
lower_case_table_names=1
mysql> show variables like ‘lower%‘;
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+
2 rows in set (0.01 sec)
因为修改了配置lower_case_table_names=1,原来在mysql的show tables里面有表的记录, select操作时会显示查不到表
+--------------------------------+
| Tables_in_testdb |
+--------------------------------+
| TABLE1|
| TABLE2|
但是
mysql> select * from TABLE1;
ERROR 1146 (42S02): Table ‘testdb.TABLE1‘ doesn‘t exist
主要原因是大小写敏感以及文件系统以小写文件名写入,而原来的表名是大写的。
2、表字段数据大小写
表字段数据的校对,bin与cs区分大小写,ci不区分大小写
MySQL对校对约定的命名方式如下:
*_ci:case insensitive collation,不区分大小写
*_cs:case sensitive collation,区分大小写
*_bin: 表示的是binary case sensitive collation,将字符串每个字符串用二进制数据编译存储。 区分大小写,而且可以存二进制的内容
(1)表字段数据区分大小写
指定校对为区分大小写
mysql> create table b(id varchar(10)) default charset=utf8 default collate=utf8_bin;
Query OK, 0 rows affected (0.03 sec)sec)
mysql> insert into b values (‘A‘),(‘a‘),(‘B‘);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from b where id like ‘a‘;
+------+
| id |
+------+
| a |
+------+
1 row in set (0.00 sec)
mysql> select id,count(*) from b group by id;
+------+----------+
| id | count(*) |
+------+----------+
| A | 1 |
| B | 1 |
| a | 1 |
+------+----------+
3 rows in set (0.03 sec)
(2)表字段数据不区分大小写
指定校对为忽略大小写
mysql> create table t (name varchar(10)) default charset=utf8 default collate=utf8_general_ci;
Query OK, 0 rows affected (0.09 sec)
mysql> insert into t values(‘a‘),(‘A‘),(‘B‘),(‘b‘),(‘c‘);
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from t where name like ‘a‘;
+------+
| name |
+------+
| a |
| A |
+------+
2 rows in set (0.01 sec)
mysql> select name,count(*) from t group by name;
+------+----------+
| name | count(*) |
+------+----------+
| a | 2 |
| B | 2 |
| c | 1 |
+------+----------+
3 rows in set (0.04 sec)
不过如果字段类型是binary则肯定会区分大小写
mysql> alter table t change name name varchar(10) binary;
Query OK, 5 rows affected (0.08 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from t where name like ‘A‘;
+------+
| name |
+------+
| A |
+------+
1 row in set (0.00 sec)
mysql> select name,count(*) from t group by name;
+------+----------+
| name | count(*) |
+------+----------+
| A | 1 |
| B | 1 |
| a | 1 |
| b | 1 |
| c | 1 |
+------+----------+
5 rows in set (0.01 sec)