mysql数据表中,有几条记录主键id为1,2,3,4,5,把id为5的这条数据删了。如果下次再insert一条数据的话

2024-12-15 10:15:59
推荐回答(4个)
回答1:

主键本来就是从原来删除的下一个值开始的 刚刚试验 有图为证

回答2:

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)

回答3:

把最后一条的记录中的id读出来,自增两次后再插入数据库

回答4:

主键设定为自动递增不就可以了么?