The difference between mysql data types int, tinyint, float and decimal

1.int, tinyint, float.
They are all (exact) integer data types, but the number of bytes occupied and the range of expression are different. First of all, it is impossible to say without this table:
write picture description here

You only need to know how many bytes the corresponding type occupies to calculate the range. For example, int occupies 4 bytes, that is, 4*8=32bits, which is about 10 digits. You can also understand why the default display number of int is 11.
The most common ones are tinyint and bigint. Tinyint is generally used to store data with small values ​​such as status and type. When it is not enough, smallint may be used. Bigint is generally used for self-incrementing primary keys.
In order to avoid the database being over-designed, boolean and enumeration types also use tinyint.
It should be noted that: whether it is defined as int(4) or int(11). The storage space occupied is certain. The M in int(M) just represents the width of the display.

2.float and decimal
MySQL uses the DECIMAL type to store values ​​with high precision requirements, such as amounts, also known as fixed-point numbers. Decimals are stored in mysql memory as binary strings. The declaration syntax is DECIMAL(M,D), which occupies M+2 bytes. M is the maximum number of digits (precision), in the range of 1-65; D is the number of digits to the right of the decimal point (scale), in the range of 0-30, but must not exceed M.
That is to say, decimal can dynamically specify how many bytes are occupied.

The method of calculating the number of occupied bytes by decimal:
Calculation method of the number of occupied bytes - decimals and integers are calculated separately, each 9 digits occupies 4 bytes, and the remaining part is converted as follows:
write picture description here

For example, DECIMAL(18,9), the integer part and the fractional part have 9 bits each, so each occupies 4 bytes, a total of 8bytes . For
example, DECIMAL(20,6), an integer of 14 bits, needs 4 bytes to store 9 bits, and 3 The byte is stored in 5 digits; the decimal is 6 digits, and 3 bytes are required. Total 10bytes

For example: define a decimal(7,3), the maximum value that can be represented is -9999.999~9999.999. It occupies 4 bytes.
123.12 -> 123.120, because the decimal point is less than 3 digits, add 0
123.1245 -> 123.125, only 3 digits are left for the decimal point, the excess is automatically rounded and truncated
12345.12 -> The save fails, because the decimal point is less than 3 digits, and the 0 is added to become 12345.120, more than 7 digits. An error is reported in strict mode, and 9999.999 is stored in non-strict mode.

The single-precision floating-point number float occupies 4 bytes. The standard syntax of float allows to specify the precision in the form of FLOAT(M), but this precision value M only determines the storage size: 0-23 has the same effect as not specifying by default, and 24-53 becomes Double precision DOUBLE too.

The difference between double and float is that double has high precision and has 16 significant digits (7 digits of float precision). But double consumes twice as much memory as float, occupying 8 bytes, and the operation speed of double is much slower than that of float.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883170&siteId=291194637