views:

824

answers:

6

I recently added Items to an ID and the the table got messed up in the transfer process so I deleted the Items from the table. Upon reentering the data instead of the ID starting at one it now starts at 332. I would like to have the table start at one instead of 332. I've removed the data from the data so it's clear. How do I reset the ID to one. Thanks and sorry if this on here somewhere I wasn't sure how to search for this.

+3  A: 

truncate table yourtable --will reseed

Oscar Cabrero
+2  A: 

Assuming MSSQL:

DBCC CHECKIDENT('MyTable', RESEED, 0) -- One less than next ID to allocate

If you want to remove the data too you can use

TRUNCATE TABLE MyTable

but you cannot use TRUNCATE TABLE on a table referenced by a Foreign Key, or if the table is part of an indexed view, and unlike DELETE MyTable any trigger(s) on the table won't be activated.

Kristen
+7  A: 

In SQL Server:

DBCC CHECKIDENT (myTable, RESEED, 0)
Chris Pebble
A: 

Set the starting identity value to 1

DBCC CHECKIDENT (tableName, RESEED, 1)
Andrew Robinson
A: 

If you are using MS Access, delete and recreate the table

Noah
Compact and repair is quite sufficient to reset an autonumber, however, if someone is concerned about the value of an autonumber, it is quite likely that they have missed the point and will next want it to be sequential, and this is unlikely over the life of a table.
Remou
A: 

you need to truncate the table

but to do so it has be empty, and no foreign keys attached to it at all

Neil N
You can truncate a table with data in it, it's just a non-logged delete.
JoshBerke