How To Reset Identity Column Values In SQL
Last Updated :
18 Dec, 2024
An Identity Column in SQL is an auto-incrementing column used to generate unique values for each row inserted into a table. This feature is commonly used in tables that require a unique identifier, such as primary keys. The identity column allows the database to automatically handle the generation of unique values without manual intervention from the user.
In this article, we will explain the concept of identity columns, their syntax, managing identity values, and resetting the identity column with examples and outputs. This article is designed to help developers effectively use identity columns to manage unique data in SQL databases.
What is an Identity Column in SQL?
The identity column is a special type of table column that automatically generates unique values for each row. A user generally cannot insert a value into an identity column. These columns are usually used to generate unique identifiers or primary key values without requiring explicit input for every insertion.
Once defined, the identity column generates values based on the specified seed (starting value) and increment (value by which it increases). When defining an identity column in a table, we specify the seed
and increment
values.
Syntax
IDENTITY [ ( seed , increment ) ]
Key Terms
- Seed : The seed represents the starting value of an ID and the default value of seed is 1.
- Increment : It will represent the incremental value of the ID and the default value of increment is 1.
Creating a Table with an Identity Column
When creating a table with an identity column, the identity property automatically generates unique values for each new row. This ensures that each record in the table has a unique identifier, which is especially useful for primary keys. Here, the ‘student_id‘ column of the table starts from 1 as the default value of seed is 1 and each row is incremented by 1.
Query:
CREATE TABLE school (
student_id INT IDENTITY,
student_name VARCHAR(200),
marks INT
);
INSERT INTO school (student_name, marks) VALUES ('Sahil', 100);
INSERT INTO school (student_name, marks) VALUES ('Raj', 78);
INSERT INTO school (student_name, marks) VALUES ('Navneet', 80);
INSERT INTO school (student_name, marks) VALUES ('Rahul', 75);
INSERT INTO school (student_name, marks) VALUES ('Sudeep', 82);
INSERT INTO school (student_name, marks) VALUES ('Azaan', 75);
SELECT * FROM school;
Output

School Table
Managing Identity Column Values
Managing identity column values involves updating or resetting the automatically generated values when necessary, especially after operations like record deletion. After deleting a record, the identity column values may no longer be sequential, requiring actions to maintain consistent and predictable numbering.
Deleting a Record
When rows are deleted, the identity column values remain unchanged, leading to non-sequential numbering.
DELETE FROM school WHERE student_id = 4;
View Updated Records
SELECT * FROM school;
Output

Managing Identity Column Values
Now, we can see that the student_id column is not in order, So we have to reset the Identity Column.
How to Reset the Identity Column in SQL
In some scenarios, we may need to reset the identity column. For example, after deleting rows, we might want to reset the identity values so that new rows start from a specific number or maintain sequential integrity.
Using DBCC CHECKIDENT to Reset Identity Values
We can use the DBCC CHECKIDENT
command in SQL Server to reset the identity column’s value.
Syntax
DBCC CHECKIDENT ('table_name', RESEED, new_value);
Note : If we reset the existing records in the table and insert new records, then it will show an error.
Example: Resetting Identity Column After Deleting Rows
Let’s say we have the School
table with some data, and we want to reset the student_id
after deleting a row.
Step 1: Create a Backup Table
CREATE TABLE new_school AS SELECT student_id, student_name, marks FROM school;
Step 2: Delete All Data from the Main Table
DELETE FROM school;
Step 3 : Reset the Identity column.
DBCC CHECKIDENT ('school', RESEED, 0);
Step 4: Re-insert Data from Backup Table.
INSERT INTO school (student_name, marks) SELECT student_name, marks FROM new_school ORDER BY student_id ASC;
Step 5: View the Reset Table Records
SELECT * FROM school;
Output

Resetting identity Column after deleting rows
Conclusion
In this guide, we covered the concept of identity columns in SQL, how to define and use them, and how to reset the identity values if necessary. The identity column is a powerful feature that simplifies the process of creating unique identifiers automatically, without needing to manually insert values.
Using an identity column is essential for scenarios where we need sequential or unique values, such as primary keys. Understanding how to reset the identity column using the DBCC CHECKIDENT
method also allows us to maintain data integrity and ensure our records follow a predictable sequence.
Similar Reads
How to Set a Column Value to Null in SQL?
You can set a column value to NULL using the SQL UPDATE statement. Through the UPDATE statement, existing records in a table can be changed. The fundamental syntax to set a column value to NULL is as follows. Syntax: UPDATE table_name set column_name=NULL WHERE Conditions; table_name: The name of th
2 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting
4 min read
How to Set a Column Value to Null in PL/SQL?
In PL/SQL, setting a column value to NULL is a common requirement when working with databases. Understanding how to set column values to NULL is essential for database developers and administrators. In this article, we will look into the concept of setting a column value to NULL in PL/SQL, covering
4 min read
How to Set a Column Value to NULL in SQLite?
SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it i
4 min read
How to Rename a Column in View in PL/SQL?
Renaming a column in a view can be a challenging task in PL/SQL, as the language does not provide a direct command for this operation. However, there are several effective methods to achieve this goal without compromising data integrity or the structure of the view. In This article, we will explore
5 min read
Inserting value for identity column in a temporary table in sql
Temporary tables in SQL are widely used for storing intermediate results during query execution, especially in complex procedures or batch operations. In certain scenarios, we may need to insert values into identity columns of temporary tables. By default, identity columns auto-generate their values
3 min read
How to Set a Column Value to NULL in MariaDB
In MariaDB, the NULL represents an unknown value in a column. Changing a column value to NULL is the most common operation performed in MariaDB that allows us to remove existing data in a specific field. It is applicable in different ways including data correction, record inclusions and values setti
4 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How to fix Cannot Insert Explicit Value For Identity Column in Table in SQL
In SQL, encountering the error message "Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF" is a common challenge faced by developers when managing identity columns. Identity columns are a powerful feature that automatically generate unique values, o
8 min read
How to Add an IDENTITY to an Existing Column in SQL Server
It enables you to store, organize, and manipulate data in a relational format, meaning data is organized into tables. It Stores and manages data for dynamic web applications, ensuring effective user experiences. In this article, we will learn about How to add an identity to an existing column in SQL
5 min read