BLOB is data type which hold large binary object. MySQL has TINYBLOB, BLOB, MEDIUM BLOB, and LARGEBLOB. These were based on the size of the data that it can hold.
And BLOB is referring to as binary strings or byte strings. On the other hand TEXT treated as character strings. BLOB doesn’t have a character set. Comparisons, Sorting will do on the numeric values of the bytes in the column. Using SQL strict mode value will be truncated to match with maximum column length.
Except few things you can treat BLOB as VARBINARY. For indexes you need to specify prefix length and there is no DEFAULT value for BLOB.
-----------------------------------------------------------------------
mysql> CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
Query OK, 0 rows affected (0.19 sec)
mysql> desc test;
+----------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------+------+-----+---------+-------+
| blob_col | blob | YES | MUL | NULL | |
+----------+------+------+-----+---------+-------+
1 row in set (0.05 sec)
mysql> CREATE TABLE test2 (blob_col BLOB, INDEX(blob_col));
ERROR 1170 (42000): BLOB/TEXT column 'blob_col' used in key specification without a key length
-----------------------------------------------------------------------
Note – prefix can be up to 1000 bytes long. On innodb it is 767 bytes. Importantly prefix length were calculated in bytes but in CREATE TABLE statements it is use no of characters.
-----------------------------------------------------------------------
mysql> CREATE TABLE test2 (blob_col BLOB, INDEX(blob_col(10000)));
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> desc test2;
+----------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------+------+-----+---------+-------+
| blob_col | blob | YES | MUL | NULL | |
+----------+------+------+-----+---------+-------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------
Below is example of BLOB field usagein java.
Write to Database – insert “fileName” to the MySQL database.
connection.setAutoCommit(false);
//I have disable the auto commit
File file = new File(fileName);
fis = new FileInputStream(file);
preparedStatement = connection.prepareStatement(INSERT_PICTURE);
preparedStatement.setBinaryStream(1, fis, (int) file.length());
preparedStatement.executeUpdate();
System.out.println("INSERT Query executed");
connection.commit();
-----------------------------------------------------------------------
Read from the Database
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectQuery);
System.out.println("SELET Query executed");
System.out.println("Going to write the files to the disk");
System.out.println("---------------------------------");
while (rs.next()) {
String fileName = "fileNameOut.jpg";
File image = new File(fileName);
fos = new FileOutputStream(image);
byte[] buffer = new byte[1];
is = rs.getBinaryStream(1);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
System.out.println("File " + fileName + " written to the disk");
-----------------------------------------------------------------------
Table definition is,
CREATE TABLE `picture` (
`picture_blob` longblob,
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
-----------------------------------------------------------------------
It’s better to have good understand about max_allowed_packet parameter when working with BLOB columns. If the client program encounter “Packet too large” or “Lost connection to MySQL server during query” better check this property. And may result in strange response specially when deal with bigger BLOB columns. Keep in mind that BLOB was larger than 100MB, and then MySQL need 3 times the size of memory allocated for BLOB processing.
mysql> show variables like "max_allowed_packet";
+--------------------+---------+
| Variable_name | Value |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
TINYBLOB – 255bytes
BLOB – 65535bytes
MEDIUMBLOB – 16777215bytes
LONGBLOB – 4GB
No comments:
Post a Comment