This section discusses the characteristics of the 本节讨论DECIMAL data type (and its synonyms), with particular regard to the following topics:DECIMAL数据类型(及其同义词)的特征,特别是以下主题:
Maximum number of digits最大位数
Storage format存储格式
Storage requirements储存要求
The nonstandard MySQL extension to the upper range of DECIMAL columnsDECIMAL列上限的非标准MySQL扩展
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:参数的值范围如下所示:
M is the maximum number of digits (the precision). M是最大位数(精度)。It has a range of 1 to 65.它的范围是1到65。
D is the number of digits to the right of the decimal point (the scale). D是小数点(刻度)右侧的位数。It has a range of 0 to 30 and must be no larger than 其范围为0到30,且不得大于M.M。
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.下表给出了剩余数字所需的存储空间。
| 0 | 0 |
| 1–2 | 1 |
| 3–4 | 2 |
| 5–6 | 3 |
| 7–9 | 4 |
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.1。For 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)列支持-999到999的范围。A DECIMAL( column permits up to M,D)M - D digits to the left of the decimal point.DECIMAL(列允许在小数点左边最多有M,D)M-D位。
The SQL standard requires that the precision of SQL标准要求NUMERIC( be exactly M,D)M digits. NUMERIC(的精度正好是M,D)M位。For 对于DECIMAL(, the standard requires a precision of at least M,D)M digits but permits more. DECIMAL(,标准要求精度至少为M,D)M位,但允许更多。In MySQL, 在MySQL中,DECIMAL( and M,D)NUMERIC( are the same, and both have a precision of exactly M,D)M digits.DECIMAL(和M,D)NUMERIC(是相同的,两者的精度都是M位。M,D)
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.c。The format is explained (with an example) in the 在decimal2bin() function.decimal2bin()函数中解释了该格式(用一个示例)。