Question

I have a hypothetical table, fruit. It looks like this:

Product | Value

Apple     1
Orange    2

I am trying to create a trigger that will execute before update that will add 2 to the value only if the new value was for the apple. What would this trigger look like? Example someone executed

UPDATE fruit SET value=2 WHERE product='apple';

The new value for apple would be 4.

I tried the trigger below but of course that updates any value with 2

BEGIN 
  SET value = new.value+2
END
Was it helpful?

Solution

Use code something like this in your trigger:

if new.product = 'Apple' then
    set new.value = new.value + 2;
end if;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top