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:
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 string stores binary strings
- It has a fixed length. Similar to char type
- The binary type has a binary character set and collation.
As for the varbinary type, it has the following characteristics:
- Variable length, similar to varchar
- Stores binary strings
- Has binary character set and collation.
SQL Create Binary and Varbinary Columns
We can create a table with binary and varbinary columns as shown:
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:
VALUES (0x134, 0x047);
You can view the table as:
This article discussed the binary and varbinary types in SQL and how to use them in your databases.