database-note-7

mac2024-04-20  38

Triggers

触发器,处理数据约束条件。

条件

某个操作的执行前或者执行后进行更新。

定义

插入后触发 12345678create trigger time_slot_check1 after insert on `section`referencing new row as nrowfor each rowwhen (nrow.time_slot_id not in ( select time_slot_id time_slot)) # 条件begin # 动作 rollbackend

新行代表刚插入数据库的元组。

删除后触发 12345678create trigger timeslot_check2 after delete on timeslot referencing old row as orow for each row when (orow.time_slot_id not in (select time_slot_id from time_slot)) and orow.time_slot_id in (select time_slot_id from section)beginrollbackend

类似RESTRCUCT级联限制。

更新前出发 1234567create trigger setnull_trigger before update of takesreferencing new row as nrowfor each row when (nrow='')begin set nrow.grade=null;end

对于此sql,可以在表takes之后使用on lt;field name>来指定在哪个字段进行更新时进行触发。

Recursion SQL

高级聚合函数

Ranking

1select ID, rank() over (order by GPA desc) as s_rank from student_grades;

可用count()模拟实现。

最新回复(0)