MySQL data type DECIMAL

The DECIMAL type is different from FLOAT and DECIMAL, where DECIMAL is actually stored as a string . The maximum possible value range of DECIMAL is the same as that of DOUBLE, but its valid value range is determined by the values ​​of M and D. If M is changed and D is fixed, its value range will become larger as M becomes larger. The first three rows of Table 2-7 illustrate this. If M is fixed and D is changed, its value range will become smaller (but the accuracy will increase) as D becomes larger. The last three rows of Table 2-7 illustrate this.



 

The range of values ​​for a given DECIMAL type depends on the version of the MySQL data type. For MySQL versions prior to 3.23, each value of the DECIMAL(M, D) column occupies M bytes, and the sign (if required) and decimal point are included in the M bytes. Therefore, columns of type DECIMAL(5, 2) have values ​​in the range -9.99 to 9 9 . 9 9 because they cover all possible 5-character values.

As in MySQL 3.23, DECIMAL values ​​are handled according to the ANSI specification, which states that DECIMAL(M, D) must be able to represent any value with M digits and D decimals.

For example, DECIMAL(5, 2) must be able to represent all values ​​from -999.99 to 999.99. And the sign and decimal point must be stored, so the DECIMAL value takes M+2 bytes since MySQL3.23. For DECIMAL(5, 2), the "longest" value (-9 9 9 . 9 9) requires 7 bytes.

At one end of the positive value range, the positive sign is not required, so the MySQL data type uses it to expand the value range beyond the value range required by the ANSI specification. For example, the maximum value of DECIMAL(5, 2) is 9 9 9 9 . 9 9, because there are 7 bytes available.

 

In short, in MySQL 3.23 and later, the value range of DECIMAL(M, D) is equal to the value range of DECIMAL(M + 2, D) in earlier versions. In all versions of the MySQL data type, if a DECIMAL column has a D of 0, no decimal point is stored. The result of this is to expand the value range of the column, because the bytes used to store the decimal point can now be used to store other numbers.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327097358&siteId=291194637