← Back to MySQL Course | Chapter 3: Data Types | Lesson 7 of 8

BLOB & Binary Types

BLOB Types

BLOB (Binary Large Object) stores raw binary data of variable size — images, audio clips, encrypted payloads — as an opaque byte sequence MySQL doesn't try to interpret or validate.

Example: BLOB Types

sql
CREATE TABLE files (id INT, content BLOB);

VARBINARY Type

VARBINARY is the binary counterpart to VARCHAR: it stores exact byte sequences rather than text, which makes it the right fit for short, fixed-format binary values like hashes or tokens rather than large files.

Example: VARBINARY Type

sql
CREATE TABLE tokens (hash VARBINARY(64));

BLOB Sizes

MySQL offers four BLOB sizes — TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB — differing only in their maximum storage capacity, so you pick based on the largest file size you realistically expect to store.

Example: BLOB Sizes

sql
CREATE TABLE uploads (
  thumbnail TINYBLOB,
  photo BLOB,
  video MEDIUMBLOB,
  archive LONGBLOB
);

Retrieving BLOB Data

Because BLOB data isn't human-readable, functions like HEX() let you inspect its raw bytes as a hexadecimal string, which is often the only practical way to eyeball binary content from a query result.

Example: Retrieving BLOB Data

sql
CREATE TABLE files (content BLOB);
INSERT INTO files VALUES (0x48656C6C6F);
SELECT HEX(content) FROM files;

BLOB Restrictions

BLOB columns can't have a DEFAULT value, and indexing one requires specifying a prefix length (indexing only the first N bytes) since indexing an entire large binary object outright isn't supported.

Example: BLOB Restrictions

sql
-- BLOB columns cannot have a DEFAULT value
CREATE TABLE files (content BLOB, INDEX idx_content (content(100)));

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.