Question

guys! I want multiple actions in one trigger but it's not working. Can someone help? Where is the problem? Where is my mistake? Please help.

DROP TRIGGER IF EXISTS  `usertrigger` ;

CREATE DEFINER =  `cmsbalti`@`localhost` TRIGGER `usertrigger`  
    BEFORE DELETE ON  `users`          
    FOR EACH ROW  
  INSERT INTO  `del_users` (fullname) 
  VALUES (old.fullname)

BEFORE INSERT ON `del_users` FOR EACH ROW
BEGIN
   DELETE FROM `gallery`
   WHERE uid = old.id;
END

Thank you.

No correct solution

OTHER TIPS

As a workaround for this i would not use triggers !! just alter your design and add a ON UPDATE CASCADE constraint ,so only one action will be needed as the constraint will do the rest. Check this link for more details.


As for the trigger option

Only one action trigger is possible. To make it easy to understand see example :

mysql> CREATE TABLE actor(
  actor_id SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  first_name VARCHAR(45) NOT NULL,
  last_name VARCHAR(45) NOT NULL,
   last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (actor_id),
  INDEX idx_actor_last_name (last_name)
 );

Create first trigger :

mysql> DELIMITER $$
mysql>
mysql> CREATE TRIGGER trigger1
    -> AFTER INSERT
    -> ON actor
    -> FOR EACH ROW
    -> BEGIN
    ->   -- code
    -> END
    -> $$
Query OK, 0 rows affected (0.00 sec)

Create second trigger :

mysql> CREATE TRIGGER trigger2
    -> AFTER UPDATE
    -> ON actor
    -> FOR EACH ROW
    -> BEGIN
    ->   -- code
    -> END
    -> $$
Query OK, 0 rows affected (0.00 sec)

Create third action triger :

mysql> CREATE TRIGGER trigger3
    -> AFTER DELETE
    -> ON actor
    -> FOR EACH ROW
    -> BEGIN
    ->   -- code
    -> END
    -> $$
DELIMITER ;Query OK, 0 rows affected (0.01 sec)

Now let's try to create a fort trigger with the same action as the first trigger

mysql> CREATE TRIGGER trigger11
    -> AFTER INSERT
    -> ON actor
    -> FOR EACH ROW
    -> BEGIN
    ->   -- code
    -> END
    -> $$
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'

So end of story - you cannot create the trigger the way you have written it - make two different triggers as the evens are different.

So my advice is to create two triggers on that table and make sure to put them in block to avoid problems at execution level (very little chance).

  1. Don't scatter the logic of deleting a user across several triggers. It will make it much harder to maintain.
  2. Just use BEGIN ... END block in your DELETE trigger and do both insert an audit record in del_users and delete related records from gallery. Don't forget to change DELIMITER in mysql client.
  3. If you're using foreign key constraints you'll have to opt out to BEFORE event for your trigger because you have to delete child records prior to deleting a parent.

That being said your trigger might look like

DELIMITER //
CREATE TRIGGER tg_ad_users 
BEFORE DELETE ON users
FOR EACH ROW
BEGIN
  INSERT INTO del_users (user_id, fullname, deleted)
  VALUES (OLD.id, OLD.fullname, NOW());
  DELETE FROM gallery 
   WHERE uid = OLD.id;
END//
DELIMITER ;

Here is SQLFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top