What Is Binary Data Type in MySQL?


Heather Bennett

The binary data type in MySQL is used to store binary data, which represents data in the form of a sequence of bytes. Binary data can include images, audio files, video files, and other types of non-textual data. In this tutorial, we will explore the binary data type in MySQL and learn how to work with it.

Creating a Table with Binary Data Type

To create a table with a binary column in MySQL, we can use the BINARY or VARBINARY data types. The BINARY type is used for fixed-length binary strings, while the VARBINARY type is used for variable-length binary strings.

Here’s an example of creating a table with a BINARY column:

    name VARCHAR(100),
    image BINARY(200)

In this example, we have created a table named images with three columns: id, name, and image. The BINARY(200) specifies that the maximum length of the binary string stored in the image column is 200 bytes.

Inserting Binary Data into a Table

To insert binary data into a table, we need to use the appropriate syntax and encoding. One way to insert binary data is by using hexadecimal notation.


  • The hexadecimal notation represents each byte of the binary data as two hexadecimal digits.
  • We prefix the hexadecimal value with “\x” in MySQL.

Here’s an example of inserting binary data into the image column:

INSERT INTO images (name, image) VALUES ('image1', x'FFD8FFE000104A46494600010100000100010000FFDB004300080606070605080707070909080A0C140D0C0B0B0C1912130F141D1A1F1E1D1A1C1C20242E2720222C231C1C2837292C30313434341F27393D38323C2E333432FFDB0043010909090C0B0C180D0D1832211C2132323232323232323232323232323232323232323232323232323

In this example, we insert a binary image data represented in hexadecimal notation. The x' prefix is used to indicate that the following value is a hexadecimal representation.

Retrieving Binary Data from a Table

To retrieve binary data from a table, we can use the SELECT statement. By default, MySQL returns binary data as a sequence of bytes.


  • If you want to display the binary data as a string, you can use the HEX() function to convert it to its hexadecimal representation.

Here’s an example of retrieving binary data from the image column:

SELECT id, name, HEX(image) AS image FROM images;

In this example, we select the id, name, and image columns from the images table. The HEX() function is used to convert the binary data in the image column to its hexadecimal representation.


The binary data type in MySQL is a useful feature for storing and managing non-textual data such as images, audio files, and video files. By understanding how to create tables with binary columns, insert binary data, and retrieve binary data, you can effectively work with binary data in MySQL.


  • It’s important to handle binary data carefully as it can be large in size and impact database performance.
  • Avoid storing large binary objects directly in the database if possible. Instead, consider storing them on a file system or using specialized storage solutions.

I hope this tutorial has provided you with a good understanding of the binary data type in MySQL and how to use it effectively in your database applications.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy