8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 14 函数和运算符 / 14.24 精密数学 / 14.24.2 DECIMAL 数据类型特征
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

14.24.2 DECIMAL 数据类型特性14.24.2 DECIMAL Data Type Characteristics

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

  • 最大位数Maximum number of digits

  • 存储格式Storage format

  • 存储要求Storage requirements

  • 非标准 MySQL 对 DECIMAL 列上限的扩展The nonstandard MySQL extension to the upper range of DECIMAL columns

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

  • M 是最大数字位数(精度)。其范围为 1 到 65。M is the maximum number of digits (the precision). It has a range of 1 to 65.

  • D 是小数点右侧的数字位数(标度)。其范围为 0 到 30,且不得大于 M。D is 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 than M.

如果省略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(M,D) column permits up to 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(M,D) be exactly M digits. For DECIMAL(M,D), the standard requires a precision of at least M digits but permits more. In MySQL, DECIMAL(M,D) and NUMERIC(M,D) are the same, and both have a precision of exactly 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.