SQL Server 2008中 [英] sql server 2008

查看:80
本文介绍了SQL Server 2008中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,正在测试中.

我有一个问题.

当我从数据库中删除所有数据时,我将"id"字段设置为自动递增1,再次将数据添加到数据库,然后"id"字段从最后一个"id"旁边开始.

假设删除前为20,删除后为21

在删除所有数据后,我想从1重新开始.

I''m developing a website, it is on testing.

I have one problem.

I set my "id" field as auto incremented by 1 when I delete all data from the database, again I add data to the database then "id" field starts from next to the last "id".

Suppose 20 before deletion after deletion 21

I want to start again from 1 after deleting all data.

推荐答案

您可以按照此
You can do this using SQServer Management Studio as outlined in this article[^].

Although if your website is still in development, why bother?


嗨Somendra,
如果要在删除所有数据后从1重新开始,则应使用"Truncate"命令来完成所有工作,删除所有记录并将Identity属性重置为其初始值,而不是使用"Delete"命令您的情况是1.
您可以在此处
删除并截断命令

但是,如果只想使用"Delete"命令,则可以重置identity属性.有关重置identity属性,请参见以下有关代码项目的文章.
Sql Server中的身份属性

如果您对我的答案有任何疑问,请告诉我.

问候
Vivek Johari
Hi Somendra,
If you want to start again from 1 after deleting all data, then instead of using "Delete" command you should use the "Truncate" command as it done both the works, deleting all the records and resets the Identity property to its initial value which is in your case is 1.
you can see the difference between the "Delete" and "Truncate" command here Difference between Delete and Truncate command

But if you want to use only the "Delete" command, then you can reset the identity property.For reset the identity property, you can see the following article on Code project.
Identity property in Sql Server

Please let me know if you have any query regarding my answer.

Regards
Vivek Johari


这不是该字段的工作方式.要按照您的意愿进行操作,您将需要实现一些复杂的逻辑以找到下一个可用的ID,但是那样就不再是自动递增的字段了. ID是一般的内部引用,如果它以21而不是1开头怎么办?
That is not how the field operates. To do as you wish you would need to implement some complex logic to find the next available ID, but then it wouldn''t be an autoincremented field any longer. IDs are general internal references, what does it matter if it starts at 21 rather than 1 ?


这篇关于SQL Server 2008中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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