mysql 增删改触发器的创建
insert 触发器
CREATE DEFINER=`root`@`localhost` TRIGGER `chilrd`.`insert` BEFORE INSERT ON `chilrd`.`dd` FOR EACH ROW BEGIN #做你想做的事情 insert into table(name,code) values(new.username,new.id); END;
update触发器 需求是在更新指定字段(sice_code)的时候去更改状(wa_status)态字段
CREATE DEFINER=`root`@`localhost` TRIGGER `chilrd`.`dd` BEFORE UPDATE ON `chilrd`.`dd` FOR EACH ROW BEGIN #就是site_code 有更新 并且wa_status 是默认的0 的时候才去更新 if new.site_code != '' and old.wa_status= 0 then set new.wa_status= 1; end if; END;
delete 触发器
CREATE DEFINER=`root`@`localhost` TRIGGER `chilrd`.`delete` BEFORE DELETE ON `chilrd`.`dd` FOR EACH ROW BEGIN delete from dev where id = old.id; END;
new 只出现在insert和update时;
old 只出现在update和delete时
new是新插入的和刚刚更新的;old是更改之前的和删除之后的