Re: Auto Increase - Mailing list pgsql-general

From Marcelo Pereira
Subject Re: Auto Increase
Date
Msg-id Pine.LNX.4.20.0201241153020.21224-100000@ni.hmmg.sp.gov.br
Whole thread Raw
In response to Auto Increase  (Marcelo Pereira <gandalf@sum.desktop.com.br>)
List pgsql-general
--- fcanedo@hotpop.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
:>
:>
:>


pgsql-general by date:

Previous
From: Lee Kindness
Date:
Subject: Select
Next
From: Bo Lorentsen
Date:
Subject: Re: Select