0
0
MySQLquery~10 mins

BLOB and binary types in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - BLOB and binary types
Start: Define column with BLOB or binary type
Insert binary data into column
Store data as raw bytes
Retrieve data as binary
Use or convert data as needed
End
The flow shows how binary data is stored and retrieved using BLOB or binary types in MySQL.
Execution Sample
MySQL
CREATE TABLE images (
  id INT PRIMARY KEY,
  data BLOB
);

INSERT INTO images VALUES (1, LOAD_FILE('cat.jpg'));

SELECT id, LENGTH(data) FROM images;
This code creates a table with a BLOB column, inserts an image file as binary data, and retrieves the size of the stored data.
Execution Table
StepActionEvaluationResult
1Create table with BLOB columnTable 'images' createdTable ready to store binary data
2Insert binary data from file 'cat.jpg'LOAD_FILE reads file bytesRow inserted with binary data in 'data' column
3Select id and LENGTH(data)Calculate length of binary dataReturns id=1 and size of binary data in bytes
4EndNo more actionsExecution complete
💡 All steps executed; binary data stored and retrieved successfully
Variable Tracker
VariableStartAfter Step 2After Step 3Final
images tableempty1 row with id=1 and binary data1 row with id=1 and binary data1 row with id=1 and binary data
data (BLOB)NULLbinary bytes of 'cat.jpg'binary bytes of 'cat.jpg'binary bytes of 'cat.jpg'
LENGTH(data)NULLNULLsize in bytes (e.g. 204800)size in bytes (e.g. 204800)
Key Moments - 2 Insights
Why do we use BLOB instead of VARCHAR for storing images?
BLOB stores raw binary data without character encoding, unlike VARCHAR which stores text. See execution_table step 2 where binary bytes are inserted.
What does LENGTH(data) return for a BLOB column?
It returns the number of bytes stored, not characters. Refer to execution_table step 3 where LENGTH(data) gives the binary size.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of step 3?
AReturns the id and size of binary data in bytes
BReturns the text content of the image
CReturns NULL because data is binary
DReturns the file path of the image
💡 Hint
Check the 'Result' column in execution_table row for step 3
At which step is the binary data actually stored in the table?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when data is inserted
If we change the column type from BLOB to VARCHAR, what would happen to the stored data?
AData size would increase but remain binary
BBinary data would be stored correctly as before
CBinary data would be stored as text, possibly corrupted
DMySQL would reject the insert
💡 Hint
Refer to key_moments about difference between BLOB and VARCHAR storage
Concept Snapshot
BLOB and binary types store raw binary data like images or files.
Use BLOB for variable-length binary data.
Use BINARY/VARBINARY for fixed/variable-length binary data.
Data is stored as bytes, not text.
Retrieve with functions like LENGTH() to get byte size.
Ideal for storing files inside the database.
Full Transcript
This visual execution shows how MySQL handles BLOB and binary types. First, a table with a BLOB column is created. Then, binary data from a file is inserted into the table. The data is stored as raw bytes, not text. When retrieving, functions like LENGTH() return the size in bytes. This process is important because BLOB stores binary data safely, unlike text types which can corrupt binary content. The execution table traces each step from table creation, data insertion, to data retrieval, showing variable states and results. Key moments clarify why BLOB is used and how LENGTH() works on binary data. The quiz tests understanding of these steps and concepts.