12.25.2 DECIMAL Data Type Characteristics十进制数据类型特征

This section discusses the characteristics of the DECIMAL data type (and its synonyms), with particular regard to the following topics:本节讨论DECIMAL数据类型(及其同义词)的特征,特别是以下主题:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). DECIMAL列的声明语法是DECIMAL(M,D)The ranges of values for the arguments are as follows:参数的值范围如下所示:

If D is omitted, the default is 0. 如果省略D,则默认值为0。If M is omitted, the default is 10.如果省略M,则默认值为10。

The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. M的最大值为65表示DECIMAL值的计算精确到65位。This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before. 65位精度的限制也适用于精确值数字文字,因此此类文字的最大范围与以前不同。(There is also a limit on how long the text of DECIMAL literals can be; see Section 12.25.3, “Expression Handling”.)DECIMAL文字的文本长度也有限制;见第12.25.3节,“表达式处理”。)

Values for DECIMAL columns are stored using a binary format that packs nine decimal digits into 4 bytes. DECIMAL列的值使用二进制格式存储,该格式将9个十进制数字压缩为4个字节。The storage requirements for the integer and fractional parts of each value are determined separately. 每个值的整数部分和小数部分的存储要求分别确定。Each multiple of nine digits requires 4 bytes, and any remaining digits left over require some fraction of 4 bytes. 九位数的每一个倍数需要4个字节,剩下的任何位数都需要4个字节的一小部分。The storage required for remaining digits is given by the following table.下表给出了剩余数字所需的存储空间。

Leftover Digits剩余数字Number of Bytes字节数
00
1–21
3–42
5–63
7–94

For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. 例如,DECIMAL(18,9)列的小数点两边各有9位数字,因此整数部分和小数部分各需要4个字节。A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. DECIMAL(20,6)列有14个整数位数和6个小数位数。The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. 整数数字中的9位需要4个字节,其余5位需要3个字节。The six fractional digits require 3 bytes.6个小数位数需要3个字节。

DECIMAL columns do not store a leading + character or - character or leading 0 digits. DECIMAL列不存储前导+字符或-字符或前导0位数。If you insert +0003.1 into a DECIMAL(5,1) column, it is stored as 3.1. 如果在DECIMAL(5,1)列中插入+0003.1,它将存储为3.1For negative numbers, a literal - character is not stored.对于负数,不存储文字字符-

DECIMAL columns do not permit values larger than the range implied by the column definition. DECIMAL列不允许值大于列定义所暗示的范围。For example, a DECIMAL(3,0) column supports a range of -999 to 999. 例如,DECIMAL(3,0)列支持-999999的范围。A DECIMAL(M,D) column permits up to M - D digits to the left of the decimal point.DECIMAL(M,D)列允许在小数点左边最多有M-D位。

The SQL standard requires that the precision of NUMERIC(M,D) be exactly M digits. SQL标准要求NUMERIC(M,D)的精度正好是M位。For DECIMAL(M,D), the standard requires a precision of at least M digits but permits more. 对于DECIMAL(M,D),标准要求精度至少为M位,但允许更多。In MySQL, DECIMAL(M,D) and NUMERIC(M,D) are the same, and both have a precision of exactly M digits.在MySQL中,DECIMAL(M,D)NUMERIC(M,D)是相同的,两者的精度都是M位。

For a full explanation of the internal format of DECIMAL values, see the file strings/decimal.c in a MySQL source distribution. 有关DECIMAL值内部格式的完整解释,请参阅MySQL源代码发行版中的文件strings/decimal.cThe format is explained (with an example) in the decimal2bin() function.decimal2bin()函数中解释了该格式(用一个示例)。