show variables like 'lower_case_table_names';
However, this same statement is permitted on Windows. To avoid problems caused by such differences, it is best to adopt a consistent convention, such as always creating and referring to databases and tables using lowercase names. This convention is recommended for maximum portability and ease of use.
How table and database names are stored on disk and used in MySQL is affected by the lower_case_table_names
system variable, which you can set when startingmysqld. lower_case_table_names
can take the values shown in the following table. This variable does not affect case sensitivity of trigger identifiers. On Unix, the default value of lower_case_table_names
is 0. On Windows, the default value is 1. On macOS, the default value is 2.
Value | Meaning |
| Table and database names are stored on disk using the lettercase specified in the |
| 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. |
| Table and database names are stored on disk using the lettercase specified in the |
If you are using MySQL on only one platform, you do not normally have to change the lower_case_table_names
variable from its default value. However, you may encounter difficulties if you want to transfer tables between platforms that differ in file system case sensitivity. For example, on Unix, you can have two different tables named my_table
and MY_TABLE
, but on Windows these two names are considered identical. To avoid data transfer problems arising from lettercase of database or table names, you have two options:
- Use
lower_case_table_names=1
on all systems. The main disadvantage with this is that when you use SHOW TABLES
or SHOW DATABASES
, you do not see the names in their original lettercase. - Use
lower_case_table_names=0
on Unix and lower_case_table_names=2
on Windows. This preserves the lettercase of database and table names. The disadvantage of this is that you must ensure that your statements always refer to your database and table names with the correct lettercase on Windows. If you transfer your statements to Unix, where lettercase is significant, they do not work if the lettercase is incorrect.
Exception: If you are using InnoDB
tables and you are trying to avoid these data transfer problems, you should set lower_case_table_names
to 1 on all platforms to force names to be converted to lowercase.
变更lower_case_table_names 操作
If you plan to set the lower_case_table_names
system variable to 1 on Unix, you must first convert your old database and table names to lowercase before stoppingmysqld and restarting it with the new variable setting. To do this for an individual table, use RENAME TABLE
:
RENAME TABLE T1 TO t1;
To convert one or more entire databases, dump them before setting lower_case_table_names
, then drop the databases, and reload them after settinglower_case_table_names
:
- Use mysqldump to dump each database:
mysqldump --databases db1 > db1.sql mysqldump --databases db2 > db2.sql ...
Do this for each database that must be recreated. - Use
DROP DATABASE
to drop each database. - Stop the server, set
lower_case_table_names
, and restart the server. - Reload the dump file for each database. Because
lower_case_table_names
is set, each database and table name is converted to lowercase as it is re-created:
mysql < db1.sql mysql < db2.sql ...