-1

I have a trigger on a table that's fired when the table gets updated.

Problem is, I need that trigger to fire only when a certain column is updated. Right now, the trigger runs when any column is updated (I learned the hard way).

How can I get the trigger to run only when a particular column is updated. For example:

col1 | col2
-----------

The trigger should run only if col1 is updated and not if any other column is updated. How can I do this?

Norman
  • 6,159
  • 23
  • 88
  • 141

1 Answers1

3

MySQL does not support firing a trigger on a specific column change. But you can check if a specific column changed in your trigger like this

delimiter $$
create trigger col1_trigger before insert on your_table
for each row begin 
  if NEW.col1 <> OLD.col1
  then 
    ...
  end if;
end 
$$
juergen d
  • 201,996
  • 37
  • 293
  • 362