(5) Data types in MySQL

The picture is transferred from Baizhan Programmer

1. Integer type

MySQL data type meaning (signed)
tinyint(m) 1 byte range (-128~127)
smallint(m) 2 byte range (-32768~32767)
mediumint(m) 3 byte range (-8388608~8388607)
int(m) 4 byte range (-2147483648~2147483647)
bigint(m) 8 byte range (+-9.22*10 to the 18th power)

The length m in the numeric type refers to the display length, not the storage length , and is only useful when the field specifies zerofill

tinyint: only one byte, can be used to represent gender, state (expressed in 0, 1)

int: The length of the value will change (for example, the serial number may increase as the number of digits increases)

For example: int(3) , if the actual value is 2, if the column specifies zerofill, the query result is 002, and the left is filled with 0

2. Floating point type

MySQL data type meaning
float(m,d) Single-precision floating-point 8-bit precision (4 bytes) m total number, d decimal places
double(m,d) Double-precision floating-point type 16-bit precision (8 bytes) m total number, d decimal places

Parentheses must be given when specifying a floating-point type and two parameters must be specified in the parentheses.

For example: float(5,2): Indicates that there are two digits after the decimal point and three integer digits 

3. Character type

MySQL data type meaning
char(n) Fixed length, up to 255 characters
tinytext Variable length, up to 255 characters
varchar(n) Variable length, up to 65535 characters
text Variable length, up to 65535 characters
mediumtext Variable length, up to 224-1 characters
longtext Variable length, up to 232-1 characters

char and varchar:


  1. The length of char is fixed, that is, each piece of data occupies the same length of byte space ; it is suitable for fixed length such as ID number and mobile phone number .
  2. Varchar has variable length , and the maximum length can be set ; it is suitable for attributes with variable length.
  3. text does not set the length , when the maximum length of the attribute is not known, it is suitable to use text. (such as a description and profile of a person)

According to the query speed: char is the fastest, followed by varchar, and text is the slowest.

char(5): If there is only one character, since there are five lengths in total, the first four will be filled with spaces 

varchar(5): If there is only one character, there is only one length of character stored, the length is 1, and no spaces will be used to fill it up

Suggestions for using string type:


  1. Use varchar for frequently changing fields
  2. Know the fixed length with char
  3. Try to use varchar
  4. More than 255 characters can only use varchar or text
  5. Where varchar can be used, text is not used

4. Date type

MySQL data type meaning
date DateYYYY-MM-DD
time TimeHH:MM:SS
datetime Date Time YYYY-MM-DD HH:MM:SS
timestamp Timestamp YYYYMMDD HHMMSS

timestamp: Timestamp, need to consider the processing of time difference when using 

If you only need to store the year, month, and day, use date 

If you only need to store hours, minutes, and seconds, use time

If you want to store, use datatime

If you need to store time across time zones, use timestamp

Five, binary data (BLOB)

Store images, audio and video, and large text data 

  1. BLOB and TEXT are stored in different ways. TEXT is stored in text, and English storage is case-sensitive, while Blob is stored in binary and is not case-sensitive .
  2. The data stored in the BLOB can only be read as a whole.
  3. TEXT can specify a character set, BLOB does not need to specify a character set, because it is stored based on the byte type.

 

Guess you like

Origin blog.csdn.net/m0_62735081/article/details/126656962