Mysql
 sql >> Database >  >> RDS >> Mysql

INSERT e UPDATE in una query MYSQL

Se utilizzerai un approccio trigger, avrai bisogno di due trigger (per inserire ed eliminare)

CREATE TRIGGER tg_ai_orders
AFTER INSERT ON orders
FOR EACH ROW
  UPDATE bands 
     SET stock = stock - 1
   WHERE band_id = NEW.band_id;

CREATE TRIGGER tg_ad_orders
AFTER DELETE ON orders
FOR EACH ROW
  UPDATE bands 
     SET stock = stock + 1
   WHERE band_id = OLD.band_id;

Ecco SQLFiddle demo