自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL 中 Blob 和 Text 數(shù)據(jù)類(lèi)型詳解

數(shù)據(jù)庫(kù) MySQL
blob(binary large object) 是一個(gè)可以存儲(chǔ)二進(jìn)制文件的容器,主要用于存儲(chǔ)二進(jìn)制大對(duì)象,例如可以存儲(chǔ)圖片,音視頻等文件。

 [[443202]]

前言:

前面文章我們介紹過(guò)一些常用數(shù)據(jù)類(lèi)型的用法,比如 int、char、varchar 等。一直沒(méi)詳細(xì)介紹過(guò) blob 及 text 類(lèi)型,雖然這兩類(lèi)數(shù)據(jù)類(lèi)型不太常用,但在某些場(chǎng)景下還是會(huì)用到的。本篇文章將主要介紹 blob 及 text 數(shù)據(jù)類(lèi)型的相關(guān)知識(shí)。

1. blob 類(lèi)型

blob(binary large object) 是一個(gè)可以存儲(chǔ)二進(jìn)制文件的容器,主要用于存儲(chǔ)二進(jìn)制大對(duì)象,例如可以存儲(chǔ)圖片,音視頻等文件。按照可存儲(chǔ)容量大小不同來(lái)分類(lèi),blob 類(lèi)型可分為以下四種:

類(lèi)型

可存儲(chǔ)大小

用途

TINYBLOB

0 - 255字節(jié)

短文本二進(jìn)制字符串

BLOB

0 - 65KB

二進(jìn)制字符串

MEDIUMBLOB

0 - 16MB

二進(jìn)制形式的長(zhǎng)文本數(shù)據(jù)

LONGBLOB

0 - 4GB

二進(jìn)制形式的極大文本數(shù)據(jù)

 

其中最常用的就是 blob 字段類(lèi)型了,最多可存儲(chǔ) 65KB 大小的數(shù)據(jù),一般可用于存儲(chǔ)圖標(biāo)或 logo 圖片。不過(guò)數(shù)據(jù)庫(kù)并不適合直接存儲(chǔ)圖片,如果有大量存儲(chǔ)圖片的需求,請(qǐng)使用對(duì)象存儲(chǔ)或文件存儲(chǔ),數(shù)據(jù)庫(kù)中可以存儲(chǔ)圖片路徑來(lái)調(diào)用。

2. text 類(lèi)型

text 類(lèi)型同 char、varchar 類(lèi)似,都可用于存儲(chǔ)字符串,一般情況下,遇到存儲(chǔ)長(zhǎng)文本字符串的需求時(shí)可以考慮使用 text 類(lèi)型。按照可存儲(chǔ)大小區(qū)分,text 類(lèi)型同樣可分為以下四種:

類(lèi)型

可存儲(chǔ)大小

用途

TINYTEXT

0 - 255字節(jié)

一般文本字符串

TEXT

0 - 65 535字節(jié)

長(zhǎng)文本字符串

MEDIUMTEXT

0 - 16 772 150字節(jié)

較大文本數(shù)據(jù)

LONGTEXT

0 - 4 294 967 295字節(jié)

極大文本數(shù)據(jù)

不過(guò)在日常場(chǎng)景中,存儲(chǔ)字符串還是盡量用 varchar ,只有要存儲(chǔ)長(zhǎng)文本數(shù)據(jù)時(shí),可以使用 text 類(lèi)型。對(duì)比 varchar ,text 類(lèi)型有以下特點(diǎn):

  • text 類(lèi)型無(wú)須指定長(zhǎng)度。
  • 若數(shù)據(jù)庫(kù)未啟用嚴(yán)格的 sqlmode ,當(dāng)插入的值超過(guò) text 列的最大長(zhǎng)度時(shí),則該值會(huì)被截?cái)嗖迦氩⑸删妗?/li>
  • text 類(lèi)型字段不能有默認(rèn)值。
  • varchar 可直接創(chuàng)建索引,text 字段創(chuàng)建索引要指定前多少個(gè)字符。
  • text 類(lèi)型檢索效率比 varchar 要低。

下面我們來(lái)具體測(cè)試下 text 類(lèi)型的使用方法:

  1. # 創(chuàng)建測(cè)試表 字符集是 utf8 
  2. mysql> show create table tb_text\G 
  3. *************************** 1. row *************************** 
  4.        Table: tb_text 
  5. Create TableCREATE TABLE `tb_text` ( 
  6.   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵'
  7.   `a` tinytext, 
  8.   `b` text, 
  9.   `c` varchar(255) DEFAULT NULL
  10.   PRIMARY KEY (`id`) 
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 
  12.  
  13. # 創(chuàng)建索引測(cè)試 發(fā)現(xiàn)text類(lèi)型必須指定前綴長(zhǎng)度 
  14. mysql> alter table tb_text add index idx_a (a); 
  15. ERROR 1170 (42000): BLOB/TEXT column 'a' used in key specification without a key length 
  16. mysql> alter table tb_text add index idx_b (b);  
  17. ERROR 1170 (42000): BLOB/TEXT column 'b' used in key specification without a key length 
  18. mysql> alter table tb_text add index idx_c (c); 
  19. Query OK, 0 rows affected (0.04 sec) 
  20. Records: 0  Duplicates: 0  Warnings: 0 
  21. mysql> alter table tb_text add index idx_b (b(10)); 
  22. Query OK, 0 rows affected (0.06 sec) 
  23. Records: 0  Duplicates: 0  Warnings: 0 
  24.  
  25. # 插入數(shù)據(jù)測(cè)試(repeat函數(shù)用于生成重復(fù)數(shù)據(jù)) 
  26. # 正常插入 
  27. mysql> insert into tb_text  (a,b,c) values (repeat('hello',3),repeat('hello',3),repeat('hello',3)); 
  28. Query OK, 1 row affected (0.01 sec) 
  29. # 插入英文字符超標(biāo) 
  30. mysql> insert into tb_text  (a) values (repeat('hello',52)); 
  31. Query OK, 1 row affected, 1 warning (0.01 sec) 
  32. mysql> show warnings; 
  33. +---------+------+----------------------------------------+ 
  34. Level   | Code | Message                                | 
  35. +---------+------+----------------------------------------+ 
  36. | Warning | 1265 | Data truncated for column 'a' at row 1 | 
  37. +---------+------+----------------------------------------+ 
  38. 1 row in set (0.00 sec) 
  39. # 插入中文超標(biāo) 
  40. mysql>  insert into tb_text  (a) values (repeat('你好',100)); 
  41. Query OK, 1 row affected, 1 warning (0.02 sec) 
  42. mysql> show warnings; 
  43. +---------+------+----------------------------------------+ 
  44. Level   | Code | Message                                | 
  45. +---------+------+----------------------------------------+ 
  46. | Warning | 1265 | Data truncated for column 'a' at row 1 | 
  47. +---------+------+----------------------------------------+ 
  48. 1 row in set (0.00 sec) 
  49. # 查看數(shù)據(jù) 發(fā)現(xiàn)數(shù)據(jù)有所截取 tinytext 類(lèi)型最多存儲(chǔ)255字節(jié)數(shù)據(jù) 
  50. mysql> select * from tb_text; 
  51. +----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+ 
  52. | id | a                                                                                                                                                                                                                                                               | b               | c               | 
  53. +----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+ 
  54. |  1 | hellohellohello                                                                                                                                                                                                                                                 | hellohellohello | hellohellohello | 
  55. |  2 | hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello | NULL            | NULL            | 
  56. |  3 | 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你                                                                                      | NULL            | NULL            | 
  57. +----+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+-----------------+ 
  58. rows in set (0.00 sec) 

通過(guò)以上測(cè)試,我們注意到,text 類(lèi)型可存儲(chǔ)容量是以字節(jié)為單位而不是字符。例如 tinytext 最多存儲(chǔ) 255 個(gè)字節(jié)而不是 255 個(gè)字符,在 utf8 字符集下,一個(gè)英文字母或數(shù)字占用一個(gè)字節(jié),而一個(gè)中文漢字占用三個(gè)字節(jié)。也就是說(shuō) tinytext 最多存儲(chǔ) 255/3=85 個(gè)漢字,text 最多存儲(chǔ) 65535/3=21845 個(gè)漢字。而 varchar(M) 中的 M 指的是字符數(shù),一個(gè)英文、數(shù)字、漢字都是占用一個(gè)字符,即 tinytext 可存儲(chǔ)的大小并不比 varchar(255) 多。

總結(jié):

本篇文章介紹了 blob 及 text 字段類(lèi)型相關(guān)知識(shí)。雖然數(shù)據(jù)庫(kù)規(guī)范中一般不推薦使用 blob 及 text 類(lèi)型,但由于一些歷史遺留問(wèn)題或是某些場(chǎng)景下,還是會(huì)用到這兩類(lèi)數(shù)據(jù)類(lèi)型的。這篇文章僅當(dāng)做個(gè)記錄了,使用到的時(shí)候可以參考下。

責(zé)任編輯:武曉燕 來(lái)源: MySQL技術(shù)
相關(guān)推薦

2009-05-11 14:36:56

數(shù)據(jù)類(lèi)型建庫(kù)策略MySQL

2020-10-26 07:16:10

MySQLSchema數(shù)據(jù)

2010-08-13 14:58:01

FlexNumber數(shù)據(jù)類(lèi)型

2010-05-21 15:33:54

MySQL text

2010-10-15 13:28:34

MySql數(shù)據(jù)類(lèi)型

2010-05-24 15:56:53

MySQL數(shù)據(jù)類(lèi)型

2024-03-14 11:54:37

C++數(shù)據(jù)類(lèi)型

2010-06-04 11:15:23

MySQL自增主鍵

2017-02-27 08:34:09

JavaScript數(shù)據(jù)引用

2016-08-18 14:13:55

JavaScript基本數(shù)據(jù)引用數(shù)據(jù)

2011-08-04 09:56:30

Objective-C 變量 數(shù)據(jù)類(lèi)型

2022-06-20 08:26:39

Spring容器類(lèi)型轉(zhuǎn)換

2017-07-10 13:38:07

MySQL數(shù)據(jù)類(lèi)型整數(shù)類(lèi)型

2010-07-22 17:57:40

2010-10-08 14:45:43

mysql中int

2010-11-08 10:27:58

SQL Server檢

2011-05-26 13:54:04

Json

2009-06-18 15:53:37

Hibernate B

2010-06-13 18:00:56

MySQL數(shù)據(jù)類(lèi)型

2010-06-10 10:06:01

MySQL數(shù)據(jù)類(lèi)型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)