MySQL Trigger with REPLACE INTO TABLE? - mysql

Is it possible to apply a trigger after a REPLACE INTO TABLE command? I figured it would be the same as after an update even but it doesn't seem so.
I have a .csv file that is downloaded once a day with the data then inserted into my Database as follows:
LOAD DATA LOCAL INFILE 'mycsv.csv'
replace INTO TABLE mytable
FIELDS TERMINATED BY ',';
Is it possible to apply a trigger on this?
My trigger which is set to execute after update:
BEGIN
IF !(NEW.`Trend Price` <=> OLD.`Trend Price`) THEN
INSERT INTO trend_prices values (old.idProduct, old.`Trend Price`, NEW.`Trend Price`, NOW());
END IF;
END

Instead of using the update trigger it uses insert and delete (when needed).
BEFORE INSERT;
BEFORE DELETE (if a row is being replaced);
AFTER DELETE (if a row is being replaced);
AFTER INSERT.
Source: https://falseisnotnull.wordpress.com/2015/09/25/mariadbmysql-on-replace-triggers/

Related

How to create a trigger in mysql Before Insert to concatenate value with timestamp

my sql query like this,
insert into transfer_job (job_name, status) values ('Drift', 'Pending');
here i have to replace coming job_name Drift with Drift_20180317 (have to concatenate timestamp along with it)
which trigger should i use?
1) Before Insert,
2) After Insert
and how to made trigger for this?
i am using mysql Database and mysqlworkbench
i am new to trigger hope i explained well...
I did it,
CREATE DEFINER=`User`#`%` TRIGGER `transfer_job_BINS` BEFORE INSERT ON `transfer_job` FOR EACH ROW
BEGIN
set NEW.job_name = concat(NEW.job_name,'_',DATE_FORMAT(NOW(),"%Y%m%d%H%i%s"));
END

How to separate DELETE, UPDATE and INSERT operations within a MySQL trigger?

I have the following trigger in Oracle database -
CREATE TRIGGER B4DEL
BEFORE DELETE ON test FOR EACH ROW
BEGIN
IF deleting THEN
INSERT INTO tab VALUES (:old.oid,'Deleting');
END IF;
IF inserting THEN
INSERT INTO tab VALUES (:new.oid,'Inserting');
END IF;
END;
I want to do something similar in MySQL database by using a single trigger.

modify the table from its own trigger in Mysql- PhpMyAdmin

I'm using Mysql in phpMyAdmin,where i have to delete a entry from tableA if i insert a row with same primary key.I thought to do it in trigger of tableA BEFORE INSERT
For Ex,
if the tableA contains
1 Hai Hello
here 1 is the primary key
And now if i insert a row 1 Bye Hello then the trigger BEFORE INSERT will delete the old entry and then the new row (2nd) will be inserted. But Mysql has restriction of not being able to update a table inside a trigger defined for that same table.
It gives the error
#1442 - Can't update table 'tableA' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
So i changed my way, i called a procedure from trigger BEFORE INSERT of tableA and in that procedure i do the task what i thought to do in trigger. But unfortunately i'm getting the same error.
In trigger BEFORE INSERT i simply called the procedure as
CALL proce1(new.Reg_No);
In procedure i have done this
DECLARE toup integer;
select count(*) into toup from tableA where Reg_No=reg;/*Here Reg_No is primary key */
if toup > 0 then
delete from tableA where Reg_No=reg;
end if;
Need some other Idea to achieve this. Help Me.....
I don't like to use triggers so much because they are hard to manage somethimes. Also they will cause you a downgrade in performance. I am not against trigger as they can be handy in some cases.
In your case have you though of using REPLACE(....) or INSERT INTO .... ON DUPLICATE KEY UPDATE or even INSERT INTO IGNORE .....
REPLACE(....) will delete a record if a record is found and insert a new one with the same ID.
INSERT INTO .... ON DUPLICATE KEY UPDATE will allow you to override existing field if a duplicate is found.
INSERT INTO IGNORE ..... will allow you to ignore the new inserted row if one already exists
Since you mentioned in the comments that you are importing records from a file, then try to use LOAD DATA INFILE logic which will allow you to REPLACE field on duplicate
LOAD DATA LOCAL INFILE 'x3export.txt'
REPLACE INTO TABLE x3export

Mysql dropping inserts with triggers

Using mysql 5.6. I have two tables. One has a whitelist of hashes. When I insert a new row into the other table, I want to first compare the hash in the insert statement to the whitelist. If it's in the whitelist, I don't want to do the insert (less data to plow through later). The inserts are generated from another program and are text files with sql statements.
I've been playing with triggers, and almost have it working:
CREATE TRIGGER `Filelist` BEFORE INSERT ON `filelist`
FOR EACH ROW BEGIN IF(
SELECT count( md5hash ) FROM whitelist WHERE md5hash = new.hash ) >0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Can not have duplicates';
END IF ;
END
But there's a problem. The Signal throwing up the error stops the import. I want to skip that line, not stop the whole import.
Some searching didn't find any way to silently skip the import.
My next idea was to create a duplicate table definition, and redirect the insert to that dup table. But the old and new don't seem to apply to table names.
Other then adding an ignore column to my table then doing a mass drop based on that column after the import, is there any way to achieve my goal? I'm having problems with this too [Ignore is a tinyint(1)]:
DELIMITER $$
CREATE TRIGGER whitelisted
BEFORE INSERT ON filelist
FOR EACH ROW BEGIN
IF (select count(md5hash) from whitelist where md5hash=new.hash) > 0 THEN
SET Ignore = true;
END IF;
END$$
/* This is now "END$$" not "END;" */
/* Reset the delimiter back to ";" */
DELIMITER ;
#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 ') THEN SET Ignore = true;
END IF; END' at line 4
Any suggestions? I've also tried
SET Ignore = 1;
SET Ignore = '1';
SET new.Ignore = {all of the above};
I'm not sure if I follow this specification:
I have two tables. One has a whitelist of hashes. When I insert a new row into the other table, I want to first compare the hash in the insert statement to the whitelist. If it's in the whitelist, I don't want to do the insert
My first attempt would be like this:
INSERT INTO filelist (filename, hash)
SELECT "myfile", "ABCD" FROM DUAL
WHERE NOT EXISTS(
SELECT md5hash FROM whitelist where md5hash = "ABCD"
);
I don't think you need triggers for this at all unless there are missing details in your requirements.
I take it you're doing some kind of ON INSERT-trigger.
You need to add the following statement to your trigger to make it work as wanted:
FOR EACH ROW
This will make the trigger execute once on every row.

MySQL IF Statement issue in a Trigger

So I am trying to create trigger that will only execute when a specific column within a table is updated. The table in question is track the column in question is track.track_hits. What I want to do is if that value changes then it inserts a value into a table called play_log. The insert should be the track_id from track and a timestamp.
The code I have done so far is below but it doesn't work, how can it be fixed?
CREATE TRIGGER pi_play AFTER UPDATE ON track
FOR EACH ROW
BEGIN
if :new.track_hits != :old.track_hits
then
INSERT INTO play_log (track_id,access_time)
VALUES (NEW.track_id, NOW())
end if;
END;
NEW and OLD do not need : in front. Also, when creating triggers, you may need to change the delimiter. If you don't change the delimiter, your create trigger statement may be terminated early.
Here's how I would do it:
delimiter |
create trigger pi_play after update on track
for each row
begin
if new.track_hits != old.track_hits then
insert into play_log(track_id, access_time) values (new.track_id, now());
end if;
end|
delimiter ;
Here's the fiddle showing it in action: http://sqlfiddle.com/#!2/3d99d/1 Notice that I changed the delimiter in the schema window to | instead of the default ;. Since SQL uses JDBC and JDBC doesn't take the command DELIMITER, I chose the delimiter.