1.建立学生信息表
-- 触发器
-- 建立学生信息表
create table s1(id int unsigned auto_increment,name varchar(30),score tinyint unsigned,dept varchar(50),primary key(id)
);
2.建立学生补考信息表
-- 建立学生补考信息表
create table s2 like s1;
3.建立触发器(一共建立了三个触发器)
-- 建立触发器
-- 插入触发器
delimiter //
create trigger t1 after insert on s1 for each row
beginif NEW.score<60 theninsert into s2 values(new.id,new.name,new.score,new.dept);end if;
end//
delimiter ;
-- 删除触发器
delimiter //
create trigger t2 after delete on s1 for each row
begindelete from s2 where old.id = id;
end//
delimiter ;
-- 更新触发器
delimiter //
create trigger t3 after update on s1 for each row
beginif new.score < 60 thenreplace into s2 value(new.id,new.name,new.score,new.dept);elsedelete from s2 where id = new.id;end if;
end//
delimiter ;
4.插入数据
-- 插入数据
insert into s1 value(null,'李四',60,'计算机科学');
insert into s1 value(null,'赵六',30,'会计');
insert into s1 value(null,'李四玉',70,'计算机科学');
insert into s1 value(null,'周五',80,'英语');
insert into s1 value(null,'赵强',10,'计算机科学');
insert into s1 value(null,'王七',98,'英语');
5.查询结果
select * from s2;
select * from s1;
查询s1表
查询s2表