一起聊聊MySQL中blob和text資料類型(範例詳解)

發布: 2022-01-13 15:42:00
轉載
2724 人瀏覽過

本篇文章我們來看mysql中的blob和text資料類型,blob是一個可以儲存二進位檔案的容器,text類型同char、varchar 類似,都可用於儲存字串,下面我們就一起來看一下這兩種資料類型的相關知識,希望對大家有幫助。

一起聊聊MySQL中blob和text資料類型(範例詳解)

  1. blob 類型

##blob(binary large object) 是一個可以儲存二進位檔案的容器,主要用於儲存二進位大對象,例如可以儲存圖片,音視頻等檔案。依照可儲存容量大小不同來分類,blob 類型可分為以下四種:

一起聊聊MySQL中blob和text資料類型(範例詳解)

#其中最常用的就是blob 欄位類型了,最多可儲存65KB 大小的數據,一般可用於儲存圖示或logo 圖片。不過資料庫不適合直接儲存圖片,如果有大量儲存圖片的需求,請使用物件儲存或檔案存儲,資料庫中可以儲存圖片路徑來呼叫。

  2. text 類型

text 類型同char、varchar 類似,都可用於儲存字串,一般情況下,遇到存儲長文本字串的需求時可以考慮使用text 類型。依照可儲存大小區分,text 類型同樣可分為以下四種:

一起聊聊MySQL中blob和text資料類型(範例詳解)

不過在日常場景中,儲存字串還是盡量用varchar ,只有要儲存長文本資料時,可以使用text 類型。對比 varchar ,text 類型有以下特點:

  • text 類型無須指定長度。

  • 若資料庫未啟用嚴格的 sqlmode ,當插入的值超過 text 資料列的最大長度時,則該值會被截斷插入並產生警告。

  • text 類型欄位不能有預設值。

  • varchar 可直接建立索引,text 欄位建立索引要指定前多少個字元。

  • text 類型檢索效率比 varchar 低。

下面我們來具體測試下text 類型的使用方法:

# 创建测试表 字符集是 utf8
mysql> show create table tb_text\G
*************************** 1. row ***************************
       Table: tb_text
Create Table: CREATE TABLE `tb_text` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `a` tinytext,
  `b` text,
  `c` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
# 创建索引测试 发现text类型必须指定前缀长度
mysql> alter table tb_text add index idx_a (a);
ERROR 1170 (42000): BLOB/TEXT column 'a' used in key specification without a key length
mysql> alter table tb_text add index idx_b (b); 
ERROR 1170 (42000): BLOB/TEXT column 'b' used in key specification without a key length
mysql> alter table tb_text add index idx_c (c);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> alter table tb_text add index idx_b (b(10));
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
# 插入数据测试(repeat函数用于生成重复数据)
# 正常插入
mysql> insert into tb_text  (a,b,c) values (repeat('hello',3),repeat('hello',3),repeat('hello',3));
Query OK, 1 row affected (0.01 sec)
# 插入英文字符超标
mysql> insert into tb_text  (a) values (repeat('hello',52));
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'a' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
# 插入中文超标
mysql>  insert into tb_text  (a) values (repeat('你好',100));
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'a' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
# 查看数据 发现数据有所截取 tinytext 类型最多存储255字节数据
mysql> select * from tb_text;
+----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+
| id | a | b | c |
+----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+
|  1 | hellohellohello | hellohellohello | hellohellohello |
|  2 | hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello | NULL| NULL|
|  3 | 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你| NULL| NULL|
+----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+
3 rows in set (0.00 sec)
登入後複製

透過上述測試,我們注意到,text 類型可儲存容量是以位元組為單位而不是字元。例如 tinytext 最多儲存 255 個位元組而不是 255 個字符,在 utf8 字符集下,一個英文字母或數字佔用一個字節,而一個中文漢字佔用三個位元組。也就是說 tinytext 最多儲存 255/3=85 個漢字,text 最多儲存 65535/3=21845 個漢字。而 varchar(M) 中的 M 指的是字符數,一個英文、數字、漢字都是佔用一個字符,即 tinytext 可存儲的大小並不比 varchar(255) 多。

總結:

本篇文章介紹了 blob 及 text 欄位類型相關知識。雖然資料庫規格中一般不建議使用 blob 及 text 類型,但由於一些歷史遺留問題或是某些場景下,還是會用到這兩類資料類型的。這篇文章僅當做個記錄了,使用到的時候可以參考下。

推薦學習:

mysql影片教學#

以上是一起聊聊MySQL中blob和text資料類型(範例詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:Mysql技术公众号
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!