国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL中blob和text數(shù)據(jù)類別怎么用

2024-07-24 12:33:42
字體:
來源:轉載
供稿:網(wǎng)友
  今天小編給大家分享一下MySQL中blob和text數(shù)據(jù)類型怎么用的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
  
  不過在日常場景中,存儲字符串還是盡量用 varchar ,只有要存儲長文本數(shù)據(jù)時,可以使用 text 類型。對比 varchar ,text 類型有以下特點:
 
  text 類型無須指定長度。
  若數(shù)據(jù)庫未啟用嚴格的 sqlmode ,當插入的值超過 text 列的最大長度時,則該值會被截斷插入并生成警告。
  text 類型字段不能有默認值。
  varchar 可直接創(chuàng)建索引,text 字段創(chuàng)建索引要指定前多少個字符。
  text 類型檢索效率比 varchar 要低。
  下面我們來具體測試下 text 類型的使用方法:
 
  # 創(chuàng)建測試表 字符集是 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
  # 創(chuàng)建索引測試 發(fā)現(xiàn)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
  # 插入數(shù)據(jù)測試(repeat函數(shù)用于生成重復數(shù)據(jù))
  # 正常插入
  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)
  # 查看數(shù)據(jù) 發(fā)現(xiàn)數(shù)據(jù)有所截取 tinytext 類型最多存儲255字節(jié)數(shù)據(jù)
  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 類型可存儲容量是以字節(jié)為單位而不是字符。例如 tinytext 最多存儲 255 個字節(jié)而不是 255 個字符,在 utf8 字符集下,一個英文字母或數(shù)字占用一個字節(jié),而一個中文漢字占用三個字節(jié)。也就是說 tinytext 最多存儲 255/3=85 個漢字,text 最多存儲 65535/3=21845 個漢字。而 varchar(M) 中的 M 指的是字符數(shù),一個英文、數(shù)字、漢字都是占用一個字符,即 tinytext 可存儲的大小并不比 varchar(255) 多。

(編輯:武林網(wǎng))

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 稻城县| 利津县| 交口县| 运城市| 手游| 扎兰屯市| 托克托县| 涪陵区| 浮梁县| 中江县| 长岭县| 得荣县| 武强县| 寻乌县| 昆山市| 华安县| 临湘市| 泾阳县| 遵化市| 仙居县| 托里县| 同仁县| 广南县| 龙山县| 天门市| 琼海市| 蚌埠市| 芒康县| 肥东县| 武宣县| 深水埗区| 桃江县| 武定县| 沧州市| 平果县| 元氏县| 东阳市| 黄平县| 松阳县| 永川市| 天台县|