Reprinted from: https://blog.csdn.net/ja_java/article/details/69255904
Note: This note is based on the book "MYSQL 5.5 from scratch". [Edited by Liu Zengjie and Zhang Shaojun]
The various data types supported by Mysql mainly include: numeric data type, date/time type, and string type.1. Integer data type and its value range:
Types of | illustrate | Storage requirements (value range) |
---|---|---|
tinyint | small integer | 1 byte ([0~255], [-128~127]); 255=2^8-1;127=2^7-1 |
smallint | small integer | 2 bytes (0~65535, -32768~32767) ;65535=2^16-1 |
mediumint | medium | 3 bytes (0~16777215) ;16777215=2^24-1 |
int(integer) | ordinary | 4 bytes (0~4294967295) ;4294967295=2^32-1 |
bigint | Large integer | 8 bytes (0~18446744073709551615); 18446744073709551615=2^64-1 |
Floating point & fixed point:
type name | illustrate | storage requirements |
---|---|---|
float | single precision floating point number | 4 bytes |
double | double precision floating point number | 8 bytes |
decimal | Compressed "strict" fixed-point numbers | M+2 bytes |
Note : Fixed-point numbers are stored in the form of strings, and it is better to use decimal when the precision is high; try to avoid subtraction and comparison operations on floating-point numbers.
2. Time/date type:
year range: 1901~2155;
time format: 'HH:MM:SS' (if it is omitted and there is no colon, the default 2 digits from the right are seconds, then minutes, and finally time );
insert the current time of the system: insert into table name values(current_date()),(now());
date type: 'YYYY-MM-DD';
datetime (date+time): 'YYYY-MM-DD HH: MM:SS' or 'YYYYMMDDHHMMSS', value range: '1000-01-01 00:00:00'~'9999-12-31 23:59:59';
timestamp format is the same as datetime, but requires 4 bytes (datetime requires 8 bytes), and is stored in UTC (Universal Standard Time) (that is, timestamp will change with the set time zone, while datetime storage will never change); timestamp range: 1970-2037.
3. String type:
text type: tinytext, text, mediumtext, longtext;
Types of | Scope |
---|---|
tinytext | 255=2^8-1 |
text | 65535=2^16-1 |
mediumtext | 16777215=2^24-1 |
longtext | 4294967295=4GB=2^32-1 |
The storage requirement of char is a fixed length specified at the time of definition; the storage requirement of varchar depends on the actual value length.
Set type format: set ('value 1', 'value 2'...) - There can be 0 or more values. For set, if the inserted value is repeated, only one will be married. If the inserted values are out of order, they are automatically inserted in order. Insert abnormal values and ignore them.
Binary type:
bit (M) - saves the bit field value (bit field type), M represents the number of bits of the value;
eg: select BIN(b+0) from table name; --b is the column name; b+0 means To convert the result in binary to the value of the corresponding number, the BIN() function converts the number to binary.
blog - binary large object, used to store a variable amount of data.
type of data | storage range (bytes) |
---|---|
tinyblog | Up to 255=2^8-1 bytes |
bolg | Up to 65535=2^16-1 bytes |
mediumblog | Up to 16777215=2^24-1 bytes |
longblog | Up to 4294967295=4GB=2^32-1 bytes |