get a pkey/id back from/after an insert - Mailing list pgsql-novice

From Corey
Subject get a pkey/id back from/after an insert
Date
Msg-id 200606191340.40569.corey@bitworthy.net
Whole thread Raw
Responses Re: get a pkey/id back from/after an insert
List pgsql-novice
Hello!

Ok, so no question too simple...

I'm using the perl dbi interface to postgres, and I'm wondering how
people do the following:

insert a row into a table, and then immediately get back the pkey/id
of the row, so that I can then use this id to insert into another related
table.

How would this be done in pure SQL, and/or is there a similar technique
to do this using the perl DBI and DBD::Pg?

Say I have a table named "Foo", which contains a not null constrained
foreign key to table "Bar":  "Foo".bar_id.

Before I can insert a new row into table "Foo", I thus need to have the
related row in table "Bar", so I need to insert into "Bar" first, get the
pkey, and then insert "Foo".

Hope this makes sense - I'm sure it could be explained more presicely,
and it seems like it would be a very common scenerio/pattern.

Many thanks!

Corey



pgsql-novice by date:

Previous
From: Richard Broersma Jr
Date:
Subject: Re: SQL-commands does not work
Next
From: "Corey"
Date:
Subject: Re: get a pkey/id back from/after an insert