Fire a trigger after the update of specific columns in MySQL - mysql

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

Related

Create a trigger that finds string value and copies ID to insert to new table

I am trying to write an MYSQL statement that will copy the patient_id where they have a value of "Dose: Second" and paste it to my table vaccinatedpatients.
Also, I wanted to implement this within a trigger to automatically update itself after each new record on the table.
Any help with this will be great!
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW BEGIN
SELECT #variable = patient_id from patient where vaccine_Dose = 'Dose: Second';
INSERT INTO vaccinatedpatients VALUES(variable);
END
You can use an IF cLause, to determines if the patient gets the second dose
DELIMITER &&
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW
BEGIN
IF NEW.vaccine_Dose = 'Dose: Second' THEN
INSERT INTO vaccinatedpatients VALUES (NEW.patinet_id);
END IF;
END&&
DELIMITER ;
The DELIMITER may not be needed.

I want to create a trigger in mysql which insert a row in a table when update a row in another table

I want to create a trigger in mysql which insert a row in a table change_history when there is any update in another table event_data if event_data.title='event_media'.
DELIMITER $$
CREATE TRIGGER `after_evdata_update`
AFTER UPDATE ON event_data
FOR EACH ROW
BEGIN
if OLD.title <=> 'event_media'
INSERT INTO change_history (badge,city,country,venue,company,
date,rehosted,type,cancelled,name,tagline,description,agenda,pricing,
edition,photo_video,exhibitors,speakers,official_url,twitter_handle,
twitter_hashtag,facebook_url,contact,post_review_id) values(1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,null);
end if;
END $$
DELIMITER ;
I think this should work. Please try.
BEGIN
IF (OLD.title <> 'event_media')
THEN
INSERT INTO `change_history` (`xxx_id`,`and_all_other_fields_similar_way`)
VALUES (old.xxx_id,old.and_all_other_fields_similar_way);
END IF;
END

Mysql : Trigger after insert copy content into another table and delete row from triggered table

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;

Sum two columns with a trigger

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 ;

I want to create more triggers on the same table

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