How to get automatically generated "id" field

Поиск
Список
Период
Сортировка
От Nikola Milutinovic
Тема How to get automatically generated "id" field
Дата
Msg-id 42336C35.7080003@ev.co.yu
обсуждение исходный текст
Ответы Re: How to get automatically generated "id" field  (Dave Cramer <pg@fastcrypt.com>)
Список pgsql-jdbc
Hi all.

This may be a bit more for general ML, but I'm in a fix and need an
advice. I have several tables with auto-generated "id" field, like this.

CREATE TABLE photo (
    id   SERIAL PRIMARY KEY,
    size   INT8,
    file   VARCHAR(256)
)

I would like to be able to insert values and get back the "id" of a
newly inserted record. What is a good way to do it?

I imagine I could open a transaction and read the value of the sequence,
but that is ugly, since the sequence name is autogenerated and PG specific.

Would it be better to change "SERIAL" to just INT8 and do in a transaction:

...start transaction
executeQuery( "SELECT max( id )+1 AS new_id FROM photo" );
..read "new_id"
executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" );
..commit

Nix.

В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Ragnar Hafstað
Дата:
Сообщение: Re: [GENERAL] MS Access to PostgreSQL
Следующее
От: Dave Cramer
Дата:
Сообщение: Re: How to get automatically generated "id" field