I have 2 tables that I want to be synchronized when insert, update or delete
can I create more than one trigger on the same table??
I already wrote code like that .. but it doesn't work .. but when I create only one trigger it works correctly.
the code is something like that:
CREATE TRIGGER photosinsert BEFORE INSERT ON photos
FOR EACH ROW BEGIN
INSERT INTO old_photo SET PhotoID = NEW.photo_id, photo_original = NEW.file_name
;
END;
delimiter |
CREATE TRIGGER photosupdate BEFORE UPDATE ON photos
FOR EACH ROW BEGIN
UPDATE old_photo SET photo_original = NEW.file_name
WHERE
PhotoID = NEW.photo_id
;
END;
delimiter |
CREATE TRIGGER photosdelete BEFORE DELETE ON photos
FOR EACH ROW BEGIN
DELETE FROM old_photo WHERE
PhotoID = OLD.photo_id
;
END;
is there a solution for that, please?
Put all code into single trigger.
You can use multiple statements between BEGIN and END
Related
I need to create a trigger for when I insert data in the first table to be able to insert in the second some data related to the first one.
CREATE TRIGGER test1
ON Telegramas
BEFORE INSERT
AS
BEGIN
INSERT INTO Dispositivos SET Dispositivos.GrupoOrigen = Telegramas.GrupoOrigen;
END;
I have created that but there is a syntax error that I can not see. Some help? or another way to make the relationship easier?
You have to change the delimiter, like this :
delimiter |
CREATE TRIGGER test1
[...]
END |
delimiter ;
But there is a problem in your trigger :
delimiter |
CREATE TRIGGER test1 BEFORE INSERT ON Telegramas
FOR EACH ROW
BEGIN
INSERT INTO Dispositivos SET GrupoOrigen = NEW.GrupoOrigen;
END |
delimiter ;
I have two tables & need to write a TRIGGER for this scenario.
1. Parameters_Table(int pkid, string param_name, string param_value)
2. Tokens_Table(int pkid,string item,string validity)
I have a requirement of deleting all entries in Tokens_Table if a particular row where param_name='Enterprise' in Parameters_Table changes its 'param_value'. The value is changed from UI. eg., row <7,enterprise,60> changes to <7,enterprise,25> in Parameters_Table then delete all entries from Tokens_Table.
How should I write the trigger. Please guide. (I need to write this for Informix Database, but mysql queries would also help)
DELIMITER $$
CREATE TRIGGER after_update AFTER UPDATE ON Parameters_Table
FOR EACH ROW
BEGIN
IF OLD.param_name ='Enterprise' THEN
IF NEW.param_value != OLD.param_value THEN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END IF;
END IF;
END$$
Just try above code.Hope this will helps.
You can try the following syntax/format:
DELIMITER $$
CREATE TRIGGER delete_tokens AFTER INSERT ON Parameters_Table
FOR EACH ROW
BEGIN
IF NEW.param_name = 'enterprise' THEN
BEGIN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END;
END IF;
END$$
In MySQL, I want to fire a trigger after the update of specific columns.
I know how to do it in Oracle and DB2:
CREATE TRIGGER myTrigger
AFTER UPDATE of myColumn1,myColumn2 ... ON myTable
FOR EACH ROW
BEGIN
....
END
How to do that with MySQL?
You can't trigger on a particular column update in SQL. It is applied on a row.
You can put your condition for columm in your trigger with an IF statement, as below:
DELIMITER //
CREATE TRIGGER myTrigger AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
IF !(NEW.column1 <=> OLD.column1) THEN
--type your statements here
END IF;
END;//
DELIMITER ;
You can't specify only to fire on specific column changes. But for a record change on a table you can do
delimiter |
CREATE TRIGGER myTrigger AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
...
END
|
delimiter ;
In your trigger you can refer to old and new content of a column like this
if NEW.column1 <> OLD.column1 ...
As a solution, I did like that:
CREATE TRIGGER myTrigger
AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
IF NEW.myColumn1 <> OLD.myColumn1 || NEW.myColumn2 <> OLD.myColumn2
then
.....
END IF
END
Here I want to copy the contents of X table into Y table and on the same lines I wanted to delete the row from X table.I tried it in following way but it is not working.
Drop trigger if exists myTrigger;
delimiter |
create trigger myTrigger
after insert on X
for each row
BEGIN
IF STRCMP(NEW.SysLogTag,"kernel:") = 0 THEN
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,1,1,100,NEW.Message);
ELSEIF NEW.SysLogTag like 'ntpd[%]:' THEN
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,6,1,100,NEW.Message);
ELSE
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,4,1,100,NEW.Message);
END IF;
delete from X where ID=NEW.ID; //Not working.
END;|
delimiter ;
From http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html#c8808 :
My solution was to create a unique/primary key on the table I want to insert into (in this case on S_ID), then if my sanity check fails I change the s_id to 0. This method will only ever create one duff record with an s_id of 0. Obviously you need to INSERT ignore!
CREATE TRIGGER sanityCheck
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF something THEN
SET NEW.S_ID = 0 ;
END IF;
END;
I have a table (myTable) with 3 columns [s1, s2, sum]
And I want to add a trigger that automatically updates sum with s1+s2 on each update. Thats my code but it doesn't work. What I'm doing wrong?
Thanks in advance
DROP TRIGGER IF EXISTS `mTrigger`;
DELIMITER //
CREATE TRIGGER `mTrigger` BEFORE UPDATE ON `myTable`
FOR EACH ROW BEGIN
SELECT NEW.s1 + NEW.s2 INTO #sum;
SET #NEW.sum = #sum;
END
//
DELIMITER ;
try something like this:
delimiter #
create trigger myTable_before_update_trig before update on myTable
for each row
begin
set new.sum = new.s1 + new.s2;
end#
delimiter ;