Introduction
In this chapter, we will learn about the BINARY
data type in MySQL. The BINARY
data type is used to store binary strings. It is particularly useful for storing fixed-length binary data, such as hashes, binary flags, or any other binary data that needs to be stored exactly as is. We will cover the syntax for defining BINARY
columns, examples, and important considerations for using BINARY
in MySQL.
Syntax
The basic syntax for defining a BINARY
column in MySQL is:
BINARY(length)
length
: Specifies the fixed length of the binary string. The length can be any value from 0 to 255.
Example
Here is an example of how to define a BINARY
column in a table:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password_hash BINARY(64)
);
In this example, password_hash
is defined as a BINARY(64)
column to store a 64-byte binary hash of user passwords.
Full Example
Let’s go through a complete example where we create a database and a table with BINARY
columns, insert data, and demonstrate their usage.
- Create a Database:
CREATE DATABASE secure_app;
- Select the Database:
USE secure_app;
- Create the Users Table with
BINARY
Columns:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password_hash BINARY(64)
);
- Insert Data into the Users Table:
INSERT INTO users (username, password_hash) VALUES
('rahul_sharma', UNHEX(SHA2('password123', 256))),
('priya_singh', UNHEX(SHA2('securepass', 256))),
('amit_kumar', UNHEX(SHA2('mypassword', 256))),
('neha_verma', UNHEX(SHA2('admin1234', 256))),
('sahil_mehta', UNHEX(SHA2('passw0rd', 256)));
- Query the Users Table:
SELECT id, username, HEX(password_hash) as password_hash FROM users;
Output
id | username | password_hash |
---|---|---|
1 | rahul_sharma | EBF3E5A3A7C02F5CE47E88F9D7D187CFF2B903153EE1A3C3C01F7B79B64BB4E0 |
2 | priya_singh | A7C7C8F5C02E1CE8A17D3DBBAFAE7E3F62137E3C3C3B3D4C7E9D4B2C2A1B1F0E |
3 | amit_kumar | B7D8E7C8E4C3B2D1A2B3E4C5D6A7F8C9D8A7E8C9D0A7B6C5D4A3B2C1E8F7C6B5 |
4 | neha_verma | C8F7E6C5D4A3B2C1E8F7E6D5C4B3A2E1C8F7E6D5C4B3A2E1D8F7E6C5D4A3B2C1 |
5 | sahil_mehta | D9E8F7C6B5A4D3C2B1E8F7E6C5B4A3D2C1F8E7D6C5B4A3D2C1F8E7D6C5B4A3D2C1 |
In this example, the password_hash
column is defined as BINARY
to store binary hashes of user passwords.
Using BINARY Values in Queries
You can use BINARY
values in queries to filter and manipulate binary data.
Example: Query Users by Password Hash
SELECT id, username FROM users WHERE password_hash = UNHEX(SHA2('password123', 256));
Output
id | username |
---|---|
1 | rahul_sharma |
Example: Query Users and Convert Binary Hash to HEX
SELECT id, username, HEX(password_hash) as password_hash FROM users;
Output
id | username | password_hash |
---|---|---|
1 | rahul_sharma | EBF3E5A3A7C02F5CE47E88F9D7D187CFF2B903153EE1A3C3C01F7B79B64BB4E0 |
2 | priya_singh | A7C7C8F5C02E1CE8A17D3DBBAFAE7E3F62137E3C3C3B3D4C7E9D4B2C2A1B1F0E |
3 | amit_kumar | B7D8E7C8E4C3B2D1A2B3E4C5D6A7F8C9D8A7E8C9D0A7B6C5D4A3B2C1E8F7C6B5 |
4 | neha_verma | C8F7E6C5D4A3B2C1E8F7E6D5C4B3A2E1C8F7E6D5C4B3A2E1D8F7E6C5D4A3B2C1 |
5 | sahil_mehta | D9E8F7C6B5A4D3C2B1E8F7E6C5B4A3D2C1F8E7D6C5B4A3D2C1F8E7D6C5B4A3D2C1 |
Important Considerations
- Fixed Length: The
BINARY
data type is best used for columns where the length of the binary string is consistent. If the length varies, consider using theVARBINARY
data type. - Storage Requirements: The
BINARY
data type always uses the specified length, even if the actual binary data is shorter. This can lead to wasted storage space if the binary data is often shorter than the defined length. - Padding:
BINARY
values are padded with\0
(null bytes) to the specified length. When retrievingBINARY
values, these padding bytes are included in the value. - Performance: The
BINARY
data type can be more efficient thanVARBINARY
for short, fixed-length binary strings because it avoids the overhead of variable-length storage.
Conclusion
The BINARY
data type is useful for storing fixed-length binary strings in MySQL. This chapter covered the syntax for defining BINARY
columns, provided a full example with different binary attributes, and discussed important considerations.