环境:(DB:5.7.27 OS:CentOS7.6,rpm包安装方式)
一、默认情况下,MySQL是区分大小写的
1、查询默认情况下,大小写是否区分
mysql> use mysql;
Database changed
mysql> select count() from USER;
ERROR 1146 (42S02): Table ‘mysql.USER’ doesn’t exist
mysql> select count() from user;
±---------+
| count(*) |
±---------+
| 4 |
±---------+
1 row in set (0.00 sec)
2、创建TEST03表
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table TEST03 (id int, name varchar(8));
Query OK, 0 rows affected (0.01 sec
mysql> show tables like ‘%TEST%’;
±-------------------------+
| Tables_in_mysql (%TEST%) |
±-------------------------+
| TEST03 |
±-------------------------+
1 row in set (0.00 sec)
二、修改参数,使MySQL不区分大小写
1、关闭数据库,修改配置文件
systemctl stop mysqld
vim /etc/my.cnf
添加如下:
lower_case_table_names=1
2、查询测试
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select count() from USER;
±---------+
| count() |
±---------+
| 4 |
±---------+
1 row in set (0.00 sec)
mysql> select count() from user;
±---------+
| count() |
±---------+
| 4 |
±---------+
1 row in set (0.00 sec)
3、创建TEST01表
mysql> create table TESt01 (id int, name varchar(8));
mysql> show tables like ‘%TEST%’;
±-------------------------+
| Tables_in_mysql (%TEST%) |
±-------------------------+
| test01 |
±-------------------------+
1 row in set (0.00 sec)
三、总结
1、MySQL5.7.27默认区分大小写;
2、可通过修改配置文件修改(需重启数据库);
3、不区分大小写后,创建的表名都为小写。
四、其他
1、where中字段内容(列值)大小写不敏感;(无论是否修改参数)
2、列名大小写不敏感。(无论是否修改参数)
3、Windows系统下都不区分大小写。
五、实现字段内容的大小写敏感
1、创建表的时候指定collate为utf8_bin,就可以实现大小写敏感,如果建表时未指定,则可修改字段的校对规则,也可以实现大小写敏感。
例:
create table xxx( xxx
)character set utf8 collate utf8_bin;
或
alter table test01 modify name varchar(20) collate uft8_bin;
2、创建表时,将字段标记为binary,二进制大小写是敏感的。
例:
name varchar(20) binary;
3、在查询条件的字段前加binary(不建议这么做,因为会使字段索引失效)
例:
select * from test01 where binary(name)=‘ee’;