Identity Column in Sql server ~ MiTechDev.com

Thursday, 9 March 2017

Identity Column in Sql server

21:01:00 Posted by raviteja swayampu , No comments
What is Identity Column:
We can say an Identity column is an auto generating column.If a column is marked as an identity column then the values of the column are automatically generated(It must be of type int).

when you insert a new row into the table. The following,create table statement marks 'Id' as an identity column with seed = 1 and Identity Increment = 1.Seed and Increment values are optional. If you don't specify the identity and seed they both default to 1.
CREATE TABLE TUser
(
  UserID int IDENTITY(1,1) PRIMARY KEY,
  Name NVARCHAR(MAX)
)
Now while inserting values into table TUser we need not to give values for Identity column (UserID) like below.
INSERT INTO TUser(Name) VALUES('RAVITEJA')
INSERT INTO TUser(Name) VALUES('KRISHNA')
INSERT INTO TUser(Name) VALUES('GOPAL')
The values will be inserted into table like below.

Note:Identity column do not allow any user input if we try to insert any thing into Identity column we will get a message like below.


I got a message while tried to execute below statement.
INSERT INTO TUser(Name) VALUES(4,'RAVITEJA')
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
So if you mark a column as an Identity column, you dont have to explicitly supply a value for that column when you insert a new row. The value is automatically calculated and provided by SQL server.

Now,delete the row that you have just inserted and insert another row. You see that the value for UserID is 2. Now if you insert another row UserID is 3. A record with UserID = 1, does not exist, and I want to fill this gap. To do this, we should be able to explicitly supply the value for identity column. To explicitly supply a value for identity column we have to turn on the Identity insert on particular column.

SET IDENTITY_INSERT TUSER ON
now we can insert value into column explicitly
INSERT INTO TUser(UserID,Name) VALUES(1,'Joseph')
As long as the Identity_Insert is turned on for a table, you need to explicitly provide the value for that column. If you don't provide the value, you get an error like this..
Msg 545, Level 16, State 1, Line 1
Explicit value must be specified for identity column in table 'TUser' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
After, the gaps in the identity column filled, and if you wish SQL server to calculate the value, turn off Identity_Insert.
SET IDENTITY_INSERT TUSER ON
If you have deleted all the rows in a table, and you want to reset the identity column value, use DBCC CHECKIDENT command. This command will reset UserID identity column.
syntax:
 DBCC CHECKIDENT(TUser, RESEED, 0)
After execution of this command in sql sever i got below message...
Checking identity information: current identity value '3', current column value '0'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
After reinsert values into table Identity column index start with '0'....

0 comments:

Post a Comment