在之前的一个项目中, mysql的自增值类型是int unsigned形式的, 我就在想, 要是超过这个值会怎样呢?
有问题, 必然有解决方法, 可以修改int unsigned类型为bigint类型, 这是64位的整数, 如果超过64位的整数, 会怎样呢? 自己可以写几个mysql语句试一下, 满了会出错. 那怎么办呢? 实际上, 你的自增id永远无法达到这个值。
假设每秒消耗1万个id, 需要多少年, 你自己算算。 吓死人。
来写几个sql语句看看:
mysql> create table t2 (id tinyint unsigned auto_increment primary key, score int);
ERROR 1050 (42S01): Table 't2' already exists
mysql> drop table t2;
Query OK, 0 rows affected (0.00 sec)
mysql> create table t2 (id tinyint unsigned auto_increment primary key, score int);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t2 values (250,1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+-----+-------+
| id | score |
+-----+-------+
| 250 | 1 |
+-----+-------+
1 row in set (0.00 sec)
mysql> insert into t2 values (null,1);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t2;
+-----+-------+
| id | score |
+-----+-------+
| 250 | 1 |
| 251 | 1 |
+-----+-------+
2 rows in set (0.00 sec)
mysql> insert into t2 values (null,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values (null,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values (null,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values (null,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values (null,1);
ERROR 1062 (23000): Duplicate entry '255' for key 'PRIMARY'
mysql>