pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Elphick <olly@lfix.co.uk>
To: Thomas SMETS <tsmets@altern.org>
Cc: psql sql <pgsql-sql@postgresql.org>
Subject: Re: References to SERIAL
Date: Sat, 30 Dec 2000 19:03:34 +0000
Message-ID: <200012301903.eBUJ3ZH18678@linda.lfix.co.uk> (raw)
In-Reply-To: <bmccoy@chapelperilous.net> <Pine.LNX.4.30.0012301316430.11787-100000@chapelperilous.net>

"Brett W. McCoy" wrote:
  >On Sat, 30 Dec 2000, Thomas SMETS wrote:
  >
  >> If i create a "internal pk" buy defining on a table a field SERIAL.
  >> How do I reference this field in the other table to set the field
  >> possible value ?
 ...
  >
  >You mean as a foreign key?  You would do something like
  >
  >create table books_authors (
  >	book integer references book(book_pk)
  >		on delete no action,
 ...

If you need to know which value was used for the SERIAL field, there are
two ways:

1. Use currval('book_book_pk_seq')  to get the last value used in this
session.


bray=# insert into junk (name) values ('Fred');
INSERT 1780993 1
bray=# select currval('junk_id_seq');
 currval 
---------
       1
(1 row)

2. Use the OID which is returned by a successful INSERT statement to look
up the newly-created row from the table:


bray=# insert into junk (name) values ('Fred');
INSERT 1780993 1
bray=# select * from junk where oid = 1780993 ;
 id | name 
----+------
  1 | Fred
(1 row)


-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Give to him that asketh thee, and from him that would 
      borrow of thee turn not away."         
                                      Matthew 5:42 





view thread (4+ messages)  latest in thread

Message-ID: <200012301903.eBUJ3ZH18678@linda.lfix.co.uk>
Permalink:  ../200012301903.eBUJ3ZH18678@linda.lfix.co.uk/
Also on:    postgresql.org/message-id/200012301903.eBUJ3ZH18678@linda.lfix.co.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: olly@lfix.co.uk, tsmets@altern.org
  Subject: Re: References to SERIAL
  In-Reply-To: <200012301903.eBUJ3ZH18678@linda.lfix.co.uk>
  References: <200012301903.eBUJ3ZH18678@linda.lfix.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox