Skip site navigation (1) Skip section navigation (2)

Re: Using a serial primary key as a foreign key in a second table

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Nathaniel <naptrel(at)yahoo(dot)co(dot)uk>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Using a serial primary key as a foreign key in a second table
Date: 2006-12-21 17:46:13
Message-ID: 20061221174613.GA19545@wolff.to (view raw or flat)
Thread:
Lists: pgsql-novice
On Thu, Dec 21, 2006 at 12:07:35 +0000,
  Nathaniel <naptrel(at)yahoo(dot)co(dot)uk> wrote:
> 
> So, I want to add the company "Looney Tunes" (which is auto-assigned  
> a company_id value), and then extract that id value so that I can use  
> it in the company_id foreign key field of Bugs Bunny's person record.

The historical way to do this was with nextval and currval. In 8.2 you
also have the option of using the RETURNING clause. Looking at the
documentation, it appears you can't directly use the output of the RETURNING
clause in a subselect. You would need to save the returned value in your
app or define a set returning function to do the company update.

In response to

Responses

pgsql-novice by date

Next:From: operationsengineer1Date: 2006-12-21 20:39:42
Subject: Re: Using a serial primary key as a foreign key in a second table
Previous:From: Richard Broersma JrDate: 2006-12-21 15:23:58
Subject: Re: Using a serial primary key as a foreign key in a second table

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group