从数据库中删除数据后,如何将Identity字段重置为1? [英] How do I reset the Identity field back to 1 after deletion of the data from database?

查看:88
本文介绍了从数据库中删除数据后,如何将Identity字段重置为1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Identity字段设置回1而不影响数据库架构。

任何建议。

请评论...

在此先感谢....

解决方案

  DBCC  CHECKIDENT('  [table_name]',RESEED,[new_reseed_value])
- 例如
DBCC CHECKIDENT(' tblUserMaster',RESEED , 0


重置identy字段的唯一方法是提交 TRUNCATE 表上的命令(但要注意!这也将清除表中的所有记录)。

但如果此标识列不允许用作另一个表中的外键;所以你必须首先根据这个主列删除每个外键关系。



为什么你关心你的身份字段的值?从技术上讲,你不应该,除非你在新的id上做空。身份字段在技术上是由数据库引擎管理的;试图与它交互至少是一个时间的浪费,最多是一个废话。


这个查询会将你的身份字段重置为0,这样输入表格的第一个值就会身份价值为1.



...希望它有帮助



  DBCC  CHECKIDENT(your_table_name,RESEED, 0 


I Wanted to set the Identity field back to 1 not affecting the database schema.
Any suggestion .
Please comment ...
Thanks In advance ....

解决方案

DBCC CHECKIDENT('[table_name]', RESEED, [new_reseed_value])
-- for example
DBCC CHECKIDENT('tblUserMaster', RESEED, 0)


The only way to reset an identy field is to commit a TRUNCATE command on the table (but beware! this will clear all the records in the table as well).
But it will not be allowed if this identity column is used as a foreign key in another table; so you have to drop every foreign key relation based on this primary column first.

Why do you care about the values of your identity fields? Technically, you should not, unless you are getting short on new id's. An identity field is technically something managed by your database engine; trying to interact with it is at least a loss of time, at most a nonsense.


This query will reset you Identity field back to 0, so that the 1st value entered into the table will have an identity value of 1.

... hope it helps

DBCC CHECKIDENT (your_table_name, RESEED, 0)


这篇关于从数据库中删除数据后,如何将Identity字段重置为1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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