MySQL 如何处理数值表达式评估期间的溢出?

WBOY
发布: 2023-08-24 10:05:02
转载
1348 人浏览过

MySQL 如何处理数值表达式评估期间的溢出?

As we know that MySQL will produce an error if overflow occurs during the assessment of numeric expressions. For example, the largest signed BIGNT is 9223372036854775807, so the following expression will produce an error −

mysql> Select 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807+1)'
登录后复制

MySQL can handle such kind of overflows in following ways:

BY CONVERTING VALUE TO UNSIGNED

MySQL enables such kind of operations by converting the values to unsigned as follows −

mysql> Select CAST(9223372036854775807 AS UNSIGNED) +1;
+------------------------------------------+
| CAST(9223372036854775807 AS UNSIGNED) +1 |
+------------------------------------------+
|                      9223372036854775808 |
+------------------------------------------+
1 row in set (0.07 sec)
登录后复制

通过使用精确值算术

MySQL可以使用精确值算术来处理上述表达式。这是因为溢出发生取决于操作数的范围。例如,可以通过使用DECIMAL值来执行上述计算,如下所示−

mysql> Select 9223372036854775807.0 + 1;
+---------------------------+
| 9223372036854775807.0 + 1 |
+---------------------------+
|     9223372036854775808.0 |
+---------------------------+
1 row in set (0.01 sec)
登录后复制

以上是MySQL 如何处理数值表达式评估期间的溢出?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板