Обсуждение: sequence

Поиск
Список
Период
Сортировка

sequence

От
"Vladimir V. Zolotych"
Дата:
Hello all

What is the safe way to add record to a table
and get it id (generated by a sequence) consistently?

E.g.
1) add record (INSERT INTO...) to a table
(the record's id will be generated
automatically due the ``id SERIAL'' column were defined).

2) get the id of a just created record.

Thanks
 

-- 
Vladimir Zolotych                         gsmith@eurocom.od.ua
 

Re: sequence

От
Andrew Perrin - Demography
Дата:
I figured this out recently with the help of the list - you want either
nextval() or currval().  In my case, I used currval().  Basically:

SELECT currval('table_field_seq');

will return the most recently assigned value to the field *by the current
backend* which means:

- There's no danger of a race condition (another user creating a record in
the time between your creating the record and calling currval); BUT
- You must be using the same backend as you did on the creation.

Hope this helps.

---------------------------------------------------------------------
Andrew J. Perrin - aperrin@demog.berkeley.edu - NT/Unix Admin/Support
Department of Demography    -    University of California at Berkeley
2232 Piedmont Avenue #2120  -    Berkeley, California, 94720-2120 USA
http://demog.berkeley.edu/~aperrin --------------------------SEIU1199

On Tue, 18 Apr 2000, Vladimir V. Zolotych wrote:

> Hello all
>
> What is the safe way to add record to a table
> and get it id (generated by a sequence) consistently?
>
> E.g.
> 1) add record (INSERT INTO...) to a table
> (the record's id will be generated
> automatically due the ``id SERIAL'' column were defined).
>
> 2) get the id of a just created record.
>
> Thanks
>
>
> --
> Vladimir Zolotych                         gsmith@eurocom.od.ua
>
>
>