删除和插入后自动递增列 [英] Auto increment column after delete and insert
问题描述
在我删除一行然后插入新行之后,自动递增行将不会分配正确的下一个数字.例如,如果我有
1个倒数第一
2个倒数第一
3个倒数第一
删除后
1个倒数第一
2个倒数第一
插入后
1个倒数第一
2个倒数第一
4个倒数第一
我需要解决此问题的帮助,非常感谢.
After i delete a row and then insert a new row the auto increment row wont assign the correct next number. example if i have
1 last first
2 last first
3 last first
after delete
1 last first
2 last first
after insert
1 last first
2 last first
4 last first
I need some help fixing this, any help would be great thanks.
推荐答案
[添加到John的响应中]
如果您想了解原因,请考虑一下如果发生以下情况会发生什么情况:
[Adding to John''s response]
If you want to understand why, think of what happens if you had this:
1 last first
2 last first
3 last first
然后删除第二行以获取此信息:
And then you deleted the 2nd row to get this:
1 last first
3 last first
现在,当您执行插入操作时,您希望索引是什么? 3肯定是错误的(因为它已经存在).您是否希望它去掉2,然后使用2?如果有多个这样的已删除行怎么办?
Now when you do an insert what would you want the index to be? 3 would certainly be wrong (since it already exists). Do you want it to go find that 2 is missing and thus use 2? What if there are multiple such deleted rows?
它没有损坏,您不应该尝试修复"它. SQL Server维护最后插入的项的索引.它应该是唯一的值,唯一可以保证的方法是允许SQL Server维护该索引.您可以*重置该值,但是在填写缺失"索引后,您将不得不再次重置它.
相信我,您什么都不担心.
It''s not broken and you shouldn''t try to "fix" it. SQL Server maintains the index of the last inserted item. It''s supposed to be a unique value, and the only way that can be guaranteed is if you allow SQL Server to maintain that index. You *can* reset the value, but you''d have to reset it again when you''ve filled the "missing" index.
Take my word for it, you''re worried about nothing.
这篇关于删除和插入后自动递增列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!