BLOB Data Types in MySQL

BLOB means binary large object. BLOB columns store binary strings (raw bytes), which makes them suitable for binary content when keeping that content inside the database is appropriate.

CREATE TABLE document_store (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    file_name VARCHAR(255) NOT NULL,
    file_data LONGBLOB NOT NULL,
    PRIMARY KEY (id)
);
On this page — Show page sectionsHide page sections

BLOB families

TypeMaximum length
TINYBLOB255 bytes
BLOB65,535 bytes
MEDIUMBLOB16,777,215 bytes
LONGBLOB4,294,967,295 bytes

BLOB values are compared as binary strings. The corresponding TEXT families store character strings and use character sets and collations.

Avoid selecting large BLOB columns when the application does not need them. Prefer an explicit column list instead of SELECT * for large tables.

Insert a small binary value and inspect it

For a reproducible example, store the five bytes representing the text Hello. UNHEX() converts hexadecimal characters into bytes; it is used here just to demonstrate storage, not to upload a real file.

INSERT INTO document_store (file_name, file_data)
VALUES ('hello.txt', UNHEX('48656C6C6F'));

SELECT file_name, OCTET_LENGTH(file_data) AS size_bytes,
       HEX(file_data) AS hex_value
FROM document_store
ORDER BY id;

Output for an initially empty document_store table

file_namesize_byteshex_value
hello.txt548656C6C6F

BLOB vs TEXT: when should each be used?

Use TEXT for character content that needs character-set and collation behavior. Use a BLOB family for bytes that are not meant to be interpreted as text. In applications serving files, also consider whether storing the file in managed object storage and only its reference in MySQL is a better fit.

Choose a BLOB size and query efficiently

The BLOB family varies in capacity; actual usable sizes can also be constrained by server settings, packet limits and application resources. Retrieve only the columns you need for list screens:

SELECT id, file_name, OCTET_LENGTH(file_data) AS size_bytes
FROM document_store
ORDER BY id;

Do not fetch file_data for every row unless those bytes are needed. For actual uploads, pass the binary data as a bound parameter. The PHP PDO BLOB example demonstrates an application-side approach.

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer