mysql text类型

人气:151 ℃/2022-12-12 22:21:00

mysql text类型是怎样的呢?下面就让我们一起来了解一下吧:

mysql下的text类型是属于一种特殊的字符串类型,其中包括了TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT,而它们的区别在于允许的长度与存储空间不同。因此,若是想要使用text类型,根据自身需求选取既能够满足需要又可以节省空间的类型即可。

说明:

TINYTEXT存放最大长度是 255 个字符的字符串。

TEXT 存放最大长度是 65,535 个字符的字符串。

MEDIUMTEXT存放最大长度是 16,777,215 个字符的字符串。

LONGTEXT存放最大长度是 4,294,967,295 个字符的字符串。

注意:

1、这几种text类型基本上是不需要指定长度的。

2、所允许的长度也就是指实际存储的字节数,但并不是实际的字符个数,例如假设一个中文字符会占两个字节,那么TEXT类型就可以存储65535/2=32767个中文字符,而varchar(100)可以存储100个中文字符,实际上占了200个字节,但是varchar(65535)并不可以存储65535个中文字符,因为已经超出了所要表达的范围。

参考范例:

mysql>usetest;mysql>createtabletext_example(e_texttinytext,v_charvarchar(255));mysql>insertintochar_examplevalues(90个中文字符,90个中文字符);mysql>insertintochar_examplevalues(80个中文字符,100个中文字符);

以上就是小编的分享了,希望能够帮助到大家。

mysql text 查询

MySQL 中 blob 和 text 数据类型详解

前言:

前面文章我们介绍过一些常用数据类型的用法,比如 int、char、varchar 等。一直没详细介绍过 blob 及 text 类型,虽然这两类数据类型不太常用,但在某些场景下还是会用到的。本篇文章将主要介绍 BLOB 及 TEXT 数据类型的相关知识。

1. blob 类型

blob(binary large object) 是一个可以存储二进制文件的容器,主要用于存储二进制大对象,例如可以存储图片,音视频等文件。按照可存储容量大小不同来分类,blob 类型可分为以下四种:

类型

可存储大小

用途

TINYBLOB

0 - 255字节

短文本二进制字符串

BLOB

0 - 65KB

二进制字符串

MEDIUMBLOB

0 - 16MB

二进制形式的长文本数据

LONGBLOB

0 - 4GB

二进制形式的极大文本数据

其中最常用的就是 blob 字段类型了,最多可存储 65KB 大小的数据,一般可用于存储图标或 logo 图片。不过数据库并不适合直接存储图片,如果有大量存储图片的需求,请使用对象存储或文件存储,数据库中可以存储图片路径来调用。

2. text 类型

text 类型同 char、varchar 类似,都可用于存储字符串,一般情况下,遇到存储长文本字符串的需求时可以考虑使用 text 类型。按照可存储大小区分,text 类型同样可分为以下四种:

类型

可存储大小

用途

TINYTEXT

0 - 255字节

一般文本字符串

TEXT

0 - 65 535字节

长文本字符串

MEDIUMTEXT

0 - 16 772 150字节

较大文本数据

LONGTEXT

0 - 4 294 967 295字节

极大文本数据

不过在日常场景中,存储字符串还是尽量用 varchar ,只有要存储长文本数据时,可以使用 text 类型。对比 varchar ,text 类型有以下特点:

下面我们来具体测试下 text 类型的使用方法:

# 创建测试表 字符集是 utf8mysql> show create table tb_text\G*************************** 1. row *************************** Table: tb_textCreate 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 lengthmysql> alter table tb_text add index idx_b (b); ERROR 1170 (42000): BLOB/TEXT column 'b' used in key specification without a key lengthmysql> alter table tb_text add index idx_c (c);Query OK, 0 rows affected (0.04 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> 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 类型,但由于一些历史遗留问题或是某些场景下,还是会用到这两类数据类型的。这篇文章仅当做个记录了,使用到的时候可以参考下。

推荐

首页/电脑版/网名
© 2026 NiBaKu.Com All Rights Reserved.