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 ;
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 have a MySQL table , look like this
t id lang title
1 7 en_UK my_title
1 7 kh_KH
I want write a trigger that update title to my_title with the same id is 7
Result
t id lang title
1 7 en_UK my_title
1 7 kh_KH my_title
From my understanding.
DELIMITER $$
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
IF (NEW.title IS NULL OR NEW.title= '') THEN
SET NEW.title= ??? ;
END IF;
END$$
DELIMITER ;
[UPDATE1]->not works (trigger not being created)
DELIMITER $$
DROP TRIGGER IF EXISTS `update_category_after_insert`
CREATE TRIGGER `update_category` AFTER INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
[UPDATE2]
DELIMITER $$
CREATE TRIGGER ``update_category_after_insert`` BEFORE INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
Finally , I found the good solution for my case
UPDATE categories c INNER JOIN categories c2 ON (
c.num_noeud = c2.num_noeud
) SET c.libelle_categorie = c2.`libelle_categorie`
Can you clarify?
Are you trying to pull the value from the title column in the 'en_UK' row that exists when you insert an new row with the same id that HAS the title column not entered?
okay
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
DECLARE loc_title VARCHAR(20);
IF (NEW.title IS NULL OR NEW.title= '') THEN
select title into loc_title from term where NEW.id = id and lang = 'en_UK';
SET NEW.title= loc_title;
END IF;
END
This should do the trick.
This was my trigger def:
CREATE DEFINER = CURRENT_USER TRIGGER `therinks`.`glreturndata_BEFORE_UPDATE` BEFORE UPDATE ON `glreturndata` FOR EACH ROW
BEGIN
DECLARE myVal VARCHAR(20);
if NEW.DESCRIPTION IS NULL or new.description = '' THEN
SELECT min(description) into myVal from glreturndata where category = NEW.category and new.idglreturndata <> idglreturndata;
Set NEW.DESCRIPTION = myval;
end if;
END
It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.
I hoped something as below will be successful. But SQL syntax error...
Clearly i don't want to insert data into a table if only a special data is changed.
CREATE TRIGGER `before_access_update` BEFORE UPDATE ON `access` FOR EACH ROW IF NOT (NEW.pin<>OLD.pin) THEN
INSERT INTO access_audit
SET action = 'update',
a_lastname = OLD.lastname,
a_firstname = OLD.firstname,
changed_on = NOW();
END IF;
You use a ; in your trigger, therefore you have to use another query delimiter for the trigger itself:
DELIMITER $$
CREATE TRIGGER `before_access_update` BEFORE UPDATE ON `access` FOR EACH ROW IF NOT (NEW.pin<>OLD.pin) THEN
INSERT INTO access_audit
SET action = 'update',
a_lastname = OLD.lastname,
a_firstname = OLD.firstname,
changed_on = NOW();
END IF$$
DELIMITER ;
I am trying to create a mysql trigger. The error code 1064 is very generic. The trigger is to see if a record exists for a particular key; and if it does update a particular column in the newly inserted row with that value. I am using a select query on the same table as the trigger is on. Does that cause a problem? Here is the code:
DELIMITER $$
CREATE TRIGGER classid_insert
AFTER INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
NEW.class_id = UUID()
ELSE
NEW.class_id = #class_id
END IF;
END
DELIMITER ;
your entire trigger code should look like below. remove the spaces after DELIMITER $$ and before DELIMITER ;. Also, the last END need to changed to END $$. Also, it has to be an BEFORE trigger and not an AFTER trigger.
Moreover, NEW.class_id = UUID() is not correct.It's missing SET as
SET NEW.class_id = UUID();
CORRECTED TRIGGER CODE:
DELIMITER $$
CREATE TRIGGER classid_insert
BEFORE INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
SET NEW.class_id = UUID();
ELSE
SET NEW.class_id = #class_id;
END IF;
END $$
DELIMITER ;
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 ;