Re: [SQL] VIEWs and FOREIGN keys

From: Jan Wieck <janwieck(at)yahoo(dot)com>
To: terry(at)greatgulfhomes(dot)com
Cc: "'Jan Wieck'" <janwieck(at)yahoo(dot)com>, "'Achilleus Mantzios'" <achill(at)matrix(dot)gatewaynet(dot)com>, pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] VIEWs and FOREIGN keys
Date: 2002-06-10 19:19:36
Message-ID: 200206101919.g5AJJaF07199@saturn.janwieck.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

terry(at)greatgulfhomes(dot)com wrote:
> I would just like to elaborate, and clarify if my understanding is correct:
>
> The implication of below is that you need a trigger in the foreign key
> target table on the DELETE event, so the foreign key table only lets you
> delete a row if there are no other tables refering to the key you want to
> delete.
>
> Views cannot have triggers, hence cannot have a DELETE trigger, therefore
> that is why the view cannot be a foreign key target table.

Right, the primary key table (what you refer to as the
foreign key target) needs to have a trigger on DELETE and
UPDATE (the key value could change and break referential
integrity by doing so). For simple views this might be
doable with a trigger on the base tables, but imagine this:

CREATE VIEW pk_view AS
SELECT t1.keypart1 || t2.keypart2 AS primkey
FROM t1, t2 WHERE t1.isactive;

CREATE TABLE fk_table (
ref varchar,
FOREIGN KEY (ref) REFERENCES pk_view (primkey)
);

Okay, let's ignore the fact that the ANSI SQL spec requires
every referenced key to have a UNIQUE constraint, and that we
cannot guarantee that in the first place.

We toggle t1.isactive on a row to false, thereby removing a
few thousand result rows from the view's result set. Any cool
idea how to check if that doesn't produce some orphaned rows
in "fk_table"? By "cool idea" I mean not a couple hand
crafted PL/pgSQL triggers, but some general solution that
works with any view.

Jan

> [...]
> > -----Original Message-----
> > From: pgsql-general-owner(at)postgresql(dot)org
> >
> > Achilleus Mantzios wrote:
> > >
> > > can someone have a foreign key constraint that references
> > > a view??
> >
> > No, and this is not planned either. Remember that it is not
> > only required for referential integrity to check if a key
> > exists on INSERT or UPDATE to the referencing table. The
> > system must guarantee that you cannot remove existing keys
> > while they are referenced (or more precise perform the
> > requested referential action).

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vincent Stoessel 2002-06-10 19:20:47 logging to a file
Previous Message Ron Snyder 2002-06-10 19:18:30 Re: Checking that Pg is running from a shell script

Browse pgsql-sql by date

  From Date Subject
Next Message terry 2002-06-10 19:43:32 Re: [SQL] VIEWs and FOREIGN keys
Previous Message terry 2002-06-10 18:49:33 Re: [SQL] VIEWs and FOREIGN keys