欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL中Identifier Case Sensitivity

程序员文章站 2022-06-24 13:48:00
在MySQL当中,有可能遇到表名大小写敏感的问题。其实这个跟平台(操作系统)有关,也跟系统变量lower_case_table_names有关系。下面总结一下,有兴趣可以查看官方文档“Identifier Case Sensitivity” In MySQL, databases correspon... ......

在mysql当中,有可能遇到表名大小写敏感的问题。其实这个跟平台(操作系统)有关,也跟系统变量lower_case_table_names有关系。下面总结一下,有兴趣可以查看官方文档identifier case sensitivity

 

in mysql, databases correspond to directories within the data directory. each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). triggers also correspond to files. consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names. this means such names are not case-sensitive in windows, but are case-sensitive in most varieties of unix. one notable exception is macos, which is unix-based but uses a default file system type (hfs+) that is not case-sensitive. however, macos also supports ufs volumes, which are case-sensitive just as on any unix. see section 1.8.1, “mysql extensions to standard sql”. thelower_case_table_names system variable also affects how the server handles identifier case sensitivity, as described later in this section.

 

在 mysql 中, 数据库对应于数据目录中的目录。数据库中的每个表对应于数据库目录中至少一个文件 (可能更多, 具体取决于存储引擎)。触发器也对应于文件。因此, 底层操作系统的区分大小写在数据库、表和触发器名称的大小写敏感度方面起着重要作用。这意味着这些名称在 windows 中不区分大小写, 但在大多数类型的 unix 中都是区分大小写的。一个显著的例外是 macos, 它是基于 unix 的, 但使用的是不区分大小写的默认文件系统类型 (hfs+)。但是, macos 还支持 ufs 卷, 它们与任何 unix 一样都是区分大小写的。参见1.8.1 节, "mysql extensions to standard sql"。lower_case_table_names 系统变量还影响服务器处理标识符大小写灵敏度的方式, 如本节后面所述。

 

 linux系统:

 

    数据库名与表名是严格区分大小写的;

    表的别名是严格区分大小写的;

    列名与列的别名在所有的情况下均是忽略大小写的;

    变量名也是严格区分大小写的;

 

  windows系统

 

    都不区分大小写

    mac os下(非ufs卷):

    都不区分大小写

 

注意事项:列名、索引、存储过程、事件名称在任何平台上都不区分大小写,列别名也不区分大小写。

 

notice:column, index, stored routine, and event names are not case sensitive on any platform, nor are column aliases.

 

下面在测试环境为red hat enterprise linux server release 5.7, mysql 5.6.20:

 

 

mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| variable_name          | value |
+------------------------+-------+
| lower_case_table_names | 0     |
+------------------------+-------+
1 row in set (0.00 sec)
 
mysql> 
 
mysql> use mydb;
database changed
mysql> create table test(id int);
query ok, 0 rows affected (0.07 sec)
 
mysql> create table test(id int);
query ok, 0 rows affected (0.09 sec)
 
mysql> insert into test values(1);
query ok, 1 row affected (0.03 sec)
 
mysql> insert into test value(2);
query ok, 1 row affected (0.00 sec)
 
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
 
mysql> select * from test;
+------+
| id   |
+------+
|    2 |
+------+
1 row in set (0.00 sec)
 
mysql> 

 

在配置文件my.cnf中设置lower_case_table_names=1后(1表示不区分大小写,0表示区分大小写),重启mysql服务后,进行如下测试:

 

mysql> use mydb;
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 * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
 
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
 
mysql> 

 

 

可以看到此时不管是test、test抑或test,都是访问的test,此时不能访问"test"表了,系统变量lower_case_table_names是只读变量,也无法在当前会话修改,这种设置下,如果存在相同的表名的话,使用mysqldump备份数据库时会遇到下面错误:

 

mysqldump: got error: 1066: not unique table/alias: 'test' when using lock tables

 

 

遇到这种情况就比较麻烦了,必须在配置文件my.cnf中设置变量lower_case_table_names=0,重启mysql服务,所以提前规划,使用统一的命名规则就非常重要,可以避免这样的问题出现。另外系统变量lower_case_table_names有三个值:分别是0、1、2. 

 

1. 设置成0:表名按你写的sql大小写存储,大写就大写小写就小写,比较时大小写敏感。 

 

2. 设置成1:表名转小写后存储到硬盘,比较时大小写不敏感。 

 

3. 设置成2:表名按你写的sql大小写存储,大写就大写小写就小写,比较时统一转小写比较。

 

 

value

meaning

0

table and database names are stored on disk using the lettercase specified in the create table or create databasestatement. name comparisons are case sensitive. you should not set this variable to 0 if you are running mysql on a system that has case-insensitive file names (such as windows or macos). if you force this variable to 0 with --lower-case-table-names=0 on a case-insensitive file system and access myisam tablenames using different lettercases, index corruption may result.

1

table names are stored in lowercase on disk and name comparisons are not case-sensitive. mysql converts all table names to lowercase on storage and lookup. this behavior also applies to database names and table aliases.

2

table and database names are stored on disk using the lettercase specified in the create table or create databasestatement, but mysql converts them to lowercase on lookup. name comparisons are not case sensitive. this works only on file systems that are not case-sensitive! innodb table names and view names are stored in lowercase, as forlower_case_table_names=1.

 

关于数据库名大小写敏感,会遇到下面问题:

 

1:error 1010 (hy000): error dropping database (can't rmdir './xxxx', errno: 39)

 

1:error 1010 (hy000): error dropping database (can't rmdir './xxxx', errno: 39)
 
mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| mydb               |
| mydb               |
| mysql              |
| performance_schema |
| tmonitor           |
| xiangrun           |
+--------------------+
7 rows in set (0.01 sec)
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| variable_name          | value |
+------------------------+-------+
| lower_case_table_names | 1     |
+------------------------+-------+
1 row in set (0.00 sec)
 
mysql> drop database mydb;
error 1010 (hy000): error dropping database (can't rmdir './mydb', errno: 39)
mysql> 

 

解决方法:在配置文件my.cnf中设置变量lower_case_table_names=0,重启mysql服务,然后就可以drop 掉数据库了。

 

 

2: error 1049 (42000): unknown database 'xxx'