Problems with MYSQL trigger copying certain data to another table - mysql

After hours of pulling my hair out I could do with a little help with a MYSQL trigger. I have two tables running logs. Let's say a 'master' and a 'slave'. When a log is entered into the 'master', if a string within a column meets a certain criteria I need certain values from that new row to be copied to a slave table.
This is the closest I have gotten so far...
CREATE TRIGGER `web_log_filter` AFTER INSERT ON `master` FOR EACH ROW
BEGIN
IF (NEW.criteria_column = 'criteriaX') THEN
INSERT INTO slave (coulmn1,column2,column3)
VALUES(NEW.coulmn1,NEW.column2,NEW.column3);
END IF;
END
It is currently returning a syntax error on line 5 and can't figure out why.
Any help is much appreciated.

Are you using DELIMITER?
DELIMITER $$
CREATE TRIGGER `web_log_filter` AFTER INSERT ON `master`
FOR EACH ROW
BEGIN
IF (NEW.criteria_column = 'criteriaX') THEN
INSERT INTO slave (coulmn1, column2, column3)
VALUES(NEW.coulmn1, NEW.column2, NEW.column3);
END IF;
END;$$
DELIMITER ;

Related

How to create 'before insert' trigger that looks at multiple tables

I am struggling to understand the format to create an 'insert before' trigger that must meet conditions of other tables.
For example, if I have a table named 'borrowed' that I would like to insert values into but must meet conditions from another table called 'member'. The condition is that MemberStatus = 'regular'. If this condition passes, I will be able to run a 'insert into borrowed' but if the conditions aren't met then it will not be able to pass.
This is how I previously understood the concept but after hours of reading i've become confused. Any help would be appreciated.
delimiter //
drop trigger if exists trigger1
//
create trigger trigger1
before insert on borrowed
for each row
begin
if...
end if;
end;
//

unable to update the record in triggered table when new record is found in mysql trigger

unable to write a trigger to insert a row if no imei found in the triggered table if found then it should update the record.
query error - lost connection to my sql server.
DELIMITER $$
create TRIGGER update_livedata4
AFTER INSERT ON `Rawdata`
FOR EACH ROW
begin
IF Livedata.IMEI <> Rawdata.IMEI THEN
INSERT INTO Livedata(IMEI,updatedTime,latitude,longitude,speed,ignition,fuel,altitude,battery,runHrs,alert,distance) VALUES (NEW.IMEI,NEW.updatedTime, NEW.latitude,NEW. longitude,NEW.speed,NEW.ignition,NEW.fuel,NEW.altitude,NEW.battery,NEW.runHrs,NEW.alert,NEW.distance);
END IF;
UPDATE Livedata SET IMEI = new.IMEI,updatedTime=new.updatedTime,latitude= NEW.latitude,longitude=NEW. longitude,speed=NEW.speed,ignition=NEW.ignition,fuel=NEW.fuel,altitude=NEW.altitude,battery=NEW.battery,runHrs=NEW.runHrs,alert=NEW.alert,distance=NEW.distance;
END$$
DELIMITER ;
It seems that you are missing where clause in the update that you have written which in turn takes more than your net read timeout....!
Are you willing to update all the records?
If that is the case then increasing the timeout would help...

MYSQL fires the trigger only when no value manually inserted - not working

Hi I just need help as I am just new to mysql trigger.
I'm trying to create a trigger that if the project id is NULL then it will set it to new value but if someone has manually insert a value then it won't do anything.
I performed this but it ain't working on mysql.
CREATE TRIGGER custom_autonums_pdl BEFORE INSERT ON project_details_logs
FOR each ROW
WHEN (new.projectid IS NULL)
BEGIN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END//
delimiter ;
If someone can direct me to correct this, appreciate it.
Thank you.
I haven't seen a MariaDb/MySQL trigger that uses the WHEN syntax available in other major RDBMS - you'd need to run the trigger for every insert and conditionally act on the id:
DELIMITER |
CREATE TRIGGER custom_autonums_pdl
BEFORE INSERT ON project_details_logs
FOR each ROW
BEGIN
IF new.projectid IS NULL THEN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END IF;
END|
DELIMITER ;

MySQL TRIGGER to INSERT BEFORE INSERT using SELECT, IF, etc

I am trying to write a trigger that combines an insert & select, I've found numerous topics online, but, none seem to relate to my exact problem, maybe I am missing something with my structure?
The aim of this is that on the event of a cancellation in our audit log, then I define a cancellation reason based on a series of business logic in another table, this logic is drawn together in a SELECT using CASE & subqueries.
I want to expand the following trigger that currently works and replace the SET cancellation_point='test' element with the SELECT query I just mentioned.
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
IF (NEW.status='cancel' AND NEW.type='0') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point='test';
END IF;
END;
$$
DELIMITER ;
I did try to construct this myself using various guidance from here, but, its just not working. I got this code to physically save as a trigger, but, it did not populate the data in the database (I have replaced my SELECT with a basic query example below):
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
DECLARE cancellation_point VARCHAR(255);
SET cancellation_point = ( SELECT * FROM x);
IF (NEW.transition='cancel' AND NEW.entity_type='property') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point=NEW.cancellation_point;
END IF;
END;
$$
DELIMITER ;
Any help would be greatly appreciated :)

Make a trigger that copies new data from table to another table after insert

I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.