8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.2.2 元数据的 UTF-812.2.2 UTF-8 for Metadata

元数据 是 “关于数据的数据。” 任何 描述 数据库的事物——而不是数据库的 内容——都是元数据。因此列名、数据库名、用户名、版本名,以及大多数来自 SHOW 的字符串结果都是元数据。INFORMATION_SCHEMA 中表的内容也是如此,因为这些表按定义包含有关数据库对象的信息。Metadata is “the data about the data.” Anything that describes the database—as opposed to being the contents of the database—is metadata. Thus column names, database names, user names, version names, and most of the string results from SHOW are metadata. This is also true of the contents of tables in INFORMATION_SCHEMA because those tables by definition contain information about database objects.

元数据的表示必须满足以下要求:Representation of metadata must satisfy these requirements:

  • 所有元数据必须使用相同的字符集。否则,用于 INFORMATION_SCHEMA 中表的 SHOW 语句或 SELECT 语句将无法正常工作,因为这些操作结果的同一列的不同行将使用不同的字符集。All metadata must be in the same character set. Otherwise, neither the SHOW statements nor SELECT statements for tables in INFORMATION_SCHEMA would work properly because different rows in the same column of the results of these operations would be in different character sets.

  • 元数据必须包含所有语言的所有字符。否则,用户将无法使用自己的语言命名列和表。Metadata must include all characters in all languages. Otherwise, users would not be able to name columns and tables using their own languages.

为了满足这两个要求,MySQL 将元数据存储在 Unicode 字符集,即 UTF-8 中。如果你从不使用带重音符号的字符或非拉丁字符,这不会引起任何问题。但如果使用,你应该注意元数据是以 UTF-8 存储的。To satisfy both requirements, MySQL stores metadata in a Unicode character set, namely UTF-8. This does not cause any disruption if you never use accented or non-Latin characters. But if you do, you should be aware that metadata is in UTF-8.

元数据要求意味着 USER()、CURRENT_USER()、SESSION_USER()、SYSTEM_USER()、DATABASE() 和 VERSION() 函数的返回值默认使用 UTF-8 字符集。The metadata requirements mean that the return values of the USER(), CURRENT_USER(), SESSION_USER(), SYSTEM_USER(), DATABASE(), and VERSION() functions have the UTF-8 character set by default.

服务器将 character_set_system 系统变量设置为元数据字符集的名称:The server sets the character_set_system system variable to the name of the metadata character set:

mysql> SHOW VARIABLES LIKE 'character_set_system';
+----------------------+---------+
| Variable_name        | Value   |
+----------------------+---------+
| character_set_system | utf8mb3 |
+----------------------+---------+

使用 Unicode 存储元数据会在多线程系统中的结果DESCRIBE中的函数character_set_system默认的字符集。当你使用SELECT column1 FROM t,即名称column1 itself is returned from the server to the client in the character set determined by the value of the character_set_results system variable, which has a default value of utf8mb4. If you want the server to pass metadata results back in a different character set, use the SET NAMES statement to force the server to perform character set conversion. SET NAMES sets the character_set_results and other related system variables. (See Section 12.4, “Connection Character Sets and Collations”.) Alternatively, a client program can perform the conversion after receiving the result from the server. It is more efficient for the client to perform the conversion, but this option is not always available for all clients.Storage of metadata using Unicode does not mean that the server returns headers of columns and the results of DESCRIBE functions in the character_set_system character set by default. When you use SELECT column1 FROM t, the name column1 itself is returned from the server to the client in the character set determined by the value of the character_set_results system variable, which has a default value of utf8mb4. If you want the server to pass metadata results back in a different character set, use the SET NAMES statement to force the server to perform character set conversion. SET NAMES sets the character_set_results and other related system variables. (See Section 12.4, “Connection Character Sets and Collations”.) Alternatively, a client program can perform the conversion after receiving the result from the server. It is more efficient for the client to perform the conversion, but this option is not always available for all clients.

如果 character_set_results 设置为 NULL,则不会执行任何转换,服务器将使用其原始字符集(由 character_set_system 指示的字符集)返回元数据。If character_set_results is set to NULL, no conversion is performed and the server returns metadata using its original character set (the set indicated by character_set_system).

从服务器返回到客户端的错误消息会像元数据一样自动转换为客户端字符集。Error messages returned from the server to the client are converted to the client character set automatically, as with metadata.

如果你在单个语句中使用(例如)USER() 函数进行比较或赋值,不用担心。MySQL 会为你执行一些自动转换。If you are using (for example) the USER() function for comparison or assignment within a single statement, don't worry. MySQL performs some automatic conversion for you.

SELECT * FROM t1 WHERE USER() = latin1_column;

这是可行的,因为 latin1_column 的内容会在比较之前自动转换为 UTF-8。This works because the contents of latin1_column are automatically converted to UTF-8 before the comparison.

INSERT INTO t1 (latin1_column) SELECT USER();

这是可行的,因为 USER() 的内容会在赋值之前自动转换为 latin1。This works because the contents of USER() are automatically converted to latin1 before the assignment.

虽然自动转换不在 SQL 标准中,但标准确实指出每个字符集(就支持的字符而言)都是 Unicode 的一个 “子集”。因为一个众所周知的原则是 “适用于超集的,也可以适用于子集,” 我们认为 Unicode 的排序规则可以用于与非 Unicode 字符串的比较。有关字符串强制转换的更多信息,请参见 第 12.8.4 节,“表达式中的排序规则强制能力”。Although automatic conversion is not in the SQL standard, the standard does say that every character set is (in terms of supported characters) a “subset” of Unicode. Because it is a well-known principle that “what applies to a superset can apply to a subset,” we believe that a collation for Unicode can apply for comparisons with non-Unicode strings. For more information about coercion of strings, see Section 12.8.4, “Collation Coercibility in Expressions”.