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

WBOY
WBOY 转载
2023-08-24 10:05:02 973浏览

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,如有侵犯,请联系admin@php.cn删除