| From: | Richard Huxton <dev(at)archonet(dot)com> |
|---|---|
| To: | mixo <mixo(at)coza(dot)net(dot)za> |
| Cc: | pgsql-sql(at)postgresql(dot)org |
| Subject: | Re: Last insert id |
| Date: | 2004-06-09 07:22:37 |
| Message-ID: | 40C6BABD.6060204@archonet.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-sql |
mixo wrote:
> I have three tables which are related a serial field, table1_id, in on
> of the tables. Updating the tables is done through a transaction. My
> problem is, once I have insert a row in the first tables with
> table1_id, I need for the other two tables. How can I get this? Is it
> safe to use "select max(table1_id) from table1" after the insert?
A SERIAL column is just an ordinary integer with a default of
nextval(<sequence-name>).
All you need to do is:
INSERT INTO t1 (...) VALUES (...)
INSERT INTO t2 (...) VALUES (currval(<sequence-name-from-table1>)...)
Sequences are guaranteed to give values relevant to your current connection.
--
Richard Huxton
Archonet Ltd
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Huxton | 2004-06-09 08:58:40 | Re: returning a recordset from PLpg/SQL |
| Previous Message | mkl | 2004-06-09 06:54:17 | Re: Last insert id |