سؤال

I have two tables:

table A
 id | level_ID | col_m | col_n

table B
 id | prev_ID | cur_ID

table A is manipulated from the application layer where new inserts are done and updates as well. On the other hand table B has values inserted after an update only on level_ID column of table A via a trigger:

DELIMITER |
DROP TRIGGER IF EXISTS trigger_happy|
CREATE TRIGGER trigger_happy AFTER UPDATE ON table A
FOR EACH ROW
  BEGIN
    IF level_ID!=NEW.level_ID THEN
    INSERT INTO table B (id, cur_ID, prev_ID)
    VALUES (OLD.id, NEW.level_ID, OLD.level_ID)
  END IF;
END;
DELIMITER ;

The problem is that I intend to have the trigger only fire if there is a change in level_ID column. An update on other columns col_m and col_n should not fire the trigger.

Help me with my trigger statement, because it does not work as it is.

EDIT

Updates the columns are done at different times within the application logic. I need trigger to be fired ONLY when an update on level_ID is done.

هل كانت مفيدة؟

المحلول 2

The solution I have found to work for me is as below:

DELIMITER |
DROP TRIGGER IF EXISTS trigger_happy|
CREATE TRIGGER trigger_happy AFTER UPDATE ON table A
FOR EACH ROW
  BEGIN
    IF NEW.level_ID!=OLD.level_ID THEN /* edit conditional statement */
    INSERT INTO table B (id, cur_ID, prev_ID)
    VALUES (OLD.id, NEW.level_ID, OLD.level_ID)
  END IF;
END;
| /* add delimiter */
DELIMITER ;

This trigger will only compare changes on the column level_ID and fire after an update. As I am using phpmyadmin, I had to specify the delimiter to |. I was leaving it at the default ;

I hope this helps someone.

نصائح أخرى

Very simply, you will need to run two updates.

One update will need the trigger on the level_ID column. The second update will update col_m and col_n columns without an associated trigger.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top