1. 程式人生 > >MySQL資料型別之BLOB與TEXT及其最大儲存限制

MySQL資料型別之BLOB與TEXT及其最大儲存限制

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位元組。