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.
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.
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.
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.
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.
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 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With