Re: currval, lastval, nextvar?

Поиск
Список
Период
Сортировка
Искать
От
A. Kretschmer
Тема
Re: currval, lastval, nextvar?
Дата
Msg-id
20090423155952.GA20043@a-kretschmer.de
Ответ на
Список
Дерево обсуждения
currval, lastval, nextvar? Carol Walter <walterc@indiana.edu>
Re: currval, lastval, nextvar? johnf <jfabiani@yolo.com>
Re: currval, lastval, nextvar? Andreas Wenk <a.wenk@netzmeister-st-pauli.de>
Re: currval, lastval, nextvar? "A. Kretschmer" <andreas.kretschmer@schollglas.com>
Re: currval, lastval, nextvar? Tom Lane <tgl@sss.pgh.pa.us>
Re: currval, lastval, nextvar? Andreas Kretschmer <akretschmer@spamfence.net>
Re: currval, lastval, nextvar? Tom Lane <tgl@sss.pgh.pa.us>
Re: currval, lastval, nextvar? Carol Walter <walterc@indiana.edu>
In response to Carol Walter :
> Hello,
> 
> I want write a query that will insert a record in a person table,  
> insert a record in a presentation table, and then insert a record into  
> a bridge table that contains the keys of both of the newly created  
> records from the other two tables.  According to the documentation,  
> takes the next number in the sequence and reports it.  Both currval  
> and lastval report the last value that was taken from the sequence,  
> but both will produce errors if a "nextval" was not called in the  
> current session.  Does writing a record that automatically updates a  
> sequence count as calling nextval?

Yes. You don't need call nextval.

You can do something like :

test=# create table t1(id serial primary key);
NOTICE:  CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey"
for table "t1"
CREATE TABLE
test=*# create table t2(id serial primary key);
NOTICE:  CREATE TABLE will create implicit sequence "t2_id_seq" for serial column "t2.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey"
for table "t2"
CREATE TABLE
test=*# create table brige (i1 int references t1, i2 int references t2);
CREATE TABLE
test=*# insert into t1 values(default);
INSERT 0 1
test=*# insert into t2 values(default);
INSERT 0 1
test=*# insert into brige values (currval('t1_id_seq'), currval('t2_id_seq'));
INSERT 0 1
test=*# select * from brige ;
 i1 | i2
----+----
  1 |  1
(1 Zeile)


-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net
В списке pgsql-novice по дате отправления
От: Carol Walter
Дата:
Сообщение: currval, lastval, nextvar?
От: Tom Lane
Дата:
Сообщение: Re: currval, lastval, nextvar?
FAQ