MySQL Database Fundamentals: Data Type Details - Binary String Type

Table of contents

foreword

1. BIT type

2. BINARY and VARBINARY types

3. BLOB type

Pay attention, prevent getting lost, if there are any mistakes, please leave a message for advice, thank you very much

 


foreword

Recently, the MySQL skill tree has been built. I have always wanted to review the basic knowledge of MySQL. I just took advantage of the completion of the MySQL skill tree to learn and review the basic knowledge of MySQL database. I have been using this database for a long time. Some basic knowledge points will be forgotten if they are not used for a long time. For example, the problem of data type overflow. Many times, when creating a table, I randomly give a similar one. As a result, when importing data, an error is reported and the table has to be deleted. Trouble, if you set the data length in advance, you can effectively avoid this problem and save a lot of space, so it is a worthwhile choice to summarize all MySQL data types.
The best way to summarize is to sort out a mind map according to the learning order, which can help us sort out our ideas quickly:

This series of articles will be written in this context, and this series of articles will be included in my column one article Quickly learn SQL database operations, basically covering the use of SQL to deal with daily business and routine query building analysis and complex operations. It took a lot of time and effort to create from basic database building and table building to dealing with complex operations of various databases, as well as professional explanations of common SQL functions. Learn the most practical and common knowledge in the first time. This blog is long and worth reading and practicing. I will pick out the best part and talk about practice in detail. Bloggers will maintain blog posts for a long time. If you have any mistakes or doubts, you can point them out in the comment area. Thank you for your support.


The binary string type in MySQL mainly stores some binary data, such as binary data such as pictures, audio and video.

1. BIT type

In the BIT type, the minimum number of digits for each value is 1, the maximum value is 64, and the default number of digits is 1. The BIT type stores binary values.

create table mytable1(
	b BIT(5)
	);
INSERT INTO mytable1(b) VALUES (2),(8),(16);

 When using b+0 to query data, you can directly query the value of the stored decimal data.

SELECT b+0 FROM mytable1;

 

 Note: When inserting data into a field of BIT type, make sure that the inserted data is within the range supported by the BIT type.

2. BINARY and VARBINARY types

The BINARY type is a fixed-length binary type. When the inserted data does not reach the specified length, "\0" characters will be filled after the data to reach the specified length. At the same time, the storage space of the field of BINARY type is also a fixed value.

The VARBINARY type is a variable-length binary type, the minimum value of the length is 0, the maximum value is the length value specified when the field of the VARBINARY type is defined, and its storage space is the actual length value of the data plus 1.

create table mytable2(
	b BINARY(10),
	vb VARBINARY(10)
	);

 The lengths specified by the b field and the vb field are both 10.

INSERT INTO mytable2(b,vb) VALUES (10,10);
SELECT LENGTH(b), LENGTH(vb) FROM mytable2;

 

It can be seen that the data length of the b field is 10, and the data length of the vb field is 2. Note that the field length of the BINARY type is a fixed value, which is the field length specified when the field is defined, while the value of the field length of the VARBINARY type is variable.

3. BLOB type

The BLOB types in MySQL include 4 types: TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB, which can store a large binary object, such as pictures, audio and video.

It should be noted that in actual work, BLOB types are often not used to store large object data in MySQL databases, and pictures, audio and video files are usually stored on the server's disk, and pictures, audio and video are accessed. The path is stored into MySQL.


Pay attention, prevent getting lost, if there are any mistakes, please leave a message for advice, thank you very much

That's all for this issue. I'm fanstuck, if you have any questions, feel free to leave a message to discuss, see you in the next issue

Guess you like

Origin blog.csdn.net/master_hunter/article/details/127066635