Reset identity seed after deleting records in SQL Server
#1
I've encountered an issue with a table in SQL Server where I have an identity column for automatically generating unique index values. It's important for application design that the identity values are in a continuous sequence. After deleting some records, I observed the gaps in the sequence, since SQL Server doesn't automatically reset the identity seed when a record is deleted, which results in those gaps.
To demonstrate, suppose I inserted records and the identity values were 1, 2, 3, 4, 5. If I then delete the record with identity value 3, the next record I insert will have the identity value of 6, thus creating a gap in the sequence.
Here's an example of what the table and inserts might look like:

Code:
);
INSERT INTO ExampleTable(Data) VALUES('First'), ('Second'), ('Third'), ('Fourth'), ('Fifth');
--Now we delete a record
DELETE FROM ExampleTable WHERE ID = 3;

What I want to achieve is to reset the identity column after deleting the records. How can I accomplish this without affecting the integrity and performance of my database? The identity column is not used as a foreign key anywhere in the database, so there shouldn't be any problems with relational integrity in this specific case.
Reply
#2
To reset the identity column after deleting records, you can use the DBCC CHECKIDENT command, which manually sets the identity seed to a new value. Keep in mind that resetting the identity value might result in identity values being reused if there were data associated with those identity values previously.
Keep in mind that if your table has a lot of records, manipulating the identity column can lock the table and affect performance. Here's the SQL command to reset the identity value after deleting the records:


This command will set the identity seed to 0, which means the next record inserted will have an identity value of 1 (or whatever your increment is set to). If you want to continue from the last existing identity value, you would first need to find that value and then use it in the command like this:

Reply
#3
I see your point. However, if the table is completely empty after deletion and I want to completely reset the identity column to start fresh from the initial seed value, would the approach be different?
Reply
#4
If you've deleted all records and want to start the identity seed from its initial value (which is usually 1), you can still use the DBCC CHECKIDENT command. Since there are no records, simply reseed it back to 0:


This sets the seed to 0, so the next record inserted will start at 1 again. But remember, if your table has retained some records, ensure you specify the correct maximum value plus one, otherwise you risk creating duplicate identity values, which will result in an error if the ID column is a primary key.
Remember to always back up your database before performing operations like this, to prevent data loss in case something goes wrong. And please do adequate testing on a development or staging environment before executing these commands on a production database.
Reply
#5
Alright, that seems straightforward. To summarize for anyone following this thread, here's a working example including the necessary T-SQL code to reset the identity column after deleting records when the identity is not related to any foreign key:

Code:
);
--Populate the table with data
INSERT INTO ExampleTable(Data) VALUES('First'), ('Second'), ('Third'), ('Fourth'), ('Fifth');
--Delete a record
DELETE FROM ExampleTable WHERE ID = 3;
--Now
let 's reset the IDENTITY
DBCC CHECKIDENT('ExampleTable', RESEED, 0);
--Alternatively,
if you need to
continue from the last highest value
DECLARE @MaxIdentityVal INT;
SELECT @MaxIdentityVal = MAX(ID) FROM ExampleTable;
DBCC CHECKIDENT('ExampleTable', RESEED, @MaxIdentityVal);

Bear in mind that careful consideration should be given before you perform such resets, as this can have implications on the data consistency, integrity, and how the application logic interprets the IDs.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)