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.
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 ;
Related
These are my tables:
I'm creating the trigger after i update this table:
Table 'avisos':
idAvisos | retirado
---------------------
1 | 0
2 | 1
Table 'recolectores':
idRecolector | num_avisos_retirados
-----------------------------------
1 | 0
2 | 4
So what i want to do is, after i update the table avisos to set "retirado" to true (1), i want to sum +1 to the column "num_avisos_retirados" of the recolectores table.
Example: when idAvisos (1) changes "retirado" to 1, the idRecolector (1) will have to change the num_avisos_retirados to 1.
Here's what i've done so far:
CREATE TRIGGER NumAvisosRecolectados_AU #after update
AFTER UPDATE ON avisos.retirado
IF (NEW.retirado = 1) THEN
UPDATE recolectores
SET recolectores.num_avisos_recolectados = recolectores.num_avisos_recolectados + 1
END IF;
Also, what i don't understand is if i have to use a WHERE clause, to actually hit the right idRecolector of the "recolectores" table. My teacher gave us an example similar to this, but she didn't use a WHERE clause to check the right ID. Does it join the right table automatically?
When i run that sql statement i get this error:
Wrong syntax near'IF (NEW.retirado = 1) THEN
UPDATE recolectores
SET recolectore' en la linea 3
UPDATE
After reading this: Fire a trigger after the update of specific columns in MySQL
I now changed my statement, because i cant use UPDATE ON avisos.retirado, instead i have to use FOR EACH ROW and an IF statement to check for a specific column change. I also added a WHERE clause to actually hit the right id that i want to update, as suggested by #nbk
Here's the code now:
DELIMITER //
CREATE TRIGGER NumAvisosRecolectados_AU #after update
AFTER UPDATE ON avisos
FOR EACH ROW
BEGIN
IF (NEW.retirado = 1) THEN
BEGIN
UPDATE recolectores
SET recolectores.num_avisos_recolectados = recolectores.num_avisos_recolectados + 1
WHERE reservas.FK_IDavisos = avisos.ID_avisos
AND reservas.FK_IDrecolector = recolectores.ID_recolector
END
END IF;
END; //
DELIMITER ;
But i'm still getting a syntax error:
#1064 - Wrong syntax near 'END
END IF;
You have need a `DELIMITER and the syntax is off
DELIMITER //
CREATE TRIGGER NumAvisosRecolectados_AU #after update
AFTER UPDATE ON avisos.retirado
FOR EACH ROW
BEGIN
IF (NEW.retirado = 1) THEN
UPDATE recolectores
SET recolectores.num_avisos_recolectados = recolectores.num_avisos_recolectados + 1
WHERE idRecolector = NEW.idAvisos;
END IF;
END//
DELIMITER ;
New edit
DELIMITER //
CREATE TRIGGER NumAvisosRecolectados_AU #after update
AFTER UPDATE ON avisos
FOR EACH ROW
BEGIN
IF (NEW.retirado = 1) THEN
UPDATE recolectores
SET recolectores.num_avisos_recolectados = recolectores.num_avisos_recolectados + 1
WHERE reservas.FK_IDavisos = avisos.ID_avisos
AND reservas.FK_IDrecolector = recolectores.ID_recolector;
END IF;
END; //
DELIMITER ;
Here the trigger is before updating a column in a directed table :
DELIMETER $$
CREATE TRIGGER update_trigger BEFORE UPDATE ON your_table_name
FOR EACH ROW
BEGIN
IF NEW.value <> 0 THEN
UPDATE table_name SET col_name = NEW.value WHERE col_id = NEW.id
END IF;
END ;
DELEMITER ;
Execute the query and everything will be fine , That's it!!
I am trying to create a mysql trigger. This is what I am trying to create, but somehow it tells me that: "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 'A
FOR EACH ROW".
What Am I doing wrong here? I have tried to naming them differently, but it does not seem to work.
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock` A
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN //belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
END IF;
END;//
delimiter ;
Try this one
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock`
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN #belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none'
WHERE id_product = OLD.id_product; #Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both'
WHERE id_product = OLD.id_product; #//Should be the id_product of A alias
END IF;
END;//
delimiter ;
This shows no errors in mysql workbbench
I have 2 tables route and loc. When either the drivername or rt_num changes in the route table I need to update the loc table. I have tried all day playing with the following code. Each UPDATE statement works as a trigger by it self. I am not sure if it can be done at all.
DELIMITER |
CREATE TRIGGER `add2` AFTER UPDATE ON `route`
FOR EACH ROW
BEGIN
AFTER UPDATE ON `route`;
UPDATE loc
SET drivername = new.driver_d_name
WHERE rt_nanme = old.rt_num;
UPDATE loc
SET rt_nanme = new.rt_num
WHERE rt_nanme = old.rt_num;
END;
|
DELIMITER ;
Why are you repeating the after update statement?
In addition, you don't need two update statements. So:
DELIMITER $$
CREATE TRIGGER `add2` AFTER UPDATE ON `route`
FOR EACH ROW
BEGIN
UPDATE loc
SET drivername = new.driver_d_name,
rt_nanme = new.rt_num
WHERE rt_nanme = old.rt_num;
END;$$
DELIMITER ;
There is nothing wrong with having multiple updates in a single trigger. But your trigger has a syntax problem, and it is this which is probably causing the failure. Try this instead:
DELIMITER |
CREATE TRIGGER `add2` AFTER UPDATE ON `route`
FOR EACH ROW
BEGIN
UPDATE loc
SET drivername = new.driver_d_name
WHERE rt_nanme = old.rt_num;
UPDATE loc
SET rt_nanme = new.rt_num
WHERE rt_nanme = old.rt_num;
END
before update compare it with old value
DELIMITER |
CREATE TRIGGER `add2` AFTER UPDATE ON `route`
FOR EACH ROW
BEGIN
IF(new.driver_d_name!=old.driver_d_name) THEN
UPDATE loc
SET drivername = new.driver_d_name
WHERE rt_nanme = old.rt_num;
END IF;
IF(new.rt_num!=old.rt_num) THEN
UPDATE loc
SET rt_nanme = new.rt_num
WHERE rt_nanme = old.rt_num;
END IF;
END;
|
DELIMITER ;
This is the trigger im trying to add. I keep getting "Updating of NEW row is not allowed in after trigger"
DELIMITER $$
DROP TRIGGER IF EXISTS leaderboard.badges_AUPD$$
USE leaderboard$$
CREATE TRIGGER `badges_AUPD` AFTER UPDATE ON `badges` FOR EACH ROW
set new.badgelevel := case when badgepercent < 50 then 0
when badgepercent < 75 then 1
else 2 end;
$$
DELIMITER ;
Your trigger should be before update since after update you cant set any column of the same table, and also missing the begin part
DELIMITER $$
DROP TRIGGER IF EXISTS leaderboard.badges_AUPD$$
USE leaderboard$$
CREATE TRIGGER `badges_AUPD` BEFORE UPDATE ON `badges`
FOR EACH ROW
BEGIN
if new.badgepercent < 50 then
set new.badgelevel = 0 ;
elseif new.badgepercent < 75 then
set new.badgelevel = 1 ;
else
set new.badgelevel = 2;
end if;
end;$$
delimiter ;
I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.