Re: currval, lastval, nextvar?

From: Carol Walter <walterc(at)indiana(dot)edu>
To:
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: currval, lastval, nextvar?
Date: 2009-04-23 17:05:17
Message-ID: 5F3E6243-B3BB-4416-B5B0-BFA0AEC23588@indiana.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Thank you all for the good information. The three "insert" lines are
all I need.

Carol

On Apr 23, 2009, at 11:59 AM, A. Kretschmer wrote:

> 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
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Adam Ruth 2009-04-23 22:51:46
Previous Message Tom Lane 2009-04-23 16:21:33 Re: currval, lastval, nextvar?