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

Re: Best practices?

From: David Fetter <david(at)fetter(dot)org>
To: SF Postgres <sfpug(at)postgresql(dot)org>
Subject: Re: Best practices?
Date: 2003-10-01 17:57:48
Message-ID: 20031001175748.GE7302@fetter.org (view raw or flat)
Thread:
Lists: sfpug
On Wed, Oct 01, 2003 at 10:28:35AM -0700, elein wrote:
> A BEFORE trigger is really the way to go here.  The trigger function
> would check that the proper number of bars exists or raise an error.
> 
> I believe this will work fine both with autocommit and block
> transactions.
> 
> The key here is to be sure you enter bars before foos or you will
> never get any valid foos.  

Here's what it looks like (modulo the proprietary bits).

CREATE TABLE foo (
  foo_id SERIAL PRIMARY KEY
);

CREATE TABLE bar (
  bar_id SERIAL PRIMARY KEY
, foo_id INTEGER NOT NULL REFERENCES foo(foo_id)
-- Must have 1 to 4 bars for each foo
-- If 0 bars, delete the foo; don't allow more than 4 bars per foo.
);

Does that explain it a little better?

Cheers,
D
-- 
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100    cell: +1 415 235 3778

In response to

Responses

sfpug by date

Next:From: Dror MatalonDate: 2003-10-01 18:36:24
Subject: Re: Best practices?
Previous:From: eleinDate: 2003-10-01 17:28:35
Subject: Re: Best practices?

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