Thursday, November 12, 2009

IDENTITY Property in SQL Server


IDENTITY Property
Sometimes we need a column whose values can uniquely identifying the rows in the table. To achieve this purpose, this column should contains the unique values and it can’t contains the NULL or empty values. We can use this column in the “Where” clause of the Select statement to retrieve the data from the table quickly.

But as a human being we sometime forget to insert the data into this column or sometimes we insert the duplicate data into this column. So it will be very helpful if the SQL Server itself insert the value in this column whenever we try to insert any row into the table. SQL Server does this with the help of the identity column. We can set the initial value for this column and the value which the previous value is incremented to get the new value. We sometimes use this like the primary key of the table.



For example, suppose we want to create a table named student whose structure is given below




           CREATE TABLE Student (Studentid int IDENTITY (1, 1) NOT NULL,Firstname nvarchar (200) NULL,Lastname nvarchar (200),Email nvarchar (100) NULL )


Here column Studentid is defined as the identity column. In the Identity column, the value of the first argument defined the initial value for this column and the value of the second argument defined the value used to add in the last value of this column for getting the next value.


Now if we want to insert the row in the table Student, we do not need to specify the value for the column Studentid. For Example,



              insert into Student (Firstname,Lastname,Email)
              Values('Vivek', 'Johari', ‘vivekjohari@abc.com')



Here we do not specify the value for the studentid column in the Insert Statement.


Although the Identity column is very useful in maintain the data in the table, sometimes we need to set this constraint off like when we importing the data into the database.


To set the Identity column property off, the following command is used.



          Set Identity_insert Tablename Off



For Example if we want to keep the identity column (studentid) property off for the Student table, we need to use the following Sql Query:-



         set identity_insert Student off



To set the Identity column property On, the following command is used.



         Set Identity_insert Tablename On


For Example,


         set identity_insert Student on


We can also reset the values of the identity column to some different value with the help of the following command:-


          dbcc checkident (Tablename, reseed, 10)



For example, to reset the value of the studentid column of the table Student we need to use the following Sql Command

         dbcc checkident (Student, reseed, 10)




Summary:-
Identity column in the Sql server is a very useful property and it can be used to retrieve the data very quickly specially in the table where no primary key is defined.


Note:-
1) Only one Identity column is possible for a table.
2) In case of truncate command on a table the identity column value is reset to its initial seed value.

7 comments:

  1. What does identity insert mean, and why would you want to turn it off when importing data?
    Is that a situation when the data being imported already has a StudentID and you want to maintain it?

    I ask because I've have been importing data from an Access Database into an SQL Server database, and the tables I'm importing into already have an Identity Column. When importing, I have simply been selecting the "Append data to exisiting table" option and 'ignoring' the PK (Autonumber) column in Access, bringing in only those columns that I need.

    The data comes into my table fine, but I've never had to select the "Enable Identity Insert" option to do so. What exactly does that option do?

    Thanks!

    ReplyDelete
  2. Nice Post Sir!!!

    I think your last statement of this post needs to be corrected.

    i.e."In case of truncate command on a table the identity column value is reset to 0."

    As per my knowledge after Truncate, the counter for that column is reset with the seed value. Which you have passed during identity column creation.


    ReplyDelete
    Replies
    1. Thanks Vimal..:-)
      I corrected it.
      Regards
      Vivek

      Delete
  3. You pointed out two common mistakes that people usually make. It is really easy to forget to insert the data in the right column. The error may not even be noticed until much later and then you have to go through step by step to do the correction.

    ReplyDelete
  4. Even though i have been writing sql queries for couple of year i have always struggled updating existing datatypes and add new columns to existing tables. thanks for this tutorial.

    ReplyDelete
  5. I find out some useful information for me here. So i want to say thank you for your sharing.

    ReplyDelete