暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL表名大小写敏感测试

原创 何权林 2020-04-07
1642

环境:(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’;

最后修改时间:2020-04-18 10:34:20
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论