BLOB

BLOB Data Type: Everything You Can Do With It

Author: Antonello Zanini
Length: 8 MINS
Type: Guide
Published: 2023-03-13
Intro
Let's find out everything you need to know about the BLOB data type. You will learn what BLOB is, why databases have a BLOB data type, and what types of data it can store.
Tools used in the tutorial
Tool
Description
DbVisualizer
Top rated database management tool and sql client
MySQL 8.0+
The MySQL database version 8 or later

In databases, the term BLOB (Binary Large Object) refers to a set of binary data saved as a single entity. Specifically, images, videos, spreadsheets, PDFs, and executable files can all be stored in cells of type BLOB. Thus, the BLOB data type allows multimedia, text, or any other type of file to be stored in a database.

As you can imagine, BLOB is a powerful data type, but it also raises questions about performance and storage usage. What is a BLOB? Why does SQL provide a BLOB data type? What benefits can it bring to your database? When to use it? These are all questions that will be answered in this article!

Follow this complete guide and become an expert on the BLOB data type!

What Is a BLOB?

BLOB stands for “Binary Large Object” and represents a database type to store binary data. Specifically, examples of BLOBs (Binary Large Objects) are complex files such as images, video, and audio. In other words, the BLOB data type is used in databases to store multimedia files and other types of files that are too large to be saved in regular fields.

BLOB objects generally represent complex, large, and heavy files. For this reason, BLOBs are not easy to deal with. So, not all database technologies support the BLOB data type. In RDBMSs that support BLOBs, you can add a BLOB column to a table as you would for any other data type.

In MySQL, you can add a profile_picture BLOB column to the users table with:

1
ALTER TABLE users ADD COLUMN profile_picture BLOB;
Launching the query to add a BLOB column in DbVisualizer.

↑  Launching the query to add a BLOB column in DbVisualizer

Let’s now better understand what a BLOB is through some examples.

Examples of BLOB

Binary Large Objects can store structured data like SQL backups, semi-structured data like CSV, and unstructured data like multimedia files. However, a BLOB object is generally used to store unstructured data in binary format.

Common examples of files stored in a BLOB data type field include:

  • Images (JPG, JPEG, PNG, GIF, HEIC, WEBP, raw binary data)
  • Videos (MP4, AVI, MOV, MKV)
  • Audio files (MP3, WAV, AAC)
  • Documents (PDF, TXT, CSV, DOCX, XLSX)
  • Archives (ZIP, RAR)
  • Executable files (EXE, MSI)
  • Backups (SQL, BAK)

Why Is BLOB Used in SQL?

As you have just learned, a column of type BLOB can contain any type of file. At the same time, BLOBs are typically used to store media files directly in a database record. For example, you can use a BLOB column to store a user's profile picture. In this way, you can get both the user's profile data and their image with a single query:

1
SELECT id, nickname, profile_picture
2
FROM users
3
WHERE id = 1
Query to retrieve user info and profile image in DbVisualizer.

↑  Retrieving all info of one user, including their profile image, with a single query in DbVisualizer

As you can see, the profile_picture column contains binary data. Double-click on it and DbVisualizer will open the following popup:

Inspecting BLOB data in DbVisualizer.

↑  Inspecting BLOB data in DbVisualizer

Here, you can notice that the cell contains a JPEG image in Base64 format.

Keep in mind that data saved in BLOB cells can make the result of your queries much heavier. For this reason, BLOB data should be retrieved only when truly necessary. So, even though BLOB columns will be queried sparingly, they still play a relevant role because they allow you to store important data for your business directly in the database.

Storing a BLOB vs. Storing as a Path

In recent years, the quality of multimedia files has increased exponentially. As a result, multimedia files have become heavier and can now take up to several GBs. Saving such large data to a database means increasing the space required for storage. This costs money and can lead to a general slowdown of the database.

For these reasons, a common alternative to BLOBs is to save files in cloud storage. In this case, you do not store the entire file in binary format in the database, but the path to the respective cloud storage object.

Let’s now look at the pros and cons of the two options. This will help to understand when to use the BLOB data type and what you can actually do with BLOBs.

BLOB Option

Pros

  • Your files are protected by database constraints and transactions.
  • Since the files are stored in the database, they can be backed up and recovered in the same way as other data. This eliminates the need for a backup system for your files.
  • You can use the database user management capabilities to ensure security and grant access privileges to files.

Cons

  • BLOBs can only be written and read synchronously. This affects database performance, especially when dealing with large files.
  • Reading and writing BLOB files involves sending binary data between the server and the database, increasing the network traffic accordingly.
  • To process a BLOB file, you need to download it locally, update it, and then upload it back to the database. This is cumbersome and inefficient.

Path Option

Pros

  • You can process and directly operate on files with clients and command-line tools, without having to download and upload them again.
  • You can share files between different applications and databases. Also, you can make them publicly available via URL.
  • Increasing your cloud storage by one GB is usually easier, faster, and cheaper than doing the same in a database. Also, storing data in cloud storage makes your database lighter, faster, and easier to back up.

Cons

  • If your cloud storage provider does not offer backup features, you may have to implement a custom backup system for your files.
  • Dealing with file access privileges may not be easy, especially if the files are shared between applications with different goals.
  • In cloud storage, you generally have less control over the underlying infrastructure, security, and management of your files than in a database.

Conclusion

In this article, you saw the definition of BLOB and what benefits BLOBs can bring to a database. Specifically, you learned that BLOB is a data type for storing binary files in a database. So, you can use BLOB to add multimedia files such as images and videos to a database. As you understood here, this is one of the most common use cases for BLOBs. Since dealing with binary files is complex, you need a database client that allows you to view, export, and import BLOBs visually and easily, such as DbVisualizer! Download it for free now!

DbVisualizer SQL Client.

FAQ About BLOBs

What is the difference between a BLOB and a file?

The main difference between a BLOB and a file is where and how the data they contain is stored. BLOBs are stored and accessed within a DBMS (Database Management system). On the other hand, files are stored and accessed in a file system.

How is BLOB stored in SQL?

In SQL, BLOBs (Binary Large Objects) are stored as binary data in a BLOB column of a table. Behind the scene, the DBMS generally stores BLOB data as a binary string. A binary string, also known as a byte string, is nothing more than a sequence of bytes. This is a non-human readable data format that computers use to store data.

What Databases Support BLOB?

MySQL, MariaDB, Oracle, SQL Server, and PostgreSQL all support the storage of binary data. At the same time, only Oracle, MySQL, and MariaDB support the BLOB data type. In SQL Server, the data types for BLOB data are BINARY and VARBINARY. In PostgreSQL, you can store BLOBs through the BYTEA data type. Generally, all major database technologies support the storage of BLOBs, although not all of them have a data type called BLOB.

What are the types of BLOBs?

In MySQL and MariaDB, the types of BLOB data are:

  • TINYBLOB: Can store up to 255 bytes of data.
  • BLOB: Can store up to 65,535 bytes of data.
  • MEDIUMBLOB: Can store up to 16,777,215 bytes of data.
  • LONGBLOB: Can store up to 4,294,967,295 bytes of data.

In Oracle, there is only one type of BLOB:

  • BLOB: Can store binary data up to 4 GB.

What are some optimizations for the BLOB type?

There are a few optimization tips you can apply when it comes to storing data in BLOB cells:

  • Compress large BLOBs before storing them to save space.
  • Consider splitting BLOB columns into a separate table to reduce memory usage for queries that do not need the BLOB data.
  • For better performance, store BLOB-specific tables on a separate storage device or database instance.
  • Consider using a binary VARCHAR column instead of an equivalent BLOB column for specific use cases.
About the author
Antonello Zanini.
Antonello Zanini
Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.
SIGN UP TO RECEIVE THE TABLE'S ROUNDUP
More from the table
TITLE
AUTHOR
Gayatri Sachdeva
TAGS
DBA'S
DronaHQ
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
TITLE
AUTHOR
Bonnie
TAGS
Generation
TITLE
AUTHOR
Bonnie
TAGS
Joins
TITLE
AUTHOR
Igor Bobriakov
TAGS
MySQL
Security
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Operators
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
NULL
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Reserved words
TITLE
AUTHOR
Igor Bobriakov
TAGS
Oracle
TITLE
AUTHOR
Antonello Zanini
TAGS
DELETE
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
MySQL
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
JSON
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
Null
TITLE
AUTHOR
Antonello Zanini
TAGS
Driver
JDBC
ODBC
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
Connection
TITLE
AUTHOR
Lukas Vileikis
TAGS
Deduplication
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
SQL
Functions
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Math
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Docker
MySQL
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Views
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Index
TITLE
AUTHOR
Bonnie
TAGS
BigQuery
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
Join
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
PostgreSQL
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
PrestoDb
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Ansible
Automation
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
PostgreSQL
NoSQL
JSON
TITLE
AUTHOR
Igor Bobriakov
TAGS
Oracle
Data types
TITLE
AUTHOR
TheTable
TAGS
ElasticSearch
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Security
TITLE
AUTHOR
Lukas Vileikis
TAGS
Language
Design
TITLE
AUTHOR
Lukas Vileikis
TAGS
CRUD
DELETE
TITLE
AUTHOR
Lukas Vileikis
TAGS
CRUD
UPDATE
TITLE
AUTHOR
Lukas Vileikis
TAGS
CRUD
SELECT
TITLE
AUTHOR
Lukas Vileikis
TAGS
CRUD
INSERT
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
PostgreSQL
TITLE
AUTHOR
TheTable
TAGS
Bug
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Daemon
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Partitions
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
Migration
MySQL
PostgreSQL
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
hstore
TITLE
AUTHOR
TheTable
TAGS
SQL
TITLE
AUTHOR
Igor Bobriakov
TAGS
SQL server
Security
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Visualize
TITLE
AUTHOR
TheTable
TAGS
MySQL
TITLE
AUTHOR
Lukas Vileikis
TAGS
SQL
Security
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
TITLE
AUTHOR
TheTable
TAGS
PostgreSQL
Docker
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Connection
TITLE
AUTHOR
Lukas Vileikis
TAGS
Performance
TITLE
AUTHOR
Lukas Vileikis
TAGS
Security
TITLE
AUTHOR
Antonello Zanini
TAGS
Columns
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Performance
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
SQL
TITLE
AUTHOR
Lukas Vileikis
TAGS
Performance
Indexes
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Subquery
TITLE
AUTHOR
Lukas Vileikis
TAGS
Performance
TITLE
AUTHOR
Lukas Vileikis
TAGS
ACID
TITLE
AUTHOR
Lukas Vileikis
TAGS
ALTER TABLE
TITLE
AUTHOR
TheTable
TAGS
MySQL
Ports
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Security
TITLE
AUTHOR
Lukas Vileikis
TAGS
ACID
MySQL
Security
TITLE
AUTHOR
Antonello Zanini
TAGS
BLOB
TITLE
AUTHOR
TheTable
TAGS
Foreign Key
PostgreSQL
TITLE
AUTHOR
Leslie S. Gyamfi
TAGS
PostgreSQL
Concurrency
TITLE
AUTHOR
Lukas Vileikis
TAGS
Security
Encryption
TITLE
AUTHOR
Lukas Vileikis
TAGS
Security
TITLE
AUTHOR
Bonnie
TAGS
Security
PostgreSQL
TITLE
AUTHOR
Antonello Zanini
TAGS
Subquery
TITLE
AUTHOR
Antonello Zanini
TAGS
Transactions
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Data structures
TITLE
AUTHOR
Antonello Zanini
TAGS
MySQL
TITLE
AUTHOR
Lukas Vileikis
TAGS
SSH
TITLE
AUTHOR
Antonello Zanini
TAGS
Stored procedure
MySQL
TITLE
AUTHOR
Antonello Zanini
TAGS
Triggers
TITLE
AUTHOR
Igor Bobriakov
TAGS
Microsoft SQL Server
Optimization
TITLE
AUTHOR
Bonnie
TAGS
PostreSQL
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
Reusable queries
TITLE
AUTHOR
Antonello Zanini
TAGS
BIG Data
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Security
TITLE
AUTHOR
TheTable
TAGS
Beginner
SQL
TITLE
AUTHOR
Ochuko Onojakpor
TAGS
CRUD
SQL Transactions
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
Security
TITLE
AUTHOR
Antonello Zanini
TAGS
PostgreSQL
JSON
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
InnoDB
ibdata1
TITLE
AUTHOR
Lukas Vileikis
TAGS
MySQL
TITLE
AUTHOR
Scott A. Adams
TAGS
Filter
TITLE
AUTHOR
Scott A. Adams
TAGS
SQLite
TITLE
AUTHOR
Scott A. Adams
TAGS
Excel
Export
TITLE
AUTHOR
Scott A. Adams
TAGS
ERD
Join

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.