|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Resetting Identity/Auto-increment Seed[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 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 quoteTibor 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 > > 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 Micus wrote:
Show quote > [VS 2005 Pro, SQL 2000] In addition to Tibor's response, a better option, in my opinion, would > > 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 > > 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. |
|||||||||||||||||||||||