https://blog.csdn.net/q3dxdx/article/details/51014357
BLOB,二进制大对象(字节流)。可以用来存储图片,声音和视频等二进制文件。没有字符集的说法。
TEXT,文本大对象(字符流)。可以用来存储大量的字符串,可以理解为超大的char或者varchar类型。由于是存储字符,所以有字符集的说法。
并且blob和text类型是无法设置默认值的。并且必要时要增大max_allowed_packet的值以适应该数据类型。
根据mysql的个性,blob和text又细分为:(存储限制来自网上摘录)
tinyblob,tinytext,最大存储限制255字节;
blob,text,最大存储限制65k(是真的吗?);
mediumblob,mediumtext,最大存储限制16M;
longblob,longtext,最大存储限制4G。
下面根据实验并以blob为例,来演示存储限制:
C:UsersAdministrator>mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 10
Server version: 5.7.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> use test1
Database changed
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql> drop table t1;
Query OK, 0 rows affected (0.28 sec)
mysql> CREATE TABLE t1 (
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> tiny_blob TINYBLOB,
-> b_blob BLOB,
-> medium_blob MEDIUMBLOB,
-> long_blob LONGBLOB
-> );
Query OK, 0 rows affected (0.36 sec)
mysql> desc t1;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tiny_blob | tinyblob | YES | | NULL | |
| b_blob | blob | YES | | NULL | |
| medium_blob | mediumblob | YES | | NULL | |
| long_blob | longblob | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> insert into t1(id) select 1;
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+-----------+--------+-------------+-----------+
| id | tiny_blob | b_blob | medium_blob | long_blob |
+----+-----------+--------+-------------+-----------+
| 1 | NULL | NULL | NULL | NULL |
+----+-----------+--------+-------------+-----------+
1 row in set (0.00 sec)
mysql>
#先建一个表t1,各种blob类型均有。
然后查看参数max_allowed_packet的大小:
mysql> select @@global.max_allowed_packet;
+-----------------------------+
| @@global.max_allowed_packet |
+-----------------------------+
| 8388608 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select 8388608/1024;
+--------------+
| 8388608/1024 |
+--------------+
| 8192.0000 |
+--------------+
1 row in set (0.00 sec)
mysql> select 8388608/1024/1024;
+-------------------+
| 8388608/1024/1024 |
+-------------------+
| 8.00000000 |
+-------------------+
1 row in set (0.00 sec)
mysql>
#8M
下面测试tinyblob的存储限制,看是否只能存储最大255字节的文件:
D:Program Filesmysql-5.7.11-winx64 emp>dir
驱动器 D 中的卷是 新加卷
卷的序列号是 0672-4D3B
D:Program Filesmysql-5.7.11-winx64 emp 的目录
2016/03/30 14:58 <DIR> .
2016/03/30 14:58 <DIR> ..
2016/03/30 14:58 255 test.255.file #255字节
2016/03/30 14:58 256 test.256.file #比255字节多1字节
2 个文件 511 字节
2 个目录 64,739,741,696 可用字节
D:Program Filesmysql-5.7.11-winx64 emp>
#我们准备两个文件,一个是255字节,另一个是256字节。
现在我们更新tinyblob字段,看是否成功:
mysql> UPDATE t1
-> SET t1.tiny_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.255.file')
-> WHERE t1.id=1;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> UPDATE t1
-> SET t1.tiny_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.256.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'tiny_blob' at row 1
mysql>
很明显,根据上面得出,tinyblob类型的最大存储限制是255字节。
继续看看blob的存储限制是否是65k:
先准备需要的文件
D:Program Filesmysql-5.7.11-winx64 emp>dir
驱动器 D 中的卷是 新加卷
卷的序列号是 0672-4D3B
D:Program Filesmysql-5.7.11-winx64 emp 的目录
2016/03/30 15:28 <DIR> .
2016/03/30 15:28 <DIR> ..
2016/03/30 15:28 65,535 test.65535.file #比64K少1字节
2016/03/30 15:28 65,536 test.65536.file #65536字节,也就是刚刚64K
2016/03/30 15:28 65,537 test.65537.file #比64K多1字节
2016/03/30 15:26 66,559 test.65559.file #比65K少1字节
2016/03/30 15:25 66,560 test.65560.file #66560字节,也就是刚刚65K
2016/03/30 15:25 66,561 test.65561.file #比65K多1字节
6 个文件 396,288 字节
2 个目录 64,777,019,392 可用字节
D:Program Filesmysql-5.7.11-winx64 emp>
#ok,文件已经准备好。来看看update是否成功:
mysql> UPDATE t1
-> SET t1.b_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.65560.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'b_blob' at row 1
mysql>
#失败了,根据网上摘录资料显示,blob类型的最大存储限制是65K,而这里演示出实际上65k是无法存储的。已经超过了最大限制。
那么65559字节呢,能存储进去吗?
mysql> UPDATE t1
-> SET t1.b_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.65559.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'b_blob' at row 1
mysql>
#可见,65559字节也是无法存储的。那么blob到底能存储多少字节呢?答案是65536-1=65535字节,也就是64K少1字节。
来看演示:
mysql> UPDATE t1
-> SET t1.b_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.65537.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'b_blob' at row 1 #失败
mysql>
mysql> UPDATE t1
-> SET t1.b_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.65536.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'b_blob' at row 1 #失败
mysql>
mysql> UPDATE t1
-> SET t1.b_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.65535.file')
-> WHERE t1.id=1;
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0 #成功
mysql>
可见,blob类型的最大存储限制是65535字节,就是64K少1字节。而不是网上说的65K。
继续来看看mediumblob类型,是16M吗?
先更改max_allowed_packet参数为256M,以满足16M:
mysql> set @@global.max_allowed_packet=256*1024*1024;Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
C:UsersAdministrator>mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.7.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select @@max_allowed_packet;
+----------------------+
| @@max_allowed_packet |
+----------------------+
| 268435456 | #256M,够大了
+----------------------+
1 row in set (0.00 sec)
mysql>
成功更改。
准备文件:
D:Program Filesmysql-5.7.11-winx64 emp>dir
驱动器 D 中的卷是 新加卷
卷的序列号是 0672-4D3B
D:Program Filesmysql-5.7.11-winx64 emp 的目录
2016/03/30 15:49 <DIR> .
2016/03/30 15:49 <DIR> ..
2016/03/30 15:48 16,777,215 test.16777215.file #比16M少1字节
2016/03/30 15:44 16,777,216 test.16777216.file #16777216字节,也就是刚刚16M
2016/03/30 15:48 16,777,217 test.16777217.file #比16M多1字节
3 个文件 50,331,648 字节
2 个目录 64,727,027,712 可用字节
D:Program Filesmysql-5.7.11-winx64 emp>
下面看看更新mediumblob是否成功:
mysql> use test1
Database changed
mysql> UPDATE t1
-> SET t1.medium_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.16777217.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'medium_blob' at row 1 #比16M多1字节,更新失败
mysql> UPDATE t1
-> SET t1.medium_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.16777216.file')
-> WHERE t1.id=1;
ERROR 1406 (22001): Data too long for column 'medium_blob' at row 1 #刚刚16M,更新也失败
mysql> UPDATE t1
-> SET t1.medium_blob=LOAD_FILE('D:/Program Files/mysql-5.7.11-winx64/temp/test.16777215.file')
-> WHERE t1.id=1;
Query OK, 1 row affected (4.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0 #比16M少1字节,更新成功
mysql>
所以,mediumblob类型的最大存储限制不是16M,而是16M少1字节。
那么longblob呢?
个人目前无法测试,毕竟64位下max_allowed_packet参数的最大值是1G。所以4G的longblob类型无法测试。
但我告诉你,是4G少1字节。
所以,
tinyblob,tinytext,最大存储限制为28-1=255字节;
blob,text,最大存储限制为 216-1=64k-1字节;
mediumblob,mediumtext,最大存储限制为 224-1=16M-1字节;
longblob,longtext,最大存储限制为232-1=4G-1字节。