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:\Users\Administrator>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 Files\mysql-5.7.11-winx64\temp>dir
驅動器 D 中的卷是 新加卷
卷的序列號是 0672-4D3B
D:\Program Files\mysql-5.7.11-winx64\temp 的目錄
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 Files\mysql-5.7.11-winx64\temp>
#我們准備兩個文件,一個是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 Files\mysql-5.7.11-winx64\temp>dir
驅動器 D 中的卷是 新加卷
卷的序列號是 0672-4D3B
D:\Program Files\mysql-5.7.11-winx64\temp 的目錄
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 Files\mysql-5.7.11-winx64\temp>
#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:\Users\Administrator>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 Files\mysql-5.7.11-winx64\temp>dir
驅動器 D 中的卷是 新加卷
卷的序列號是 0672-4D3B
D:\Program Files\mysql-5.7.11-winx64\temp 的目錄
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 Files\mysql-5.7.11-winx64\temp>
下面看看更新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字節。
