DEV Community

DbVisualizer
DbVisualizer

Posted on • Originally published at dbvis.com

BLOB Data Type: Everything You Can Do With It

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.

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:

ALTER TABLE users ADD COLUMN profile_picture BLOB;
Enter fullscreen mode Exit fullscreen mode

Inspecting BLOB data in DbVisualizer

Inspecting BLOB data 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:

SELECT id, nickname, profile_picture
FROM users
WHERE id = 1
Enter fullscreen mode Exit fullscreen mode

Inspecting BLOB data in DbVisualizer

Inspecting BLOB data 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!

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 is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

Top comments (0)