Skip to main content

BINARY and VARBINARY

The BINARY and VARBINARY types are similar to the CHAR and VARCHAR types, except that they store binary data instead of strings.

The BINARY and VARBINARY types use the binary character set and binary collation.

BINARY

The BINARY type is similar to the CHAR type, except that it stores binary byte strings. The syntax is as follows:

BINARY[(M)]

The optional length parameter M specifies the column length in bytes. If you omit this parameter, the default value of M is 1.

The maximum length of the BINARY type is the same as that of the CHAR type, but the length is specified in bytes. If strict SQL mode is not enabled, values assigned to a BINARY column that exceed the maximum length of the column are truncated and a warning is generated.

When storing a BINARY value, the value is padded on the right with 0x00 (zero bytes) to the specified length. If the inserted data ends with 0x00, it is retained without being deleted, to avoid affecting retrieval. In byte comparisons, including ORDER BY and DISTINCT operations, 0x00 and spaces are different, and 0x00 is sorted before spaces.

VARBINARY

The VARBINARY type is similar to the VARCHAR type, except that it stores binary byte strings. The syntax is as follows:

VARBINARY(M)

M specifies the maximum column length in bytes.

The maximum length of the VARBINARY type is the same as that of the VARCHAR type, but the length is specified in bytes. If strict SQL mode is not enabled, values assigned to a VARBINARY column that exceed the maximum length of the column are truncated and a warning is generated.

For VARBINARY, no padding is performed when inserting data, and no trailing padding bytes are deleted. In byte comparisons, including ORDER BY and DISTINCT operations, 0x00 and spaces are different, and 0x00 is sorted before spaces.