Can I have a trigger to check any insert or update on whole database?
I tried this but it didn't work:
create trigger updateHistory after insert on database
Yaou need to elaborate the database into separate tables. And give the action. Eg:
CREATE TRIGGER updatehistory
BEFORE UPDATE ON `table,table2`
FOR EACH ROW
BEGIN
INSERT INTO
SET action = 'insert',
record = OLD.record,
changedat = NOW();
END
And as answered here, you cannot create both insert and update at the same trigger, but you can move the common code into a procedure and have them both call the procedure.
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 ;
I had run two threads, one for insert another one for update in MySQL. The two operations are performed concurrently, my problem was the insert operation will performed first (before insert the update operation will be restricted) before update operation.
create a new trigger, you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:
CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
...
END;
Create a trigger after insert
CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom
FOR EACH ROW
BEGIN
//update query
UPDATE BookingRequest
SET status = 1
WHERE idRequest = NEW.idRequest;
END
learn more about triggers at techonthenet.com
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.
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)