trigger after update to delete row on condition - mysql

i want to create trigger after update to delete row on specific condition
i did find similar question here but the code won't work with me.
This is what i try to do :
DELIMITER $$
CREATE TRIGGER delete_rejected_friendship
AFTER UPDATE
ON friendship FOR EACH ROW
BEGIN
IF Update(RequestState)
BEGIN
DELETE FROM friendship WHERE RequestState = 'reject'
END
END
END$$
DELIMITER ;
This is the error which appears:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Update(RequestState)
Begin
DELETE FROM friendship WHERE Reque' at line 9`
so i have friendship table that have three columns userID , friendID and RequestState. this table represent friendship relationship and friendship request state. if RequestState= accept then they are friends and if RequestState = reject the row should be deleted.
Edit :
if update(requeststate) for if the column = requeststate updated (changed from 'waiting' state to either 'accept' or 'reject' state) then the other condition in delete statement (WHERE RequestState = 'reject') to specify only change to 'reject' state

I think, You are using Update(RequestState) condition like PostgreSQL, in MySQL or MariaDB could be like that:
DELIMITER $$
CREATE TRIGGER delete_rejected_friendship
AFTER UPDATE
ON friendship FOR EACH ROW
BEGIN
IF NEW.RequestState <> OLD.RequestState THEN
BEGIN
DELETE FROM friendship WHERE RequestState = 'reject';
END;
END IF;
END;
$$
DELIMITER ;

Related

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

roadblock creating trigger in Mysql , it is giving me syntax #1064 error

SQL query I have written to create trigger, is as follows , but it is giving me #1064 syntax error.
DELIMITER $$
CREATE TRIGGER beforeUserGroupDelete BEFORE UPDATE ON user_groups
FOR EACH ROW BEGIN
IF NEW.isactive = 0 THEN
UPDATE users_usergroups SET isactive=0 where group_id = NEW.id;
END IF;
END$$
DELIMITER;
What I am trying to do is when user will disable the group in user_groups table then in other user-group association(users_usergroups) table, all the entry for that group will be disabled as well.

Trigger creation query failed

I have three tables, when data is inserted into third table, I am checked whether newly inserted row has field value in table1. If value exists then I am moving the same row from table3 to table2. My code is like below, But I am getting error while executing this
DELIMITER $$
CREATE TRIGGER onRosterAdd
AFTER INSERT ON yet_tobe
FOR EACH ROW
BEGIN
SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END
END$$
DELIMITER ;
And here is the error
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 'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
How can I fix this.
Try it like this:
DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END IF;
END$$
DELIMITER ;
Here's a working fiddle

Two triggers and I am getting an error when updating table

I got the two following triggers, each one worked alone, but together they don't.
How can I make them to work together? They update different fields in table.
trigger 1:
create trigger wys_sk_u after update on `things`
for each row
begin
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id= s.id
SET s.`curr_cash` = u.cash1 * b.cash2/ 100;
end;
$$
trigger 2:
create trigger suma_u after update on `current`
for each row
begin
UPDATE `current`
SET `mysum` = `curr_cash` + `mysum`;
end;
$$
First one should update when cash1 or cash2 updates, and change value of curr_cash.
Second should update when curr_cash updates, and change mysum.
I got following error when I edit table things:
#1442 - Can't update table 'current' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
#edit
added new answear to the question.
What if I want to do something like this:
CREATE TRIGGER total BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
if new.`new_pay_date` <> old.`new_pay_date`
SET new.`total_cash` = new.`curr_cash` + new.`total_cash`;
end if;
END;
$$
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 'SET new.`total_cash` = new.`curr_cash` + new.`total_cash`; end if;' at line 4
This was working without
if new.`new_pay_date` <> old.`new_pay_date`
end if;
But I need to check this, and only update with date change.
current table:
curr_cash
new_pay_date
id_person
id_thing
total_cash
Anyone can help me with this one?
The problem is in the second trigger. Try to use BEFORE UPDATE trigger to change field value using SET statement -
CREATE TRIGGER suma_u BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
SET new.`mysum` = new.`curr_cash` + new.`mysum`;
END;
It is not possible to update a table for for which the trigger is created in the trigger. There is locking mechanism on MySQL so that you can't update the row of the same table triggered the trigger, it can cause recursive call to the trigger and infinite loop.

Something wrong in creating mysql trigger

Hi i'm trying to create below trigger
CREATE TRIGGER TRIGBEFORE INSERT ON employee
FOR EACH
ROW
BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END
it is giving me below mysql error, please suggest what is wrong in it.
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
you forgot to set the delimiter and misspelled a word:
delimiter |
CREATE TRIGGER TRIG BEFORE INSERT ON employee
FOR EACH ROW BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END;
|
delimiter ;
If you don't set another delimiter than ; the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the stamentment should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;
I think you have a typo mistake TRIGBEFORE: manual here
CREATE TRIGGER trigger_name BEFORE INSERT ON ...