X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id AE9369FA0EC for ; Thu, 6 Jul 2006 15:46:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 17009-10 for ; Thu, 6 Jul 2006 15:46:35 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web33314.mail.mud.yahoo.com (web33314.mail.mud.yahoo.com [68.142.206.129]) by postgresql.org (Postfix) with SMTP id 816A89F94F3 for ; Thu, 6 Jul 2006 15:46:35 -0300 (ADT) Received: (qmail 50285 invoked by uid 60001); 6 Jul 2006 18:46:34 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=COCwYIutU9pp8E5z6boMJn+XVXF3eNtoRktL/jIqkCo5zYoZa9eYp+zk/imNZuGJcXBj6AeGjt6HCjO5Rfi6fCMx6xrNs1+A5xKedb/EOys6CSDFJrxm6VSH+hPbkC0+D+mXjNna4EGLf1nAsUL6R2ijkVQDYglBDZ7lZw6/hPE= ; Message-ID: <20060706184634.50283.qmail@web33314.mail.mud.yahoo.com> Received: from [12.9.179.126] by web33314.mail.mud.yahoo.com via HTTP; Thu, 06 Jul 2006 11:46:34 PDT Date: Thu, 6 Jul 2006 11:46:34 -0700 (PDT) From: Subject: Re: Foreign Key: what value? To: Davi Leal , pgsql-sql@postgresql.org In-Reply-To: <200607051719.27466.davi@leals.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/68 X-Sequence-Number: 25642 > Hi, > How to know the value which I must set in the > foreign key field?. I have two > tables: > > > CREATE TABLE AAAAAA ( > Id SERIAL PRIMARY KEY, > data char(9) > ); > > CREATE TABLE BBBBBB ( > BBBBBB_Id integer REFERENCES AAAAAA(Id) NOT NULL, > field char(5) > ); > > > > > I insert a register on table AAAAAA, > > INSERT INTO AAAAAA (data) VALUES ('123456789'); > > > and then, I want to insert a related register in > table BBBBBB, but I do not > know how get the proper value to the Foreign key > BBBBBB_Id. Note that a lot of > client are inserting at the same time on the AAAAAA > table, so I can not just > get the greater value of AAAAAA.Id > > Maybe using transactions ?. Any tip, URI, ... will > be welcome. > > INSERT INTO BBBBBB (BBBBBB_Id, field) VALUES > (??????,'12345'); > i alsways make my foreign key column data type int4. i'm not sure if i read that somewhere. anyone, please feel free to chime in if this isn't good practice. read up on currval, nextval and that whole section. you can begin by getting the nextval, assigning it to a variable, insert it into your primary table and then insert it into your related table as a foreign key. from what i understand, either way should be bulletproof. the way i described is more code, but some minds might not mind paying that price b/c they like the process better. good luck. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com