pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: operationsengineer1@yahoo.com
To: Davi Leal <davi@leals.com>
To: pgsql-sql@postgresql.org
Subject: Re: Foreign Key: what value?
Date: Thu, 6 Jul 2006 11:46:34 -0700 (PDT)
Message-ID: <20060706184634.50283.qmail@web33314.mail.mud.yahoo.com> (raw)
In-Reply-To: <200607051719.27466.davi@leals.com>

> 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 



view thread (4+ messages)  latest in thread

Message-ID: <20060706184634.50283.qmail@web33314.mail.mud.yahoo.com>
Permalink:  ../20060706184634.50283.qmail@web33314.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/20060706184634.50283.qmail@web33314.mail.mud.yahoo.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: operationsengineer1@yahoo.com, davi@leals.com
  Subject: Re: Foreign Key: what value?
  In-Reply-To: <20060706184634.50283.qmail@web33314.mail.mud.yahoo.com>

* 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