The BINARY
and VARBINARY
types are similar to CHAR
and VARCHAR
, except that they store binary strings rather than nonbinary strings. BINARY
和VARBINARY
类型与CHAR
和VARCHAR
类似,只是它们存储二进制字符串而不是非二进制字符串。That is, they store byte strings rather than character strings. 也就是说,它们存储字节字符串而不是字符串。This means they have the 这意味着它们具有binary
character set and collation, and comparison and sorting are based on the numeric values of the bytes in the values.binary
字符集和排序规则,比较和排序基于值中字节的数值。
The permissible maximum length is the same for 对于BINARY
and VARBINARY
as it is for CHAR
and VARCHAR
, except that the length for BINARY
and VARBINARY
is measured in bytes rather than characters.BINARY
和VARBINARY
来说,允许的最大长度与CHAR
和VARCHAR
允许的最大长度相同,只是BINARY
和VARBINARY
的长度是以字节而不是字符来度量的。
The BINARY
and VARBINARY
data types are distinct from the CHAR BINARY
and VARCHAR BINARY
data types. BINARY
和VARBINARY
数据类型不同于CHAR BINARY
和VARCHAR BINARY
数据类型。For the latter types, the 对于后两种类型,BINARY
attribute does not cause the column to be treated as a binary string column. BINARY
属性不会导致该列被视为二进制字符串列。Instead, it causes the binary (相反,它会导致使用列字符集(如果未指定列字符集,则使用表默认字符集)的二进制(_bin
) collation for the column character set (or the table default character set if no column character set is specified) to be used, and the column itself stores nonbinary character strings rather than binary byte strings. _bin
)排序规则,并且列本身存储非二进制字符串,而不是二进制字节字符串。For example, if the default character set is 例如,如果默认字符集为utf8mb4
, CHAR(5) BINARY
is treated as CHAR(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
. utf8mb4
,则CHAR(5) BINARY
将被视为CHAR(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
。This differs from 这与BINARY(5)
, which stores 5-byte binary strings that have the binary
character set and collation. BINARY(5)
不同,后者存储具有binary
字符集和排序规则的5字节二进制字符串。For information about the differences between the 有关binary
collation of the binary
character set and the _bin
collations of nonbinary character sets, see Section 10.8.5, “The binary Collation Compared to _bin Collations”.binary
字符集的binary
排序规则与非二进制字符集的_bin
排序规则之间差异的信息,请参阅第10.8.5节,“二进制排序规则与二进制排序规则的比较”。
If strict SQL mode is not enabled and you assign a value to a 如果未启用严格SQL模式,并且为超过列最大长度的BINARY
or VARBINARY
column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. BINARY
或VARBINARY
列指定了一个值,则会截断该值以适合该列,并生成警告。For cases of truncation, to cause an error to occur (rather than a warning) and suppress insertion of the value, use strict SQL mode. 对于截断的情况,要引起错误(而不是警告)并禁止插入值,请使用严格SQL模式。See Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节,“服务器SQL模式”。
When 当BINARY
values are stored, they are right-padded with the pad value to the specified length. BINARY
值被存储时,它们被pad值右填充到指定的长度。The pad value is pad值是0x00
(the zero byte). 0x00
(零字节)。Values are right-padded with 插入时用0x00
for inserts, and no trailing bytes are removed for retrievals. 0x00
右填充值,检索时不删除尾随字节。All bytes are significant in comparisons, including 所有字节在比较中都是重要的,包括ORDER BY
and DISTINCT
operations. ORDER BY
和DISTINCT
操作。0x00
and space differ in comparisons, with 0x00
sorting before space.0x00
和空格在比较中不同,0x00
在空格之前排序。
Example: For a 例如,对于BINARY(3)
column, 'a '
becomes 'a \0'
when inserted. BINARY(3)
列,'a '
在插入时变成了'a \0'
。'a\0'
becomes 'a\0\0'
when inserted. 'a\0'
在插入时变成了'a\0\0'
。Both inserted values remain unchanged for retrievals.两个插入的值在检索时保持不变。
For 对于VARBINARY
, there is no padding for inserts and no bytes are stripped for retrievals. VARBINARY
,插入没有填充,检索也没有剥离字节。All bytes are significant in comparisons, including 所有字节在比较中都是重要的,包括ORDER BY
and DISTINCT
operations. ORDER BY
和DISTINCT
操作。0x00
and space differ in comparisons, with 0x00
sorting before space.0x00
和空格在比较中不同,0x00
在空格之前排序。
For those cases where trailing pad bytes are stripped or comparisons ignore them, if a column has an index that requires unique values, inserting values into the column that differ only in number of trailing pad bytes results in a duplicate-key error. 对于尾部填充字节被剥离或比较忽略的情况,如果列具有需要唯一值的索引,则在列中插入仅尾部填充字节数不同的值将导致重复键错误。For example, if a table contains 例如,如果某个表包含了'a'
, an attempt to store 'a\0'
causes a duplicate-key error.'a'
,试图存储'a\0'
会导致重复键错误。
You should consider the preceding padding and stripping characteristics carefully if you plan to use the 如果计划使用BINARY
data type for storing binary data and you require that the value retrieved be exactly the same as the value stored. BINARY
数据类型存储二进制数据,并且要求检索到的值与存储的值完全相同,则应仔细考虑前面的填充和剥离特性。The following example illustrates how 以下示例说明0x00
-padding of BINARY
values affects column value comparisons:BINARY
值的0x00
填充如何影响列值比较:
mysql>CREATE TABLE t (c BINARY(3));
Query OK, 0 rows affected (0.01 sec) mysql>INSERT INTO t SET c = 'a';
Query OK, 1 row affected (0.01 sec) mysql>SELECT HEX(c), c = 'a', c = 'a\0\0' from t;
+--------+---------+-------------+ | HEX(c) | c = 'a' | c = 'a\0\0' | +--------+---------+-------------+ | 610000 | 0 | 1 | +--------+---------+-------------+ 1 row in set (0.09 sec)
If the value retrieved must be the same as the value specified for storage with no padding, it might be preferable to use 如果检索到的值必须与为不带填充的存储指定的值相同,则最好改用VARBINARY
or one of the BLOB
data types instead.VARBINARY
或BLOB
数据类型之一。
Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the 在mysql客户机中,二进制字符串使用十六进制表示法显示,具体取决于--binary-as-hex
. --binary as hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。