SQL Keywords

SQL LONGBLOB

What is the MySQL LONGBLOB data type?

MySQL data type for storing binary data up to 4 GB (4,294,967,295 bytes).
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL LONGBLOB: Supported: MySQL, MariaDB. Not Supported: PostgreSQL, SQL Server, Oracle, SQLite (use BYTEA, VARBINARY(MAX), BLOB, etc. instead).

SQL LONGBLOB Full Explanation

LONGBLOB is one of four Binary Large Object (BLOB) types available in MySQL (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB). It holds variable-length binary data such as images, audio, video, compressed files, or application-specific binary formats. A single LONGBLOB column can contain up to 4 GB minus 1 byte, making it the largest binary container in MySQL.The column stores raw bytes and performs no character-set conversion. Unlike TEXT types, BLOB types—including LONGBLOB—are compared byte by byte unless a binary collation is overridden with CAST() to CHAR(). When using InnoDB, large LONGBLOB values are stored off-page with only a 20-byte pointer kept in the main row, improving row performance but increasing I/O on access.Because LONGBLOB can exceed the default max_allowed_packet and memory limits, you must tune server parameters for inserts, updates, and replication. Indexing a LONGBLOB directly is impossible; however, you can index a prefix of up to 65,535 bytes, though smaller prefixes (often 255 bytes) are typical for performance reasons. LONGBLOB cannot have a default value other than NULL, and most client libraries stream the value to avoid loading the entire field into memory.

SQL LONGBLOB Syntax

CREATE TABLE table_name (
    column_name LONGBLOB [NULL | NOT NULL]
);

-- Add to an existing table
ALTER TABLE table_name ADD COLUMN column_name LONGBLOB;

SQL LONGBLOB Parameters

Example Queries Using SQL LONGBLOB

-- 1. Create a table with a LONGBLOB column
CREATE TABLE user_files (
    id INT PRIMARY KEY AUTO_INCREMENT,
    filename VARCHAR(255) NOT NULL,
    data LONGBLOB NOT NULL,
    uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. Insert a file using a prepared statement (pseudo-client syntax)
INSERT INTO user_files (filename, data)
VALUES ('logo.png', LOAD_FILE('/path/to/logo.png'));

-- 3. Retrieve the file
SELECT filename, OCTET_LENGTH(data) AS bytes
FROM user_files
WHERE id = 1;

Expected Output Using SQL LONGBLOB

  • Table is created with a nullable or non-nullable LONGBLOB column.
  • One row inserted; data column stores the binary contents of logo.png.
  • Query returns the filename and the byte size of the stored object.

Use Cases with SQL LONGBLOB

  • Persisting user-uploaded images, PDFs, or documents without external object storage
  • Storing large encrypted payloads that must remain in the database for transactional consistency
  • Keeping firmware binaries or software patches tied to version metadata
  • Archiving logs or diagnostic blobs for audit purposes

Common Mistakes with SQL LONGBLOB

  • Attempting to specify a length for LONGBLOB (e.g., LONGBLOB(1024)); size modifiers are invalid.
  • Forgetting to raise max_allowed_packet, causing Packet too large errors on insert.
  • Treating LONGBLOB as text and expecting character-set conversions.
  • Trying to create an index on the entire LONGBLOB column instead of a length-limited prefix.
  • Assuming LONGBLOB can have a non-NULL default value.

Related Topics

BLOB, TINYBLOB, MEDIUMBLOB, LONGTEXT, VARBINARY, BYTEA, LOB

First Introduced In

MySQL 3.23

Frequently Asked Questions

How large can a LONGBLOB column be?

A LONGBLOB can store up to 4,294,967,295 bytes, which is effectively 4GB.

Is LONGBLOB slower than other BLOB types?

Larger objects mean more disk I/O and network transfer, so accessing LONGBLOB values can be slower. For objects under 16MB, prefer MEDIUMBLOB or smaller.

Can I set a default value for a LONGBLOB column?

No. MySQL only allows NULL or no default for BLOB types, including LONGBLOB.

How do I tune MySQL for LONGBLOB inserts?

Increase max_allowed_packet and, if using replication, sync_binlog and slave_max_allowed_packet to accommodate the largest expected object size.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!