欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入淺析MySQL 中 Identifier Case Sensitivity問題

 更新時間:2018年09月25日 08:37:40   作者:瀟湘隱者  
這篇文章主要介紹了MySQL 中 Identifier Case Sensitivity,需要的朋友可以參考下

在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)站的支持!

相關(guān)文章

  • MySQL中時間函數(shù)操作大全

    MySQL中時間函數(shù)操作大全

    在使用SQL語言進行數(shù)據(jù)查詢和數(shù)據(jù)分析中,常常需要借助日期時間函數(shù)來計算相關(guān)的指標或生成日期輔助列,下面這篇文章主要給大家介紹了關(guān)于MySQL中時間函數(shù)操作的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • SQL常用的四個排序函數(shù)梳理

    SQL常用的四個排序函數(shù)梳理

    這篇文章主要介紹了SQL常用的四個排序函數(shù)梳理,四個排序函數(shù)分別是SQL?Server排序中經(jīng)常用到的ROW_NUMBER()、RANK()、DENSE_RANK()、NTILE()、下文簡單分享,需要的小伙伴可以參考一下
    2022-07-07
  • mysql 8.0.19 winx64.zip安裝教程

    mysql 8.0.19 winx64.zip安裝教程

    這篇文章主要為大家詳細介紹了mysql 8.0.19 winx64.zip安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 使用Visual Studio Code連接MySql數(shù)據(jù)庫并進行查詢

    使用Visual Studio Code連接MySql數(shù)據(jù)庫并進行查詢

    這篇文章主要介紹了使用Visual Studio Code連接MySql數(shù)據(jù)庫并進行查詢,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • MySQL中表復制:create table like 與 create table as select

    MySQL中表復制:create table like 與 create table as select

    這篇文章主要介紹了MySQL中表復制:create table like 與 create table as select,需要的朋友可以參考下
    2014-12-12
  • MySQL修改存儲過程的詳細步驟

    MySQL修改存儲過程的詳細步驟

    這篇文章主要給大家介紹了關(guān)于MySQL修改存儲過程的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 數(shù)據(jù)庫查詢哪個對像里面包含什么字段方法語句

    數(shù)據(jù)庫查詢哪個對像里面包含什么字段方法語句

    在本篇文章里小編給大家整理的關(guān)于數(shù)據(jù)庫查詢哪個對像里面包含什么字段方法語句有需要的朋友們可以學習下。
    2019-08-08
  • MySQL MEM_ROOT詳解及實例代碼

    MySQL MEM_ROOT詳解及實例代碼

    mysql中使用MEM_ROOT來做內(nèi)存分配的部分,這篇文章會詳細解說MySQL中使用非常廣泛的MEM_ROOT的結(jié)構(gòu)體,同時省去debug部分的信息,僅分析正常情況下。需要的朋友可以參考一下
    2016-11-11
  • 一文帶你理解MySQL?TCL?事務(wù)控制

    一文帶你理解MySQL?TCL?事務(wù)控制

    本文主要介紹了MySQL?TCL事務(wù)控制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-11-11
  • mysql把主鍵定義為自動增長標識符類型

    mysql把主鍵定義為自動增長標識符類型

    這篇文章主要介紹了mysql中如何把主鍵定義為自動增長標識符類型,下面有個不錯的示例,大家可以參考下
    2014-07-07

最新評論