1

I need some help identifying what I did wrong.

I created two tables to track history from my main table. When I update a state from 0 to 1, the first trigger copies the data into history table 1.

But when I change the state from 1 to 2, the second trigger is not triggering, thus not copying the data into history table 2.

CREATE TABLE stateOneHistoryTable (
  historyDate DATETIME DEFAULT NULL,
  eventID int(11) NOT NULL,
  eventTIME decimal(15,0) NOT NULL,
  newestEventTime decimal(20,0) DEFAULT NULL,
  deviceIP varchar(20) NOT NULL,
  syslogMessage varchar(1000) NOT NULL,
  syslogState decimal(2,0) NOT NULL,
  eventCount decimal(5,0) NOT NULL
)

CREATE TABLE stateTwoHistoryTable (
  historyDate DATETIME DEFAULT NULL,
  eventID int(11) NOT NULL,
  eventTIME decimal(15,0) NOT NULL,
  newestEventTime decimal(20,0) DEFAULT NULL,
  deviceIP varchar(20) NOT NULL,
  syslogMessage varchar(1000) NOT NULL,
  syslogState decimal(2,0) NOT NULL,
  eventCount decimal(5,0) NOT NULL
)

Below are the triggers I created.

DELIMITER //

CREATE TRIGGER stateOneHistoryTableTrigger
AFTER UPDATE
ON syslog 
FOR EACH ROW

BEGIN
    IF (old.syslogState=0 & new.syslogState=1)
        THEN
            INSERT INTO stateOneHistoryTable (historyDate, eventID, eventTIME, newestEventTime, deviceIP, syslogMessage, syslogState, eventCount)
            VALUES (utc_timestamp(), new.eventID, new.eventTIME, new.newestEventTime, new.deviceIP, new.syslogMessage, new.syslogState, new.eventCount);
    END IF;
END; //

DELIMITER ; 




DELIMITER //

CREATE TRIGGER stateTwoHistoryTableTrigger
AFTER UPDATE
ON syslog 
FOR EACH ROW

BEGIN
    IF (old.syslogState=1 & new.syslogState=2)
        THEN
            INSERT INTO stateTwoHistoryTable (historyDate, eventID, eventTIME, newestEventTime, deviceIP, syslogMessage, syslogState, eventCount)
            VALUES (utc_timestamp(), new.eventID, new.eventTIME, new.newestEventTime, new.deviceIP, new.syslogMessage, new.syslogState, new.eventCount);
    END IF;
END; //

DELIMITER ; 

2 Answers 2

1

Are you sure stateTwoHistoryTableTrigger exists? I'm asking since at least some versions of MySQL don't support multiple triggers with the same action time and event for the same table. You can check if the trigger exists like:

select trigger_name 
from information_schema.triggers 
where EVENT_OBJECT_TABLE = 'syslog';

If that is the problem you can add both actions in one trigger like:

DELIMITER //

CREATE TRIGGER stateOneHistoryTableTrigger
AFTER UPDATE ON syslog
FOR EACH ROW
BEGIN
    IF (old.syslogState=0 & new.syslogState=1)
    THEN
        INSERT INTO stateOneHistoryTable (historyDate, eventID, eventTIME, newestEventTime, deviceIP, syslogMessage, syslogState, eventCount)
        VALUES (utc_timestamp(), new.eventID, new.eventTIME, new.newestEventTime, new.deviceIP, new.syslogMessage, new.syslogState, new.eventCount);

ELSEIF (old.syslogState=1 & new.syslogState=2)
    THEN
        INSERT INTO stateTwoHistoryTable (historyDate, eventID, eventTIME, newestEventTime, deviceIP, syslogMessage, syslogState, eventCount)
        VALUES (utc_timestamp(), new.eventID, new.eventTIME, new.newestEventTime, new.deviceIP, new.syslogMessage, new.syslogState, new.eventCount);

    END IF;
END; //

DELIMITER ;

May I ask why you want two history tables? If you keep all history in one table you can derive the two history tables from there:

CREATE TABLE stateHistoryTable (
    historyDate DATETIME DEFAULT NULL,
    eventID int(11) NOT NULL,
    eventTIME decimal(15,0) NOT NULL,
    newestEventTime decimal(20,0) DEFAULT NULL,
    deviceIP varchar(20) NOT NULL,
    syslogMessage varchar(1000) NOT NULL,
    syslogState decimal(2,0) NOT NULL,
    eventCount decimal(5,0) NOT NULL
) engine=innodb;

CREATE TRIGGER stateHistoryTableTrigger
AFTER UPDATE ON syslog
FOR EACH ROW
BEGIN
    IF (old.syslogState + 1 = new.syslogState)
    THEN INSERT ...
END IF; 

CREATE VIEW stateOneHistoryTableTrigger AS
    SELECT ...
    FROM stateHistoryTable
    WHERE syslogState = 1;

CREATE VIEW stateTwoHistoryTableTrigger AS
    SELECT ...
    FROM stateHistoryTable
    WHERE syslogState = 2;
1
  • Thank you for your help creating the nested IF statement helped resolve my issue.
    – Fallin85
    Commented Jun 27, 2017 at 21:14
0

Your trigger definition is wrong.

It should be

      DELIMITER //

       CREATE TRIGGER stateTwoHistoryTableTrigger
       AFTER UPDATE ON table_name
        FOR EACH ROW

      BEGIN
IF (old.syslogState=1 & new.syslogState=2)
    THEN
        INSERT INTO stateTwoHistoryTable (historyDate, eventID, eventTIME, newestEventTime, deviceIP, syslogMessage, syslogState, eventCount)
        VALUES (utc_timestamp(), new.eventID, new.eventTIME, new.newestEventTime, new.deviceIP, new.syslogMessage, new.syslogState, new.eventCount);
END IF;

END; //

DELIMITER ;

4
  • can you explain what is wrong with the original trigger? Commented Jun 27, 2017 at 19:32
  • You are creating trigger table on syslog table...as far as i understand from your question, Table which you have posted in create statement should come. Commented Jun 27, 2017 at 19:36
  • I'm not the OP, but what you are saying is that you created stateTwoHistoryTableTrigger on table table_name instead of syslog, correct? Commented Jun 27, 2017 at 20:39
  • I believe that table which is responsible for causing second trigger to run is statetwohistorytable instead of syslog . Correct me if m wrong Commented Jun 28, 2017 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.