删除行后重用身份值 [英] Reuse identity value after deleting rows

查看:48
本文介绍了删除行后重用身份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server 2008 Express中删除行之后,是否可以重用标识字段值?这是一个例子.假设我有一个带有ID字段作为主键(身份)的表.如果添加五行,则将具有以下5个ID:1、2、3、4、5.如果要删除这些行,然后再添加五行,则新行将具有ID:6、7、8, 9,10.是否可以让它从1重新开始?

Is it possible to reuse an identity field value after deleting rows in SQL Server 2008 Express? Here is an example. Suppose I have a table with an Id field as a primary key (identity). If I add five rows, I will have these 5 Ids: 1, 2, 3, 4, 5. If I were to delete these rows, and then add five more, the new rows would have Ids: 6, 7, 8, 9, 10. Is it possible to let it start over at 1 again?

我是否必须从另一个表中删除数据才能完成此操作?感谢您的帮助.

Do I have to delete data from another table in order to accomplish this? Thanks for your help.

推荐答案

您可以使用以下设置IDENTITY值:

You can use the following to set the IDENTITY value:

DBCC CHECKIDENT (orders, RESEED, 999)

这意味着您将必须基于每个DELETE运行该语句.那应该开始强调为什么这是一个坏主意...

That means you'll have to run the statement based on every DELETE. That should start to highlight why this is a bad idea...

数据库不关心顺序值-仅用于表示.

The database doesn't care about sequential values - that's for presentation only.

这篇关于删除行后重用身份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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