The content of the "Preface" article is roughly the data type of the database.
"Belonging column" MySQL
"Homepage link" personal homepage
"Author" Mr. Maple Leaf (fy)
Table of contents
1. Data Type Classification
The C/C++ language has its own data types, and MySQL also has its own data types. The common data types are as follows:
Note: The red ones are the types to be explained below
The role of the data type:
- Determines the size of the space that should be opened up when storing data.
- Determines the value range of the data.
2. Value type
2.1 tinyint type
Next, I will explain the integer type of the numerical type. The value range of the integer type is similar to the value range of the data type of the C/C++ language. There are also signed and unsigned differences.
Integers take tinyint as an example
Create a table that contains a column of type tinyint, which is a signed type by default.
Parentheses 4. We will talk about the constraints in the next chapter. Ignore
the tinyint type occupying 1 byte for the time being. The value range of signed tinyint is -128~127
, Insert data
If the inserted data is not in the range of -128~127, the insertion is not allowed and an error is reported:
Below is the unsigned tinyint range test
Create a table, which contains a tinyint type column, and specify it as an unsigned type
tinyint type occupies 1 byte, the value range of unsigned tinyint is 0~255
, insert data:
if the inserted data is not in 0~255
the range, it is not allowed Insert, error:
Notice: Insert illegal data into MySQL, MySQL will directly intercept our operation; if we have successfully inserted the data into the table, then the inserted data must be legal. So in MySQL, the data type itself is a constraint.
It can also be seen from the above example that the attribute column in the MySQL table is created with the column name first and the type second.
num(列名) tinyint(类型)
Notice: Do not use unsigned as much as possible. For data that may not fit in the int type, int unsigned may also not fit in the data
. Instead of this, it is better to promote the int type to the bigint type when designing
The above is the introduction of tinyint, and other integer types are similar, so I won’t demonstrate it anymore
2.2 bit type
Basic syntax:
bit[(M)] : 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1
Note: The part of [] can be omitted
Create a table, the table contains an int type and a bit type, the bit defaults to 1 bit
Insert data into the table, a bit only allows insertion of 0, 1
to view the data in the table, and found that the bit type is not displayed, the root cause is because When the bit type is displayed, it is ASCII
displayed according to the value corresponding to the code, and some characters cannot be displayed.
If you want to view the value, force it to decimal.
Modify the table and change it to bit (10)
and insert the data again. The result proves that it is indeed pressed ASCII
The value corresponding to the code is stored in
bit(M), and the maximum value of M is 64. If it exceeds the value of table creation
, it will fail. space
2.3 Decimal types
2.3.1 float type
grammar:
float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节
Create a table that contains a float(4,2) type column, which is signed by default.
The range represented by float(4,2) is that -99.99 ~ 99.99
data can be inserted within this range, and an error will be reported if the range exceeds the range.
Try to insert beyond the range
illustrate: MySQL will round up when saving the value
Even if multiple decimal places are entered, it will be displayed in the specified format
Notice: The value range of the unsigned float type actually cuts off the negative part of the corresponding signed float type, so the value range of float(4,2) is 0~99.99
There is another problem to pay attention to, the float type will lose precision
2.3.2 decimal type
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
- This is the same as float
Decimal and float are very similar, but there is a difference: float and decimal represent different precision
Test accuracy
Create a table that contains a column of float(10,8) and a column of decimal(10,8) respectively
Insert data into the table, the result of the table lookup will have a certain loss of accuracy for float, but not for decimal
- The precision represented by float is about 7 digits
- 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, the default is 0. If m is omitted, the default is 10 (may be affected by MySQL version)
The precision of decimal is more accurate, so if we want a certain data to represent high precision, we can choose decimal
2.4 String type
2.4.1 char type
grammar:
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
test:
Create a table that contains a char(2) column
to insert data. It should be noted that in MySQL, the characters mentioned here are not the same as those in C/C++. In MySQL, a Chinese character is also a character
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.
2.4.2 varchar type
grammar:
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
test:
Create a table that contains a varchar(2)
to insert data, varchar is no different from char here
Notes on varchar type
Regarding varchar(len), how big is len? This len value is closely related to the encoding of the table:
- The length of varchar can be specified as a value between
0到65535
bytes1 - 3
, but there is a byte used to record the data size, so the effective number of bytes is65532
- When the encoding of our table is utf8, the maximum value of the parameter n of varchar(n) is
65532/3=21844
(because in utf8, a character occupies 3 bytes) - If the encoding is gbk, the maximum parameter n of varchar(n) is
65532/2=32766
(because in gbk, a character occupies 2 bytes)
2.4.3 Comparison between char and varchar
char and varchar comparison
- 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.
Take utf8 encoding as an example:
How to choose fixed-length (char) or variable-length string (varchar)?
- If the length of the data is determined to be the same, use a fixed length (char), such as: ID card, mobile phone number, md5
- If the data length changes, use variable length (varchar), such as: name, address, but you must ensure that the longest one can be stored in it.
- Fixed-length disk space is wasteful, but efficient.
- Variable length saves disk space, but is less efficient.
- 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
2.5 Date and time type
There are three commonly used dates:
date
: dateyyyy-mm-dd
, occupying three bytesdatetime
The time and date formatyyyy-mm-dd HH:ii:ss
represents the range from1000 到 9999
, occupying eight bytestimestamp
: Timestamp, the format from 1970yyyy-mm-dd HH:ii:ss
is exactly the same as datetime, occupying four bytes
Create a table that contains date, datetime, and timestamp columns of three types of time and date
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 isCURRENT_TIMESTAMP
If you insert data into the table, t3 will be automatically updated to the latest time (no need to manually insert)
Insert values into the table and
update the data, the timestamp will be updated
2.6 enums and sets
enum: enumeration, "single selection" type;
enum('选项1','选项2','选项3',...);
- This setting only provides the values of several options, and in the end, only one of the values is actually stored in a cell (only one of the values is allowed to be selected)
- For the sake of efficiency, these values actually store "numbers", because each option value of these options corresponds to the following numbers in turn: , the most;
1,2,3,....
when65535
we add enumeration values, we can also add the corresponding number number
set: set, "multiple selection" type:
set('选项值1','选项值2','选项值3', ...);
- This setting only provides the values of several options, and finally in a cell, the design can store any number of values (one or more of which can be selected)
- 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,4,8,16,32,....
up to 64
Notice: It is not recommended to use numbers when adding enumeration values and collection values, because it is not conducive to reading
example
Create a survey table that contains the name, gender (only one) and hobbies (multiple) of the respondent.
When inserting records into the table, the gender of the respondent can only be selected from male and female. The hobbies of the respondent can choose one or more from the several options provided, and multiple hobbies need to be separated by English commas
enum can be set by number
The number starts from 1, and there are several values, the largest number is the number, for example, enum('male', 'female'), the number 1 represents male, the number 2 represents female, the maximum is 2, if it exceeds the range, it is not allowed to insert the root
cause The reason is that, for the sake of efficiency, MySQL actually stores "numbers" for these values (such as male and female), because each option value of these options corresponds to the following numbers in turn: , 1,2,3,....
up 65535
to
can be set by number
When inserting records, in addition to specifying multiple options to set preferences, you can also set them digitally Inserting with
numbers is not a subscript, but a bitmap is used for insertion
. For example:
'羽毛球','游泳','篮球','写代码' // 4个爱好
用比特位表示:0000
0001 (1)是羽毛球
0010 (2)是游泳
0011 (3)是羽毛球,游泳
0100 (4)是篮球
依次类推
1111 (15)则是'羽毛球','游泳','篮球','写代码'
Note: Although enum and set can be set numerically, this approach is seriously not recommended, because the readability of such SQL is too poor
enum and set lookup
If you want to filter out all the women or men in the survey form, you can specify gender='女'
or directly when screening. But if you want to filter out the hobbies in the survey form including swimming, you can’t use where, which is not what we want resultgender='男'
Collection queries use the find_ in_ set function:
find_in_set(sub,str_list)
: If sub is in str_list, return the subscript; if not, return 0;str_list
string is a comma separated string
--------------------- END ----------------------
「 作者 」 枫叶先生
「 更新 」 2023.7.17
「 声明 」 余之才疏学浅,故所撰文疏漏难免,
或有谬误或不准确之处,敬请读者批评指正。