MySQL 提供对精确数学的支持:数值处理能产生非常精确的结果,并对无效值有高度控制。精确数学基于以下两个特性:MySQL provides support for precision math: numeric value handling that results in extremely accurate results and a high degree control over invalid values. Precision math is based on these two features:
控制服务器在接受或拒绝无效数据时的严格程度的 SQL 模式。SQL modes that control how strict the server is about accepting or rejecting invalid data.
用于定点算术的 MySQL 库。The MySQL library for fixed-point arithmetic.
这些特性对数值操作有多种影响,并提供了高度的标准 SQL 兼容性:These features have several implications for numeric operations and provide a high degree of compliance with standard SQL:
精确计算:对于精确数值的数字,计算不会引入浮点误差。而是使用精确精度。例如,MySQL 将数字视为
.0001精确值,而非近似值,将其求和 10,000 次会得到的结果正好是1,而不是仅仅“接近”1 的值。Precise calculations: For exact-value numbers, calculations do not introduce floating-point errors. Instead, exact precision is used. For example, MySQL treats a number such as.0001as an exact value rather than as an approximation, and summing it 10,000 times produces a result of exactly1, not a value that is merely “close” to 1.明确定义的舍入行为:对于精确数值,
ROUND()的结果取决于其参数,而不取决于环境因素,例如底层 C 库的工作方式。Well-defined rounding behavior: For exact-value numbers, the result ofROUND()depends on its argument, not on environmental factors such as how the underlying C library works.平台独立性:对精确数值的操作在不同平台(如 Windows 和 Unix)上都是相同的。Platform independence: Operations on exact numeric values are the same across different platforms such as Windows and Unix.
对无效值处理的控制:溢出和除以零是可以检测到的,并且可以被视为错误。例如,你可以将对列来说过大的值视为错误,而不是将该值截断以适应列的数据类型范围。类似地,你可以将除以零视为错误,而不是作为产生
NULL结果的操作。选择采用哪种方法取决于服务器 SQL 模式的设置。Control over handling of invalid values: Overflow and division by zero are detectable and can be treated as errors. For example, you can treat a value that is too large for a column as an error rather than having the value truncated to lie within the range of the column's data type. Similarly, you can treat division by zero as an error rather than as an operation that produces a result ofNULL. The choice of which approach to take is determined by the setting of the server SQL mode.
以下讨论涵盖了精确数学运算的几个方面,包括可能与旧应用程序的不兼容性。最后,给出了一些示例,演示 MySQL 如何精确处理数值运算。有关控制 SQL 模式的信息,请参见 第 7.1.11 节,“服务器 SQL 模式”。The following discussion covers several aspects of how precision math works, including possible incompatibilities with older applications. At the end, some examples are given that demonstrate how MySQL handles numeric operations precisely. For information about controlling the SQL mode, see Section 7.1.11, “Server SQL Modes”.