VARBINARY

Variable-length binary string type. VARBINARY columns store binary strings of variable length up to a specified maximum.

Syntax

VARBINARY(M)

Description

The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings. M represents the maximum column length in bytes.

It contains no character set, and comparison and sorting are based on the numeric value of the bytes.

If the maximum length is exceeded, and SQL strict mode is not enabled , the extra characters will be dropped with a warning. If strict mode is enabled, an error will occur.

Unlike BINARY values, VARBINARY values are not right-padded when inserting.

Use Cases for Zero Length

A BINARY(0) or VARBINARY(0) column is restricted to an empty byte string or NULL.

  • Schema Preservation: Use these columns when a system expects a specific column to exist, but no data storage is required for your current application.

  • Space-Efficient Indicators: These columns can act as a two-state indicator where the presence of an empty byte string represents one state and NULL represents another.

If you attempt to insert a value other than an empty string, MariaDB returns an ERROR 1406 (22001) indicating the data is too long for the column.

Oracle Mode

In Oracle mode, RAW is a synonym for VARBINARY.

Examples

Inserting too many characters, first with strict mode off, then with it on:

Sorting is performed with the byte value:

Using CAST to sort as a CHAR instead:

VARBINARY

For our example of VARBINARY, we picked a maximum size that avoids overflowing the maximum row size (65535). Keep in mind that a multi-byte character set would need more space in the row than a single-byte character set. We also avoid the auto-conversion of a VARBINARY into a BLOB, MEDIUMBLOB, or LONGBLOB that can happen when STRICT_TRANS_TABLES is not set in the SQL_MODE.

The example:

Data too Long

When SQL_MODE is strict (the default), a value is considered "too long" when its length exceeds the size of the data type, and an error is generated.

Example of data too long behavior for VARBINARY:

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sqlarrow-up-right

spinner

Last updated

Was this helpful?