MySQL中blob和text数据类型怎么用

发布时间:2022-01-13 16:09:24 作者:iii
来源:亿速云 阅读:168

今天小编给大家分享一下MySQL中blob和text数据类型怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

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 类型的使用方法:

# 创建测试表 字符集是 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) 多。

以上就是“MySQL中blob和text数据类型怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. MySQL字段设计规范应如何避免使用TEXT/BLOB类型
  2. 关于InnoDB存储引擎text和blob类型的优化

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql blob text

上一篇:GIS开发中Mapbox Vector tiles的示例分析

下一篇:GIS开发中NASA火灾地图的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》