【发布时间】:2020-09-18 19:47:10
【问题描述】:
我正在尝试创建一个限制组中学生人数的前插入触发器,我的意思是,如果组中的学生人数大于 5,则用户不能在同一组中引入任何其他记录小组,但他可以在其他学生少于 5 人的地方。
在表格中我有以下信息:
所以触发器应该允许在除第二行之外的所有行中添加记录,因为那里已经有 5 个学生。
我尝试了以下代码:
create or replace trigger groups_capacity
before insert on s183410_group
for each row
declare
counter INTEGER;
BEGIN
select count(*)into counter from s183410_group group by class_id;
if counter>5 and :old.class_id=:new.class_id then
raise_application_error(-20002,'This class is full.There cannot be more than 5 students in the same group.');
end if;
END;
我认为计数器不会改变每一行的值。我是 oracle 新手,不知道如何使用它。
非常感谢您!
【问题讨论】:
标签: oracle plsql count database-trigger