Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 6163447604A for ; Tue, 6 Aug 2002 13:45:09 -0400 (EDT) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by postgresql.org (Postfix) with ESMTP id 2EBC8475F74 for ; Tue, 6 Aug 2002 13:45:08 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-30.mail.demon.net with esmtp (Exim 3.35 #1) id 17c8OI-000LTF-0U; Tue, 06 Aug 2002 18:45:07 +0100 Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 888C811DD2; Tue, 6 Aug 2002 18:38:25 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Norman Khine , Subject: Re: Multi table insert and passing sequence ids Date: Tue, 6 Aug 2002 18:38:23 +0100 User-Agent: KMail/1.4.1 References: <20020806123829.C1040-100000@reaper.spot5.com> In-Reply-To: <20020806123829.C1040-100000@reaper.spot5.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200208061838.23195.dev@archonet.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/112 X-Sequence-Number: 8958 On Tuesday 06 Aug 2002 1:55 pm, Norman Khine wrote: > Hello, > I have a database that has 4 tables > I also have an html put form which is an almagamation of all the fields > from these tables and am trying to write the sql which will allow me to > put this data into the database, in particular I am unsure as to howto > pass the sequence id from business_name to the business_address_1 FOREIGN > KEY, perhaps I'll need to do it using 4 different sql statements? Norman - see the thread "problem fetching currval of sequence" for someone= =20 doing just this. You will need 4 statements, but I don't see the sequence definition for=20 business_name > CREATE TABLE business_name( > business_name_id int4 NOT NULL CONSTRAINT UC_business_name1 UNIQUE, > business_name varchar(40), > business_url varchar(35), > CONSTRAINT PK_business_name1 PRIMARY KEY (business_name_id)); You'll want a "DEFAULT nextval('business_name_id_seq')" (or whatever you ca= ll=20 the sequence) on business_name_id . Then you can do: BEGIN; INSERT INTO busines_name (business_name,business_url) VALUES (...); INSERT INTO county (business_name_id,...) VALUES=20 (currval('business_name_id_seq',...) etc... COMMIT; Wrapping the statements in begin/commit ensure they all take place in one= =20 transaction and so either all happen or none do. The sequence value is=20 guaranteed to be unique for the current process, so two people can insert= =20 entries at the same time. If you didn't want to do the DEFAULT bit with business_name.business_name_i= d=20 (and I'd recommend you do) then you'd use nextval('business_name_id_seq') t= o=20 get the new id to use. Check the docs on currval/nextval/create sequence fo= r=20 details. HTH - Richard Huxton