Re: primary and candidate index by Rush
Rush
Wed Sep 17 14:10:42 CDT 2003
I'm not one to recycle PKs in the first place, so I won't argue in its
favor - but my assumption is that rather than create a "new" ID 317, your PK
generator would first look for a deleted record to recycle, then either
toggle the ID back to positive, or replace it entirely. In any case, it
should not reassign a PK that had already been created - this is the type of
fault that GUIDs avoid!
And while GUIDs are big and ugly, I try not to spend much time looking at
them. Their size was once a concern, but when you look at how dramatically
we see improvements in disk storage space, processor speed, and RAM, I
think that such issues are becoming more and more trivial.
I like the GUID approach most because it eliminates the overhead involved in
assuring uniqueness, which saves me time in development. There may be a
performance advantage in using integers, but I suspect that it is immaterial
in most applications.
- Rush
"Tim Witort" <trw7at@ixdot.netcomdotcom> wrote in message
news:Xns93F973A54FA08timwitortwrotethis@207.217.77.202...
> That won't work, Rush.
>
> Consider a table with the primary key being the "emp_id" field:
>
> Record with ID 317 is created:
>
> emp_id deleted?
> ------ --------
> 317 No
>
> Record with ID 317 is deleted, ID changed to -ABS(emp_id)
>
> emp_id deleted?
> ------ --------
> -317 Yes
>
> Record with ID 317 is created (as per the original question)
>
> emp_id deleted?
> ------ --------
> -317 Yes
> 317 No
>
> Record with ID 317 is deleted, ID changed to -ABS(emp_id)
>
> emp_id deleted?
> ------ --------
> -317 Yes
> -317 Yes
>
> Primary key violation.
>
> Leaving the second deleted record positive only delays the
> primary key violation to the next record insertion:
>
> Record with ID 317 is deleted, ID unchanged
>
> emp_id deleted?
> ------ --------
> -317 Yes
> 317 Yes
>
> Record with ID 317 is created
>
> emp_id deleted?
> ------ --------
> -317 Yes
> 317 Yes
> 317 No
>
> Primary key violation.
>
> Without knowing the design of the application, who can
> say whether any of these scenarios are "odd" or not?
> They may be quite common. GUIDs are an alternative to
> surrogate integer keys. They're just big and ugly.
>
> -- TRW
>
> Rush Strong seemed to utter in
> news:ucTph$#eDHA.1060@TK2MSFTNGP12.phx.gbl:
>
> > Deleting twice? Odd that the user would have that opportunity - but
> > surely;
> >
> > REPLACE pk WITH -ABS(pk)
> >
> > is a trivial fix. As far as "most reliable" is concerned, I'm not
> > convinced that using GUIDs is any less reliable . . . maybe more so.
> >
> > - Rush
> >
> > "Tim Witort" <trw7at@ixdot.netcomdotcom> wrote in message
> > news:Xns93F75CECDA975timwitortwrotethis@207.217.77.203...
> >> That will work until the user deletes the same record twice.
> >> Then you will get a primary key violation on trying to set
> >> the key to the negative value.
> >>
> >> As mentioned by others, the most reliable way is to have
> >> surrogate integer primary keys that are automatically
> >> generated via the default value in the field settings.
> >> Once this is in place and done correctly (which is not
> >> difficult), you will never see another primary key
> >> violation because every record will always have a
> >> unique key value - whether deleted or not.
> >>
> >> -- TRW
> >>
> >> Anders Altberg seemed to utter in news:#tGFfWpeDHA.1700
> >> @TK2MSFTNGP10.phx.gbl:
> >>
> >> > Before deleting, change the pk number to -pknumber.
> >> > REPLACE pk WITH -pk
> >> >
> >> > -Anders
> >> >
> >> > "sangeko" <not_this@another.else> wrote in message
> >> > news:0ca201c3791d$aa9e2810$a101280a@phx.gbl...
> >> >> Primary and candidate index best to avoid double data,
> >> >> but unfortunately in share environment We ecounter problem.
> >> >>
> >> >> Because we cannot pack the deleted record, meanwhile index
> >> >> ignore the state of delete so it still exists in index.
> >> >> When we add a record with key that has been deleted but
> >> >> not packed yet, it will violate the unique.
> >> >>
> >> >> Is there any way to solve this ?
> >> >>
> >> >> Thanks in advance
> >> >
> >> >
> >>
> >>
> >>
> >> --
> >> _______________________________________
> >> My e-mail: t r w 7
> >> @ i x . n e t c o m . c o m
> >> _______________________________________
> >
> >
> >
>
> --
> _______________________________________
> My e-mail: t r w 7
> @ i x . n e t c o m . c o m
> _______________________________________