Reset identity seed after deleting records in SQL Server
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with SQL Server, managing the identity seed of an incremental primary key after records have been deleted can play a crucial role in maintaining data integrity and ensuring consistency in your database. In this article, we’ll discuss how and why to reset the identity seed and dive into some practical examples.
Understanding Identity Seed
In SQL Server, an identity column automatically generates sequential numeric values. Every time a new record is inserted into the table, the identity seed (also known as the identity column) increments based on a defined interval, often set to 1. This property is typically used for generating unique primary keys. The seed maintains the last used value, and after a record deletion, it does not automatically reset, which may lead to gaps in the key sequence.
Why Reset the Identity Seed?
Resetting the identity seed is particularly useful after deleting records in bulk. This practice can help in:
- Avoiding huge gaps in the primary key values, especially after deleting a large number of records which might otherwise suggest to an observer or end user that data is missing.
- Maintaining smaller numbers for readability and management, especially when data is archived or removed based on its age or relevance.
It might not always be necessary to reset the identity seed. For tables with high transaction volumes and where the gaps are inconsequential for the application’s functionality and reporting, resetting can be omitted.
How to Reset Identity Seed in SQL Server
To reset the identity seed, you can use the DBCC CHECKIDENT command. This command allows you to manually set the seed value for an identity column. Below is a typical use case for resetting an identity seed after deleting records:
Example: Resetting Identity After Deletion
Suppose you have a table named Orders with an identity column OrderID. If you delete some of the order records and want to reset the identity seed to the last highest value, you would proceed as follows:
- Delete the records:
- Reset the identity seed:First, find the maximum
OrderIDcurrently in use after deletion:
Assume the result is 1000. Now reset the identity:
The next order inserted will have an OrderID of 1001.
Considerations When Using DBCC CHECKIDENT
The DBCC CHECKIDENT command can be used with care:
- If it’s set to a number lower than the current maximum identity value, SQL Server does not prevent inserting duplicate identity values, which could lead to primary key violations.
- Reseeding to 0 or a negative number is allowed but the next inserted value will be 1 or 0, respectively.
Summary Table
Here's a quick summary of key points about resetting identity seeds:
| Feature | Detail |
| Primary Use Case | Rediscovering sequence after bulk deletions |
| SQL Command | DBCC CHECKIDENT |
| Argument 1 (Table name) | The table whose identity needs reset |
| Argument 2 (Operation) | RESEED |
| Argument 3 (New Seed Value) | The new value to reseed to |
| Consideration | Avoid lower than max existing value |
Conclusion
Understanding how to reset the identity seed after deleting records in SQL Server is an essential skill for maintaining a clean and orderly database. It can help prevent potential issues with primary key gaps and ensure the consistency and correctness of data in systems where accurate sequential numbering is required. Always be cautious with its usage, as improper reseeding can lead to data integrity issues.

