MySQL运维实战(4.9) SQL_MODE之NO_UNSIGNED_SUBTRACTION
在mysql数据库中,unsigned表示不存负数,如果unsigned类型的字段作运算,得到的结果为负数,SQL会报错。
mysql> create table t1(a tinyint unsigned); Query OK, 0 rows affected (0.01 sec) mysql> insert into t1 values(100); Query OK, 1 row affected (0.01 sec) mysql> select a-1 from t1; +------+ | a-1 | +------+ | 99 | +------+ 1 row in set (0.00 sec)
-- 默认情况下,unsigned数字参与到减法运算中,如果结果为负数,SQL会报错。 mysql> set sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select 1-a from t1; ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(1 - `test_sqlmode`.`t1`.`a`)'
在SQL_MODE中加入NO_UNSIGNED_SUBTRACTION后,运算结果可以为负数。
-- 设置NO_UNSIGNED_SUBTRACTION后,结果可以为负数 mysql> set sql_mode='NO_UNSIGNED_SUBTRACTION'; Query OK, 0 rows affected (0.01 sec) mysql> select 1-a from t1; +------+ | 1-a | +------+ | -99 | +------+ 1 row in set (0.00 sec)