This page was exported from - TechnicalStack Export date: Sat Oct 2 2:47:19 2021 / +0000 GMT

Identity Column in SQL Server

Manytimes we need a column whose values can uniquely identifying the rows in the .This column should contains the unique values and it can't contains the NULL or empty values. This coloumn can be used in the ?Where? clause.

At Times by mistake we may miss to the data into this column or insert the duplicate data into this column. So we need a mechanism to handle such case here in SQL Server does this job for us.It 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. This coloumn can be used as of the table.

For example:

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

In this example the column Studentid is 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(‘Rajesh’, ‘Shailesh’, [email protected]’) 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 .

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, use the below command.

Set Identity_insert Tablename On

Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 1/2 | This page was exported from - TechnicalStack Export date: Sat Oct 2 2:47:19 2021 / +0000 GMT

For Example: set identity_insert Student on

Some time we may need to reset the values of the identity column to some different value with the help of the following command:- dbcc checkident (Tablename, reseed, 10)

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)

Remember:

- Only one Identity column is possible for a table. - In case of command on a table the identity column value is reset to 0.

Getting the last generated identity column value in SQL Server]

In SQL server the last added identity column values can be obtained using the below three ways: 1>SCOPE_IDENTITY() 2>@@identity 3>IDENT_CURRENT('TableName')

Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 2/2 |