[MySQL Getting Started Guide] Summary of Data Types in MySQL

MySQL data type

​ The data types supported in MySQL can be roughly divided into four categories: numeric types, text/binary types, time and date types, and string types. To be summarized as follows:

1. Value type

1. Type overview

image-20230424210300565

[Basic description]:

  • It is necessary to divide the data types finely. For example, if we store the user's age, it is enough to TINYINT UNSIGNEDuse 1 byte. If INT occupies 4 bytes, although it seems to be only 3 bytes more, but × 100,000, × 1 million, plus the backup copy of the database, there is a lot of wasted space

  • In the C language, when our data crosses the boundary, the data will be truncated . But in MySQL, when the data crosses the boundary, the current operation will be terminated directly, so as to ensure the integrity of the data.reliability. Just imagine, if truncation is allowed, then the 1 stored in the database is originally 1, or becomes 1 after truncation, which will cause ambiguity, but the database has high requirements for reliability.

 This is essentially a constraint . Constraints are a very important concept in MySQL, which forces programmers to abide by certain rules.

2. BIT type

①Basic Grammar

bit[M] -- 位字段类型。M表示每个值的比特位数,范围从1~64,默认为1
  • bit(1) can only store the value of one bit, and the data range is 0~1
  • bit(8) can store eight-bit numbers, and the data range is 0~255

② Case 1

mysql> create table t1(
    -> id int,
    -> b bit(8)
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into t1 values(10, 10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | b    |
+------+------+
|   10 | 
    |
+------+------+
1 row in set (0.00 sec)

[Question 1]: Why is 10 of type bit(8) not displayed?

​ The bit field is displayed according to the characters corresponding to the ASCLL code. In the ASCLL code, 10 corresponds to the carriage return character, so the result is if. We insert the observation result of 65, and the ASCLL code corresponding to 65 is 65

mysql> use info;

Database changed
mysql> insert into t1 values(11, 65);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | b    |
+------+------+
|   10 | 
 |
|   11 | A    |
+------+------+
2 rows in set (0.00 sec)

​ If we have such a value, such as storing the user's gender. Only 0 or 1 needs to be stored, and bit(1) can be defined at this time, which can save space.

​ Now try to insert data larger than 8 bits (255) to verify the constraints of MySQL. As expected, our behavior was terminated directly

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| b     | bit(8)  | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t1 values(12, 256);
ERROR 1406 (22001): Data too long for column 'b' at row 1

3. INT series type

important point:

  1. Pay attention to choose the appropriate type when using
  2. You can use UNSIGNEDthe keyword to indicate that a field is unsigned, for exampleage TINYINT UNSIGNED

4. Floating point type

1) float and double

①Basic Grammar

float[(M, D)] [unsigned] -- M指定显示长度,d指定小数位数,占用空间4个字节

[illustrate]:

  • M specifies the longest total number of digits in the data, and D specifies the longest number of decimal places in the data
  • M and D also belong to a constraint
  • Double has higher precision than float, and there is no difference in usage

②Case

​ The data range represented by float(4, 2) is -99.99~99.99. In mysql5.7 version, it will be rounded when saving the value (different versions may be different)

mysql> create table t2(
    -> id int,
    -> f float(4, 2) 
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t2 values(1, 1.1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values(1, 99.991);  -- 发生四舍
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values(1, 99.995); -- 五入时溢出报错
ERROR 1264 (22003): Out of range value for column 'f' at row 1

mysql> select * from t2;
+------+-------+
| id   | f     |
+------+-------+
|    0 |  1.10 |
|    1 | 99.99 |
+------+-------+
2 rows in set (0.00 sec)

​ If the specified type is float unsigned, then the data range becomes 0 ~ 99.99, and there is no rounding problem at this time. For example, inserting -0.1 will fail:

mysql> create table t3(
    -> id int, 
    -> f float unsigned
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t3 values(1, 0);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values(1, -0.1);
ERROR 1264 (22003): Out of range value for column 'f' at row 1
2.float and decimal

①Basic Grammar

decimal(M, D) [unsigned] -- M指定显示长度,d指定小数位数,占用空间可变
  • The range represented by decimal(5,2) is -999.99 ~ 999.99
  • The range represented by decimal(5,2) unsigned is 0 ~ 999.99
  • The maximum number of digits m of a decimal integer is 65. The maximum number of decimal places supported is 30. If d is omitted, it defaults to 0. If m is omitted, it defaults to 10. It is worth noting that decimal is variable length.

②The difference between float and decimal:

​ The reason why the DECIMAL type is accurate is that it uses a fixed-point number representation , that is, a number is represented as a combination of an integer part and a decimal part , and the number of digits in the decimal part is fixed, which needs to be specified when creating a table. For example, DECIMAL(10,2) means a total of 10 digits, with 2 digits for the fractional part and up to 8 digits for the integer part. When stored internally, values ​​of type DECIMAL are represented exactly as strings of decimal digits.

In contrast, the FLOAT and DOUBLE types use floating-point representation, that is, a number is represented as the product of the mantissa (mantissa) and the exponent (exponent), where the mantissa and the exponent are both expressed in binary. Since the storage structure of floating-point numbers requires that the size range of the exponent be guaranteed when representing large numbers, the precision of the mantissa will decrease as the exponent increases. In other words, when representing a large number, the number of significant digits that can be represented by the mantissa is reduced, so there will be a certain loss of precision. How floating point numbers are stored in memory

-- 说明:float表示的小数精度大约是7位,decimal为30位
mysql> create table t4(
    -> f float(12, 10),
    -> d decimal(12, 10)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t4 values(1.1234567, 1.234567);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values(1.12345678, 1.2345678);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values(1.123456789, 1.23456789);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t4;
+--------------+--------------+
| f            | d            |
+--------------+--------------+
| 1.1234567165 | 1.2345670000 |
| 1.1234568357 | 1.2345678000 |
| 1.1234568357 | 1.2345678900 |
+--------------+--------------+
3 rows in set (0.00 sec)

​ From the above we can see that if we want to store decimals with higher precision, we should use the decimal type

Second, the string type

1. Type overview

image-20230425083631897

2. CHAR type

①Basic Grammar

char(L)-- 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
  • L represents the character setnumber of characters, not the number of bytes. For example, char(2) can store 'UK'these two English letters and '中国'these two Chinese characters. Under utf8 encoding, one English word occupies 1 byte, and one Chinese character needs 3 bytes.
  • The maximum value of L is 255

②Use case:

mysql> create table t5(
    -> id int,
    -> name char(3)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t5 values(0, '唐僧');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 values(1, '孙悟空');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 values(0, '观音菩萨');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from t5;
+------+-----------+
| id   | name      |
+------+-----------+
|    0 | 唐僧      |
|    1 | 孙悟空     |
+------+-----------+
2 rows in set (0.00 sec)

3. VARCHAR type

①Basic Grammar

varchar(L) -- 可变长度字符串,L表示字符长度,最大长度65535个字节

[illustrate]:

​ L also means the most storednumber of charactersrather than the number of bytes. Since the maximum byte length that VARCHAR can store is 65535, when using different character sets, the maximum number of characters that VARCHAR(n) can store will also be different:

  • Because the length occupied by the varchar data type is variable, there are 1 - 3 bytes used to record the length information of the data, so the effective number of bytes is 65532.

  • When the encoding of our table is utf8, the maximum value of the parameter n of varchar(n) is 65532/3=21844 [because in utf, a character occupies up to 3 bytes], if the encoding is gbk, the value of varchar(n) The maximum parameter n is 65532/2=32766 (because in gbk, a character occupies up to 2 bytes).

②The difference between char and varchar:

image-20230425092356260

(assuming a character occupies 3 bytes)

​ The char type is a fixed-length data type, so if the length of the string stored in the defined char(n) column does not meet n, MySQL will automatically add spaces after it until the defined length is met.

​ The varchar type is a variable-length data type that allocates space according to the actual number of characters. Although there is still an upper limit on the length, this does not conflict with variable length.

③ How to choose a fixed-length or variable-length string?

  • Fixed-length disk space is wasteful, but efficient.
  • Variable-length disk space is more economical, but inefficient (reading, updating, etc. of the length field will reduce efficiency).
  • The meaning of fixed length is to directly open up the corresponding space
  • The meaning of variable length is how much to use and how much to open without exceeding the custom range

3. Date and time type

①Basic Grammar

There are three commonly used dates:

  • date: The date format is yyyy-mm-dd, occupying three bytes in total.
  • datetime: Time and date format yyyy-mm-dd HH:ii:ssOccupies eight bytes. Can represent all dates and times from January 1, 1000 AD 00:00:00 to December 31, AD 9999 AD 23:59:59.9999999.
  • timestamp : Timestamp, yyyy-mm-dd HH:ii:ss since 1970. The format is exactly the same as datetime, occupying four bytes.

②Case:

mysql> create table t7(
    -> t1 date,
    -> t2 datetime,
    -> t3 timestamp
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t7(t1, t2) values('2023-4-25', '2023-4-25 12:14:30');
Query OK, 1 row affected (0.01 sec)

mysql> select * from t7;
+------------+---------------------+---------------------+
| time1      | time2               | time3               |
+------------+---------------------+---------------------+
| 2023-04-25 | 2023-04-25 12:14:30 | 2023-04-25 12:15:18 |
+------------+---------------------+---------------------+
1 row in set (0.00 sec)

Special Note:

​ timestamp is a special type of time that does not require manual input by the user. When adding data, the timestamp will be automatically yyyy-mm-dd HH:ii:ssadded in the form of according to the time of the current operation. Similarly, when we modify the data of the field, the timestamp will be automatically modified:

mysql> update t7 set time1='2022-5-1';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t7;
+------------+---------------------+---------------------+
| time1      | time2               | time3               |
+------------+---------------------+---------------------+
| 2022-05-01 | 2023-04-25 12:14:30 | 2023-04-25 12:19:53 |
+------------+---------------------+---------------------+
1 row in set (0.00 sec)

4. Enumeration type

1. enum type

①Basic Grammar

enum('选项一','选项二','选项三'……)   -- 枚举类型,单选

illustrate:

  • This setting only provides the values ​​of several options, and in the end, only one of the values ​​is actually stored in a cell
  • For the sake of efficiency, these values ​​actually store "numbers", because each option value of these options corresponds to the following numbers in turn: 1, 2, 3, ... up to 65535 (starting from 1)
  • When we add an enumeration value, we can also add the corresponding number

②Use case

mysql> create table t8(
    -> id int,
    -> sex enum('男','女')
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t8 values(0,'男');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t8 values(1,1);   -- 1就指代第一个选项
Query OK, 1 row affected (0.00 sec)

mysql> insert into t8 values(2,2);   -- 同理2指代第二个
Query OK, 1 row affected (0.01 sec)

mysql> select * from t8;
+------+------+
| id   | sex  |
+------+------+
|    0 ||
|    1 ||
|    2 ||
+------+------+
3 rows in set (0.00 sec)

2.set type

①Basic Grammar

set('选项一', '选型二', '选型三',……) -- 集合类型,多选

illustrate:

  • This setting only provides the values ​​of several options. Finally, in a cell, the design can store any number of values , that is, multiple choices can be made.

  • For the sake of efficiency, what is actually stored in the cell is a "number", because each option value of these options corresponds to the following numbers in turn: 1,2,4,8,16,32. When representing multiple options, the numbers are ANDed, which essentially uses bitmaps for state compression .

    Taking three options as an example, option one is represented by 001, option two is represented by 010, and option three is represented by 100. Representing the combination of one and three options is then ANDed, that is, 101.

    It is not recommended to use numbers when adding enumeration values ​​and collection values, because it is not conducive to reading.

②Use case

mysql> create table t9(
    -> id int,
    -> hobby set('爬山','游泳','篮球')
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t9 values(0, '爬山');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(0, 1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(1, '游泳,篮球');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(1, 3);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t9;
+------+---------------+
| id   | hobby         |
+------+---------------+
|    0 | 爬山           |
|    0 | 爬山           |
|    1 | 游泳,篮球      |
|    1 | 爬山,游泳      |
+------+---------------+
4 rows in set (0.00 sec)

3. Search in set

The following data are available:

+---------+----------------------+------+
| usrname | hobby                | sex  |
+---------+----------------------+------+
| 刘备    | 爬山,游泳,篮球         ||
| 关羽    | 爬山,游泳             ||
| 张飞    | 篮球                 ||
| 貂蝉    | 游泳                 ||
+---------+----------------------+------+

To find out all the people who like swimming, use the following query:

mysql> select * from t10 where hobby='游泳';
+---------+--------+------+
| usrname | hobby  | sex  |
+---------+--------+------+
| 貂蝉     | 游泳   ||
+---------+--------+------+
1 row in set (0.00 sec) 

However, only Diao Chan was selected, but this is obviously unreasonable. Liu Bei and Guan Yu also like swimming.

Therefore, when querying in a collection, you need to use find_in_set(sub,str_list)the function

  • If sub is in str_list, return the subscript; if not, return 0;
  • str_list comma-separated string.
mysql> select * from t10 where find_in_set('游泳', hobby);
+---------+----------------------+------+
| usrname | hobby                | sex  |
+---------+----------------------+------+
| 刘备     | 爬山,游泳,篮球         ||
| 关羽     | 爬山,游泳             ||
| 貂蝉     | 游泳                 ||
+---------+----------------------+------+
3 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/whc18858/article/details/130366632