Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Trigger with IF THEN

My storage is INNODB, I'm trying to create an trigger with 2 queries in IF statement. Down you can see the trigger that gives me the error

delimiter |
CREATE TRIGGER count_delete_videos BEFORE DELETE ON videos
  FOR EACH ROW BEGIN
    UPDATE counts SET count = count - 1 WHERE name = 'all';

    IF OLD.published = 1 THEN
        DELETE FROM videos_categories WHERE id_video = OLD.id;
        DELETE FROM videos_tags WHERE id_video = OLD.id;
    END IF;
  END;
|
delimiter ;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= OLD.id;
    END IF;
  END' at line 6

This are the 2 triggers that i activate with the first one.

delimiter |
CREATE TRIGGER count_delete_videos_tags AFTER DELETE ON videos_tags
  FOR EACH ROW BEGIN
    UPDATE tags SET count = count - 1 WHERE id = OLD.id_tag;
  END;
|
delimiter ;


delimiter |
CREATE TRIGGER count_delete_videos_categories AFTER DELETE ON videos_categories
  FOR EACH ROW BEGIN
    UPDATE categories SET count = count - 1 WHERE id = OLD.id_category;

    IF OLD.id_category <> 20 AND OLD.id_category <> 34 THEN
        UPDATE counts SET count=count-1 WHERE name='english';
    ELSEIF OLD.id_category = 34 THEN
        UPDATE counts SET count=count-1 WHERE name='german';
    ELSEIF OLD.id_category = 20 THEN
        UPDATE counts SET count=count-1 WHERE name='italian';
    END IF;
  END;
|
delimiter ;

But this one works perfectly

delimiter |
    CREATE TRIGGER count_delete_videos BEFORE DELETE ON videos
      FOR EACH ROW BEGIN
        UPDATE counts SET count = count - 1 WHERE name = 'all';

        IF OLD.published = 1 THEN
            DELETE FROM videos_categories WHERE id_video = OLD.id;
        END IF;
      END;
    |
    delimiter ;

Query OK, 0 rows affected (0.16 sec)

How can i make first trigger work? what i'm doing wrong? Thx for helping me.

like image 255
Alex P. Avatar asked Apr 29 '12 17:04

Alex P.


People also ask

Can you use if statements in MySQL?

The MySQL IF() function is used for validating a condition. The IF() function returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that can be either numeric or strings depending upon the context in which the function is used.

How do I know if MySQL trigger is working?

Sql_mode: Displays the SQL_MODE when the trigger is invoked. Definer: It defines the account of the user that has implemented the trigger. Character_set_client: It determines the character set through which the statements are provided by the client.

What is trigger in MySQL with example?

In MySQL, a trigger is a stored program invoked automatically in response to an event such as insert, update, or delete that occurs in the associated table. For example, you can define a trigger that is invoked automatically before a new row is inserted into a table.

Which statement can a trigger not be based on?

Since triggers execute as part of a transaction, the following statements are not allowed in a trigger: All create commands, including create database, create table, create index, create procedure, create default, create rule, create trigger, and create view. All drop commands. alter table and alter database.


1 Answers

As far as I can tell both triggers are OK, but you might try the following:

DELIMITER $$
CREATE TRIGGER count_delete_videos BEFORE DELETE ON videos
FOR EACH ROW 
BEGIN
  UPDATE counts SET count = count - 1 WHERE name = 'all';

  IF OLD.published = 1 THEN BEGIN
    DELETE FROM videos_categories WHERE id_video = OLD.id;
    DELETE FROM videos_tags WHERE id_video = OLD.id;
  END; END IF;
END$$

DELIMITER ;
like image 198
Johan Avatar answered Sep 25 '22 10:09

Johan