The BINARY and VARBINARY data types are similar to CHAR and VARCHAR, except that they store binary data rather than strings.
BINARY and VARBINARY use the binary character set and binary collation.
BINARY
The BINARY data type is similar to the CHAR data type, but it stores binary strings of bytes. The syntax is as follows:
BINARY[(M)]
The optional length parameter M specifies the column length in bytes. If omitted, the default value of M, which is 1, is used.
The maximum length of BINARY is the same as that of CHAR, but it is measured in bytes. When strict SQL mode is not enabled, if a value assigned to a BINARY column exceeds its maximum length, the excess part will be truncated and an alert will be generated.
When storing BINARY values, the data is right-padded with 0x00 (zero bytes) to the specified length. If the inserted data already ends with 0x00, those bytes are retained without any trailing padding deletion to avoid affecting retrieval results. In byte comparisons, including those used by ORDER BY and DISTINCT, 0x00 is considered different from a space, and 0x00 sorts before a space.
VARBINARY
The VARBINARY data type is similar to the VARCHAR data type, but it stores binary strings of bytes. The syntax is as follows:
VARBINARY(M)
M indicates the maximum column length in bytes.
The maximum length of VARBINARY is the same as that of VARCHAR, but it is measured in bytes. When strict SQL mode is not enabled, if a value assigned to a VARBINARY column exceeds its maximum length, the excess part will be truncated and an alert will be generated.
For VARBINARY, no padding is required when inserting data, and no trailing padding bytes are deleted. In byte comparisons, including those used by ORDER BY and DISTINCT, 0x00 is considered different from a space; 0x00 sorts before a space.