?
Solved

Multitable delete with TRIGGER.

Posted on 2014-11-23
2
Medium Priority
?
114 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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