字符集的 字符表 是该集合中的字符集合。The repertoire of a character set is the collection of characters in the set.
字符串表达式有一个 repertoire 属性,它可以有两个值:String expressions have a repertoire attribute, which can have two values:
ASCII:表达式只能包含 ASCII 字符;也就是说,字符范围在 Unicode 范围U+0000到U+007F之间。ASCII: The expression can contain only ASCII characters; that is, characters in the Unicode rangeU+0000toU+007F.UNICODE:表达式可以包含 Unicode 范围内的字符U+0000到U+10FFFF。这包括基本多语言平面(BMP)范围内的字符 (U+0000到U+FFFF) 以及 BMP 范围外的补充字符 (U+10000到U+10FFFF)。UNICODE: The expression can contain characters in the Unicode rangeU+0000toU+10FFFF. This includes characters in the Basic Multilingual Plane (BMP) range (U+0000toU+FFFF) and supplementary characters outside the BMP range (U+10000toU+10FFFF).
ASCII 范围是 UNICODE 范围的一个子集,因此包含 ASCII 字符集的字符串可以安全地转换为任何包含 UNICODE 字符集的字符串,而不会丢失信息。它也可以安全地转换为任何包含 ascii 字符集的超集的字符集。(除了 swe7,它会将一些标点字符重用于瑞典语带重音字符之外,所有 MySQL 字符集都是 ascii 的超集。)The ASCII range is a subset of
UNICODE range, so a string with
ASCII repertoire can be converted safely
without loss of information to the character set of any string
with UNICODE repertoire. It can also be
converted safely to any character set that is a superset of the
ascii character set. (All MySQL character
sets are supersets of ascii with the
exception of swe7, which reuses some
punctuation characters for Swedish accented characters.)
使用字符集库可以在许多情况下实现表达式中的字符集转换,否则当排序规则的强制能力规则不足以解决歧义时,MySQL 将返回“非法的排序规则混合”错误。(有关强制能力的信息,请参阅 第 12.8.4 节,“表达式中的排序规则强制能力”。)The use of repertoire enables character set conversion in expressions for many cases where MySQL would otherwise return an “illegal mix of collations” error when the rules for collation coercibility are insufficient to resolve ambiguities. (For information about coercibility, see Section 12.8.4, “Collation Coercibility in Expressions”.)
以下讨论提供了表达式及其表达集的示例,并描述了使用表达集如何改变字符串表达式的求值:The following discussion provides examples of expressions and their repertoires, and describes how the use of repertoire changes string expression evaluation:
字符串常量的表达集取决于字符串内容,并可能与字符串字符集的表达集不同。请考虑以下语句:The repertoire for a string constant depends on string content and may differ from the repertoire of the string character set. Consider these statements:
SET NAMES utf8mb4; SELECT 'abc'; SELECT _utf8mb4'def';
虽然在前面的每种情况下字符集都是
utf8mb4,但这些字符串实际上并不包含任何超出 ASCII 范围的字符,因此它们的字符集是ASCII而不是UNICODE。Although the character set isutf8mb4in each of the preceding cases, the strings do not actually contain any characters outside the ASCII range, so their repertoire isASCIIrather thanUNICODE.一个具有
ascii字符集的列因为其字符集而具有ASCII字符集。在下表中,c1具有ASCII字符集:A column having theasciicharacter set hasASCIIrepertoire because of its character set. In the following table,c1hasASCIIrepertoire:CREATE TABLE t1 (c1 CHAR(1) CHARACTER SET ascii);
以下示例说明了在发生错误时,如果没有储备如何确定结果:The following example illustrates how repertoire enables a result to be determined in a case where an error occurs without repertoire:
CREATE TABLE t1 ( c1 CHAR(1) CHARACTER SET latin1, c2 CHAR(1) CHARACTER SET ascii ); INSERT INTO t1 VALUES ('a','b'); SELECT CONCAT(c1,c2) FROM t1;没有储备时,会发生此错误:Without repertoire, this error occurs:
ERROR 1267 (HY000): Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (ascii_general_ci,IMPLICIT) for operation 'concat'
使用字符集,从子集到超集(
ascii到latin1)可以进行转换并返回结果:Using repertoire, subset to superset (asciitolatin1) conversion can occur and a result is returned:+---------------+ | CONCAT(c1,c2) | +---------------+ | ab | +---------------+
带有一个字符串参数的函数继承其参数的字符集。
UPPER(_utf8mb4'abc')的结果具有ASCII字符集,因为其参数具有ASCII字符集。(尽管有_utf8mb4前缀,字符串'abc'中不包含 ASCII 范围之外的字符。)Functions with one string argument inherit the repertoire of their argument. The result ofUPPER(_utf8mb4'abc')hasASCIIrepertoire because its argument hasASCIIrepertoire. (Despite the_utf8mb4introducer, the string'abc'contains no characters outside the ASCII range.)对于返回字符串但没有字符串参数且使用
character_set_connection作为结果字符集的函数,如果character_set_connection是ascii,则结果字符集为ASCII,否则为UNICODE:For functions that return a string but do not have string arguments and usecharacter_set_connectionas the result character set, the result repertoire isASCIIifcharacter_set_connectionisascii, andUNICODEotherwise:FORMAT(
numeric_column, 4);使用曲目会改变 MySQL 对以下示例的评估方式:Use of repertoire changes how MySQL evaluates the following example:
SET NAMES ascii; CREATE TABLE t1 (a INT, b VARCHAR(10) CHARACTER SET latin1); INSERT INTO t1 VALUES (1,'b'); SELECT CONCAT(FORMAT(a, 4), b) FROM t1;
没有储备时,会发生此错误:Without repertoire, this error occurs:
ERROR 1267 (HY000): Illegal mix of collations (ascii_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'concat'
使用曲目时,将返回一个结果:With repertoire, a result is returned:
+-------------------------+ | CONCAT(FORMAT(a, 4), b) | +-------------------------+ | 1.0000b | +-------------------------+
具有两个或更多字符串参数的函数使用结果字符集的“最宽”参数集,其中
UNICODE比ASCII更宽。考虑以下CONCAT()调用:Functions with two or more string arguments use the “widest” argument repertoire for the result repertoire, whereUNICODEis wider thanASCII. Consider the followingCONCAT()calls:CONCAT(_ucs2 X'0041', _ucs2 X'0042') CONCAT(_ucs2 X'0041', _ucs2 X'00C2')
对于第一次调用,字符集是
ASCII,因为两个参数都在 ASCII 范围内。对于第二次调用,字符集是UNICODE,因为第二个参数在 ASCII 范围之外。For the first call, the repertoire isASCIIbecause both arguments are within the ASCII range. For the second call, the repertoire isUNICODEbecause the second argument is outside the ASCII range.函数返回值的字符集和校对规则取决于仅影响结果字符集和校对规则的参数集合。The repertoire for function return values is determined based on the repertoire of only those arguments that affect the result's character set and collation.
IF(column1 < column2, 'smaller', 'greater')
结果编码是
ASCII,因为两个字符串参数(第二个参数和第三个参数)都使用ASCII编码。第一个参数对于结果编码没有影响,即使表达式使用了字符串值。The result repertoire isASCIIbecause the two string arguments (the second argument and the third argument) both haveASCIIrepertoire. The first argument does not matter for the result repertoire, even if the expression uses string values.