get a pkey/id back from/after an insert

From: "Corey" <corey(at)bitworthy(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: get a pkey/id back from/after an insert
Date: 2006-06-19 20:40:40
Message-ID: 200606191340.40569.corey@bitworthy.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Corey 2006-06-19 21:18:01 Re: get a pkey/id back from/after an insert
Previous Message Richard Broersma Jr 2006-06-19 20:27:28 Re: SQL-commands does not work