Bird
Raised Fist0
MySQLquery~5 mins

BLOB and binary types in MySQL

Choose your learning style10 modes available
Introduction
BLOB and binary types let you store data like pictures, music, or any files in a database.
When you want to save a photo uploaded by a user.
When storing audio or video files inside the database.
When you need to keep encrypted or compressed data.
When saving any file or data that is not plain text.
When you want to keep small files directly in your database for easy access.
Syntax
MySQL
CREATE TABLE table_name (
  column_name BLOB,
  column_name BINARY(n),
  column_name VARBINARY(n)
);
BLOB stores variable-length binary data up to 65,535 bytes.
BINARY(n) stores fixed-length binary data of size n bytes.
VARBINARY(n) stores variable-length binary data up to n bytes.
Examples
Creates a table to store photos as BLOBs, which can hold variable-length binary data.
MySQL
CREATE TABLE images (
  id INT PRIMARY KEY,
  photo BLOB
);
Stores files up to 255 bytes in size using VARBINARY.
MySQL
CREATE TABLE files (
  id INT PRIMARY KEY,
  file_data VARBINARY(255)
);
Stores fixed-length binary data of exactly 16 bytes, useful for things like encryption keys.
MySQL
CREATE TABLE keys (
  id INT PRIMARY KEY,
  key_data BINARY(16)
);
Sample Program
This creates a table to store documents with their content as BLOB. It inserts a file from the server and then shows the document info with content size.
MySQL
CREATE TABLE documents (
  id INT PRIMARY KEY,
  doc_name VARCHAR(50),
  doc_content BLOB
);

INSERT INTO documents (id, doc_name, doc_content) VALUES
(1, 'example.txt', LOAD_FILE('/var/lib/mysql-files/example.txt'));

SELECT id, doc_name, LENGTH(doc_content) AS content_size FROM documents;
OutputSuccess
Important Notes
BLOB types are good for storing large binary data but can slow down queries if overused.
LOAD_FILE() reads a file from the server's file system into a BLOB column (requires file permissions).
Use BINARY for fixed-size data to save space and VARBINARY for variable-size binary data.
Summary
BLOB and binary types store non-text data like images or files.
BLOB is variable-length and can hold large data, BINARY is fixed-length, VARBINARY is variable-length.
Use these types when you want to keep files or binary info inside your database.

Practice

(1/5)
1. Which MySQL data type is best suited for storing large binary files like images or videos?
easy
A. BLOB
B. VARCHAR
C. TEXT
D. INT

Solution

  1. Step 1: Understand data types for binary storage

    BLOB is designed to store large binary data such as images or videos.
  2. Step 2: Compare with other types

    VARCHAR and TEXT store text data, INT stores numbers, so they are not suitable for binary files.
  3. Final Answer:

    BLOB -> Option A
  4. Quick Check:

    Large binary data = BLOB [OK]
Hint: Large binary data? Choose BLOB type [OK]
Common Mistakes:
  • Choosing VARCHAR or TEXT for binary data
  • Using INT for storing files
  • Confusing BLOB with text types
2. Which of the following is the correct way to declare a fixed-length binary column of 16 bytes in MySQL?
easy
A. BLOB(16)
B. VARBINARY(16)
C. BINARY(16)
D. TEXT(16)

Solution

  1. Step 1: Identify fixed-length binary type

    BINARY(n) stores fixed-length binary data of n bytes.
  2. Step 2: Check other options

    VARBINARY is variable-length, BLOB and TEXT are for large data, not fixed-length binary.
  3. Final Answer:

    BINARY(16) -> Option C
  4. Quick Check:

    Fixed-length binary = BINARY(n) [OK]
Hint: Fixed-length binary? Use BINARY(size) [OK]
Common Mistakes:
  • Using VARBINARY for fixed-length data
  • Trying to specify size with BLOB or TEXT
  • Confusing TEXT with binary types
3. Given the table files(id INT, data BLOB), what will the following query return?
SELECT LENGTH(data) FROM files WHERE id = 1;
medium
A. The first byte of the 'data' column
B. The size in bytes of the binary data stored in 'data' for id 1
C. The number of characters in the 'data' column as text
D. An error because LENGTH() does not work on BLOB

Solution

  1. Step 1: Understand LENGTH() on BLOB

    LENGTH() returns the number of bytes in the binary data stored in a BLOB column.
  2. Step 2: Analyze the query

    The query selects LENGTH(data) for the row where id = 1, so it returns the size in bytes of that binary data.
  3. Final Answer:

    The size in bytes of the binary data stored in 'data' for id 1 -> Option B
  4. Quick Check:

    LENGTH(BLOB) = byte size [OK]
Hint: LENGTH() on BLOB returns byte size [OK]
Common Mistakes:
  • Thinking LENGTH() counts characters in binary
  • Expecting an error from LENGTH() on BLOB
  • Confusing LENGTH() with SUBSTRING()
4. You wrote this table definition:
CREATE TABLE images (id INT, img BINARY(255));

But when inserting images larger than 100 bytes, data gets truncated. What is the problem?
medium
A. BINARY cannot store binary data, use TEXT instead
B. The id column should be AUTO_INCREMENT
C. You must use VARCHAR for binary data
D. BINARY(255) is fixed-length and pads or truncates data to 255 bytes

Solution

  1. Step 1: Understand BINARY(n) behavior

    BINARY(n) stores fixed-length binary data of exactly n bytes, padding or truncating as needed.
  2. Step 2: Identify truncation cause

    If inserted data is larger than 255 bytes, it will be truncated to fit fixed length.
  3. Final Answer:

    BINARY(255) is fixed-length and pads or truncates data to 255 bytes -> Option D
  4. Quick Check:

    Fixed-length truncates data > size [OK]
Hint: Fixed-length binary truncates data exceeding size [OK]
Common Mistakes:
  • Thinking BINARY can store variable-length data
  • Using VARCHAR for binary data
  • Ignoring fixed-length padding/truncation
5. You want to store user profile pictures in a MySQL table with columns id INT and picture. You expect pictures up to 2MB. Which data type should you choose for picture and why?
hard
A. MEDIUMBLOB, because it supports up to 16MB of binary data
B. BINARY(2000000), because fixed-length is faster
C. VARCHAR(2000000), because it stores variable-length data
D. TEXT, because pictures are text data

Solution

  1. Step 1: Check size requirements

    Pictures up to 2MB require a BLOB type that can hold at least 2 million bytes.
  2. Step 2: Choose suitable BLOB type

    MEDIUMBLOB supports up to 16MB, enough for 2MB pictures. BINARY(2000000) is fixed-length and too large, VARCHAR and TEXT are for text, not binary.
  3. Final Answer:

    MEDIUMBLOB, because it supports up to 16MB of binary data -> Option A
  4. Quick Check:

    Large binary data > 64KB = MEDIUMBLOB [OK]
Hint: Use MEDIUMBLOB for binary data > 64KB [OK]
Common Mistakes:
  • Using fixed-length BINARY for large data
  • Choosing VARCHAR or TEXT for binary files
  • Underestimating BLOB size limits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard