Re: Auto Increase

From: Marcelo Pereira <gandalf(at)sum(dot)desktop(dot)com(dot)br>
To: fcanedo(at)hotpop(dot)com
Cc: Mike Mascari <mascarm(at)mascari(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Auto Increase
Date: 2002-01-24 13:55:00
Message-ID: Pine.LNX.4.20.0201241153020.21224-100000@ni.hmmg.sp.gov.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

--- fcanedo(at)hotpop(dot)com, with his fast fingers, wrote:

:> Nice problem. I solve it by using a trigger that always overrides the
:> value of the id (in your case local_cod) with the value of
:> nextval('some_sequence'). It's also advisable to set the default for your
:> id to some bogus value like 1!!! This way you don't force people to set
:> the value in insert statements.
:>
:> Warning: untested example follows!
:> ======================================
:> CREATE FUNCTION some_trigger_function()
:> RETURNS OPAQUE
:> AS
:> '
:> BEGIN
:> NEW.id := nextval('some_sequence');
:>
:> RETURN NEW;
:> END;'
:> LANGUAGE 'plpgsql';
:> ======================================

Ok, but where (and how) do I have to use this function while creating the
database?

[]'s

Marcelo Pereira

-- Remember that only God and Esc+:w saves.
__
(_.\ Marcelo Pereira |
/ / ___ |
/ (_/ _ \__ Matematica/99 - IMECC |
_______\____/_\___)___Unicamp_______________/

:>
:>
:> On Thu, 24 Jan 2002, Marcelo Pereira wrote:
:>
:> > Hi Mike,
:> >
:> > Ok, I agree. But, take a look:
:> >
:> > => insert into patr_local (local_cod,local_descr) values (3,'local A');
:> > INSERT
:> > => insert into patr_local (local_descr) values ('local B');
:> > INSERT
:> > => insert into patr_local (local_descr) values ('local C');
:> > INSERT
:> > => insert into patr_local (local_descr) values ('local D');
:> > ERROR: Duplicated key
:> > => insert into patr_local (local_descr) values ('local D');
:> >
:> > select * from patr_local
:> > 3 - local A
:> > 1 - local B
:> > 2 - local C
:> > 4 - local D
:>
:>
:>

Browse pgsql-general by date

  From Date Subject
Next Message Lee Kindness 2002-01-24 14:22:57 Select
Previous Message Mail Delivery Subsystem 2002-01-24 13:41:47 Returned mail: see transcript for details