Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want a trigger to DELETE from 2 tables in MySQL

I have 3 MySQL tables (food, apple, and orange).

I want to delete rows from:

apple(idapple, iduser, name) 
orange(idornge, iduser, name)

When deleting a row in food(iduser, name) using one trigger?

Here is my trigger so far:

  CREATE TRIGGER `food_before_delete`

    AFTER DELETE ON `food` 
    FOR EACH ROW 

      DELETE FROM apple, orange 
      WHERE 
      apple.iduser=OLD.iduser and 
      orange.iduser=OLD.iduser

But it won't compile. How can make a trigger that deletes from two tables at once?

like image 371
Alexander Shlenchack Avatar asked Jan 25 '11 21:01

Alexander Shlenchack


1 Answers

Something simpler maybe?

DELETE f,a,o FROM
food AS f 
LEFT JOIN apple AS a USING (iduser)
LEFT JOIN orange AS o USING (iduser)
WHERE f.name = ...

No trigger needed.

like image 175
Mchl Avatar answered Nov 15 '22 16:11

Mchl