主键本来就是从原来删除的下一个值开始的 刚刚试验 有图为证
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.00 sec)
mysql> USE test;
Database changed
mysql> CREATE TABLE t1(
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `content` char(100) NULL,
-> PRIMARY KEY(`id`)
-> );
Query OK, 0 rows affected (0.14 sec)
mysql> INSERT INTO t1(content) VALUES('test1');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO t1(content) VALUES('test2');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM t1;
+----+---------+
| id | content |
+----+---------+
| 1 | test1 |
| 2 | test2 |
+----+---------+
2 rows in set (0.00 sec)
mysql> DELETE FROM t1 WHERE id=2;
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO t1(content) VALUES('test3');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM t1;
+----+---------+
| id | content |
+----+---------+
| 1 | test1 |
| 3 | test3 |
+----+---------+
2 rows in set (0.00 sec)
把最后一条的记录中的id读出来,自增两次后再插入数据库
主键设定为自动递增不就可以了么?