When MySQL stores a value in a numeric column that is outside the permissible range of the column data type, MySQL clips the value to the appropriate endpoint of the range and stores the resulting value instead.
For example, when an out-of-range value is assigned to an
integer column, MySQL stores the value representing the
corresponding endpoint of the column data type range. If you
store 256 into a TINYINT or
TINYINT UNSIGNED column, MySQL stores 127 or
255, respectively. When a floating-point or fixed-point column
is assigned a value that exceeds the range implied by the
specified (or default) precision and scale, MySQL stores the
value representing the corresponding endpoint of that range.
Column-assignment conversions that occur due to clipping are
reported as warnings for ALTER
TABLE, LOAD
DATA INFILE, UPDATE,
and multiple-row INSERT
statements.
In MySQL 4.1, overflow handling during numeric expression evaluation depends on the types of the operands:
Integer overflow results in silent wraparound.
DECIMAL overflow results in a truncated
result and a warning.
Floating-point overflow produces a NULL
result. Overflow for some operations can result in
+INF, -INF, or
NaN.
For example, the largest signed
BIGINT value is
9223372036854775807, so the following expression wraps around to
the minimum BIGINT value:
mysql> SELECT 9223372036854775807 + 1;
+-------------------------+
| 9223372036854775807 + 1 |
+-------------------------+
| -9223372036854775808 |
+-------------------------+
To enable the operation to succeed in this case, convert the value to unsigned;
mysql> SELECT CAST(9223372036854775807 AS UNSIGNED) + 1;
+-------------------------------------------+
| CAST(9223372036854775807 AS UNSIGNED) + 1 |
+-------------------------------------------+
| 9223372036854775808 |
+-------------------------------------------+
Whether overflow occurs depends on the range of the operands, so
another way to handle the preceding expression is to use
exact-value arithmetic because
DECIMAL values have a larger
range than integers:
mysql> SELECT 9223372036854775807.0 + 1;
+---------------------------+
| 9223372036854775807.0 + 1 |
+---------------------------+
| 9223372036854775808.0 |
+---------------------------+
Subtraction between integer values, where one is of type
UNSIGNED, produces an unsigned result by
default. If the result would otherwise have been negative, it
becomes the maximum integer value. If the
NO_UNSIGNED_SUBTRACTION SQL
mode is enabled, the result is negative.
mysql>SET sql_mode = '';mysql>SELECT CAST(0 AS UNSIGNED) - 1;+-------------------------+ | CAST(0 AS UNSIGNED) - 1 | +-------------------------+ | 18446744073709551615 | +-------------------------+ mysql>SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';mysql>SELECT CAST(0 AS UNSIGNED) - 1;+-------------------------+ | CAST(0 AS UNSIGNED) - 1 | +-------------------------+ | -1 | +-------------------------+
If the result of such an operation is used to update an
UNSIGNED integer column, the result is
clipped to the maximum value for the column type, or clipped to
0 if NO_UNSIGNED_SUBTRACTION
is enabled.

User Comments
Add your own comment.