Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id eBUJ3hL05603 for ; Sat, 30 Dec 2000 14:03:44 -0500 (EST) (envelope-from olly@lfix.co.uk) Received: from lfix.demon.co.uk ([158.152.59.127] helo=linda.lfix.co.uk) by anchor-post-31.mail.demon.net with esmtp (Exim 2.12 #1) id 14CRHz-000E0Y-0V; Sat, 30 Dec 2000 19:03:35 +0000 Received: from lfix.co.uk (olly@localhost [127.0.0.1]) by linda.lfix.co.uk (8.11.1/8.11.1/Debian 8.11.0-6) with ESMTP id eBUJ3ZH18678; Sat, 30 Dec 2000 19:03:35 GMT Message-Id: <200012301903.eBUJ3ZH18678@linda.lfix.co.uk> X-Authentication-Warning: linda.lfix.co.uk: Host olly@localhost [127.0.0.1] claimed to be lfix.co.uk X-Mailer: exmh version 2.2 06/23/2000 (debian 2.2-1) with nmh-1.0.4+dev X-URL: http://www.lfix.co.uk/oliver X-face: "xUFVDj+ZJtL_IbURmI}!~xAyPC"Mrk=MkAm&tPQnNq(FWxv49R}\>0oI8VM?O2VY+N7@F- KMLl*!h}B)u@TW|B}6 cc: psql sql Subject: Re: References to SERIAL In-reply-to: Message from "Brett W. McCoy" of Sat, 30 Dec 2000 13:21:40 EST. Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sat, 30 Dec 2000 19:03:34 +0000 From: "Oliver Elphick" X-Archive-Number: 200012/313 X-Sequence-Number: 653 "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