Before talking about MySQL data types, let me explain an important point:
SQL statements in MySQL are not case sensitive, such as query keywords: SELECT and select are both possible;
But table names and database names are strictly case-sensitive, and variable names are also strictly case-sensitive
1. Naming convention
(1) The library name, table name, and field name must use lowercase letters and be separated by underscores
a) MySQL has a configuration parameter lower_case_table_names, which cannot be changed dynamically. The Linux system defaults to 0, that is, the library table names are stored in actual conditions and are case sensitive;
If it is 1, it is stored in lowercase and is not case sensitive;
If it is 2, it is stored in the actual situation, but compared in lowercase.
b) If the upper and lower case are mixed, there may be multiple tables such as abc, Abc, ABC, etc., which may cause confusion;
c) The field name display is case-sensitive, but the actual use is not, that is, it is not possible to create two fields with the same name but different capitalization;
d) In order to unify the specification, use lowercase letters for library names, table names, and field names.
(2) The library name, table name, field name must not exceed 32 characters
The library name, table name, and field name support up to 64 characters, but in order to unify the specification, easy identification and reduce the amount of transmission, it is forbidden to exceed 32 characters
(3) MySQL reserved words are forbidden to be used in library names, table names, and field names
When the library name, table name, field name and other attributes contain reserved words, the SQL statement must quote the attribute name with backquotes, which will make the writing of SQL statements and the escape of variables in SHELL scripts very complicated.
2. Numerical type
(1) MySQL supports all standard SQL numeric data types, these types include strict numeric data types (integer, smallint, decimal, and numeric), and approximate numeric data types (float, real, double precision);
(2) The keyword int is a synonym for integer, and the keyword dec is a synonym for decimal;
(3) The bit data type saves field values, and supports MyISAM, MEMORY, InnoDB, and BDB tables;
(4) As an extension of the SQL standard, MySQL also supports certificate types tinyint, mediumint, and bigint.
Types of |
size |
Range (signed) |
Range (unsigned) |
use |
tinyint |
1byte |
(-128,127) |
(0,255) |
Small integer value |
smallint |
2bytes |
(-32768,32767) |
(0,65535) |
Large integer value |
mediumint |
3bytes |
(-8388 608,8388 607) |
(0,16777215) |
Large integer value |
int or integer |
4bytes |
(-2147483648,2147483 647) |
(0,4294967295) |
Large integer value |
bigint |
8bytes |
(-9223372036854775808,9 223372036854775807) |
(0,18446744073 709551615) |
Very large integer value |
float |
4bytes |
(-3.402823466E+38,-1.175494 351E-38),0,(1.175494351 E-38,3.402823466351E+38) |
0,(1.175494351 E-38,3.402823 466 E+38) |
Floating point value |
double |
8bytes |
(-1.7976931348623157E + 308, -2.2250738585072014 E-308),0,(2.2250738585072014 E-308.1.7976931348623157 E + 308) |
0,(2.225073858 5072014E-308, 1.797693134862 3157 E+308) |
Floating point value |
decimal |
decimal(M,D), if M>D, it is M+2; Otherwise D+2 |
Depends on the value of M and D |
Depends on M And the value of D |
Decimal value |
3. Date and time type
The date and time types representing time values are DATETIME, DATE, TIMESTAMP, TIME, and YEAR;
Each time type has a valid value range and a "zero" value. When specifying an illegal value that MySQL cannot represent, the "zero" value is used.
Types of | Large and small (bytes) | range | 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, Beijing time 2038-1-19 11:14:07, GMT on January 19, 2038 03:14:07 |
YYYYMMDD HHMMSS |
Mixed date and time values |
3. String type
String types mainly refer to CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM, SE
Types of | size | use |
---|---|---|
CHAR | 0-255 bytes | Fixed-length string |
VARCHAR | 0-65535 bytes | Variable length string |
TINYBLOB | 0-255 bytes | Binary string of no more than 255 characters |
TINYTEXT | 0-255 bytes | Short text string |
BLOB | 0-65 535 bytes | Long text data in binary form |
TEXT | 0-65 535 bytes | Long text data |
MEDIUMBLOB | 0-16 777 215 bytes | Medium-length text data in binary form |
MEDIUMTEXT | 0-16 777 215 bytes | Medium-length text data |
LONGBLOB | 0-4 294 967 295 bytes | Very large text data in binary form |
LONGTEXT | 0-4 294 967 295 bytes | Very large text data |