Skip to main content
BINARY(M) VARBINARY(M) Since v3.0, the database supports the BINARY/VARBINARY data type, which is used to store binary data. The supported length range is [1, 1048576]. Unit: bytes. If M is not specified, 1048576 is used by default. Unlike VARCHAR, whose maximum length increases in v4.2, the maximum BINARY/VARBINARY length remains 1048576 bytes. Binary data types contain byte strings while character data types contain character strings. BINARY is an alias of VARBINARY. The usage is the same as VARBINARY.

Limits and usage notes

  • VARBINARY columns are supported in Duplicate Key, Primary Key, and Unique Key tables. They are not supported in Aggregate tables.
  • VARBINARY columns cannot be used as partition keys, bucketing keys, or dimension columns of Duplicate Key, Primary Key, and Unique Key tables. They cannot be used in ORDER BY, GROUP BY, and JOIN clauses.
  • BINARY(M)/VARBINARY(M) are not right-padded in the case of unaligned length.

Examples

Create a column of VARBINARY type

When creating a table, use the keyword VARBINARY to specify column j as a VARBINARY column.

Load data and store it as BINARY type

The database supports the following ways to load data and store it as BINARY type.
  • Method 1: Use INSERT INTO to write data to a constant column of BINARY type (such as column j), where the constant column is prefixed with x''.
  • Method 2: Use the to_binary function to convert VARCHAR data to binary data.
  • Method 3: Use Broker Load to load a Parquet or ORC file and store the file as BINARY data. For more information, see Broker Load.
    • For Parquet files, convert parquet::Type::type::BYTE_ARRAY to TYPE_VARBINARY directly.
    • For ORC files, convert orc::BINARY to TYPE_VARBINARY directly.
  • Method 4: Use Stream Load to load a CSV file and store the file as BINARY data. For more information, see Load CSV data.
    • CSV file uses the hex format for binary data. Please ensure the input binary value is a valid hex value.
    • BINARY type is only supported in CSV file. JSON file does not support BINARY type.
    For example, t1 is a table with a VARBINARY column b.

Query and process BINARY data

The database supports querying and processing BINARY data, and supports the use of BINARY functions and operators. This example uses table test_binary. Note: If you add the --binary-as-hex option When you access the database from your MySQL client, binary data will be displayed using hex notation.
Text
Example 1: View binary data using the hex function.
Example 2: View binary data using the to_base64 function.
Example 3: View binary data using the from_binary function.