1、创建触发器。
//创建一个触发器在给section关系插入后触发
create trigger timeslot_check1 after insert on section
referencing new row as nrow
//对每个插入的行都执行
for each row
//when指定一个条件,仅对于满足条件的元组才会执行触发器剩余的部分
when (nrow.time_slot_id not in (
select time_slot_id
from time_slot))
begin
rollback
end;
referencing new row as创建一个变量称为过渡变量用于存储插入、更新后的新元组,相应的,referencing old row as创建一个过渡变量用于存储已经更新或删除的行的旧值。
触发器也可以在事件之前激发,只要把after改为before。触发器只能相应insert, delete和update三种语句。
单个执行语句触发器
create table people_logs(
id int,
time datetime,
log_text text
)
create trigger trigger_insert after insert on people for each row
insert into people_logs values(null,NOW(),concat(new.user_id));
insert into people(user_id,visit_time) values
(1007,'2023-10-01 08:01:23');
多个执行语句触发器
delimiter //
#多个执行语句触发器
create trigger trigger_update after update
on people for each row
begin
insert into people_logs values(null,NOW(),concat(old.visit_time));
insert into people_logs values(null,NOW(),concat(new.visit_time));
end;//
2.查看触发器;
show triggers; show triggers/G;
select * from information_schema.triggers\G;
select * from information_schema.triggers where=triggers_name="";
3.触发器删除;
drop trigger " ";
4.无效触发器
alter trigger “” disable;