MySQL & MariaDB error 1690: BIGINT UNSIGNED value is out of range
BIGINT UNSIGNED value is out of range in '(`t_0`.`stock`.`qty` - 5)' (5.6.51) An arithmetic expression produced a value outside the range of its type, most often subtracting from an UNSIGNED column, where the result would be negative.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_DATA_OUT_OF_RANGE | ER_DATA_OUT_OF_RANGE |
| SQLSTATE | 22003 | 22003 |
| Type | Server error | Server error |
| Versions | since 5.5.63 active | every release since 5.5 active |
What it means
BIGINT UNSIGNED value is out of range in '(stock.qty - 5)'. When one operand is UNSIGNED, MySQL computes the result as UNSIGNED too, so 2 - 5 can’t be represented. The same error appears for signed overflow (BIGINT value is out of range), for example 9223372036854775807 + 1.
Why it happens
- Subtracting on
UNSIGNEDcolumns: stock, balances, counters,UNIX_TIMESTAMP()differences. - Multiplying large values in a report.
- It has been an error since MySQL 5.5.5; before that, the result silently wrapped around to a huge number.
How to fix it
- Cast before the arithmetic:
CAST(qty AS SIGNED) - 5. - Or enable
NO_UNSIGNED_SUBTRACTIONinsql_modeso subtraction results are signed. - Reconsider
UNSIGNEDfor values that can legitimately go negative in calculations.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE stock (item VARCHAR(10), qty INT UNSIGNED); INSERT INTO stock VALUES ('a', 2);Run:
SELECT qty - 5 FROM stock;
You get: BIGINT UNSIGNED value is out of range in '`t_0`.`stock`.`qty` - 5'(11.8.9)
Verified fixes
Cast to SIGNED before subtracting (or enable NO_UNSIGNED_SUBTRACTION)
SELECT CAST(qty AS SIGNED) - 5 FROM stock;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Subtraction on an UNSIGNED column goes below zero repro | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 | ✓ 1690 |
| Cast to SIGNED before subtracting (or enable NO_UNSIGNED_SUBTRACTION) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |