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

深入淺析MySQL 中 Identifier Case Sensitivity問(wèn)題

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

在MySQL當(dāng)中,有可能遇到表名大小寫(xiě)敏感的問(wèn)題。其實(shí)這個(gè)跟平臺(tái)(操作系統(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ù)庫(kù)對(duì)應(yīng)于數(shù)據(jù)目錄中的目錄。數(shù)據(jù)庫(kù)中的每個(gè)表對(duì)應(yīng)于數(shù)據(jù)庫(kù)目錄中至少一個(gè)文件 (可能更多, 具體取決于存儲(chǔ)引擎)。觸發(fā)器也對(duì)應(yīng)于文件。因此, 底層操作系統(tǒng)的區(qū)分大小寫(xiě)在數(shù)據(jù)庫(kù)、表和觸發(fā)器名稱(chēng)的大小寫(xiě)敏感度方面起著重要作用。這意味著這些名稱(chēng)在 Windows 中不區(qū)分大小寫(xiě), 但在大多數(shù)類(lèi)型的 Unix 中都是區(qū)分大小寫(xiě)的。一個(gè)顯著的例外是 macOS, 它是基于 Unix 的, 但使用的是不區(qū)分大小寫(xiě)的默認(rèn)文件系統(tǒng)類(lèi)型 (HFS+)。但是, macOS 還支持 UFS 卷, 它們與任何 Unix 一樣都是區(qū)分大小寫(xiě)的。參見(jiàn)1.8.1 節(jié), “MySQL Extensions to Standard SQL“。lower_case_table_names 系統(tǒng)變量還影響服務(wù)器處理標(biāo)識(shí)符大小寫(xiě)靈敏度的方式, 如本節(jié)后面所述。

 Linux系統(tǒng):

數(shù)據(jù)庫(kù)名與表名是嚴(yán)格區(qū)分大小寫(xiě)的;
表的別名是嚴(yán)格區(qū)分大小寫(xiě)的;
列名與列的別名在所有的情況下均是忽略大小寫(xiě)的;
變量名也是嚴(yán)格區(qū)分大小寫(xiě)的;

Windows系統(tǒng):

都不區(qū)分大小寫(xiě)
Mac OS下(非UFS卷):
都不區(qū)分大小寫(xiě)

注意事項(xiàng):列名、索引、存儲(chǔ)過(guò)程、事件名稱(chēng)在任何平臺(tái)上都不區(qū)分大小寫(xiě),列別名也不區(qū)分大小寫(xiě)。

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

下面在測(cè)試環(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ū)分大小寫(xiě),0表示區(qū)分大小寫(xiě)),重啟MySQL服務(wù)后,進(jìn)行如下測(cè)試:

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>

可以看到此時(shí)不管是test、TEST抑或Test,都是訪問(wèn)的test,此時(shí)不能訪問(wèn)”TEST”表了,系統(tǒng)變量lower_case_table_names是只讀變量,也無(wú)法在當(dāng)前會(huì)話修改,這種設(shè)置下,如果存在相同的表名的話,使用mysqldump備份數(shù)據(jù)庫(kù)時(shí)會(huì)遇到下面錯(cuò)誤:

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ī)則就非常重要,可以避免這樣的問(wèn)題出現(xiàn)。另外系統(tǒng)變量lower_case_table_names有三個(gè)值:分別是0、1、2.

1. 設(shè)置成0:表名按你寫(xiě)的SQL大小寫(xiě)存儲(chǔ),大寫(xiě)就大寫(xiě)小寫(xiě)就小寫(xiě),比較時(shí)大小寫(xiě)敏感。

2. 設(shè)置成1:表名轉(zhuǎn)小寫(xiě)后存儲(chǔ)到硬盤(pán),比較時(shí)大小寫(xiě)不敏感。 

3. 設(shè)置成2:表名按你寫(xiě)的SQL大小寫(xiě)存儲(chǔ),大寫(xiě)就大寫(xiě)小寫(xiě)就小寫(xiě),比較時(shí)統(tǒng)一轉(zhuǎn)小寫(xiě)比較。

 

關(guān)于數(shù)據(jù)庫(kù)名大小寫(xiě)敏感,會(huì)遇到下面問(wèn)題:

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ù)庫(kù)了。

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èn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

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

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

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

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

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

    mysql 8.0.19 winx64.zip安裝教程

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

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

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

    MySQL中表復(fù)制:create table like 與 create table as select

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

    MySQL修改存儲(chǔ)過(guò)程的詳細(xì)步驟

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

    數(shù)據(jù)庫(kù)查詢哪個(gè)對(duì)像里面包含什么字段方法語(yǔ)句

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

    MySQL MEM_ROOT詳解及實(shí)例代碼

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

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

    本文主要介紹了MySQL?TCL事務(wù)控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • mysql把主鍵定義為自動(dòng)增長(zhǎng)標(biāo)識(shí)符類(lèi)型

    mysql把主鍵定義為自動(dòng)增長(zhǎng)標(biāo)識(shí)符類(lèi)型

    這篇文章主要介紹了mysql中如何把主鍵定義為自動(dòng)增長(zhǎng)標(biāo)識(shí)符類(lèi)型,下面有個(gè)不錯(cuò)的示例,大家可以參考下
    2014-07-07

最新評(píng)論