Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Using triggers to cascade deletes
Fri, Nov 15 2013 7:50 AMPermanent Link

Ian Turner

In the past I have implemented cascaded deletes from within Delphi but for my current project I would like to try using triggers (the first time I have ever used them, mmm). Tim wrote a post on this back in March 2010 -

<<
Start a transaction on just the two tables in the BEFORE DELETE trigger:

CREATE TRIGGER BeforeDeleteTrigger BEFORE DELETE ON Master
BEGIN
  START TRANSACTION ON TABLES Master, Detail;
END

commit the transaction after the delete:

CREATE TRIGGER AfterDeleteTrigger AFTER DELETE ON Master
BEGIN
  EXECUTE IMMEDIATE 'DELETE FROM Detail WHERE MasterID='+OLDROW.ID;
  COMMIT;
END

be sure to roll it back if there's an error using an ERROR trigger:

CREATE TRIGGER ErrorDeleteTrigger ERROR DELETE ON Master
BEGIN
  ROLLBACK;
END
>>

I am just a little confused by the use of the AfterDeleteTrigger. Normally in Delphi, I would put me detail delete routine in the Master Table BeforeDelete event. Will deleting the Master record first not cause a problem with any foreigh key constraint, or does this mean that fk constraints cannot be used in this instance?

Thanks,
Ian
Wed, Nov 20 2013 2:58 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Ian,

<< I am just a little confused by the use of the AfterDeleteTrigger.
Normally in Delphi, I would put me detail delete routine in the Master Table
BeforeDelete event. Will deleting the Master record first not cause a
problem with any foreigh key constraint, or does this mean that fk
constraints cannot be used in this instance? >>

Yeah, that's a mistake - move the DELETE statement for the detail into the
Before Delete trigger, but keep the COMMIT in the After Delete trigger.

Tim Young
Elevate Software
www.elevatesoft.com
Image