Re: easy way to insert same value into 2 columns

From: Sebastian Boeck <sebastianboeck(at)freenet(dot)de>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: Csaba Nagy <nagy(at)ecircle-ag(dot)com>, Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: easy way to insert same value into 2 columns
Date: 2003-10-16 11:58:21
Message-ID: 3F8E87DD.8030404@freenet.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Richard Huxton wrote:
> On Thursday 16 October 2003 11:11, Sebastian Boeck wrote:
>
>>Csaba Nagy wrote:
>>
>>>For your specific question I don't know the answer.
>>>
>>>For this particular case you could use:
>>>
>>>create table your_table (
>>> id integer default nextval ('public.zeit_id_seq'::text),
>>> pos_id integer default currval ('public.zeit_id_seq'::text),
>>> ...
>>>);
>>>
>>>That would work fine as long as you use inserts which don't specify id
>>>if pos_id is not specified (otherwise the currval will throw you an
>>>error cause it cannot be called without nextval being called).
>>>
>>>HTH,
>>>Csaba.
>>
>>Thanks a lot, but is it save to use?
>
>
> Not really.
>
>
>>Do i always get the same value, even if an other insert is changing
>>the sequence >public.zeit_id_seq< just at the same time?
>
>
> Other processes can't interfere - the whole point of sequences is that they
> are safe for this sort of thing.
>
> Where you will have problems is that if one of the developers decides it's
> more efficient to process fields backwards (zzz...pos_id, id) rather than
> forwards (id, pos_id, ...zzz) then it will break.
>
> Use a trigger here. If nothing else so you can stop people like me doing:
>
> INSERT INTO your_table (id,pos_id) VALUES (-1,DEFAULT);
>

Is it possible to create the function called by a trigger entirely
in SQL, or do i have to use another language?

Regards

Sebastian

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Shridhar Daithankar 2003-10-16 12:07:25 Re: easy way to insert same value into 2 columns
Previous Message Sebastian Boeck 2003-10-16 11:53:27 Re: easy way to insert same value into 2 columns