在sct数据库中创建操作信息表Operate,表信息如下:
create table operate
(
op_id int(10) not null unique auto_increment,
op_type varchar(20) not null,
op_time time not null,
primary key(op_id)
)engine = Innodb;
在student表上创建触发器,分别为BeforeInsert、Afterupdate、Afterdelete等3个触发器。
- BeforeInsert,触发器名称为stu_bf_in,执行部分为向Operate表中插入操作方式和操作时间;
Create trigger stu_bf_in
Before insert on student for each row
Insert into operate values(null, ‘insert into student’, now());
- Afterupdate,触发器名称为stu_af_update,执行部分为向Operate表中插入操作方式和操作时间;
Create trigger stu_af_update
After update on student for each row
Insert into operate values(null, ‘update student’, now());
- Afterdelete,触发器名称为stu_af_del,执行部分为向Operate表中插入操作方式和操作时间;
Create trigger stu_af_del
After delete on student for each row
Insert into operate values(null, ‘delete from student’, now());
(4)对student表分别执行插入、更新、删除,分别查看Operate表中数据变化。
(5)删除BeforeInsert触发器。
Drop trigger stu_bf_in;
- 在表sc中创建一个触发器sc_update_trigger,用于每次更新表sc中的成绩时,将该表中score列的值在原值的基础上加1。
Create trigger sc_update_trigger
before update on sc
For each row
set new.score = old.score + 1;