Re: last id - Mailing list pgsql-novice

From Josh Berkus
Subject Re: last id
Date
Msg-id 200306262128.56135.josh@agliodbs.com
Whole thread Raw
In response to last id  ("Ramil G. Sagum" <ramil@astra.ph>)
List pgsql-novice
Ramil,

> 1. insert a row to table A
> 2. get the value of the serial field from the now in no.1
> 3. insert a row to table B (one of the fields uses the value from no.2)
>
> in -one transaction-?

BEGIN TRANSACTION

INSERT INTO table_a ( 2, 3, 4 )
VALUES ( x, y, z );
INSERT INTO table_b ( a_FK, 3, 4 )
VALUES ( CURRVAL('table_a_seq'), m, q );

COMMIT TRANSACTION

Easy, neh?   Now go to the online docs and do some reading about Sequences.

--
Josh Berkus
Aglio Database Solutions
San Francisco

pgsql-novice by date:

Previous
From: "Ramil G. Sagum"
Date:
Subject: last id
Next
From: "Giorgio Ponza"
Date:
Subject: Probably a stupid question