在MySQL當中,有可能遇到表名大小寫敏感的問題。其實這個跟平臺(操作系統(tǒng))有關(guān),也跟系統(tǒng)變量lower_case_table_names有關(guān)系。下面總結(jié)一下,有興趣可以查看官方文檔“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 中, 數(shù)據(jù)庫對應(yīng)于數(shù)據(jù)目錄中的目錄。數(shù)據(jù)庫中的每個表對應(yīng)于數(shù)據(jù)庫目錄中至少一個文件 (可能更多, 具體取決于存儲引擎)。觸發(fā)器也對應(yīng)于文件。因此, 底層操作系統(tǒng)的區(qū)分大小寫在數(shù)據(jù)庫、表和觸發(fā)器名稱的大小寫敏感度方面起著重要作用。這意味著這些名稱在 Windows 中不區(qū)分大小寫, 但在大多數(shù)類型的 Unix 中都是區(qū)分大小寫的。一個顯著的例外是 macOS, 它是基于 Unix 的, 但使用的是不區(qū)分大小寫的默認文件系統(tǒng)類型 (HFS+)。但是, macOS 還支持 UFS 卷, 它們與任何 Unix 一樣都是區(qū)分大小寫的。參見1.8.1 節(jié), “MySQL Extensions to Standard SQL“。lower_case_table_names 系統(tǒng)變量還影響服務(wù)器處理標識符大小寫靈敏度的方式, 如本節(jié)后面所述。
Linux系統(tǒng):
數(shù)據(jù)庫名與表名是嚴格區(qū)分大小寫的;
表的別名是嚴格區(qū)分大小寫的;
列名與列的別名在所有的情況下均是忽略大小寫的;
變量名也是嚴格區(qū)分大小寫的;
Windows系統(tǒng):
都不區(qū)分大小寫
Mac OS下(非UFS卷):
都不區(qū)分大小寫
注意事項:列名、索引、存儲過程、事件名稱在任何平臺上都不區(qū)分大小寫,列別名也不區(qū)分大小寫。
Notice:Column, index, stored routine, and event names are not case sensitive on any platform, nor are column aliases.
下面在測試環(huán)境為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中設(shè)置lower_case_table_names=1后(1表示不區(qū)分大小寫,0表示區(qū)分大小寫),重啟MySQL服務(wù)后,進行如下測試:
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”表了,系統(tǒng)變量lower_case_table_names是只讀變量,也無法在當前會話修改,這種設(shè)置下,如果存在相同的表名的話,使用mysqldump備份數(shù)據(jù)庫時會遇到下面錯誤:
mysqldump: Got error: 1066: Not unique table/alias: ‘test' when using LOCK TABLES
遇到這種情況就比較麻煩了,必須在配置文件my.cnf中設(shè)置變量lower_case_table_names=0,重啟MySQL服務(wù),所以提前規(guī)劃,使用統(tǒng)一的命名規(guī)則就非常重要,可以避免這樣的問題出現(xiàn)。另外系統(tǒng)變量lower_case_table_names有三個值:分別是0、1、2.
1. 設(shè)置成0:表名按你寫的SQL大小寫存儲,大寫就大寫小寫就小寫,比較時大小寫敏感。
2. 設(shè)置成1:表名轉(zhuǎn)小寫后存儲到硬盤,比較時大小寫不敏感。
3. 設(shè)置成2:表名按你寫的SQL大小寫存儲,大寫就大寫小寫就小寫,比較時統(tǒng)一轉(zhuǎn)小寫比較。
關(guān)于數(shù)據(jù)庫名大小寫敏感,會遇到下面問題:
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中設(shè)置變量lower_case_table_names=0,重啟MySQL服務(wù),然后就可以drop 掉數(shù)據(jù)庫了。
2: ERROR 1049 (42000): Unknown database ‘xxx'
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0.01 sec)
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MyDB |
| mysql |
| performance_schema |
| tmonitor |
| xiangrun |
+--------------------+
6 rows in set (0.01 sec)
mysql> use MyDB;
ERROR 1049 (42000): Unknown database 'mydb'
mysql>
參考資料:
https://dev.mysql.com/doc/refman/5.7/en/identifier-case-sensitivity.html
總結(jié)
以上所述是小編給大家介紹的MySQL 中 Identifier Case Sensitivity問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- MySQL case when使用方法實例解析
- mysql存儲過程之case語句用法實例詳解
- MySQL排序中使用CASE WHEN的方法示例
- mysql case when group by 實例詳解
- 詳解Mysql case then使用
- mysql update case 更新字段值不固定的操作