慎用lower_case_table_names来实现表名不区分大小写

OS:Red Hat Enterprise Linux Server release 6.X

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

我的目的是想达到如下的效果:

'select  * from Tn;' 等同于'select * from tn;' ,即对数据表对象(表名为大写)操作时,不区分表对象的大小写。

在网上查询了一下,发现很多文章的都介绍了‘lower_case_table_names’参数,貌似设置此参数可以达到‘Linux上不区分mysql表名大小写’的目的,不过在我的测试中是没有达到我的目的。

关于'lower_case_table_names'参数的官方描述如下:

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表

 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=1

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的表信息,它的名字已经自动转变成小写了。

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后,会对先前的表产生影响,所以请谨慎修改此参数。

 

 

你可能感兴趣的:(mysql5.5)