data types in mysql

The types of data in Mysql are as follows:
write picture description here

1. Numerical type

1. Integer type
write picture description here
write picture description here
description:
1. In MySQL, integer types can be specified as signed and unsigned, and the default is signed .
2. Specify unsigned by UNSIGNED.

However, when using UNSIGNED, there are some small details
write picture description here
ab hints are out of bounds, we guess it is because the range of int is too small. Do another experiment and create a table with columns of type bigint.
write picture description here
It is still out of bounds, but in some MySQL versions, it will display a particularly large value of 4294967295. Why is this?
For unsigned integer values, it is the maximum value of the integer, which is 2^32-1. For signed numbers, the first bit represents the sign bit. If it is 1, it represents a negative number. Invert and add one to the negative value, that is, -1.

So, how to get the value of -1? The parameter SQL_MODE can be set.
write picture description here
2.bit type
syntax : bit[(M)]
M represents the number of bits per value, ranging from 1 to 64, M is optional, the default is 1.
write picture description here
Description:
The bit field displays the corresponding display according to the ASCLL code.

The bit field is often used to store 0 or 1, defining bit(1). Can save space.
write picture description here
3. Decimal type
float type
Syntax: float[(m,d)][unsigned]
M is the total length, d is the number of decimal places, occupying four bytes
write picture description here
float(4,2) Range: -99.99~99.99
float(4 ,2) Unsigned range: 0~99.99
MySQL rounds the value when saving it.

decimal type
Syntax: decimal[(m,d)][unsigned]
is the same as float, the difference is that the precision of the representation is different. The maximum number of digits m of decimal integers is 65, and the maximum number of digits d that supports decimals is 30. d defaults to 10.
write picture description here
It is generally recommended to use the decimal type.

Two, text, binary type

String
char(L): Fixed-length string, L is the length that can be stored, the unit is character, and the maximum length is 255.
varchar(L) : Variable-length string, L indicates the character length, and the maximum length is 65535 bytes.
write picture description here
Description:
1.char(2) means that two characters can be stored, which can be letters or Chinese characters, but cannot exceed two.
2. A Chinese character in C++ occupies 2 characters, and both Chinese and English in MySQL database represent one character.
write picture description here
Notes on len: 1. 1~3 bytes will be set aside in
the attribute header defined by varchar to record the size of the data , so the effective number of bytes is 65535-3=65532. 2. The encoding is utf8 , varchar(n), the maximum value of n is 65532/3= 21844 (one Chinese character in utf8 occupies 3 bytes), the encoding is gbk , varchar(n), the maximum value of n is 65532/2= 32766 (gbk a Chinese character occupies 2 bytes). 3. In fact, the maximum length of len is 15000, and the larger text type is used instead.

Difference between char and varchar? (Interview frequently asked questions)
1. The retrieval efficiency of char is higher than that of varchar;
2. Fixed-length strings will waste disk space, but the search efficiency is high;
3. Variable-length strings save space, but the search efficiency is low.

3. Time and date

datetime : time and date format year-month-day hour-minute-seconds, indicating the range is from 1000 to 9999, occupying 8 bytes.
date : time and date format year-month-day, occupying 3 bytes.
timestamp : Timestamp, in the same format as datetime, occupying 4 bytes.
When adding data, the timestamp is automatically supplemented with the current time.
write picture description here

4. String type

Enum and set
write picture description here
description:
1. The enumeration type is single-selection, and the set type can be multiple-selection.
2. When adding an enum value, you can add the corresponding number number;

Sometimes we have the need to find people whose hobbies include an option.
write picture description here
In this way, only people whose hobbies are only "climbing" will be displayed, which is not the result we want.
The find_in_set property just solves this problem.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643692&siteId=291194637