SQL Standard

SQL Binary Data Type

A binary data type in SQL is used to store byte strings. They are closely similar to char and varchar types, but instead, they hold binary strings.

Characteristics of Binary Types

Most SQL database engines provide two types of binary types:

  1. BINARY
  2. VARBINARY

Binary and varbinary are closely similar to char and varchar types. The only difference is that they store binary strings.

The following are the characteristics of these types.

Binary

  1. Binary string stores binary strings
  2. It has a fixed length. Similar to char type
  3. The binary type has a binary character set and collation.

Varbinary

As for the varbinary type, it has the following characteristics:

  1. Variable length, similar to varchar
  2. Stores binary strings
  3. Has binary character set and collation.

SQL Create Binary and Varbinary Columns

We can create a table with binary and varbinary columns as shown:

CREATE TABLE bin(
    column1 BINARY(5),
    column2 varbinary(50)
);

In the example above, we create two columns. One holds binary type with a length of 5, and the other holds varbinary with a length of 50

Since varbinary has a variable length, you do not need to specify the length.

SQL Insert Binary Values

We can insert binary values as we would on a normal column as shown:

INSERT INTO bin(column1, column2)
VALUES (0x134, 0x047);

You can view the table as:

SELECT * FROM bin;

This returns:

Closing

This article discussed the binary and varbinary types in SQL and how to use them in your databases.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list