Home  >  Article  >  Database  >  How does MySQL CAST handle overflow?

How does MySQL CAST handle overflow?

WBOY
WBOYforward
2023-09-07 22:17:11921browse

MySQL CAST 如何处理溢出?

#MySQL CAST can handle overflows that occur during the evaluation of numeric expressions. Suppose that if a numeric expression evaluation produces an overflow, MySQL will reflect an error message. Now to handle this overflow we can change the value to UNSIGNED with the help of CAST.

For example, when adding 1 to the maximum value of BIGINT, MySQL generates an error due to overflow as shown below -

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

Now, with the help of CAST, MySQL handles this as follows Overflow:

mysql> Select CAST(9223372036854775807 AS UNSIGNED) +1;

+------------------------------------------+
| CAST(9223372036854775807 AS UNSIGNED) +1 |
+------------------------------------------+
| 9223372036854775808                      |
+------------------------------------------+
1 row in set (0.07 sec)

The above is the detailed content of How does MySQL CAST handle overflow?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete