[MySQL] Supported data types

The principle of table building: use the smallest data type that can store data correctly. Choose the appropriate field type for each column:

1. Numerical type

1.1 Integer

Integer type Occupied bytes range
TINYINT 1 Signed: [-128,127] or unsigned: [0,255]
SMALLINT 2 Signed: [-32768,32767] or unsigned: [0,65535]
MEDIUMINT 3 Signed: [-8333608,8388607] or unsigned: [0,1677215]
INT、INTEGER 4 Signed: [-21 billion or more, 2.1 billion or more] or unsigned: [04,2 billion or more]
BIGINT 8 Very large, 19-digit range

There are 5 types of INT, and the maximum storage range of different types is different. gender? Use TINYINT, because ENUM is also an integer storage.

1) All integer types have an optional attribute UNSIGNED (unsigned), at this time the upper limit value is twice the original
Insert picture description here

2) There is another attribute is AUTO_INCREMENT (self-increment), this attribute can only be used for integer types

1.2 Floating point

Attributes storage accuracy Accuracy Description
FLOAT(M, D) 4 bytes Single precision Imprecise Single-precision floating-point type, total number of m, decimal place of d
DOUBLE(M, D) 8 bytes Double precision Higher accuracy than Float Double-precision floating-point type, total number of m, decimal place of d

Note that FLOAT can easily cause loss of accuracy

1.3 Fixed point DECIMAL

1) High-precision data types, commonly used to store transaction-related data

2)DECIMAL(M,N)

  • M stands for total accuracy
  • N represents the number of digits to the right of the decimal point (scale)
  • -1 < M < 254, 0 < N < 60

3) Storage space becomes longer

2. String type

Types of unit maximum characteristic
CHAR character Maximum of 255 characters Storage fixed length, easy to cause waste of space
VARCHAR character Can exceed 255 characters Storage becomes longer, saving storage space
TEXT byte The total size is 65535 bytes, which is about 64KB -

1) MySQL defaults to utf-8, then 1 character = 1 byte in English mode, and 1 character = 3 bytes in Chinese mode.

2) Most of the storage format of TEXT in MySQL is overflow page, which is not as efficient as CHAR

3) VARCHAR saves more space when the character type is variable length, but for VARCHAR fields, one byte is required to record the length. Therefore, use CHAR instead of VARCHAR for fixed length.

3. Time type

Types of byte Representation Example
DATE 3 bytes yyyy-mm-dd 2011-11-11
TIME 3 bytes hh:mm:ss 11:11:00
DATETIME 8 bytes yyyy-mm-dd hh:mm:ss 2011-11-11 11:11:00
TIMESTAMP 4 bytes yyyy-mm-dd hh:mm:ss 2011-11-11 11:11:00

Timestamp translates to Chinese as "time stamp", which is the number of seconds from the current time to the first year of Unix (0:00:00, January 1, 1970). For some time calculations, it will be more difficult if it is in the form of datetime. If I was born on 2011-11-11 11:11:00 and the current time is 2020-01-01 10:04:50, then I have to calculate How many seconds do I live, I still need a function to convert with datetime, but the timestamp can be subtracted directly.

1) After MySQL 5.6.4, TIMESTAMP and DATETIME support microseconds

2) The difference in storage range

  • TIMESTAMP storage range: 1970-01-01 00::00:01 to 2038-01-19 03:14:07
  • The storage range of DATETIME: 1000-01-01 00:00:00 to 9999-12-31 23:59:59

3) TIMESTAMP and DATETIME can accept the form of input parameters

// 下面都是 MySQL 允许的形式,MySQL 会自动处理
2011-11-11 20:48:59
2011#11#11 20/48/59
20111111204859

4) If the stored value is NULL, TIMESTAMP will automatically store the current time, and DATETIME will store NULL

5) TIMESTAMP will be converted according to the system time zone, DATETIME will not, so TIMESTAMP is generally used for internationalization

6) Note that the digital type BIGINT is used for storing the timestamp

Note that non-empty fields should be defined as NOT NULL as much as possible, provide default values, or use special values ​​or empty strings instead of null. There are problems with the storage, optimization, and use of NULL types.

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/114005397