Re: Multiple Cascade Paths

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Andrew John (aj_at_DELETEmistrose.com)
Date: 07/27/04


Date: Tue, 27 Jul 2004 17:22:21 +1000

Luke,

Books Online does say why:

"No table can appear more than once in the list of all cascading
referential actions that result from the DELETE or UPDATE. The tree of
cascading referential actions must not have more than one path to any
given table"

See index under "Cascading Referential Integrity Constraints"

In your example an update of a can cause an update on c either directly,
or via table b, so that explains the error.

The only work around I know of, is to replace ONE OF the FK paths with
triggers. Not neat, but it is workable. Before you get too down on MS
for this have you tried Oracle or MySQL ? "mutating tables error" when
you even try to look at the base table in a trigger, or no triggers at
all ( MySQL version < 5.1)

Regards
  AJ

>Luke Amery wrote:
> Hi all,
>
> I have an issue with foreign key constraints and multiple cascade paths as
> the title of my post would suggest.
>
> Here is a simple example:
>
> CREATE TABLE a
> (
> keyofa int not null primary key
> )
>
> CREATE TABLE b
> (
> keyofb int not null primary key,
> fkinatob int not null foreign key references a(keyofa) on update cascade
> )
>
> CREATE TABLE c
> (
> keyofc int not null primary key,
> fkinctob int not null foreign key references b(keyofb) on update
> cascade,
> fkinctoa int not null foreign key references a(keyofa) on update cascade
> )
>
>
> if you run it you will see that the foreign key linking c to a is invalid
> due to either a cycle or multiple cascade path.
>
> It can't be a cycle - because there are no cycles in this example (that I
> can see).
>
> It must be a multiple cascade path - I haven't found a definition of this -
> but according to any definition I can dream up - there is no way to multiply
> cascade a change to c through changing a or b. So I consider this a short
> coming of SQL Server.
>
> Ok, so sql server dropped the ball - and Microsoft isn't going to fix it for
> me, so what are my alternatives?
>
> I can use triggers - I can remove the on update cascade options on the
> foreign key constraints that refer to table a - then put a trigger on table
> a to keep table b and table c in sync with table a. Trouble with this is - I
> can no longer update table a in any way because the constraints are checked
> before the trigger is fired.
>
> So my next option is to drop the constraints all together and use triggers
> on table a to keep table b and table c in sync. Then I can use triggers on
> table b and table c to enforce integrity manually - this I think will work.
> But is this what I have to do???? what a pathetic solution!!
>
> anybody got any other ideas?
>
>



Relevant Pages

  • Re: Multiple Cascade Paths
    ... multiple ways to update the same fields - but you are suggesting that it is ... That is a pretty big limitation - and triggers aren't that workable - ... >> I have an issue with foreign key constraints and multiple cascade paths ...
    (microsoft.public.sqlserver.programming)
  • foreign key constraint fails
    ... Tabelle produkte ... FOREIGN KEY REFERENCES `kunden` ON UPDATE CASCADE, ...
    (de.comp.datenbanken.mysql)
  • Re: Unique PK across two tables?
    ... > identifier (VIN), and add two mutually exclusive sub-classes, Sport ... > FOREIGN KEY ... > REFERENCES Vehicles ... > ON DELETE CASCADE, ...
    (microsoft.public.sqlserver.programming)
  • Re: CASCADE !!
    ... Defining a cascading Foreign Key will do this for you. ... (category_group_id INT IDENTITY(1,1) ... REFERENCES category_group ... > ON DELETE CASCADE) ...
    (microsoft.public.sqlserver.server)
  • Re: Multiple Cascade Paths
    ... Multiple cascade paths are allowed in the next version, ... > That is a pretty big limitation - and triggers aren't that workable - ... >>> I have an issue with foreign key constraints and multiple cascade paths ...
    (microsoft.public.sqlserver.programming)