重置自动递增主键? [英] Ressetting an auto increment primary key?

查看:68
本文介绍了重置自动递增主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

一段时间以来一直困扰着我,我一直在通过更新,编辑和删除来严格测试数据库.

我在删除时注意到的一件事是,从datagrid删除时,主键上的自动增量功能不会恢复.

例如:

如果我在表中有5个条目(ID键1、2、3、4、5)并删除最后一个条目(5),当我重新添加到该表中时,它仍在计数,因此ID为(ID键1 2,3,4,6).

有没有一种简单的方法可以确保id密钥正确计数而不是++''ing而不管数据删除是什么?

与表定义上的身份规范"下的字段有关系吗?

很抱歉,如果即时通讯无法清除即时通讯询问的内容,这是我认为可以解释的唯一方法....

问候
Matt.

解决方案

您可以在SSMS中使用此方法重置计数

 DBCC CHECKIDENT(<  表名 <  > )



将表名替换为要重置的表的名称,并将newvalue替换为要以-1开头的数字.例如,如果要从5开始编号,则种子值为4.


是的,我明白了,我将数据库重新连接到SSMS,我从表中删除了主键,手动重置了ID,然后取消了您有用的SQL语句并将其设置为继续值.

重置主键,然后重新索引.

分离并重新导入以进行测试,它可以正常工作

感谢您的帮助,非常感谢:)

最好的问候
马特.


Hi Guys.

Something has been bothering me for a while, i have been rigorously testing my database with updating, editing, and deleting.

one thing i have noticed while deleting is that the auto increment frunction on the primary keys do not revert when deleting from the datagrid.

for example:

if i have 5 entries in a table (id key 1, 2, 3, 4, 5) and delete the last entry (5), when i add back into that table it is still counting so the ids are (id key 1, 2, 3, 4, 6).

is there an easy way to ensure that the id keys count correcty instead of ++''ing regardless of deletion of data?

has it something to do with the fields under Identity Specification on the table definitions?

sorry if im not to clear on what im asking as its the only way i can think of explaining it....

Regards
Matt.

解决方案

You can reset the count by using this in SSMS

DBCC CHECKIDENT (<tablename>, RESEED, <newvalue>)



Replace the tablename with the name of the table that you want to reset and replace newvalue with the number you want to start with - 1. For example if you want to start numbering back at 5 then the seed value will be 4.


Right, i get what your saying, ive reattached the DB to SSMS, ive droped the primary key from the table manually reset the ID''s then antered your helpful SQL statement and set it to continue on the value.

reset the primary key, and indexing back on.

detached and re imported to test and it works

Thanks for your help guys much appreciated :)

Best Regards
Matt.


这篇关于重置自动递增主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆