From: | Jason Earl <jason(dot)earl(at)simplot(dot)com> |
---|---|
To: | Roland Roberts <roland(at)astrofoto(dot)org> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: INSERT question |
Date: | 2001-11-15 21:28:26 |
Message-ID: | 87y9l7u3k5.fsf@npa01zz001.simplot.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Yes that is entirely correct. As you pointed out my example is *very*
dangerous if not wrapped in a transaction. My query clearly should
have looked like this:
BEGIN;
INSERT INTO parent_table (data) values ('some data');
INSERT INTO child_table (parent, more_data)
(currval('parent_table_p_key_seq'),
'more data');
COMMIT;
Thanks for pointing that out.
Roland Roberts <roland(at)astrofoto(dot)org> writes:
> >>>>> "Jason" == Jason Earl <jason(dot)earl(at)simplot(dot)com> writes:
>
> Jason> Even better, however, is to use the functions nextval() and
> Jason> currval() like so:
>
> Jason> INSERT INTO parent_table (data) values ('some data');
> Jason> INSERT INTO child_table (parent, more_data)
> Jason> (currval('parent_table_p_key_seq'),
> Jason> 'more data');
>
> The above is probably the best for a pure-SQL way with the caveat
> that it should be wrapped in a transaction or currval() may not be
> what you expect; i.e., another client may add a row and you get a
> value different from what you inserted.
>
> roland
> --
> PGP Key ID: 66 BC 3B CD
> Roland B. Roberts, PhD RL Enterprises
> roland(at)rlenter(dot)com 76-15 113th Street, Apt 3B
> roland(at)astrofoto(dot)org Forest Hills, NY 11375
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
From | Date | Subject | |
---|---|---|---|
Next Message | Aasmund Midttun Godal | 2001-11-15 21:50:49 | Re: [SQL] PL/pgSQL examples NOT involving functions |
Previous Message | Andrew G. Hammond | 2001-11-15 21:23:16 | Re: INSERT question |