Identifier Case Sensitivity

9.2.2 Identifier Case Sensitivity


In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Triggers also correspond to files. Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names. This means such names are not case sensitive in Windows, but are case sensitive in most varieties of Unix. One notable exception is OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive. However, OS X also supports UFS volumes, which are case sensitive just as on any Unix. See Section 1.7.1, “MySQL Extensions to Standard SQL”. The lower_case_table_namessystem variable also affects how the server handles identifier case sensitivity, as described later in this section. >>对于mysql来说,数据库对应着数据目录下的一个目录。数据库中的一个表对应着数据库目录下的一个或者多个文件(具体是几个文件根据存储引擎而不同)。触发器也对应着一个文件。因此mysql数据库所在操作系统是否大写敏感,对数据库名,表名,触发器名是否大小写敏感,影响很大。这样就意味着在windows操作系统下mysql的数据库名,表名,触发器名都是大小写不敏感的,而在unix上可以是大小写敏感的(windows操作系统大小写不敏感,unix操作系统大小写敏感)。

Note

Although database, table, and trigger names are not case sensitive on some platforms, you should not refer to one of these using different cases within the same statement. The following statement would not work because it refers to a table both as my_table and as MY_TABLE: >>尽管数据库名,表名,触发器名在一些平台上是大小写不敏感的,你也不应该在一个句子中同时使用大写和小写来描述一个对象。下面的句子执行的时候可能会遇到问题,因为在大小写敏感的平台上my_table和MY_TABLE表示的是两个不同的表

mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1;

Column, index, stored routine, and event names are not case sensitive on any platform, nor are column aliases. >>列名,索引名,存储过程名,事件名在任何平台上都是大小写不敏感的,列别名也是大小写不敏感的。

However, names of logfile groups are case sensitive. This differs from standard SQL. >>然而,日志组的名字是大小写敏感的。

By default, table aliases are case sensitive on Unix, but not so on Windows or OS X. The following statement would not work on Unix, because it refers to the alias both as a and as A: >>默认在unix平台下表的别名是大小写敏感的,在windows或者OS X上是不敏感的。下面的语句在unix上不能正确执行,因为在unix平台上A和a 不是代表同一张表

mysql> SELECT col_name FROM tbl_name AS a
    -> WHERE a.col_name = 1 OR A.col_name = 2;

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. >>然后上面同样的语句在windows平台上是可以执行的。为了避免类似的错误,最好使用惯例,例如在所有的平台上都使用小写(设置lower_case_table_names=1)。使用上面的惯例,能够保证最大的可用性和可移植性。

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 starting mysqldlower_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 OS X, the default value is 2. >>数据库名,表名在磁盘上怎么存储,在mysql中怎么使用受到lower_case_table_names参数的影响,你可以在启动mysqld之前设置该参数。这个参数可取值参考下面表(0,1,2)。这个参数不影响触发器大小写敏感性。在unix操作系统上lower_case_table_names默认值是0,windows操作系统上默认值是1,在OS X操作系统上默认值是2

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 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. >>参数值为0时,表名和数据库名根据create table或者create database语句中指定字母来保存(指定的是大写就保存大写,指定的是小写就保存小写)。在数据库使用中名字也是区分大小写的。如果你mysql数据库所在的操作系统是大小写不敏感的,那么此时你不能把该参数设置为0(比如说windows或者OS X).如果你在大小写不敏感的操作系统上,强制把该参数设置为0,并且在访问MyISAM表时同时使用了 大写和小写的表明,可能会导致索引损坏
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.  >>该参数设置为1时,磁盘上表名和数据库名都使用小写保存,在使用数据库时表和数据库名也是大小写不敏感的。
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 for lower_case_table_names=1.  >>参数值为0时,表名和数据库名根据create table或者create database语句中指定字母来保存(指定的是大写就保存大写,指定的是小写就保存小写)。但是在mysql数据库使用表和数据库时会把名字转换成小写,即在数据库中使用时表名和数据库名是大小写不敏感的。这个只适用于大小写不敏感的文件系统!InnoDB表的表名还是以小写保存,就跟参数值被设置为1一样。

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: >>如果你的mysql都在同样的平台上,那么你不需要去修改lower_case_table_names参数的默认值。然而,如果你想在大小写敏感性不同的平台间迁移表,可能会遇到一些问题。例如,在unix平台上,你可以有两张表,分别名为my_table和MY_TABLE,但是在windows上这两张表被认为是同一张表。为了避免这样的问题,你有如下两个选择:

  • 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.  >>第一种方案是在所有系统上都把这个参数设置为1.这种方法的主要缺点是,使用show tables和show databases时你看不出来 建表时指定的表名(是大写还是小写又或者是大小写混合)

  • 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.  >>第二种方案是在unix上指定参数为0,在windows上指定参数为2.这样能保留表或者数据名原来的大小写情况。该方案的缺点是在使用数据库时你必须按照表创建时的大小情况指定表名。

    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. >>有一个列外,如果你使用innodb表并且想避免不同平台间迁移数据的问题,你应该把所有平台下lower_case_table_names设为1

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 stopping mysqld and restarting it with the new variable setting. To do this for an individual table, use RENAME TABLE: >>如果你计划把你的unix平台上mysql的lower_case_table_names参数设置为1,你必须先把那些大写的数据库名和表名转换为小写,然后再cnf文件修改该参数再重启mysql。

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: >>如果是要修改一个或者多个库中所有的表,那么可以先使用mysqldump导出数据库,然后删除数据库,修改参数,重启mysql,然后再导入数据库

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

  2. Use DROP DATABASE to drop each database.

  3. Stop the server, set lower_case_table_names, and restart the server.

  4. Reload the dump file for each database. Because lower_case_table_names is set, each database and table name will be converted to lowercase as it is recreated:

    mysql < db1.sql
    mysql < db2.sql
    ...
    

Object names may be considered duplicates if their uppercase forms are equal according to a binary collation. That is true for names of cursors, conditions, procedures, functions, savepoints, stored routine parameters, stored program local variables, and plugins. It is not true for names of columns, constraints, databases, partitions, statements prepared with PREPARE, tables, triggers, users, and user-defined variables. >>

File system case sensitivity can affect searches in string columns of INFORMATION_SCHEMA tables. For more information, see Section 10.1.7.9, “Collation and INFORMATION_SCHEMA Searches”.

你可能感兴趣的:(Identifier Case Sensitivity)