11.3.3 The BINARY and VARBINARY TypesBINARY类型和VARBINARY类型

The BINARY and VARBINARY types are similar to CHAR and VARCHAR, except that they store binary strings rather than nonbinary strings. BINARYVARBINARY类型与CHARVARCHAR类似,只是它们存储二进制字符串而不是非二进制字符串。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.对于BINARYVARBINARY来说,允许的最大长度与CHARVARCHAR允许的最大长度相同,只是BINARYVARBINARY的长度是以字节而不是字符来度量的。

The BINARY and VARBINARY data types are distinct from the CHAR BINARY and VARCHAR BINARY data types. BINARYVARBINARY数据类型不同于CHAR BINARYVARCHAR 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_binThis 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 BINARY or VARBINARY column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. 如果未启用严格SQL模式,并且为超过列最大长度的BINARYVARBINARY列指定了一个值,则会截断该值以适合该列,并生成警告。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 0x00 (the zero byte). pad值是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 BYDISTINCT操作。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 BYDISTINCT操作。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.如果检索到的值必须与为不带填充的存储指定的值相同,则最好改用VARBINARYBLOB数据类型之一。

Note注意

Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. 在mysql客户机中,二进制字符串使用十六进制表示法显示,具体取决于--binary as hex的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”