MySQL IF Statement issue in a Trigger - mysql

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.

Related

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 ;

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.

How to get the time when a mysql trigger was created or last updated?

We have a trigger in our mysql database which on update affect some data in database. But for some calculation we need to know when this trigger was first created and when it was updated last. Is it possible to get this data from information_schema or any where else?
Unfortunately the answer is no. There is a column CREATED in the schema for triggers, but it is unused (always NULL) at the present time:
The following columns currently always contain NULL: TRIGGER_CATALOG,
EVENT_OBJECT_CATALOG, ACTION_CONDITION, ACTION_REFERENCE_OLD_TABLE,
ACTION_REFERENCE_NEW_TABLE, and CREATED.
You can find information about triggers in INFORMATION_SCHEMA.TRIGGERS:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
Hope that helps.
Try this,
I don't know what datatype you are using for created.
But for example, I use DATETIME.
Before insert, and you just need to use SET:
Create:
DELIMITER $$ CREATE
TRIGGER yourtriggername BEFORE INSERT ON yourtablename
FOR EACH ROW
SET NEW.created = NOW();
END$$ DELIMITER ;
Update:
DELIMITER $$ CREATE
TRIGGER yourtriggername AFTER UPDATE ON yourtablename
FOR EACH ROW BEGIN
SET NEW.updated = CURRENT_TIMESTAMP where.....yourconditions;
END$$ DELIMITER ;
Hope it might help.

MySQL trigger after bulk insert

I bulk insert a variable amount of rows in a MySQL table:
INSERT INTO table (col1, col2) VALUES (0,0), (0,1), ..., (0,N)
The first column is always the same, the second column changes in every entry. Now I want to implement a MySQL trigger that uses col1 to update another table. E.g.
DELIMITER //
CREATE TRIGGER updateOtherTable AFTER INSERT ON table
FOR EACH ROW BEGIN
CALL someProcedure(NEW.col1);
END;
//
However, that would execute someProcedure() N times which is not necessary since col1 is always the same for each INSERT.
Is it possible to call this trigger only once and if so, how?
I do not think you can prevent the execution of the trigger for each row.
One option is to use a 9.4. User-Defined Variables (be careful with User-Defined Variables) that controls whether to make the call to the stored procedure.
Something like:
DELIMITER //
CREATE TRIGGER `updateOtherTable` AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
IF #`callSomeProcedure` THEN
SET #`callSomeProcedure` := NULL;
CALL `someProcedure`(NEW.`col1`);
END IF;
END//
DELIMITER ;
SQL Fiddle example

Incorrect syntax in trigger

I have two tables called account and transaction. I need to insert data to transaction automatically when account inserts data. I created this trigger in MySQL. It gives error 1064 (Syntax error). What is the problem?
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
MySQL gets confused with semicolons inside the BEGIN END block, so you must use DELIMITER to temporarily force MySQL to use a different delimiter.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;
You probably need to replace:
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
with:
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
Here is you original trigger
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
Replace account. with NEW. (optional : replace " with ')
DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
NEW.deposit,NEW.accNo,
'Teller','Cash','Deposit');
END $$
DELIMITER ;
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
-- trigger body
-- this code is applied to every
-- inserted/updated/deleted row
END;
Seems like you're missing a table name after 'account'. (edit: or a database name before account, I'm not certain as to what account pertains to)