Home All Groups Group Topic Archive Search About

Resetting Identity/Auto-increment Seed

Author
14 Sep 2006 1:26 PM
Micus
[VS 2005 Pro, SQL 2000]

Hi all,

     I've been using an SQL 2000 database for testing/debugging code. The
code is ready for the real world however I would like to purge all content
from the database and reset it to a 'new' state. Specifically, a few tables
contain an auto-increment (Identity) integer field which the counter will
not return to zero even though all the rows have been deleted. For example,
I insert 50 rows into a table and the auto-incr field is numbered 1-50. I
delete all the rows and insert another 50, but the auto-incr field is
numbered 51-100. Can anyone recommend a course of action to reset the field
counters?

Thanks,
M

Author
14 Sep 2006 1:31 PM
Tibor Karaszi
TRUNCATE TABLE will reset the identity increment and is also faster than DELETE.

But it won't work if the table is referenced by foreign keys. In this case, use DBCC CHECKIDENT.

Show quote
"Micus" <no_one@no_where.com> wrote in message news:OidsiFA2GHA.2196@TK2MSFTNGP06.phx.gbl...
> [VS 2005 Pro, SQL 2000]
>
> Hi all,
>
>     I've been using an SQL 2000 database for testing/debugging code. The
> code is ready for the real world however I would like to purge all content
> from the database and reset it to a 'new' state. Specifically, a few tables
> contain an auto-increment (Identity) integer field which the counter will
> not return to zero even though all the rows have been deleted. For example,
> I insert 50 rows into a table and the auto-incr field is numbered 1-50. I
> delete all the rows and insert another 50, but the auto-incr field is
> numbered 51-100. Can anyone recommend a course of action to reset the field
> counters?
>
> Thanks,
> M
>
>
Author
14 Sep 2006 1:37 PM
Micus
Wow! that was fast.... Thanks Tibor....

Show quote
"Tibor Karaszi" <tibor_please.no.email_kara***@hotmail.nomail.com> wrote in
message news:%232$YKIA2GHA.2376@TK2MSFTNGP05.phx.gbl...
> TRUNCATE TABLE will reset the identity increment and is also faster than
> DELETE.
>
> But it won't work if the table is referenced by foreign keys. In this
> case, use DBCC CHECKIDENT.
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
>
> "Micus" <no_one@no_where.com> wrote in message
> news:OidsiFA2GHA.2196@TK2MSFTNGP06.phx.gbl...
>> [VS 2005 Pro, SQL 2000]
>>
>> Hi all,
>>
>>     I've been using an SQL 2000 database for testing/debugging code. The
>> code is ready for the real world however I would like to purge all
>> content from the database and reset it to a 'new' state. Specifically, a
>> few tables contain an auto-increment (Identity) integer field which the
>> counter will not return to zero even though all the rows have been
>> deleted. For example, I insert 50 rows into a table and the auto-incr
>> field is numbered 1-50. I delete all the rows and insert another 50, but
>> the auto-incr field is numbered 51-100. Can anyone recommend a course of
>> action to reset the field counters?
>>
>> Thanks,
>> M
Author
14 Sep 2006 2:54 PM
Tracy McKibben
Micus wrote:
Show quote
> [VS 2005 Pro, SQL 2000]
>
> Hi all,
>
>      I've been using an SQL 2000 database for testing/debugging code. The
> code is ready for the real world however I would like to purge all content
> from the database and reset it to a 'new' state. Specifically, a few tables
> contain an auto-increment (Identity) integer field which the counter will
> not return to zero even though all the rows have been deleted. For example,
> I insert 50 rows into a table and the auto-incr field is numbered 1-50. I
> delete all the rows and insert another 50, but the auto-incr field is
> numbered 51-100. Can anyone recommend a course of action to reset the field
> counters?
>
> Thanks,
> M
>
>

In addition to Tibor's response, a better option, in my opinion, would
be to generate DDL scripts for all of your database objects, and then
use those scripts to create a new "live" database.  This gives you a
couple of things:

1.  A clean, untouched database to start fresh with

2.  DDL scripts that can be checked into a source control package, and
then used as a baseline for making future database modifications.


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com

AddThis Social Bookmark Button