红联Linux门户
Linux帮助

mysql表名大小写敏感

发布时间:2016-04-25 15:35:47来源:linux网站作者:xbuding

在ubuntu下安装的mysql版本是 5.6.25-0ubuntu1

linux下的mysql的表名是大小写敏范的。而在在windows下安装的mysql是大小写不敏感的。

原因是因为lower_case_table_names该属性在linux下默认为0,windows下默认为1

0---大小写敏感

1---大小写不敏感

所以通过修改mysql的my.cnf的配置,既可以达到大小写不敏感.


linux下修改my.cnf(如果是ubuntu的版本,不存在/ect/my.cnf而是存在于,/ect/mysql/my.cnf)来修改该配置:

在[mysqld]组中,添加如下语句:

[mysqld]
lower_case_table_names = 1

然后重启 service mysql restart即可。


由于之前本人建了一张EMP的表(在lower_case_table_names=0的时候):

CREATE TABLE EMP (
ENAME VARCHAR(10),
HIREDATE DATE,
SAL DECIMAL(10,2)
);

是大写的表名。在修改配置之后,大小写不敏感了。


使用如下sql语句均报错:

DESC emp;
DESC EMP;

均报同样的错误:

[Err] 1146 - Table 'test.emp' doesn't exist

按正常来说,如果DESC EMP;应该报:

[Err] 1146 - Table 'test.EMP' doesn't exist

才对的,但是,两者却是报同样的错误。


然后查看文档:

Value   Meaning 
0   Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE
statement. Name comparisons are case sensitive. You should not set this variable to 0 if you are running MySQL on a
system that has case-insensitive file names (such as Windows or Mac OS X). If you force this variable to 0 with
--lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases,
index corruption may result. 
1   Table names are stored in lowercase on disk and name comparisons are not case sensitive. MySQL converts all table
names to lowercase on storage and lookup. This behavior also applies to database names and table aliases. 
2   Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE
statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. This works only
on file systems that are not case sensitive! InnoDB table names are stored in lowercase, as forlower_case_table_names=1.


可以看出

0是严格按找字母大小写查找

2是通过把大写转换成小写再进行查询(前提是大小写不敏感)。所有在已经存在大写的表时,转换成小写时,就找不到原来表,表名是EMP,但是更改配置后,查询表名的时候查找名称为emp的表,故而报[Err] 1146 - Table 'test.emp' doesn't exist,

通过把lower_case_table_names调节为0,然后将表名改回小写,然后,再调节回1.问题即可解决。


本文永久更新地址:http://www.linuxdiyf.com/linux/20082.html