trigger update on column change - mysql

Hello currently my trigger updates on table update, and I need to change this to only fire when specific column changes.
code:
create trigger money after update on things
for each row
begin
UPDATE `current` c
INNER JOIN things t ON c.id2 = t.id2
INNER JOIN dude_base d ON d.id1 = c.is1
SET c.`curr_cash` = t.thing_cost * d.salary / 100;
end;
$$
And I need to change so it will turn on, when there is update on "thing_cost" from things.
#edit
I have to update that curr_cash and things cost are totaly different values, I cannot compare them, they will always be different.
When I used
if NEW.things_cost <> OLD.things_cost
I get following 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 10
#edit
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
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.thing_cost * b.salary/ 100;
end if;
$$

Try this code...
**create table sales** (orderno INT, sale INT,empsalary int, ts TIMESTAMP);
**create table history** (updated varchar(20), oldvalue INT,newvalue INT);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(1,700,7000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(2,800,8000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(3,900,9000);
DROP TRIGGER test.instrigger;
DELIMITER ///
CREATE TRIGGER test.instrigger AFTER UPDATE ON sales
FOR EACH ROW
BEGIN
IF NEW.sale <> OLD.sale THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('sale', OLD.sale,NEW.sale);
END IF;
IF NEW.empsalary <> OLD.empsalary THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('empsalary', OLD.empsalary,NEW.empsalary);
END IF;
END;
///
DELIMITER ;

delimiter $$
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
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.thing_cost * b.salary/ 100;
end if;
end;
$$

Related

Before Insert Trigger to check a value exists in database

I would like to creating a trigger that updates the database when a new message is sent.
If the SenderId and RecipentId already have a conversationId, then update the database so that the New.conversationId = existing conversationId.
Table: mailbox_message
Create a trigger that updates the conversationId to the same value whenever users 11 & 5 send messages to each other.
delimiter $$
create trigger before_insert_conversationId
before insert on data25k29_mailbox_message
for each row
begin
IF ( EXISTS (
SELECT data25k29_mailbox_message.conversationId FROM data25k29_mailbox_message
WHERE (data25k29_mailbox_message.senderId='5'
and data25k29_mailbox_message.recipientId = '11')
and (data25k29_mailbox_message.senderId= '11'
and data25k29_mailbox_message.recipientId= '5')
)
)
THEN
SET NEW.conversationId = data25k29_mailbox_message.conversationId;
ELSE
SET NEW.conversationId = NEW.conversationId;
END IF;
end
$$
The value doesn't change after inserting values. Any help in this regard is greatly appreciated. Thanks
This is my working snippet of this question.
Drop trigger if exists after_insert_conversationId;
delimiter |
CREATE TRIGGER after_insert_conversationId
before insert ON data25k29_mailbox_message
FOR EACH ROW
BEGIN
Declare v1 int;
select conversationId into v1 from data25k29_mailbox_message
where senderId = new.senderId and recipientId = new.recipientId limit 1;
if v1 is NULL
then
set new.conversationId = new.conversationId;
else
set new.conversationId = v1;
End if;
END|

Mysql trigger if statement update second table

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

WHATS WRONG IN FOLLOWING CODE,

CREATE OR REPLACE TRIGGER T12
AFTER INSERT OR UPDATE OR DELETE
ON EMP
FOR EACH ROW
DECLARE
OP VARCHAR2(20);
NEW NUMBER(5);
BEGIN
IF INSERTING THEN
OP := 'INSERT';
ELSIF UPDATING THEN
OP :='UPDATE';
ELSE
OP :='DELETE';
END IF;
INSERT INTO EMP_AUDIT
VALUES(:NEW.EMPNO,:NEW.ENAME,:NEW.SAL,:OLD.EMPNO,:OLD.ENAME,:OLD.SAL);
END;
You have an error in your SQL syntax; it seems the error is around: 'TRIGGER T12 AFTER INSERT OR UPDATE OR DELETE ON EMP FOR EACH ROW DECLARE O'
CREATE
OR REPLACE TRIGGER T12
AFTER
INSERT
OR
UPDATE
OR DELETE ON EMP FOR EACH ROW DECLARE OP VARCHAR2(20);NEW
NUMBER(5);BEGIN IF INSERTING THEN OP: = 'INSERT';ELSIF UPDATING THEN
OP:
= 'UPDATE';ELSE OP: = 'DELETE';END IF;
INSERT INTO
EMP_AUDIT
VALUES(
:NEW.EMPNO,
:NEW.ENAME,
:NEW.SAL,
:OLD.EMPNO,
:OLD.ENAME,
:OLD.SAL
);END;
Please check the TRIGGER T12

Error #1109 in MySql

This is my trigger.
I want to make trigger on 1 table (rezervare).
Trigger
DELIMITER //
CREATE TRIGGER verificare_nr_locuri
BEFORE INSERT ON Rezervare
FOR EACH ROW
BEGIN
IF ( SELECT Masa.NrLocuri FROM Rezervare, Masa
WHERE Masa.NumarMasa = Rezervare.NumarMasa ) < Rezervare.NumarPersoane THEN
SET NEW.NumarMasa = NULL;
END IF;
END //
DELIMITER ;
INSERT INTO Rezervare VALUES (123,106,2,'2016-12-11','2016-12-24',5);
INSERT INTO Rezervare VALUES (124,106,2,'2016-12-11','2016-12-24',4);
When I execute the trigger, it has been created. But, when I insert data into table rezervare, It become
#1109 - Unknown table 'rezervare' in field list.
How can I resolve this?
Table
I think you might need to use a JOIN for your SELECT clause.
BEGIN
IF (SELECT Masa.NrLocuri, Rezervare.NumarMasa FROM Masa JOIN Rezervare
WHERE Masa.NumarMasa = Rezervare.NumarMasa) < Rezervare.NumarPersoane THEN
SET NEW.NumarMasa = NULL;
END IF;

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