Thread: Can I prevent my sequence to increment if an insert is rejected?
Hi, when I try to insert a record and it curses with fields with a unique key on them my sequence I use for my primary key does increment. How can I prevent this from happening? Thanks Frederic
Re: [SQL] Can I prevent my sequence to increment if an insert is rejected?
From
"tjk@tksoft.com"
Date:
Frederic, You could (a) do the work inside a transaction, or (b) do the insert after you have retrieved the key and verified it doesn't exist in the table. I.e. get the nextval from the sequence first and then do the insert. P.S. If a record was inserted with a used key, wouldn't you want to skip that sequence value anyway? Troy Troy Korjuslommi Tksoft OY, Inc. tjk@tksoft.com Software Development Open Source Solutions Hosting Services > > Hi, > > when I try to insert a record and it curses with fields with a unique > key on them my sequence I use for my primary key does increment. How can > I prevent this from happening? > > > Thanks > Frederic > >
At 19:54 +0300 on 20/07/1999, Frederic De Leersnijder wrote: > Hi, > > when I try to insert a record and it curses with fields with a unique > key on them my sequence I use for my primary key does increment. How can > I prevent this from happening? You can't. The point of sequences is that they ensure you of unique values, but they DO NOT ensure you of gap-free values. This allows sequences to be used momentarily, without being locked for the duration of a transaction, thus allowing many users to get values with minimal contention. Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma
Herouth Maoz wrote: > > At 19:54 +0300 on 20/07/1999, Frederic De Leersnijder wrote: > > > Hi, > > > > when I try to insert a record and it curses with fields with a unique > > key on them my sequence I use for my primary key does increment. How can > > I prevent this from happening? > > You can't. The point of sequences is that they ensure you of unique values, > but they DO NOT ensure you of gap-free values. This allows sequences to be > used momentarily, without being locked for the duration of a transaction, > thus allowing many users to get values with minimal contention. I missed the original question, but don't you get sequential numbers as long as cache is 0? -- Chris Bitmead mailto:chris@tech.com.au http://www.techphoto.org - Photography News, Stuff that Matters
At 12:36 +0300 on 21/07/1999, Chris Bitmead wrote: > I missed the original question, but don't you get sequential numbers as > long as cache is 0? No. If an insert failed or a transaction failed, the sequence remains in its new state. Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma