BINARY and VARBINARY types are similar to CHAR and VARCHAR, except that they store binary data instead of strings.
BINARY and VARBINARY use the binary character set and binary collation.
BINARY
The BINARY type is similar to the CHAR type, but stores binary byte strings. The syntax is as follows:
BINARY[(M)]
The optional length parameter M indicates the column length in bytes. If omitted, the default value of M is 1.
The maximum length of BINARY is the same as that of CHAR, but the length is measured in bytes. If strict SQL mode is not enabled, values assigned to a BINARY column that exceed the maximum length will be truncated and an alert will be generated.
When storing BINARY values, trailing zero bytes (0x00) are added to the right to reach the specified length. If the inserted data ends with 0x00, these trailing zero bytes are retained and not removed to avoid affecting retrieval. In byte comparisons, including ORDER BY and DISTINCT operations, 0x00 and spaces are treated as different characters, with 0x00 sorted before spaces.
VARBINARY
The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings. 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 the length is measured in bytes. If strict SQL mode is not enabled, values assigned to a VARBINARY column that exceed the maximum length will be truncated and an alert will be generated.
For VARBINARY, no padding is added when inserting data, and no trailing zero bytes are removed. In byte comparisons, including ORDER BY and DISTINCT operations, 0x00 and spaces are treated as different characters, with 0x00 sorted before spaces.
