
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - BLOB
Many user applications require the storage of different types of data, including text, images, files, and more. Using BLOBs in a MySQL database allows you to store all these types of data within the same database, eliminating the need for a separate file system.
The MySQL BLOB Data Type
The MySQL BLOB (Binary Large Object) data type is used to store binary data, such as images, audio, video, or any other type of binary file. BLOB columns can store variable-length binary data, making it suitable for handling files of various sizes.
Consider an application that collects user information through forms. This information may include personal details, such as name and address, along with image proofs like PAN cards or AADHAR cards. Instead of managing these files separately in a file system, you can store them as BLOBs in a MySQL database.
Syntax
Following is the basic syntax to assign BLOB data type on a table field −
CREATE TABLE table_name (column_name BLOB,...)
Example
Let us consider a basic example to show how to assign BLOB datatype to a table field. Here, we are creating a table named 'demo_table' with two fields "ID" and "DEMO_FILE" −
CREATE TABLE demo_table ( ID INT NOT NULL, DEMO_FILE BLOB );
Following is the output obtained −
Query OK, 0 rows affected (0.01 sec)
You can see the table structure with the following command −
DESC demo_table;
The table obtained is as follows −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | NULL | ||
DEMO_FILE | blob | YES | NULL |
Inserting Data into BLOB Fields
You can insert some values into a database table, by loading a file to the BLOB field using the LOAD_FILE() function. However, before doing so, ensure that the following conditions are met −
File Existence −The file you want to insert must exist on the MySQL server host location. To determine the required location, you can use the secure_file_priv variable with the following command. If the result of this command is not empty, the file to be loaded must be located in that specific directory.
SHOW VARIABLES LIKE secure_file_priv;
Specify Full File Path − When using the LOAD_FILE() function, you must pass the full path of the file as an argument, like '/users/tutorialspoint/file_name.txt'. For Windows users, remember to use double backslashes as escape characters in the path ('//users//tutorialspoint//file_name.txt').
Check 'max_allowed_packet' Value − MySQL Server has a max_allowed_packet variable that determines the maximum allowed file size for loading. To check the value of this variable, you can use the following command −
SHOW VARIABLES LIKE max_allowed_packet;
Ensure that the file size does not exceed the value specified in this variable.
Grant FILE Privileges − Make sure the MySQL user account has FILE privileges granted. To grant file privileges to a user, you can use the following command (usually performed by a user with administrative privileges, such as 'root') −
GRANT FILE ON *.* TO 'username'@'hostname'; FLUSH PRIVILEGES;
File Readability − Lastly, make sure that the file is readable by the MySQL server.
Example
To insert values into a previously created table 'demo_table', you can use the following INSERT query −
INSERT INTO demo_table VALUES(1, LOAD_FILE("C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\sample.txt"));
To verify the insertion, you can retrieve the data from the 'demo_table' using the following query −
SELECT * FROM demo_table;
We can see in the output below, the table contains the hex string of content present in the 'sample.txt' file. You can load any type of files into MySQL, like images, multimedia files, PDF documents etc. −
ID | DEMO_FILE |
---|---|
1 | 0x5468697320697320612073616D706C652066696C65 |
Types of BLOB Datatype
MySQL provides four types of BLOB datatypes, each with varying maximum storage capacities. While they all serve the same purpose of storing binary data, such as images or files, they differ in the maximum size of objects they can accommodate. Here are the four BLOB datatypes −
TINYBLOB − It can store a maximum of 255 bytes, or 255 characters.
BLOB − It can store up to 65,535 (216 - 1) bytes, which is equivalent to 64KB of data.
MEDIUMBLOB − It can store up to 16,777,215 (224 - 1) bytes, or 4GB.
LONGBLOB − It is the largest among these datatypes and can store objects up to 4,294,967,295 bytes (232 - 1), or 4GB.
Let us try to create tables with all types of BLOB datatypes mentioned above.
Creating a Table with TINYBLOB Datatype
In this example, we are creating a table named 'demo_tinyblob' with TINYBLOB datatype on a field −
CREATE TABLE demo_tinyblob (ID INT, DEMO_FIELD TINYBLOB);
Output
Following is the output obtained −
Query OK, 0 rows affected (0.02 sec)
Verification
You can see the table structure with the following command −
DESC demo_tinyblob;
The table obtained is as follows −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | YES | NULL | ||
DEMO_FIELD | tinyblob | YES | NULL |
Creating a Table with MEDIUMBLOB Datatype
Here, we are creating a table named 'demo_mediumblob' with a field of type MEDIUMBLOB using the following query −
CREATE TABLE demo_mediumblob (ID INT, DEMO_FIELD MEDIUMBLOB);
Output
Output of the above code is as follows −
Query OK, 0 rows affected (0.02 sec)
Verification
You can see the table structure with the following command −
DESC demo_mediumblob;
Following is the table obtained −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | YES | NULL | ||
DEMO_FIELD | mediumblob | YES | NULL |
Creating a Table with LONGBLOB Datatype
In this case, we are creating a table named 'demo_longblob' with a field of type LONGBLOB −
CREATE TABLE demo_longblob (ID INT, DEMO_FIELD LONGBLOB);
Output
Following is the result produced −
Query OK, 0 rows affected (0.02 sec)
Verification
You can see the table structure with the command given below −
DESC demo_longblob;
The table produced is as shown below −
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | YES | NULL | ||
DEMO_FIELD | longblob | YES | NULL |