Article Directory
1. Common data types
1、数值型:
整型
小数:
定点数
浮点型
2、字符型:
较短的文本:char、varchar
较长的文本:text、blob(较长的二进制数据)
3、日期型
1. Numerical
(1) Integer
Integer type | byte | range |
---|---|---|
Tinyint | 1 | Signed: -128~127, unsigned: 0~225 |
Smallint | 2 | Signed: -32768~32767, Unsigned: 0~65535 |
Mediumint | 3 | Signed: -8388608~8388607, Unsigned: 0~1677215 (large anyway) |
INT、integer | 4 | It's huge anyway |
Bigint | 8 | It's huge anyway |
Classification : tinyint, smallint, mediumint, int/integer, bigint (the saved return increases in order)
Features:
① The default is signed, if you want to set unsigned, you need to add the unsigned keyword
CREATE TABLE tab_int (t1 INT UNSIGNED) ;
② If the inserted value exceeds the range of the integer type, an OUT of range exception will be reported, and the critical value will be inserted;
③ If the length is not set, there will be a default length. The
length represents the maximum width of the display. If it is not enough, it will be filled with 0 on the left, but it must be used with zerofill!
For example:
INT(7) The 7 in the brackets does not refer to the range, the range is determined by the data type, but only represents the width of the displayed result
CREATE TABLE tab_int (t1 INT(7) ZEROFILL) ;#创建表
INSERT INTO tab_int VALUES(123);#插入数据
SELECT *FROM tab_int;#查看数据
Results:
(2) Decimal
classification:
1、浮点型
FLOAT(M,D) 占4个字节
DOUBLE(M,D) 占8个字节(精度更高)
2、定点型
DEC(M,D)
DECIMAL(M,D)
3、其他
BINARY和VARBINARY用于保存较短的二进制
ENUM用于保存枚举
SET用于保存集合
Features:
①
M: integer part + decimal part
D: decimal part
If it exceeds the range, insert the critical value
②
Both M and D can be omitted.
If it is DECIMAL, M defaults to 10, and D defaults to 0.
If it is FLOAT and DOUBLE, the precision will be determined according to the precision of the inserted value
③The precision of fixed-point type is higher. If higher precision of inserted value is required, such as currency calculation, consider using
Principle : The simpler the selected type, the better, the smaller the type that can store the value, the better
2. Character type
1、较短的文本
char
varchar
2、较长的文本
text
blob(较大的二进制)
Short text:
Note: CHAR(M), M indicates the maximum number of characters in the value of the field
String type | Maximum number of characters | Description and storage requirements | Features | Consumption of space | effectiveness |
---|---|---|---|---|---|
char(M) | M (can be omitted, the default is 1) | M is an integer between 0 and 255 | Fixed-length characters | Relatively expensive | high |
varchar(M) | M | M is an integer between 0 and 65535 | Variable length characters | Compare savings | low |
3. Date type
classification:
1、date只保存日期
2、datetime保存日期+时间
3、timestamp存日期+时间(时间戳)
4、time只保存时间
5、year只保存年
Features:
Types of | byte | range | Influence of time zone, etc. |
---|---|---|---|
datetime | 8 | 1000——9999 | Not affected by |
timestamp | 4 | 1970-2038 | Receive |