binary 字符集是用于二进制字符串的字符集,二进制字符串是字节序列。binary 字符集只有一个排序规则,也名为 binary。比较和排序是基于字节的数值,而不是字符代码的数值(对于多字节字符,字符代码的数值与字节数值不同)。有关 binary 字符集的 binary 排序规则与非二进制字符集的 _bin 排序规则之间差异的信息,请参见 第 12.8.5 节,“二进制排序规则与 _bin 排序规则的比较”。The binary character set is the character set
for binary strings, which are sequences of bytes. The
binary character set has one collation, also
named binary. Comparison and sorting are
based on numeric byte values, rather than on numeric character
code values (which for multibyte characters differ from numeric
byte values). For information about the differences between the
binary collation of the
binary character set and the
_bin collations of nonbinary character sets,
see Section 12.8.5, “The binary Collation Compared to _bin Collations”.
对于 binary 字符集,不适用字母大小写和重音等价的概念:For the binary character set, the concepts of
lettercase and accent equivalence do not apply:
对于以二进制字符串存储的单字节字符,字符边界和字节边界是相同的,因此在比较中字母大小写和重音差异是重要的。也就是说,
binary排序是区分大小写和重音的。For single-byte characters stored as binary strings, character and byte boundaries are the same, so lettercase and accent differences are significant in comparisons. That is, thebinarycollation is case-sensitive and accent-sensitive.mysql>
SET NAMES 'binary';mysql>SELECT CHARSET('abc'), COLLATION('abc');+----------------+------------------+ | CHARSET('abc') | COLLATION('abc') | +----------------+------------------+ | binary | binary | +----------------+------------------+ mysql>SELECT 'abc' = 'ABC', 'a' = 'ä';+---------------+------------+ | 'abc' = 'ABC' | 'a' = 'ä' | +---------------+------------+ | 0 | 0 | +---------------+------------+对于以二进制字符串存储的多字节字符,字符边界和字节边界是不同的。字符边界会丢失,因此依赖字符边界的比较没有意义。For multibyte characters stored as binary strings, character and byte boundaries differ. Character boundaries are lost, so comparisons that depend on them are not meaningful.
要对二进制字符串进行字母大小写转换,首先使用适合字符串中存储数据的字符集将其转换为非二进制字符串:To perform lettercase conversion of a binary string, first convert it to a nonbinary string using a character set appropriate for the data stored in the string:
mysql>SET @str = BINARY 'New York';mysql>SELECT LOWER(@str), LOWER(CONVERT(@str USING utf8mb4));+-------------+------------------------------------+ | LOWER(@str) | LOWER(CONVERT(@str USING utf8mb4)) | +-------------+------------------------------------+ | New York | new york | +-------------+------------------------------------+
将字符串表达式转换为二进制字符串,这些构造是等价的:To convert a string expression to a binary string, these constructs are equivalent:
BINARYexprCAST(exprAS BINARY) CONVERT(exprUSING BINARY)
如果一个值是字符字符串字面量,可以使用 _binary 引导符将其指定为二进制字符串。例如:If a value is a character string literal, the
_binary introducer may be used to designate
it as a binary string. For example:
_binary 'a'
_binary 引导符也允许用于十六进制字面量和位值字面量,但通常不需要;此类字面量默认就是二进制字符串。The _binary introducer is permitted for
hexadecimal literals and bit-value literals as well, but
unnecessary; such literals are binary strings by default.
有关引导符的更多信息,请参见 第 12.3.8 节,“字符集引导符”。For more information about introducers, see Section 12.3.8, “Character Set Introducers”.
在 mysql 客户端中,二进制字符串显示为十六进制表示,具体取决于 --binary-as-hex 的值。有关该选项的更多信息,请参阅 第 6.5.1 节,“mysql — MySQL 命令行客户端”。Within the mysql client, binary strings
display using hexadecimal notation, depending on the value of
the --binary-as-hex. For more
information about that option, see Section 6.5.1, “mysql — The MySQL Command-Line Client”.