The DECIMAL
and NUMERIC
types store exact numeric data values. DECIMAL
和NUMERIC
类型存储精确的数字数据值。These types are used when it is important to preserve exact precision, for example with monetary data. 当需要保持精确精度时(例如货币数据),可以使用这些类型。In MySQL, 在MySQL中,NUMERIC
is implemented as DECIMAL
, so the following remarks about DECIMAL
apply equally to NUMERIC
.NUMERIC
实现为DECIMAL
,因此下面关于DECIMAL
的注释同样适用于NUMERIC
。
MySQL stores MySQL以二进制格式存储DECIMAL
values in binary format. DECIMAL
值。See Section 12.25, “Precision Math”.请参见第12.25节,“精确数学”。
In a 在DECIMAL
column declaration, the precision and scale can be (and usually is) specified. DECIMAL
列声明中,可以(通常是)指定精度和小数位数。For example:例如:
salary DECIMAL(5,2)
In this example, 在本例中,5
is the precision and 2
is the scale. 5
表示精度,2
表示刻度。The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.精度表示为值存储的有效位数,小数位数表示小数点后可以存储的位数。
Standard SQL requires that 标准SQL要求DECIMAL(5,2)
be able to store any value with five digits and two decimals, so values that can be stored in the salary
column range from -999.99
to 999.99
.DECIMAL(5,2)
能够存储任何五位数和两位小数的值,因此可以存储在salary
列中的值的范围是-999.99到999.99
。
In standard SQL, the syntax 在标准SQL中,语法DECIMAL(
is equivalent to M
)DECIMAL(
. M
,0)DECIMAL(M)
等价于DECIMAL(M,0)
。Similarly, the syntax 类似地,语法DECIMAL
is equivalent to DECIMAL(
, where the implementation is permitted to decide the value of M
,0)M
. DECIMAL
相当于DECIMAL(M,0)
,其中允许实现决定M
的值。MySQL supports both of these variant forms of MySQL支持这两种不同形式的DECIMAL
syntax. DECIMAL
语法。The default value of M
is 10.M
的默认值为10。
If the scale is 0, 如果刻度为0,则DECIMAL
values contain no decimal point or fractional part.DECIMAL
值不包含小数点或小数部分。
The maximum number of digits for DECIMAL
is 65, but the actual range for a given DECIMAL
column can be constrained by the precision or scale for a given column. DECIMAL
的最大位数是65,但给定的DECIMAL
列的实际范围可能会受到给定列的精度或小数位数的限制。When such a column is assigned a value with more digits following the decimal point than are permitted by the specified scale, the value is converted to that scale. 如果为此类列分配的值的小数点后位数超过指定小数位数所允许的位数,则该值将转换为该小数位数。(The precise behavior is operating system-specific, but generally the effect is truncation to the permissible number of digits.)(精确的行为是特定于操作系统的,但通常效果是截断到允许的位数。)