Identity Column in SQL Server

This article explains how to supply explicit values for an identity column and how to reset it.

Identity Column

Identity columns are columns with values automatically generated when a new row is inserted into a table.

Let's look at an example.

Example without identity column

I have created a database named “IdentityDatabase” and in that I have created a first table named “NonIdentityTable” that has the two columns Id (primary key) and Name.

  1. CREATE DATABASE IdentityDatabase;  
  2. GO  
  3. USE IdentityDatabase;  
  4. GO  
  5. Create table NonIdentityTable(  
  6. Id INT CONSTRAINT pk_Id PRIMARY KEY,  
  7. Name NVARCHAR(20) NOT NULL  
  8. );  
  9. GO  
  10. INSERT INTO NonIdentityTable (Id,NameVALUES (1,'abc'),(2,'def'),(3,'ghi'),(4,'jkl');  
Above, I have inserted four rows by specifying the values for both the columns.

(If the column is a non-identity column, then we need to pass the value for that column.)

If you executed the following query, you will get the output as:
  1. SELECT * FROM NonIdentityTable;  


Now what if I don't want to insert the values for the Id column and whenever I insert a new row, I want the values for the id column to be inserted automatically? For that we can create an Identity column.

Example using identity column

In the same database I have created another table named “IdentityTable” that has two columns, Id and Name, where the Id column is an identity column and also a primary key column.
  1. Create table IdentityTable(  
  2. Id INT IDENTITY(1,1) CONSTRAINT pk__Id PRIMARY KEY,  
  3. Name NVARCHAR(20) NOT NULL  
  4. );  
IDENTITY(1,1)

The first parameter is the seed (from where the value will start) and the second parameter is the increment value.

Note: Seed and Increment values are optional. If you don't specify it, then by default they both are 1.

Let's try to insert rows by providing values for both of the columns as in the following:
  1. INSERT INTO IdentityTable(Id,NameVALUES (1,'abc'),(2,'def'),(3,'ghi'),(4,'jkl');  


We get an error. So, when a column in a table is an identity column then we won't be able to insert the values for that column explicitly.
  1. INSERT INTO IdentityTable(NameVALUES ('abc'),('def'),('ghi'),('jkl');  
  1. SELECT * FROM IdentityTable;  


Let's look at another example, where you must delete a record with an id value of 2.
  1. DELETE FROM IdentityTable WHERE Id = 1;  
  1. SELECT * FROM IdentityTable;  


But by mistake you have deleted the wrong record and you want to retrieve that record.

In this kind of situation what can do is to write an insert query as in the following:
  1. INSERT INTO IdentityTable (Id,Namevalues (1,'abc');  
But when you execute the preceding query, you will get an error stating:


 
So, first we need to turn the Identity_Insert for IdentityTable to ON.

Syntax
  1. SET IDENTITY_INSERT tablename ON;  
Query
  1. SET IDENTITY_INSERT IdentityTable ON;  
The preceding query will allow us to insert the value for the Id column explicitly.
  1. INSERT INTO IdentityTable (Id,Namevalues (1,'abc');   
  1. SELECT * FROM IdentityTable;    


Note:
Always set the Identity_Insert for a table to ON, only when these kind of issues occur.

To turn off
  1. SET IDENTITY_INSERT IdentityTable OFF;  
Let's look at another example, where there is requirement to delete all the records and add new records.

To delete all the records from a table, we write the following query:

Syntax
  1. DELETE FROM tableName;  
Query
  1. DELETE FROM IdentityTable;  


Currently all the records are deleted. So, now let's add some new records.
  1. INSERT INTO IdentityTable(NameVALUES ('Sam'),('Aiden'),('James'),('Sara');  

  1. SELECT * FROM IdentityTable;    


We have added the new records to our table. But there is a slight problem in the Id column. After deleting the old records, the Id column still remembers the last incremented value, in other words 4, and when we insert a new record it increments by 1 and inserts the value 5 and so on. But we want the Id value to start from 1 and for that it is very important to reseed the Identity columns, whenever you delete all the rows.

But first remove the recently added rows from the table again.
  1. DELETE FROM IdentityTable;  


To reseed an Identity column write and execute the following query:
  1. DBCC CHECKIDENT ('IdentityTable',RESEED,0)  


What this preceding query will do is, it will check the current last identity value of the given table and will reseed it.

Insert the values again.
  1. INSERT INTO IdentityTable(NameVALUES ('Sam'),('Aiden'),('James'),('Sara');  
  1. SELECT * FROM IdentityTable;  

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now