mysql的AUTO_INCREMENT如果达到最大值会怎样呢?

探讨MySQL自增ID在不同数据类型下溢出的情况,通过实验验证tinyint unsigned类型的自增字段达到最大值后的表现。

        在之前的一个项目中, 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> 



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值