当前位置:网站首页>数据类型二进制字符串类型
数据类型二进制字符串类型
2022-07-24 13:37:00 【华为云】
9.4 二进制字符串类型
MySQL中的二进制字符串类型主要存储一些二进制数据,比如可以存储图片、音频和视频等二进制数据,本节就简单介绍下MySQL支持的二进制字符串类型。
9.4.1 二进制字符串类型概述
MySQL中支持的二进制字符串类型主要包括BIT、BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB类型。每种类型的长度和存储空间如表9-10所示。
表9-10 二进制字符串类型长度与占用空间


每种二进制字符串类型所占用的存储空间也是不同的。
9.4.2 BIT类型
BIT类型中,每个值的位数最小值为1,最大值为64,默认的位数为1。BIT类型中存储的是二进制值。
创建数据表t20,t20数据表中包含一个BIT类型的字段b。
mysql> CREATE TABLE t20 ( -> b BIT(5) -> );Query OK, 0 rows affected (0.02 sec)向t20表中插入数据2、8、16。
mysql> INSERT INTO t20 (b) VALUES (2), (8), (16);Query OK, 3 rows affected (0.00 sec)Records: 3 Duplicates: 0 Warnings: 0查看t20表中的数据。
mysql> SELECT * FROM t20;+------+| b |+------+| || || |+------+3 rows in set (0.00 sec)使用SELECT*FROM t20语句查询出的数据无法查看,这是因为BIT类型存储的是二进制数据。
使用如下语句查询t20表中的数据。
mysql> SELECT BIN(b+0) FROM t20;+----------+| BIN(b+0) |+----------+| 10 || 1000 || 10000 |+----------+3 rows in set (0.00 sec)可以看出,已经正确查询出2、8、16的二进制值。其中,查询语句中的b+0(b表示定义的字段名称b)表示将存储的二进制值的结果转化为对应的二进制数字的值。BIN()函数将数字转化为了二进制。
也可以使用如下语句查询t20表中存储的数据。
mysql> SELECT b+0 FROM t20; +------+| b+0 |+------+| 2 || 8 || 16 |+------+3 rows in set (0.00 sec)可以看到,使用b+0查询数据时,可以直接查询出存储的十进制数据的值。
注意:在向BIT类型的字段中插入数据时,一定要确保插入的数据在BIT类型支持的范围内。
9.4.3 BINARY与VARBINARY类型
BINARY类型为定长的二进制类型,当插入的数据未达到指定的长度时,将会在数据后面填充“\0”字符,以达到指定的长度。同时BINARY类型的字段的存储空间也为固定的值。
VARBINARY类型为变长的二进制类型,长度的最小值为0,最大值为定义VARBINARY类型的字段时指定的长度值,其存储空间为数据的实际长度值加1。
创建数据表t21,t21表中有一个BINARY类型的字段b及一个VARBINARY类型的字段vb。
mysql> CREATE TABLE t21 ( -> b BINARY(10), -> vb VARBINARY(10) -> );Query OK, 0 rows affected (0.02 sec)其中,b字段与vb字段指定的长度均为10。
向t21表中插入数据。
mysql> INSERT INTO t21 (b ,vb) VALUES (10, 10);Query OK, 1 row affected (0.01 sec)查询t21表中数据的长度。
mysql> SELECT LENGTH(b), LENGTH(vb) FROM t21;+-----------+------------+| LENGTH(b) | LENGTH(vb) |+-----------+------------+| 10 | 2 |+-----------+------------+1 row in set (0.00 sec)可以看到,b字段数据的长度为10,vb字段的数据长度为2。说明BINARY类型的字段长度为固定值,为定义字段时指定的字段长度,而VARBINARY类型的字段长度的值是可变的。
9.4.4 BLOB类型
MySQL中的BLOB类型包括TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB 4种类型,可以存储一个二进制的大对象,比如图片、音频和视频等。
需要注意的是,在实际工作中,往往不会在MySQL数据库中使用BLOB类型存储大对象数据,通常会将图片、音频和视频文件存储到服务器的磁盘上,并将图片、音频和视频的访问路径存储到MySQL中。
边栏推荐
- Handler learning
- Editor formula
- Group knowledge map: distributed knowledge transfer and federated map reasoning
- Go deadlock problem
- Interface document evolution atlas, some ancient interface document tools, you may not have used them
- On node embedding
- [paper reading] temporary binding for semi-superior learning
- Network security - function bypass injection
- Spelling words~
- 支持鹏程系列开源大模型应用生态演化的可持续学习能力探索
猜你喜欢

脑注意力机制启发的群体智能协同避障方法

Overview of multi view learning methods based on canonical correlation analysis

Win10 log in with Microsoft account and open all programs by default with administrator privileges: 2020-12-14

为什么函数式接口 Comparator 中有 “两个抽象方法”?

Inversion of array (output in reverse order) (define an array and assign a value to output the array in reverse order)

Chinese character style migration --- diversity regularization stargan for Chinese character multi font generation

Go deadlock problem

Bayesian width learning system based on graph regularization

Summary of embedded network problems (packet loss of network card, unrecognized network card)

From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"
随机推荐
爱可可AI前沿推介(7.24)
Icml2022 | branch reinforcement learning
Network security - function bypass injection
Thread multithreading
Paper notes: swing UNET: UNET like pure transformer for medicalimage segmentation
Browser failed to get cookies, browser solution
Cmake basic grammar (1)
Common OJ questions of stack and queue
Odoo+ test
Happy number ~ ~ ~ (in fact, I'm not happy at all) & ugly number
交换机链路聚合详解【华为eNSP】
Can communication protocol (I)
汉字风格迁移篇---无监督排版传输
Group knowledge map: distributed knowledge transfer and federated map reasoning
Sorting method: bubble sorting (use an array to arrange a string of numbers in order (from large to small or from small to large))
Adjust the array order so that odd numbers precede even numbers
网络安全——文件上传内容检查绕过
[paper reading] temporary binding for semi-superior learning
基于典型相关分析的多视图学习方法综述
Bayesian width learning system based on graph regularization