8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 11 语言结构 / 11.2 模式对象名称 / 11.2.3 标识符大小写敏感
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

11.2.3 标识符的大小写敏感性11.2.3 Identifier Case Sensitivity

在 MySQL 中,数据库对应于数据目录中的目录。数据库中的每个表至少对应于数据库目录中的一个文件(根据存储引擎的不同,可能还会有更多文件)。触发器也对应于文件。因此,底层操作系统的大小写敏感性会影响数据库、表和触发器名称的大小写敏感性。这意味着在 Windows 中这些名称不区分大小写,而在大多数 Unix 系统中则区分大小写。一个显著的例外是 macOS,它基于 Unix,但使用默认的文件系统类型(HFS),该类型不区分大小写。然而,macOS 也支持 UFS 卷,其与任何 Unix 系统一样区分大小写。参见 第 1.7.1 节,“MySQL 对标准 SQL 的扩展”。lower_case_table_names 系统变量也会影响服务器如何处理标识符的大小写敏感性,如本节后面所述。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 macOS, which is Unix-based but uses a default file system type (HFS+) that is not case-sensitive. However, macOS 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_names system variable also affects how the server handles identifier case sensitivity, as described later in this section.

Note

尽管在某些平台上,数据库、表和触发器的名称不区分大小写,但在同一条语句中不应使用不同的大小写来引用它们。以下语句无法正常工作,因为它既以 my_table 又以 MY_TABLE 的形式引用了该表: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:

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

分区、子分区、列、索引、存储过程、事件和资源组名称在任何平台上都不区分大小写,列别名也同样不区分大小写。Partition, subpartition, column, index, stored routine, event, and resource group names are not case-sensitive on any platform, nor are column aliases.

然而,日志文件组的名称是区分大小写的。这与标准 SQL 不同。However, names of logfile groups are case-sensitive. This differs from standard SQL.

默认情况下,表别名在 Unix 上区分大小写,但在 Windows 或 macOS 上则不区分大小写。下面的语句在 Unix 上无法工作,因为它同时将别名引用为 a 和 A:By default, table aliases are case-sensitive on Unix, but not so on Windows or macOS. The following statement would not work on Unix, 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;

然而,同样的语句在 Windows 上是允许的。为了避免由于这些差异引起的问题,最好采用一致的约定,例如始终使用小写名称创建和引用数据库及表。为了最大限度的可移植性和易用性,推荐采用这种约定。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.

表和数据库名称在磁盘上的存储方式以及在 MySQL 中的使用方式受到 lower_case_table_names 系统变量的影响。lower_case_table_names 可以取下表所示的值。该变量 不会 影响触发器标识符的大小写敏感性。在 Unix 上,lower_case_table_names 的默认值为 0。在 Windows 上,默认值为 1。在 macOS 上,默认值为 2。How table and database names are stored on disk and used in MySQL is affected by the lower_case_table_names system variable. 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.

lower_case_table_names 只能在初始化服务器时配置。服务器初始化后,禁止更改 lower_case_table_names 设置。lower_case_table_names can only be configured when initializing the server. Changing the lower_case_table_names setting after the server is initialized is prohibited.

价值Value 意义Meaning
0 Table and database names are stored on disk using the lettercase specified in the CREATE TABLE或者CREATE DATABASE statement. Name comparisons are case-sensitive. You should 在多线程系统中 set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or macOS). 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.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 macOS). 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 表名在磁盘上以小写存储,名称比较不区分大小写。MySQL 在存储和查找时会将所有表名转换为小写。此行为同样适用于数据库名和表别名。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 表和数据库名称在磁盘上以 CREATE TABLE 或 CREATE DATABASE 语句中指定的大小写存储,但 MySQL 在查找时将其转换为小写。名称比较不区分大小写。此功能 仅 在不区分大小写的文件系统上有效!InnoDB 表名和视图名按小写存储,与 lower_case_table_names=1 的情况相同。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 and view names are stored in lowercase, as for lower_case_table_names=1.

如果你只在一个平台上使用 MySQL,通常不需要使用除默认值之外的 lower_case_table_names 设置。然而,如果你想在文件系统大小写敏感性不同的平台之间传输表,可能会遇到困难。例如,在 Unix 上,你可以有两个不同的表分别命名为 my_table 和 MY_TABLE,但在 Windows 上,这两个名字被认为是相同的。为了避免因数据库或表名的字母大小写导致的数据传输问题,你有两个选择:If you are using MySQL on only one platform, you do not normally have to use a lower_case_table_names setting other than the default. 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:

  • 在所有系统上使用 lower_case_table_names=1。其主要缺点是,当你使用 SHOW TABLES 或 SHOW DATABASES 时,你看不到其原始的字母大小写。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.

  • 在 Unix 上使用 lower_case_table_names=0,在 Windows 上使用 lower_case_table_names=2。这可以保留数据库和表名的字母大小写。其缺点是在 Windows 上,你必须确保语句始终以正确的字母大小写引用数据库和表名。如果将语句转移到区分字母大小写的 Unix 上,当字母大小写不正确时,这些语句将无法工作。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.

    例外:如果您使用的是 InnoDB 表,并且您试图避免这些数据传输问题,您应该在所有平台上使用 lower_case_table_names=1 来强制将名称转换为小写。Exception: If you are using InnoDB tables and you are trying to avoid these data transfer problems, you should use lower_case_table_names=1 on all platforms to force names to be converted to lowercase.

如果对象名称的大写形式在二进制排序规则下相等,则可能会被认为是重复的。对于游标、条件、存储过程、函数、保存点、存储例程参数、存储程序局部变量和插件的名称,这一点是正确的。但对于列、约束、数据库、分区、使用 PREPARE 预处理的语句、表、触发器、用户以及用户定义变量的名称,则不适用。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.

文件系统的大小写敏感性可能会影响 INFORMATION_SCHEMA 表中字符串列的搜索。有关更多信息,请参阅 第 12.8.7 节,“在 INFORMATION_SCHEMA 搜索中使用排序规则”。File system case sensitivity can affect searches in string columns of INFORMATION_SCHEMA tables. For more information, see Section 12.8.7, “Using Collation in INFORMATION_SCHEMA Searches”.