Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hi
i created a table ID int is a primary key for that table i put

Identity Specification :yes
IsIdentity :yes
Identity Increment :1
Identity Seed :1

my problem is

when i inserting records its working fine

but when i deleting records the identity is not decrimented

for example i have 15 records i deleted the 15 th record and inserted the new record but now it takes 16 as new record insted of 15.

The Above experiment is done on Gridview control in asp.net website i did this from Grideview Enable Deleting Property
how to solve this
Posted
Updated 8-Jun-11 21:57pm
v2

No, it won't be. The reason is simple:
What if you have a second table, which contains information related to the first?

For example, if your first table is Customers, and your second is Purchases, then there would be a numeric field in your second table to relate the purchase to a specific customer in the first. If you do not tell SQL about it, it will still work fine, but if SQL re-ordered any identity fields then purchases would not be affected and would reflect on the wrong customer.


No: SQL does not re-order identity fields: in fact, it will not generate the same number twice - that's the whole idea. If you really must work with ordered numbers, then do not use an identity field, but work from the ordinal position of each record for a given sort order.
 
Share this answer
 
Comments
JustWorking 9-Jun-11 3:59am    
cascade update & cascade delete ;)
OriginalGriff 9-Jun-11 4:18am    
Only if you set it: and even then it doesn't know you didn't miss a table...
Plus, you may not want to: if you move a customer to a GoneBustOnUs Table for example.
CPallini 9-Jun-11 4:02am    
My 5. However what you call re-order I call re-number (since deleting rows preserves the order inside the row set).
OriginalGriff 9-Jun-11 4:19am    
It does indeed - but I didn't want to introduce the SQL internals to what is clearly a beginner! :laugh:
JustWorking 9-Jun-11 4:10am    
What about solution number 1?
This is the point of Incriment there is no way to decriment unless you enter the ID manualy and not by autonumber.

You get the MAX ID and add 1 and insert it to the new record.
 
Share this answer
 
Comments
JustWorking 9-Jun-11 3:00am    
PS: If you find my answer helpfull Accept Solution and vote 5 :)
Hi,
The identity is never decremented on delete. It either increments or decrements. You can simulate the behavior of decrement on delete (by resetting the seed), but its strongly discouraged.
Regards
 
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