get a pkey/id back from/after an insert

Поиск
Список
Период
Сортировка
От Corey
Тема get a pkey/id back from/after an insert
Дата
Msg-id 200606191340.40569.corey@bitworthy.net
обсуждение исходный текст
Ответы Re: get a pkey/id back from/after an insert  ("Corey" <corey@bitworthy.net>)
Список 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 по дате отправления:

Предыдущее
От: Richard Broersma Jr
Дата:
Сообщение: Re: SQL-commands does not work
Следующее
От: "Corey"
Дата:
Сообщение: Re: get a pkey/id back from/after an insert