Click here to Skip to main content
Click here to Skip to main content

How to RESET identity columns in SQL Server

By , 6 Oct 2011
 

Introduction

During application development, we often input dummy data into our database for testing purposes. But then we come to the point where we want all records of the table to be deleted and also want to start the identity column values from 0. For this, we delete existing data using the truncate command. This will delete data from table and also reset the identity column value to 0.

Solutions

One way is...

truncate table [table_name]
-- for example
truncate table product

But the truncate command fails to delete the data if there is a relationship given to the table and the identity column is not reset.

The other way is...

In this case, first you need to delete data from the child and the master table.

After deleting data, fire this command and it will reset your identity column to 0.

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

License

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

About the Author

Tejas Vaishnav
Software Developer
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberStrange_Pirate29 Sep '12 - 0:13 
GeneralMy vote of 1memberStrange_Pirate29 Sep '12 - 0:05 
QuestionNeed Your expert advisememberStrange_Pirate28 Sep '12 - 2:01 
GeneralReason for my vote of 5 Never used truncate before this :P n...memberWongSSJ19 Jan '12 - 12:01 
GeneralRe: Reason for my vote of 5Never used truncate before this :P n...memberTejas_Vaishnav21 Sep '12 - 2:44 
GeneralReason for my vote of 5 useful informationmemberThi Dang T11 Oct '11 - 11:24 
GeneralRe: Reason for my vote of 5useful informationmemberTejas_Vaishnav21 Sep '12 - 2:45 
BugRe: Reason for my vote of 5useful informationmemberStrange_Pirate29 Sep '12 - 0:06 
GeneralReason for my vote of 4 Nicememberkiran dangar5 Oct '11 - 1:15 
GeneralRe: Reason for my vote of 4NicememberTejas_Vaishnav21 Sep '12 - 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 7 Oct 2011
Article Copyright 2011 by Tejas Vaishnav
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid