Solved

Multitable delete with TRIGGER.

Posted on 2014-11-23
2
113 Views
Last Modified: 2014-11-23
Neilsr helped me get started with my first stored procedure. Here is what I have now after rewriting his example for my database. Below is an abbreviated version of a 13 table delete. When I try to import it into phpmysql I get a 1064 syntact error after BEGIN. Every table involved including gallery has a galid.

I have tried:
WHERE tags.galid = OLD.id
WHERE tags.galid = OLD.galid

Maybe the problem is else where. Can someone help? Thanks.

DELIMITER $$
DROP TRIGGER IF EXISTS gal_delete;

CREATE TRIGGER gal_delete AFTER DELETE on gallery
FOR EACH ROW
BEGIN

DELETE FROM tags
    WHERE galid = OLD.id;

DELETE FROM comments
    WHERE galid = OLD.id;

DELETE FROM log
    WHERE galid = OLD.id;

END$$

Open in new window

0
Comment
Question by:kadin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40461313
Because you have changed the delimiter you need to use $$ after your DROP statement:
DELIMITER $$
DROP TRIGGER IF EXISTS gal_delete$$

CREATE TRIGGER gal_delete AFTER DELETE on gallery
FOR EACH ROW
BEGIN

DELETE FROM tags
    WHERE galid = OLD.id;

DELETE FROM comments
    WHERE galid = OLD.id;

DELETE FROM log
    WHERE galid = OLD.id;

END$$

Open in new window

However, I cannot help but notice that in your case a FOREIGN KEY with the CASCADE DELETE option could be a better approach.    
What you can do is create the foreign keys in each of your child tables this way:
ALTER TABLE tags
    ADD FOREIGN KEY
    fk_tags_galid (galid)
    REFERENCES gallery (galid)
    ON DELETE CASCADE
    ON UPDATE CASCADE 

Open in new window

(I have added ON UPDATE CASCADE, meaning that when you update the galid in the master table it will be automatically updated in the child. You can remove this clause if your business rules are different)
0
 

Author Comment

by:kadin
ID: 40461464
That was it. Thanks for your help. I will think about your foriegn key suggestion.
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question