慎用lower_case_table_names來實現表名不區分大小寫

OS:Red Hat Enterprise Linux Server release 6.Xmysql

DB Version: 5.5.22-enterprise-commercial-advanced-logsql

個人目的是想達到以下的效果:app

'select  * from Tn;' 等同於'select * from tn;' ,即對數據表對象(表名爲大寫)操做時,不區分表對象的大小寫。測試

在網上查詢了一下,發現不少文章的都介紹了‘lower_case_table_names’參數,貌似設置此參數能夠達到‘Linux上不區分mysql表名大小寫’的目的,不過在個人測試中是沒有達到個人目的。日誌

關於'lower_case_table_names'參數的官方描述以下:code

If set to 0, table names are stored as specified and comparisons are case sensitive. If set to 1, table names are stored in lowercase on disk and comparisons are not case sensitive. If set to 2, table names are stored as given but compared in lowercase. This option also applies to database names and table aliases. 對象

1.lower_case_table_names=0(defalut value)時,建立T1表ci

mysql> show variables like 'lower_case_table_names' \G

*************************** 1. row ***************************

Variable_name: lower_case_table_names

        Value: 0

1 row in set (0.00 sec)

mysql> create table T1(USER_ID int);

Query OK, 0 rows affected (0.02 sec)

2.修改lower_case_table_names=1it

mysql> show variables like 'lower_case_table_names' \G

*************************** 1. row ***************************

Variable_name: lower_case_table_names

        Value: 1

1 row in set (0.00 sec)

mysql> create table T2(USER_ID int);   --建表時表名爲大寫
Query OK, 0 rows affected (0.03 sec)

3.查看T2的表信息,它的名字已經自動轉變成小寫了。io

CREATE TABLE `t2` (`USER_ID` int(11) DEFAULT NULL) ENGINE=InnoDB;

4.插入數據(分別以大小寫不一樣的表名爲插入表對象)

mysql> insert into t2 values(1);

Query OK, 1 row affected (0.00 sec)

mysql> insert into T2 values(2);

Query OK, 1 row affected (0.00 sec)

5.查看T1表(在lower_case_table_names=0時新建的表)數據,不管表名是大寫仍是小寫,都沒法查詢:

mysql> select * from T1;

ERROR 1146 (42S02): Table 'test.t1' doesn't exist

mysql> select * from t1;

ERROR 1146 (42S02): Table 'test.t1' doesn't exist

mysql>

6.修改lower_case_table_names=2,在重啓後,DB會自動將其還原成默認值0

err log的日誌片斷:

 120706 14:49:06 [Warning] lower_case_table_names was set to 2, even though your the file system '$mysql_data_dir/' is case sensitive.  Now setting lower_case_table_names to 0 to avoid future problems.

結論:很明顯個人目的是否能實現的最終拍板權仍是文件系統。另外,此參數修改成1後,會對先前的表產生影響,因此請謹慎修改此參數。

相關文章
相關標籤/搜索