触发器可以在增删改之前或之后触发,我们这节课讲触发器在'增'之前或之后触发
根据需求,定义触发器。要求通过触发器记录tb_user表的数据变更日志,将变更日志插入到新建的日志表use_logs中,包含增、删、改
xxxxxxxxxx
create table user_logs(
id int(11) not null auto_increment,
operation varchar(20) not null comment '操作类型, insert/update/delete',
operate_time datetime not null comment '操作时间',
operate_id int(11) not null comment '操作的ID',
operate_params varchar(500) comment '操作参数',
primary key(`id`)
)engine=innodb default charset=utf8;
插入数据触发器
create trigger tb_user_insert_trigger
after insert on tb_user for each row
#我们对tb_user表进行更新数据的时候就会触发
begin
insert into user_logs(id,operation,operate_time,operate_id,operate_params) values
(null,'insert',now(),new.id,concat('插入的数据内容为:id= ',new.id,',name= ',new.name,',phone=',new.phone,',email=',new.email,',profession=',new.profession));
#new.id新插入数据的id,以此类推
#now()是时间函数,显示当前时间
end;