从 1 开始截断后插入;但删除后插入从以前的值恢复 [英] Insert after truncate start from 1; but insert after delete resumes from previous value
问题描述
在我的表中有列名 id,它是自动递增的整数.当我截断表然后尝试执行插入查询时,它从 1 开始,但是如果我执行删除操作然后尝试插入,那么它只会从以前的值恢复.为什么插入查询对删除和截断的执行不同.如果表中没有条目,则应始终从 1 开始.
In my table there is column name id which is auto increment integer. When I truncate table and then try to perform insert query it starts from 1, but if I do delete operation and then try to insert then it just resumes from previous value. Why does insert query performs differently for delete and truncate. It should always start from 1 if there is not entry in table.
示例:(1) 原表
(2) 删除所有行然后插入
(2) delete all rows and then insert
(3) 截断表然后插入
(3) truncate the table then insert
推荐答案
自动增量函数的当前最大值存储在表定义中(运行show create table idle
.当您从表中删除所有行时,自动增量值将保持不变.截断表时,您基本上是删除表并重新创建它,这会将自动增量值重置为 0.
The current max value for the autoincrement function is stored in the table definition (you can see it when you run show create table idle
.
When you delete all rows from the table, the autoincrement value will stay the same. When you truncate the table you basically drop the table and recreate it, which resets the the autoincrement value to 0.
希望这会有所帮助.
这篇关于从 1 开始截断后插入;但删除后插入从以前的值恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!