Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Ex: If I have table Employee with column EmpId INT(IDENTITY(1,1),EmpName VARCHAR(60)
table looks like before delete
--------- -------
EmpId EmpName
1 xxx
2 yyy

table after delete second row i.e EmpId =2
--------- -------
EmpId EmpName
1 xxx

table after insert new row
--------- -------
EmpId EmpName
1 xxx
3 zzz

Here comes my question
How this EmpId is automatically incremented as 3 instead of 2. Is there any temporary table to store the last value of Identity column ?
Or
How it is getting Identified for the next row of the table's EmpId need to given as 3?

What I have tried:

I tried Google search but result comes out related to changing the identity value using "Reseed" in SQL SERVER
Posted

Don't.
You can do it - but it's really dangerous, particularly in a multi user system like SQL.
IDENTITY columns are supposed to give a unique value to a row, not to provide a sequential number that has no gaps. If you want that, then use ROW_NUMBER (Transact-SQL)[^] instead - it provides a sequence for the current SELECT statment.
 
Share this answer
 
The ID sequence is simply a property of the table that the database engine in question maintains, it isn't held in a table on its own. If you want to alter this number then the process is known as "re-seeding" but there are many reasons why you shouldn't alter the number unless you really have to, it is fine to have "gaps" in your IDs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900