MySQL trigger result - 1064 - mysql

i need to create a trigger in sql to do the following:
Wordpress Table 'options' has in id 1 and id 2 the option_value URL of the Website.
If that URL gets changed in that particular place (id 1 & id2), the trigger should run to set back to old.option_value
CREATE TRIGGER tr_up_otpions_value
BEFORE UPDATE ON option_value FOR EACH ROW
BEGIN
IF (:old.option_value = 'https://example.com') then
:new.option_value := 'https://example.com';
end if;
end;
but i get the following error:
#1064 - Error ':old.option_value = 'https://example.com') then
:new.option_value := 'h...' Row 4

You need to remove the : sign from the old and new values
Also you need to add set keyword.
After that, if you have a table that looks something like this:
create table Zr_options (option_value varchar(500))
then this BEFORE UPDATE TRIGGER will work:
CREATE TRIGGER tr_up_otpions_value
BEFORE UPDATE
ON Zr_options FOR EACH ROW
BEGIN
IF old.option_value = 'https://example.com' then
set new.option_value = 'https://example.com';
end if;
end;
Here is a demo:
DEMO

Related

MYSQL - How to create a trigger after an UPDATE in a table, to increment +1 the value of a column from another table?

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!!

Update Trigger on same table MySQL 5.6

I have a table 'foods' with columns 'featured', 'restaurant_id', 'retaurant_stock'.
How can I update column 'restaurant_stock' using trigger or any other solution available on mySQL version 5.6 after update of column 'featured' on the same table on a condition?
Calculated columns are not available on mySQL version 5.6.
Here's my trigger:
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
UPDATE `foods` SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
UPDATE `foods` SET NEW.restaurant_stock = 0;
END IF;
END
Now when I update column 'featured' I get the following error message:
#1442 - Can't update table 'foods' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for you help!
You can't update the current row that way, you can use set to change the new values
DELIMITER $$
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
SET NEW.restaurant_stock = 0;
END IF;
END$$
DELIMITER ;
CREATE TRIGGER tr_bu_foods
BEFORE UPDATE
ON foods
FOR EACH ROW
-- update restaurant_stock
SET NEW.restaurant_stock = NEW.restaurant_id * (NEW.featured = 1);

mysql trigger after update

i want to create a mysql trigger to manipulate a column
this is the code:
CREATE TRIGGER gestion_absences after update
ON Etudiant FOR EACH ROW
begin
if( NEW.Present = 'Non')
then
update Etudiant set Nbr_Absences = (OLD.Nbr_Absences) + 1 where (NEW.Present) = 'Non';
end if;
End;
the error is:
# 1064 - Syntax error near '' at line 6
I don't know where is the problem
I want when a student is absent , le number is incremented by 1;
if he's present , do nothing.
You cannot update the same table in your trigger AFTER updating it. However, you may create the trigger BEFORE update and set the correct values for Nbr_Absences if present is Non.
CREATE TRIGGER gestion_absences BEFORE UPDATE ON Etudiant
FOR EACH ROW
BEGIN
IF NEW.Present = 'Non' THEN
SET new.Nbr_Absences = old.Nbr_Absences + 1;
END IF;
END
;

Create trigger on update column in table

I try to create trigger before table is updated. I want to it works when two specific columns are changed: status and sale_profit columns. If one of these columns is updated, trigger must set is_changed column's value to 1. But the below code gives me error like this:
Error:
MySQL Error 1064: You have an error in your SQL syntax ... syntax use near at line 7
SQL code:
CREATE TRIGGER updateIsChanged
BEFORE UPDATE ON manage_product
FOR EACH ROW
BEGIN
IF NEW.`status` <> OLD.`status` || NEW.sale_profit <> OLD.sale_profit
then
SET NEW.is_changed = 1;
END IF;
END
Ok. I solved my problem by adding delimiter to beginning and end of the code.
UPDATED CODE:
delimiter $$
CREATE TRIGGER updateIsChanged
BEFORE UPDATE ON manage_product
FOR EACH ROW
BEGIN
IF NEW.`status` <> OLD.`status` || NEW.sale_profit <> OLD.sale_profit THEN
SET NEW.is_changed = 1;
END IF;
END$$

Can I update the just added row using MySQL triggers

The default initial value of one column in my database is the same as the row's auto-incremented id. I'm trying to use triggers to set it.
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END
But this keeps throwing a syntax error
#1064 - 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 '' at line 5
I've tried all sorts of permutations of this with no luck. Can anyone see what I'm doing wrong?
As zerkms said, you need to change the delimeter. But since you only use 1 line of code, you don't need the BEGIN and END. And that way, you don't need to change the delimiter either
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
Since you are getting an error you cannot update the row, I suggest the following:
Do NOT perform the update query at all. On default the order value = the ID value. So when the order value changes, you can update it properly.
If you are requesting the data with php, do something like this:
$order = $row['order'];
if ($order == '')
$order = $row['id'];
After you need it updating, you've got the correct value.
I don't think you can do that. An AFTER INSERT trigger cannot modify the same table, neither by issuing an UPDATE nor by something like this:
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
SET NEW.`order` = NEW.id ;
which results in this error:
> Error Code: 1362. Updating of NEW row is not allowed in after trigger
You can't either use a BEFORE INSERT trigger because then the NEW.id is not known (if you modify the above, the order column will get 0 value after the Insert.
What you can do, is use a transaction:
START TRANSACTION ;
INSERT INTO clusters (id)
VALUES (NULL);
UPDATE clusters
SET `order` = id
WHERE id = LAST_INSERT_ID();
COMMIT ;
You get the error because mysql treats ; in line 5 as the end of your trigger declaration, which obviously leads to the syntax error.
So you need to redefine delimiter before you specify the trigger body:
delimiter |
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END;
|
delimiter ;
You can create just BEFORE INSERT TRIGGER, it's works like this:
CREATE TRIGGER `default_order_value`
BeFORE INSERT ON `clusters`
FOR EACH ROW
BEGIN
SET NEW.`order` = NEW.id ;
END
same as below we are using
DELIMITER $$
USE `e_store`$$
DROP TRIGGER /*!50032 IF EXISTS */ `Test`$$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `Test` BEFORE INSERT ON `categories`
FOR EACH ROW
BEGIN
DECLARE vtype VARCHAR(250) DEFAULT NULL;
SET vtype = NEW.name;
IF (NEW.MDNAME IS NULL)
THEN
-- SET NEW.MDNAME = 'NA';
SET NEW.MDNAME=MD5(NEW.name);
END IF;
END;
$$
DELIMITER ;
This worked for me:
CREATE TRIGGER `update_table_2`
AFTER UPDATE ON `table_1`
FOR EACH ROW
UPDATE table2
JOIN table_1
SET table_2.the_column = NEW.the_column
WHERE table_2.auto_increment_field = OLD.auto_increment_field