本节讨论 DECIMAL 数据类型(及其同义词)的特性,特别关注以下主题:This section discusses the characteristics of the
DECIMAL data type (and its
synonyms), with particular regard to the following topics:
DECIMAL 列的声明语法为 DECIMAL(。参数的值范围如下:The declaration syntax for a
M,D)DECIMAL column is
DECIMAL(.
The ranges of values for the arguments are as follows:M,D)
M是最大数字位数(精度)。其范围为 1 到 65。Mis the maximum number of digits (the precision). It has a range of 1 to 65.D是小数点右侧的数字位数(标度)。其范围为 0 到 30,且不得大于M。Dis the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger thanM.
如果省略D,默认值为 0。如果省略M,默认值为 10。If D is omitted, the default is 0. If
M is omitted, the default is 10.
M的最大值为 65,意味着在DECIMAL值上的计算精确到 65 位数字。这个 65 位精度的限制也适用于精确值的数字字面量,因此此类字面量的最大范围与之前不同。(DECIMAL字面量的文本长度也有限制;参见第 14.24.3 节,“表达式处理”。)The maximum value of 65 for M means
that calculations on DECIMAL values
are accurate up to 65 digits. This limit of 65 digits of precision
also applies to exact-value numeric literals, so the maximum range
of such literals differs from before. (There is also a limit on
how long the text of DECIMAL
literals can be; see
Section 14.24.3, “Expression Handling”.)
对于 DECIMAL 列的值使用二进制格式存储,每 4 字节打包九个十进制数字。每个值的整数部分和小数部分的存储需求分别确定。每九位数字需要 4 字节,剩余的数字则需要 4 字节的一部分。剩余数字所需的存储空间如下表所示。Values for DECIMAL columns are
stored using a binary format that packs nine decimal digits into 4
bytes. 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. The storage required for
remaining digits is given by the following table.
| 剩余数字Leftover Digits | 字节数Number of Bytes |
|---|---|
| 0 | 0 |
| 1–2 | 1 |
| 3–4 | 2 |
| 5–6 | 3 |
| 7–9 | 4 |
例如,一个 DECIMAL(18,9) 列在小数点的两侧各有九位数字,因此整数部分和小数部分各需要 4 个字节。一个 DECIMAL(20,6) 列有十四位整数数字和六位小数数字。整数数字中九位需要 4 个字节,其余五位需要 3 个字节。六位小数数字需要 3 个字节。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. A
DECIMAL(20,6) column has fourteen integer
digits and six fractional digits. The integer digits require four
bytes for nine of the digits and 3 bytes for the remaining five
digits. The six fractional digits require 3 bytes.
DECIMAL 列不会存储前导的 + 字符、- 字符或前导 0 数字。如果你将 +0003.1 插入到 DECIMAL(5,1) 列中,它会被存储为 3.1。对于负数,字面上的 - 字符不会被存储。DECIMAL columns do not store a
leading + character or -
character or leading 0 digits. If you insert
+0003.1 into a DECIMAL(5,1)
column, it is stored as 3.1. For negative
numbers, a literal - character is not stored.
DECIMAL 列不允许超出列定义所暗示的范围的值。例如,DECIMAL(3,0) 列支持的范围是 -999 到 999。DECIMAL( 列允许小数点左边最多有 M,D)M - D 位数字。DECIMAL columns do not permit
values larger than the range implied by the column definition. For
example, a DECIMAL(3,0) column supports a range
of -999 to 999. A
DECIMAL(
column permits up to M,D)M -
D digits to the left of the decimal
point.
SQL 标准要求 NUMERIC( 的精度 恰好 为 M,D)M 位数字。对于 DECIMAL(,标准要求精度至少为 M,D)M 位数字,但允许更多。在 MySQL 中,DECIMAL( 和 M,D)NUMERIC( 是相同的,并且两者的精度都恰好为 M,D)M 位数字。The SQL standard requires that the precision of
NUMERIC(
be exactly M,D)M
digits. For
DECIMAL(,
the standard requires a precision of at least
M,D)M digits but permits more. In MySQL,
DECIMAL(
and
M,D)NUMERIC(
are the same, and both have a precision of exactly
M,D)M digits.
有关 DECIMAL 值内部格式的完整说明,请参阅 MySQL 源代码分发中的文件 strings/decimal.c。该格式在 decimal2bin() 函数中有解释(附例子)。For a full explanation of the internal format of
DECIMAL values, see the file
strings/decimal.c in a MySQL source
distribution. The format is explained (with an example) in the
decimal2bin() function.