December 5, 2008 at 4:25 pm
We have a table like this
ID Val
1 Data item 1
2 Data item 2
3 Data item 3
4 Data item 4
5 Data item 5
where ID ia a primary key (referenced by other tables) as well as an Identity field. Suppose we delete record 2 so the table
now looks like this:
ID Val
1 Data item 1
3 Data item 3
4 Data item 4
5 Data item 5
We want the next record added to this table to take the place of the deleted recrod:
ID Val
1 Data item 1
2 New Data item 2
3 Data item 3
4 Data item 4
5 Data item 5
and the next record after that oen added like this:
ID Val
1 Data item 1
2 New Data item 2
3 Data item 3
4 Data item 4
5 Data item 5
6 Data item 6
In short, in this case we want the primary key of the deleted record to be reused and all following
records to use the the value generated by the Identity field. The actual table could contain multiple
"deleted" rows and the number of them determine when we restart the numbering based on the Identity
field.
Any suggestions how to do this appreciated.
TIA,
barkingdog
December 5, 2008 at 4:28 pm
Very simple: don't.
Identities should not be reused - and, you really shouldn't care what the actual number is. If you really care about what the number is, then you probably shouldn't be using an identity in the first place.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
December 5, 2008 at 4:55 pm
Barkingdog... can you please explain WHY you want to reuse those numbers? What you want to do isn't impossible, although you couldn't designate the column to be an identity column. But there may be much better answers if you are just using them to designate a sequence.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 5, 2008 at 5:08 pm
If you're using it as a Primary Key, then I'm on board with Jeffrey Williams - don't do it. This sets up "evil twin" scenarios and/or relational integrity problems which will make your head spin. Nothing quite like finding you had an unenforced "relation" that now matches something that USED to be related to an older RowID 2, to a NEW completely different RowID 2. And it's an invoicing scheme, so someone just got billed for something they didn't get...That way leads to much pain, which of course leads to the Dark side. Find another way young padawan....
If you're using it for some other purpose than PK, then Bob is onto something (there are better ways to do that most of the time - so what's the goal?)
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 6, 2008 at 7:02 pm
Heh... one religous battle coming up... ready?
NEVER DELETE! NEVER REUSE PK's!
--Jeff Moden
Change is inevitable... Change for the better is not.
December 7, 2008 at 2:58 am
in the interests of database\referential integrity don't re use them. As stated, if you're needing to re use the column it probably shouldnt be a PK.
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
December 7, 2008 at 3:13 am
As others already recommended - don't reuse the delete primary key. It will only lead to integrity problems, but I am curios to know why you want to reuse the deleted values. Can you write a bit about your situation? Maybe we will be able to suggest a better alternative.
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
December 7, 2008 at 10:12 am
Jeff Moden (12/6/2008)
Heh... one religous battle coming up... ready?NEVER DELETE! NEVER REUSE PK's!
I have to say - we haven't managed to trigger a full-out religious war on any topic in some time now. Very strange...:):w00t:
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 7, 2008 at 10:55 am
No heretical rebuttals from me. I just want to know if they are using that column to enforce a sequence.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 8, 2008 at 9:10 am
The idea was to always have the primary key sequence like this : 1,2,3,4.... (a psychological, not computer, issue.)
WELL, after reading the replies to the question I have scrapped the idea as being thoroughly bad and wil not attempt to implement it.
Thanks to everyone for their guidance.
Barkingdog
December 8, 2008 at 9:48 am
Good plan. Psychological issues are better served by counseling 😉
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 9, 2008 at 10:31 pm
... heh... or some very high velocity pork chops fired at point blank range. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2008 at 11:20 am
By trebuchet or air cannon?
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 10, 2008 at 12:10 pm
Jeff Moden (12/9/2008)
... heh... or some very high velocity pork chops fired at point blank range. 😉
Of course - close encounters with high velocity pork chops (whether fired from slingshot, trebuchet or whatever) might result in needing the previously mentioned counseling....:)
"Traumatic flying pork stress disorder"....:D:sick:
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 10, 2008 at 12:45 pm
:w00t:
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply