时间:2021-07-01 10:21:17 帮助过:12人阅读
触发器类型 | NEW和OLD的使用 |
insert型触发器 | NEW表示将要或者已经新增的数据 |
update型触发器 | OLD表示修改之前的数据,NEW表示将要或已经修改后的数据 |
delete型触发器 | OLD表示将要或者已经删除的数据 |
-- 建表sql create table emp_logs( id int(11) not null auto_increment, opertation varchar(20) not null comment‘操作类型:inset/update/delete‘ , opertate_time datetime not null comment‘操作时间‘ , opertate_id int(11) not null comment‘操作表的ID‘ , opertate_params varchar(500) comment‘操作参数‘ , primary key(id) )engine=innodb default charset=utf8;
2. insert触发器
drop trigger if exists emp_insert_trigger; delimiter $ create trigger emp_insert_trigger after insert on emp for each row begin insert into emp_logs(id,opertation,opertate_time,opertate_id,opertate_params) values(null,‘insert‘,now(),new.id,concat(‘插入后(id:‘,new.id,‘,name:‘,new.name,‘,age:‘,new.age,‘,salary:‘,new.salary,‘)‘)); end$ delimiter ; insert into emp(id,name,age,salary) values(null,‘光明左使‘,30,3500); select * from emp_logs;
3. update触发器
drop trigger if exists emp_update_trigger; delimiter $ create trigger emp_update_trigger after update on emp for each row begin insert into emp_logs(id,opertation,opertate_time,opertate_id,opertate_params) values(null,‘update‘,now(),new.id,concat(‘修改前(id:‘,old.id,‘,name:‘,old.name,‘,age:‘,old.age,‘,salary:‘,old.salary,‘)‘, ‘ 修改后(id:‘,new.id,‘,name:‘,new.name,‘,age:‘,new.age,‘,salary:‘,new.salary,‘)‘)); end$ delimiter ; update emp set age=39 where id=3; select * from emp_logs;
4. delete触发器
drop trigger if exists emp_delete_trigger; delimiter $ create trigger emp_delete_trigger after delete on emp for each row begin insert into emp_logs(id,opertation,opertate_time,opertate_id,opertate_params) values(null,‘delete‘,now(),old.id,concat(‘删除前(id:‘,old.id,‘,name:‘,old.name,‘,age:‘,old.age,‘,salary:‘,old.salary,‘)‘)); end$ delimiter ; delete from emp where id=4; select * from emp_logs;
5. 查看触发器
show triggers
6. 删除触发器
drop trigger test.emp_delete_trigger
持续更新!!!
【mysql】四、mysql的学习---触发器
标签:使用 校验 primary varchar rgb HERE ODB into arch