Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 27-Nov-10 10:10am
v2

You can do this using SQServer Management Studio as outlined in this article[^].

Although if your website is still in development, why bother?
 
Share this answer
 
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
 
Share this answer
 
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 ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900