当前位置:网站首页>数据类型二进制字符串类型
数据类型二进制字符串类型
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中。
边栏推荐
- 网络安全——使用Evil Maid物理访问安全漏洞进行渗透
- Outdoor billboards cannot be hung up if you want! Guangzhou urban management department strengthens the safety management of outdoor advertising
- 汉字风格迁移篇---无监督排版传输
- Chrome plug-in development tutorial
- Mass data excel download - the author of this article only tried to download 510000 data, which took 7 seconds
- Kunyu installation details
- HCIP第十三天
- Unity UGUI中scroll bar在游戏中启动界面时没有从最上面显示
- 基于典型相关分析的多视图学习方法综述
- Error reported when using activiti to create a database table
猜你喜欢

Overview of multi view learning methods based on canonical correlation analysis

Network security - Web information collection

网络安全——使用Evil Maid物理访问安全漏洞进行渗透

How to quickly learn Embedded

From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"

Pointer advanced part (1)

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

Common OJ questions of stack and queue

Game thinking 04 summary: a summary of frame, state and physical synchronization (it was too long before, and now it's brief)

Vscode configuration user code snippet (including deletion method)
随机推荐
Why are there "two abstract methods" in the functional interface comparator?
[paper reading] mean teachers are better role models
网络安全——Web渗透测试
Network security - Web penetration testing
How to render millions of 2D objects smoothly with webgpu?
代码签名证书与SSL证书区别
Mongodb uses mongotemplate operations to add, delete, modify, query, page, sort, aggregate (including embedded data), file upload and download
Interface document evolution atlas, some ancient interface document tools, you may not have used them
Win10 log in with Microsoft account and open all programs by default with administrator privileges: 2020-12-14
Browser failed to get cookies, browser solution
Overview of multi view learning methods based on canonical correlation analysis
指针进阶部分(1)
Mass data excel download - the author of this article only tried to download 510000 data, which took 7 seconds
Simple order management system small exercise
How to verify the domain name after applying for SSL digital certificate?
Common OJ questions of stack and queue
Research and progress of traffic situation awareness based on social media data enhancement
【论文阅读】TEMPORAL ENSEMBLING FOR SEMI-SUPERVISED LEARNING
网络安全——过滤绕过注入
网络安全——文件上传渗透测试