[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In MySQL, databases correspond to directories within the data directory. Tables within a database correspond to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system determines the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. One notable exception is Mac OS X, for which the default filesystem type (HFS+) is not case sensitive. However Mac OS X also supports UFS volumes, which are case sensitive just as on any Unix. See section 1.8.4 MySQL Extensions to the SQL-92 Standard.
Note: Although database and table names are not case sensitive on
some platforms, you should not refer to a given database or table using
different
cases within the same query. The following query would not work because it
refers to a table both as my_table
and as MY_TABLE
:
mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1; |
Column names, index names, and column aliases are not case sensitive on any platform.
Table aliases are case sensitive before MySQL 4.1.1. The following
query would not work because it refers to the alias both as a
and
as A
:
mysql> SELECT col_name FROM tbl_name AS a -> WHERE a.col_name = 1 OR A.col_name = 2; |
If you have trouble remembering the lettercase for database and table names, adopt a consistent convention, such as always creating databases and tables using lowercase names.
How table names are stored on disk and used in MySQL is defined by the
lower_case_table_names
variable, which you can set when starting
mysqld
. It can take one of the following values:
Value | Meaning |
0 | Table and database names are case sensitive and are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. This is the default on Unix systems. |
1 | Table and database names are not case sensitive and are stored on disk in lowercase. MySQL will convert all table names to lowercase on storage and lookup. This is the default on Windows and Mac OS X systems. (This option also applies to database names as of MySQL 4.0.2, and to table aliases as of 4.1.1.) |
2 | New in 4.0.18: Table and database names are not case sensitive, but are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. MySQL will convert all table names to lowercase on storage and lookup. Note: This works only on file systems that are not case sensitive! |
If you are using MySQL on only one platform, you don't normally have to
change the lower_case_table_names
variable. However, you may encounter
difficulties if you want to transfer tables between platforms that differ in
filesystem case sensitivity. For example, on Unix, you can have two different
tables named my_table
and MY_TABLE
, but on Windows those names
are considered the same. To avoid data transfer problems stemming from
database or table name lettercase, you have two options:
lower_case_table_names=1
on all systems. The main disadvantage
with this is that when you use SHOW TABLES
or SHOW DATABASES
,
you don't see the names in their original case.
lower_case_table_names=0
on Unix and lower_case_table_names=2
on Windows. This will preserve the case of database and table names.
The disadvantage of this is that you must ensure that you always refer
to your database and table names with the correct case on Windows as things will
not work on Unix if you use the wrong case (as Unix is case sensitive).
Note that if you set lower_case_table_names
to 1 on Unix, you must
first convert your old database and table names to lowercase before restarting
mysqld
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |