Anuja Khemka
Anuja Khemka

Reputation: 265

Create Trigger to update another table in change of value

I have two tables TABLE_A and TABLE_B. I have a value in TABLE_A called ID. The value is updated every 30 min by a cron job. I need to create a trigger that TABLE_B stores the old and new value of ID only when IT IS CHANGED. The cron job updates the TABLE_A regardless and cannot be changed.

Example:

If the value of ID changes from 18 to 19, then it should add to TABLE_B. When it changes from 18 to 18, it should not.

I am not sure if i need to use a trigger + procedure or can only a trigger do?

Upvotes: 0

Views: 2711

Answers (1)

Justin Cave
Justin Cave

Reputation: 231661

You can code your trigger to look at the old and new values

CREATE OR REPLACE TRIGGER trigger_name
  AFTER UPDATE ON table_a
  FOR EACH ROW
BEGIN
  IF( :new.id != :old.id )
  THEN
    <<something changed>>
  END IF;
END;

If id can be NULL, you'll need a slightly more complicated condition.

Upvotes: 1

Related Questions