MySql data type summary

One, string type

Insert picture description here
1 、 CHAR 与 VARCHAR

char()------fixed-length character string, up to 255 characters. Fixed length will waste space
varchar()----variable length (variable length) string, the longest does not exceed 65535 bytes, generally more than 255 bytes, will use the text type. Variable length saves space, the remaining space will be Leave it to other data

2. TEXT
TEXT is divided into 4 types, which are different from SQLServer:

Insert picture description here
Summary: char, varchar, and text can all represent string types. The difference is:
(1) When char saves data, if the length of the stored string is less than the specified length n, it will be filled with spaces afterwards.
(2) When varchar and text save data, they are stored according to the true length of the data, and the remaining space can be reserved for other data.
(3) Char will cause space waste (if the length is less than the specified length, it will be filled with spaces), but because There is no need to calculate the length of the data, so the speed is faster. (That is, space is wasted and time is saved)
(4) Varchar and text save space, but the storage speed is not as fast as char (because the actual length of the data needs to be calculated)

3. ENUM type

The ENUM type (enumeration type), the same concept as C#, specifies the value range when defining it.

Attribute name ENUM('value 1','value 2','value 3'...'value n') ENUM has a NOT NULL attribute, and its default value is the first element of the value list;
ENUM has no NOT NULL, then ENUM The type will allow the insertion of NULL, and NULL is the default value;

CREATE TABLE Test4(Sex ENUM('Male','Female'));
INSERT INTO Test4 VALUES('Male');
INSERT INTO Test4 VALUES('Master'); --This line reports an error
SELECT *
FROM Test4;

4. SET type

When creating the table, specify the value range of the SET type.

Property name SET ('value 1','value 2','value 3'...,'value n')
  What is the difference between it and ENUM?
  Insert picture description here

It's basically a multi-select ENUM.

Two, floating-point type

Insert picture description here

tinyint: occupies 1 sss byte, relative to byte in java
smallint: occupies 2 bytes, relative to short
int in java : occupies 4 bytes, relative to int in java,
bigint: occupies 8 bytes , Relative to long
float in java : 4-byte single-precision floating-point type, relative to float
double in java : 8-byte double-precision floating-point type, relative to double in java

Three, time and date type

All types of MySQL date and time are as follows: Insert picture description here
Insert picture description here
1, date: year, month, day
2, time: hour, minute
, second, 3, datetime: year, month, day, hour, minute, and second,
4. timestamp: timestamp, which stores the same data as datetime.
The maximum timestamp means 2038, and the datetime range is 1000~9999.
Timestamp can be automatically updated to the current system time when inserting data or modifying data.

In addition, the TimeStamp type and DateTime should be noted when there is no input:

TimeStamp uses Current_TimeStamp() and DateTime uses NOW (to get the current time);
when NULL is entered, the system will enter the current date and time of the system; when there is no input, the system will enter the current date and time of the system;

Fourth, the choice of data type

Integer type: Determined according to the maximum value to be displayed;
Floating point type: To display decimals. If you want to be accurate to 10 digits after the decimal point, choose DOUBLE instead of FLOAT. DECIMAL has higher precision and floating-point numbers will have errors. If the precision is higher, you should choose fixed-point DECIMAL;
string type: the difference between fixed length and variable length, CHAR type takes up more space, but the processing speed is faster than VARCHAR, if The length does not change much, such as the ID card number, it is best to choose the CHAR type. For the comment string, it is best to choose VARCHAR;
time: according to the type to be displayed, especially TIMESTAMP, if the time that needs to be displayed corresponds to the time zone, you should choose TIMESTAMP;
ENUM type and SET type: the length is different, the ENUM type is the most There can be 65535 members, and the SET type can only contain up to 64 members. And ENUM can only be single-selected, while SET type can be multiple-selected;
TEXT type and BLOB type: TEXT can only store character data, and BLOB can store binary data. If it is plain text, it is suitable for TEXT. If it is a picture, it is suitable for storing binary;

Guess you like

Origin blog.csdn.net/m0_47605113/article/details/110630847