MySQL - Data Types

Table of contents

1. Classification of data types

2. Value type

1. tinyint type

2. bit type

3. float type

4. decimal type

3. String type

1. char type

2. varchar type

3. Comparison of char and varchar

4. Date and time type

Five, enum and set types 


1. Classification of data types

Classification type of data illustrate
value type BIT(M) Bit type: M specifies the number of bits, the default value is 1, and the range is 1-64
BOOL Boolean type: use 1 for true and 0 for false
TINYINT [UNSIGNED] Occupies 1 byte, signed by default
SMALLINT [UNSIGNED] Occupies 2 bytes, signed by default
MEDIUMINT [UNSIGNED] Occupies 3 bytes, signed by default
INT [UNSIGNED] Occupies 4 bytes, signed by default
BIGINT [UNSIGNED] Occupies 8 bytes, signed by default
FLOAT[(M,D)] [UNSIGNED] M specifies the display length, D specifies the number of decimal places, occupying 4 bytes
DOUBLE[(M,D)] [UNSIGNED] M specifies the display length, D specifies the number of decimal places, occupying 8 bytes
DECIMAL(M,D) [UNSIGNED] M specifies the display length, D specifies the number of decimal places, every 4 bytes represent 9 numbers, and the decimal point occupies 1 byte
text, binary type CHAR(L) Fixed-length string: L specifies the length of the string, up to 255
VARCHAR(L) Variable-length string: L specifies the upper limit of the string length, up to 65535 bytes
BLOB for storing binary data
TEXT for storing large text data
date and time DATE / DATETIME Date type: YYYY-MM-DD format / YYYY-MM-DD HH:MM:SS format
TIMESTAMP Timestamp: Displayed in YYYY-MM-DD HH:MM:SS format
string type ENUM Enumeration type: The value range of the ENUM type needs to be specified when defining the field. When setting the field value, only a single value is allowed to be selected from the members. The required storage space is determined by the number of members specified when defining the ENUM type
SET Collection type: The value range of the SET type needs to be specified when defining the field, and one or more values ​​can be selected from the members when setting the field value, and the required storage space is determined by the number of members specified when defining the SET type

2. Value type

1. tinyint type

Signed tinyint test (the default is signed, the range is -128~127), we first create a t1 table and insert data into it to see the effect:

Unsigned tinyint test (the default is signed, the range is 0~255), we create a t2 table and insert data into it to see the effect:

        From the above test results, the data type of MySQL acts as a constraint, which is what we will learn later. Here is a general understanding of the concept of constraints. The type you choose constrains you to execute the correct SQL. sentence;

2. bit type

bit[(M)] : bit field type. M represents the number of bits per value, ranging from 1 to 64. If M is omitted, it defaults to 1. 

Create a t3 table, which contains an id column of int type and an a column of 8-bit bit type. Insert a record into the table. The value of the specified id and a in the record are both 10. After inserting the record and looking at the table, you will find that the value of a is not 10. as follows:

        The fundamental reason is that when the bit type is displayed, it is displayed according to the value corresponding to the ASCII code, and in the ASCII code table, 10 corresponds to the control character LF, which means line break. If you specify that the values ​​of id and a are both 65 when inserting a record into the table, since 65 in the ASCII code table corresponds to the character A, you will find that the value of a shows A when you check the table after inserting the record. as follows:

range test for bit type

If we have such a value, only store 0 or 1, then we can define bit(1). This saves space.

        In the t4 table we created, there is a gender column, which indicates gender, either male or female. At this time, 1 bit just means male or female, which saves space; we can also find that when you It is possible to insert 0/1, but it is not possible to insert 2/3, because 2/3 cannot be represented by 1 bit;

3. float type

Signed float range test:

        MySQL will round up when saving the value, so the actual insertable range of float(4,2) is -99.994~99.994. If the inserted data is not within this range, an error will be reported when inserting the data. 

 Unsigned float range test:

If the inserted data is not in the range of 0~99.994, an error will be reported when inserting the data. 

        Through the t6 table above and the t7 table below, I believe you should be able to know more clearly the specific meaning of the numbers in brackets after float (that is, the range represented);

4. decimal type

decimal(m, d) [unsigned] : the fixed-point number m specifies the length, and d indicates the number of decimal places

The decimal and float types are used in the same way, but the precision of decimal is higher than that of float.

Create a table that contains a float(10,8) column and a decimal(10,8) column respectively. as follows:

        Insert a record into the table, and specify the value of float and decimal to be 3.141592653, but when you finally look up the table, you will find that decimal basically maintains the original appearance of the data, while float will have a certain loss of precision. as follows:

3. String type

1. char type

char(L): Fixed-length string, L is the length that can be stored, the unit is character, the maximum length value can be 255 

Create a table that contains a char(2) name column. as follows: 

        Note: 2 here means that the number of characters that can be stored is 2. Therefore, as long as the number of inserted characters does not exceed 2, it can be successfully inserted (Chinese characters are also possible). If the number of inserted characters exceeds 6, an error will be reported when inserting data. as follows:

         In different encodings, the number of bytes occupied by a character is different. For example, a character occupies 3 bytes in utf8, and a character occupies 2 bytes in gbk. MySQL's concept of delimited characters is not bytes, so that users don't have to care about complex encoding details.

2. varchar type

varchar(L): variable-length string, L represents the character length, the maximum length is 65535 bytes  

Create a table with an id column and a varchar(6) name column. as follows: 

Since varchar(6) can store up to 6 characters, as long as the number of inserted characters does not exceed 6, it can be successfully inserted. as follows:

If the number of inserted characters exceeds 6, an error will be reported when inserting data. as follows:

The maximum number of characters that can be specified for the varchar type

The varchar type occupies a maximum of 65535 bytes, of which 1~2 bytes are used to indicate the actual data length, and 1 byte is used to store other control information, so the effective number of bytes of the varchar type is at most 65532 bytes.

The upper limit of the number of characters that can be specified by the varchar type is related to the encoding format of the table:

For utf8 encoding, one character occupies three bytes, so L in varchar(L) can be specified as

65532 ÷ 3 = 21844            


For gbk encoding, one character occupies two bytes, so L in varchar(L) can be specified as

65532 ÷ 2 = 32766          

Therefore, when defining a table whose encoding format is utf8, if L in varchar(L) exceeds 21844, an error will be reported. as follows: 

When defining a table whose encoding format is gbk, if L in varchar(L) exceeds 32766, an error will be reported. as follows:

3. Comparison of char and varchar

The difference between char and varchar is as follows:

  • The char type can store up to 255 characters, and the varchar type can store up to 255 characters depending on the encoding format of the table.
  • After char(L) is defined, no matter whether the length of the stored string reaches L or not, a fixed-length space for storing L characters will be opened. If the length of the stored string exceeds L, an error will be reported.
  • After varchar(L) is defined, it will open up space on demand according to the length of the stored string, and need to use 1-3 bytes of space to represent the length of the stored string and other control information, if the stored string length exceeds L will report an error.

How to choose char and varchar types?

The advantages and disadvantages of char and varchar are as follows:

  • The data of char type is fixed-length , so disk space is wasteful, but the efficiency is high.
  • The data of the varchar type is of variable length , so the disk space is relatively saved, but the efficiency is low (need to read the length of the stored string first, and then access the space of the specified length).

        If the data to be stored is of fixed length, then use the char type for storage, such as ID number, mobile phone number, md5, etc. If the data to be stored is of variable length, then use the varchar type for storage, such as name, address, etc.

4. Date and time type

There are three commonly used dates:  

  • date : date 'yyyy-mm-dd', occupying three bytes
  • datetime time and date format 'yyyy-mm-dd HH:ii:ss' indicates the range from 1000 to 9999, occupying eight bytes
  • timestamp: timestamp, the yyyy-mm-dd HH:ii:ss format from 1970 is exactly the same as datetime, occupying four bytes

Create a table that contains columns of three time and date types: date, datetime, and timestamp. as follows: 

 Looking at the table structure, you can see that the t3 column of timestamp type is not allowed to be empty, and its default value is CURRENT_TIMESTAMP. as follows:

 If the t3 column is not inserted when inserting data, the current timestamp will be automatically inserted. as follows:

Use case of timestamp type: Microwave/CSDN's comments

Create a comment table that contains the nickname of the commenter, the content of the comment, and the time when the comment was posted. as follows:

Five, enum and set types 

The difference between enum and set types is as follows:

  • When defining the enum field, you need to provide several option values, and only one of the values ​​is allowed to be selected when setting the enum field value .
  • When defining the set field, you need to provide the values ​​of several options, and you can select one or more of them when setting the value of the set field .

        For example, the gender of a person can only be selected from male and female, so it can be defined as an enum type, and there may be multiple options for a person's hobbies, so it can be defined as a set type.

Use Case: Questionnaire

Create a questionnaire that includes the respondent's name, gender, and hobbies. as follows: 

When inserting records into the table, the respondent’s gender can only be selected from male and female, and the respondent’s hobbies can be selected from several options provided. need to be separated by commas. as follows:

Set enum by number

When inserting a record, besides specifying male and female to set gender, you can also set gender by inserting numbers 1 and 2. as follows: 

The fundamental reason is that, for the sake of efficiency, MySQL actually stores numbers when storing enum values, and the option values ​​provided in enum correspond to numbers 1, 2, 3, ..., up to 65535, so when setting enum values, you can Set up digitally.

set by number

When inserting records, in addition to specifying multiple options to set preferences, you can also set them numerically. as follows:

 When MySQL stores the set value, it actually stores numbers. The option values ​​provided in the set correspond to numbers 1, 2, 4, 8, ..., up to 64. Therefore, when setting the set value, it can be set in numbers. You set several hobbies, there are several bits, and the numbers you give need to be converted into corresponding binary digits, which bits are 1, which corresponds to which hobby;

suggestion:

  • Although enum and set can be set numerically, this approach is seriously not recommended, because the readability of such SQL is too poor, resulting in higher maintenance costs later.

enum and set lookup 

If you want to filter out the information of all lesbians in the questionnaire, you can directly specify gender = 'female' when filtering, because the value of the enum type can only choose one more. as follows:

But if you want to filter out the information of people whose hobbies include playing badminton in the questionnaire, it will be more troublesome. If you continue to use the above method, then the information of people whose hobbies are only typing codes will be finally screened out. as follows:

At this time, you need to use the find_in_set(str, strlist) function. The function of this function is to query whether str is contained in strlist. If it is contained, it returns the position of str in strlist (starting from 1), otherwise it returns 0.

For lookup, we will introduce it in a later blog 

Guess you like

Origin blog.csdn.net/sjsjnsjnn/article/details/128762859