Create trigger on update column in table - mysql

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

Related

MySQL trigger result - 1064

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

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 syntax error while inserting for trigger action

I am trying to create a trigger such that if there is any change in a particular table column then event should be created in another. I am using following sql query for this
CREATE TRIGGER enabled_update BEFORE UPDATE ON useraccount
FOR EACH ROW
BEGIN
IF (NEW.enabledAccount != OLD.enabledAccount) THEN
INSERT INTO logEvents
SET NEW.event = OLD.employeeID;
END IF;
END;
But I am getting following error when I am trying to run this query.
Error in query (1064): Syntax error near '' at line 6 and Error in query (1064): Syntax error near 'END IF' at line 1. Where I am mistake?
Use the DELIMITER command, see 23.1 Defining Stored Programs.
DROP TRIGGER IF EXISTS `enabled_update`;
DELIMITER //
CREATE TRIGGER `enabled_update`
BEFORE UPDATE ON `useraccount`
FOR EACH ROW
BEGIN
IF (NEW.`enabledAccount` != OLD.`enabledAccount`) THEN
INSERT INTO `logEvents`
SET `event` = OLD.`employeeID`;
END IF;
END//
DELIMITER ;

MySQL AFTER UPDATE TRIGGER to increase count

Any ideas what's wrong with this query? I searched around stackoverflow and it seems everyone is using this and it works.
mysql> CREATE TRIGGER upd_entry
-> AFTER UPDATE ON entry FOR EACH ROW
-> BEGIN
-> UPDATE entry
-> SET count = count +1
-> WHERE id = NEW.id
-> END;
ERROR 1064 (42000): 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 'END' at line 7
Run this first to change the delimiter
DELIMITER //
create the trigger
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry
SET count = count +1
WHERE id = NEW.id;
END//
After change it back
DELIMITER ;
forget ; at end of line
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry SET count = count +1 WHERE id = NEW.id;//forget to close line ;
END;//
delimiter;
Mysql Trigger

MySQL trigger: error when checking if variable exists

I have a trigger which references a global variable that's coming from a delete query which sets the trigger off:
DELIMITER ;;
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN #userID := 0
END IF;
END
;;
DELIMITER ;
For some reason I'm getting SQL error:
SQL 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 '#userID := 0
I don't understand this, it's a very basic IF statement, why doesn't it work?
UPDATE: I'm totally confused, I've tried this
IF TRUE
THEN TRUE
END IF;
and it still throws an error... Seriously?
UPDATE 2: SOLVED This actually works, however it is very weird
IF (SELECT #userID IS NULL)
THEN SET userID = 0;
END IF;
Apparently you need a semicolon inside IF statement?
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN
SET #userID = 0;
END IF;
END;