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)
);
| Type | Maximum length |
|---|---|
| TINYBLOB | 255 bytes |
| BLOB | 65,535 bytes |
| MEDIUMBLOB | 16,777,215 bytes |
| LONGBLOB | 4,294,967,295 bytes |
BLOB values are compared as binary strings. The corresponding TEXT families store character strings and use character sets and collations.
SELECT * for large tables.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_name | size_bytes | hex_value |
|---|---|---|
| hello.txt | 5 | 48656C6C6F |
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.
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.