MySQL Database Basics: Data Type Details - Text String Type

Table of contents

foreword

1. CHAR and VARCHAR types

2. TEXT type

1.text

3. ENUM type

4. SET type

5. JSON type

see


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. 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 database construction and analysis as well as 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.
I have an article that has combed MySQL's time type data very well:

A quick study - play with MySQL to get time, format conversion and various operation methods

Therefore, the time type data will not be discussed in detail. This article mainly explains in detail the description of the text string data type and the actual operation. main


1. CHAR and VARCHAR types

Both of them can store shorter strings. The field length of the char type is fixed when it is declared. The minimum value is 0 and the maximum value is 255. If the actual length of the data is less than the declared length of the CHAR type when saved, spaces are padded on the right to the specified length. When MySQL retrieves data of type CHAR, fields of type CHAR will strip trailing spaces . For CHAR type data, when defining a CHAR type field, the declared field length is the number of bytes of the storage space occupied by the CHAR type field.

The character string modified by the VARCHAR type is a variable-length character string with a minimum length of 0 and a maximum length of 65535. When retrieving field data of type VARCHAR, trailing spaces are preserved. The storage space occupied by a field of type VARCHAR is the actual length of the string plus 1 byte.

Let's create a table to test it out:

create table mytable1(
	vc VARCHAR(4),
	c CHAR(4)
	);
INSERT INTO mytable1 (vc, c) VALUES ('abc', 'abc');

 

 At this time, no spaces are inserted normally. Let's take a look at the case where there are spaces:

INSERT INTO mytable1 (vc, c) VALUES ('a  ', 'a  ');

 

SELECT LENGTH(vc), LENGTH(c) FROM mytable1;

 

 The string space saved by vc is reserved, while the char does not retain the trailing space. We append another b:

SELECT CONCAT(vc, 'b'), CONCAT(c, 'b') FROM mytable1;

 

It can be seen that the field vc of the VARCHAR type retains the trailing spaces, while the field c of the CHAR type removes the trailing spaces.

2. TEXT type

1.text

Text is used to save the string of text type, which contains 4 types in total, namely TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT types. When saving and querying data to a TEXT type field, the trailing spaces of the data will not be removed, which is the same as the VARCHAR type.

create table mytable2(
	t TEXT
	);
INSERT INTO mytable2 (t) VALUES ('a  ');
SELECT LENGTH(t) FROM mytable2;

SELECT CONCAT(t, 'b') FROM mytable2;

 

3. ENUM type

The ENUM type is also called an enumeration type. The value range of the ENUM type needs to be specified when the field is defined, and the storage space required is determined by the number of members specified when the ENUM type is defined. When the ENUM type contains 1 to 255 members, a storage space of 1 byte is required; when the ENUM type contains 256 to 65535 members, a storage space of 2 bytes is required. The upper limit of the number of members of the ENUM type is 65535.

CREATE TABLE mytable3(
sex ENUM('男','女','unknow')
);
INSERT INTO mytable3
VALUES('男'),('女');

INSERT INTO mytable3
VALUES('UNKNOW');

 

It can be found here that ENUM is case-insensitive, and the declared data shall prevail.

An error is reported when adding an undefined value:

When adding multiple defined values, an error will also be reported:

Calls to enumerate elements can be made using indices, starting from 1:

INSERT INTO mytable3
VALUES('1'),(2);

Values ​​can be added without restricting to non-null null:

INSERT INTO mytable3
VALUES(NULL);

4. SET type

SET represents a string object, which can contain 0 or more members, but the upper limit of the number of members is 64. When the number of members contained in the SET type is different, the storage space occupied by it is also different.

Number of members range (L represents the actual number of members) Take up storage space
1<=L<=8 1 byte
9<=L<=16 2 bytes
17<=L<=24 3 bytes
25<=L<=32 4 bytes
33<=L<=64 8 bytes

To a certain extent, when the SET type stores data, the more members there are, the more storage space it occupies. Note: The SET type can select multiple members at a time when selecting members, which is different from the ENUM type.

CREATE TABLE mytable4(
sex SET('男','女','unknow')
);
INSERT INTO mytable4(sex)
VALUES('男'),('男,女');

 

Multiple comma-separated valid member values ​​can be inserted into a field of type SET.

When a duplicate SET type member is inserted into a SET type field s in a table, MySQL automatically deletes the duplicate member.

INSERT INTO mytable4(sex)
VALUES ('男,女,女');

MySQL throws an error when inserting a value that does not exist in a SET member into a field of type SET.

5. JSON type

In MySQL 5.7, JSON data type has been supported. In MySQL 8.x version, JSON type provides JSON documents that can be automatically validated and optimized storage structure, making it more convenient and efficient to store and read JSON type data in MySQL.

CREATE TABLE mytable5 (
     id_info JSON
     );
		 

 Insert JSON data.

INSERT INTO mytable5 (id_info) VALUES ('{"name":"fanstuck", "age":23, "address":{"province":"zhejiang", "city":"hangzhou"}}');

 When you need to retrieve a specific value of data in a field of JSON type, you can use the "->" and "->>" symbols.

SELECT id_info->'$.name' as name,
			 id_info->'$.address.city' as city
FROM mytable5

 

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

 


see

[mysql] ENUM type

Text string type - data type - CSDN MySQL entry skill tree

Guess you like

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