Skip to content

mysql无符号整型溢出

Published: at 12:02 PMSuggest Changes

首先创建一张测试用表

mysql> CREATE TABLE `t1` (
-> `id`  int UNSIGNED NOT NULL AUTO_INCREMENT ,
-> `val`  int UNSIGNED NOT NULL DEFAULT 0 ,
-> PRIMARY KEY (`id`)
-> );
Query OK, 0 rows affected (0.04 sec)

初始化一条数据:

mysql> insert into t1(val) values(0);
Query OK, 1 row affected (0.00 sec)

查看结果:

mysql> select * from t1;
+----+-----+
| id | val |
+----+-----+
|  1 |   0 |
+----+-----+
1 row in set (0.00 sec)

进行溢值计算:

mysql> update t1 set t1.val = t1.val - 1;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

产生了一个警告,查看下当前结果:

mysql> select * from t1;
+----+------------+
| id | val        |
+----+------------+
|  1 | 4294967295 |
+----+------------+
1 row in set (0.00 sec)

这个结果不难推测:0-1=-1,而-1(int)刚刚好是2^32-1. 将val置0:

mysql> update t1 set t1.val = 0;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

查看当前数据:

mysql> select * from t1;
+----+-----+
| id | val |
+----+-----+
|  1 |   0 |
+----+-----+
1 row in set (0.00 sec)

进行溢值计算:

mysql> update t1 set t1.val = t1.val - 2;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

猜猜结果是多少?

mysql> select * from t1;
+----+------------+
| id | val        |
+----+------------+
|  1 | 4294967295 |
+----+------------+
1 row in set (0.00 sec)

再进行一个溢值操作:

mysql> update t1 set t1.val = t1.val + 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

可以看到Changed: 0,查看下数据:

mysql> select * from t1;
+----+------------+
| id | val        |
+----+------------+
|  1 | 4294967295 |
+----+------------+
1 row in set (0.00 sec)

记录一下


Previous Post
使用shell搜索匹配文件
Next Post
php持久化连接数据库