The basic principle:
Smaller is usually better, simple is better, try to avoid null
type of data:
Integer type:
TINYINT | 1 byte | (-128,127) | (0,255) | small integer value |
SMALLINT | 2 bytes | (-32 768,32 767) | (0,65 535) | large integer value |
MEDIUMINT | 3 bytes | (-8 388 608,8 388 607) | (0,16 777 215) | large integer value |
INT or INTEGER | 4 bytes | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | large integer value |
BIGINT | 8 bytes | (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | very large integer value |
Real type:
FLOAT | 4 bytes | (-3.402 823 466 E + 38 , -1.175 494 351 E-38) , 0 , (1.175 494 351 E-38,3.402 823 466 351 E + 38) | 0 , (1,175 494 351 E-38,3,402 823 466 E + 38) | single-precision floating-point value |
DOUBLE | 8 bytes | (-1.797 693 134 862 315 7 E + 308 , -2.225 073 858 507 201 4 E-308) , 0 , (2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) | 0 , (2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) | Double-precision floating-point value |
DECIMAL | For DECIMAL(M,D), if M>D, it is M+2 else it is D+2 | depends on the values of M and D | depends on the values of M and D | decimal value |
DECIMAL is just a storage format, and DOUBLE is still used for calculations.
String type:
CHAR | 0-255 bytes | fixed-length string |
VARCHAR | 0-65535 bytes | variable length string |
The CHAR and VARCHAR types are similar, but they are stored and retrieved differently. They also differ in terms of their maximum length and whether trailing spaces are preserved. No case conversion is done during storage or retrieval.
The BINARY and VARBINARY classes are similar to CHAR and VARCHAR, except that they contain binary strings instead of non-binary strings. That is, they contain byte strings instead of character strings. This means they have no character set, and sorting and comparison is based on the numeric values of the column value bytes.
The length of char is immutable, and the length of varchar is variable, that is to say, if you define a char[10] and varchar[10], if it is stored in 'csdn', then the length occupied by char is still 10. In addition to the character 'csdn', followed by six spaces, and varchar immediately changes the length to 4. When fetching data, trim() should be used to remove extra spaces for char type, and varchar is not needed. .
Despite this, the number of accesses of char is still much faster than that of varchar, because its length is fixed, which is convenient for program storage and search; but char also pays the price of space, because its length is fixed, so it is inevitable to There are extra space placeholders occupying space, it can be said that space is exchanged for time efficiency, and varchar is space efficiency first.
Furthermore, the storage method of char is that it occupies 1 byte for English characters (ASCII) and 2 bytes for a Chinese character; while the storage method of varchar is that each English character occupies 2 bytes, and Chinese characters also occupy 2 bytes. Occupies 2 bytes.
VARCHAR(10) is a better choice than VARCHAR(1000).
Date and time type:
Date and time types that represent time values are DATETIME, DATE, TIMESTAMP, TIME, and YEAR.
Each time type has a range of valid values and a "zero" value, which is used when specifying a value that is not legal MySQL cannot represent.
The TIMESTAMP type has a proprietary auto-update feature that will be described later.
Types of | size (bytes) |
Scope | Format | use |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | date value |
TIME | 3 | '-838:59:59'/'838:59:59' | HH:MM:SS | time value or duration |
YEAR | 1 | 1901/2155 | YYYY | year value |
DATETIME | 8 | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | Mixed date and time values |
TIMESTAMP | 4 | 1970-01-01 00:00:00/2038 The end time is 2147483647 seconds, 2038-1-19 11:14:07 Beijing time, January 19, 2038 03:14:07 GMT |
YYYYMMDD HHMMSS | mixed date and time values, timestamp |