Solved

Multitable delete with TRIGGER.

Posted on 2014-11-23
2
107 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
2 Comments
 
LVL 24

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now