oracle - 用于测试班级学生人数的 Oracle-Trigger
问题描述
我正在尝试创建一个限制组中学生人数的前插入触发器,我的意思是,如果组中的学生人数大于 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 的新手,不知道如何使用它。
非常感谢您提前!!
解决方案
您需要使用 aStatement Trigger
来检查 class_id 的任何组是否违反规则,而不是 aRow Level Trigger
为了不发生变异触发错误:
CREATE OR REPLACE TRIGGER groups_capacity
AFTER INSERT ON s183410_group
DECLARE
counter INT;
BEGIN
SELECT MAX(COUNT(*)) INTO counter FROM s183410_group GROUP BY class_id;
IF counter > 5 THEN
raise_application_error(-20002,
'This class is full.
There cannot be more than 5 students in the same group.');
END IF;
END;
推荐阅读
- php - 显示错误未更新数据库中的值
- python - Matplotlib Xticks 不习惯字符串值
- r - 在 r 中使用 ggplot2 的两个因素的可信区间
- java - Java:Threadpoolexecutor - 使用作业列表提交作业?
- laravel - 带有 Socket/Redis 的 Laravel - 私有通道路由不起作用
- android - 多布局基础语言环境 - Android
- javascript - 无法将获取的数据推送到全局数组中的函数
- slack-api - 当机器人在松弛的交互式消息中替换消息时如何禁用自动滚动?
- knockout.js - 有没有办法使用 KnockoutJS 将 XML 文件转换为 CSV?
- asp.net - 无法在 C# windows 窗体中创建新的子窗体