如果学生的AVG(RESULT)超过3,我想创建一个阻止删除记录的触发器
CREATE OR REPLACE TRIGGER DeleteStudentResults
BEFORE DELETE ON SESSION_RESULTS
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
WHEN ((SELECT AVG(RESULT) from session_results where STUDENT_ID = :new.STUDENT_ID)>= 3)
BEGIN
raise_application_error (-20100, 'You can not delete initial record');
END;
但甲骨文表示禁止在子句中包含SELECT语句。 如果平均值大于或等于3,我怎样才能实现阻止删除的行为?
答案 0 :(得分:2)
将其视为建议
CREATE OR REPLACE TRIGGER DeleteStudentResults
BEFORE DELETE ON SESSION_RESULTS
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
declare
avg_result number;
BEGIN
(SELECT AVG(RESULT) into avg_result from session_results where STUDENT_ID = :old.STUDENT_ID);
if(avg_result>=3) then
raise_application_error (-20100, 'You can not delete initial record');
end if;
END;
答案 1 :(得分:1)
通过删除触发器
来解决这个问题并不是一个好主意例如使用session_results
id studend_id result
1 9 1
5 9 2
7 9 5
如果删除此学生,则取决于行的处理顺序。
delete id=1 avg(1,2,5)= 2.7
delete id=5 avg(2,5)=3.5 --> raise error
但
delete id=7 avg(1,2,5)= 2.7
delete id=1 avg(1,2)=1.5
delete id=5 avg(2)=2
尝试通过物化视图解决此问题,有关详细信息,请参阅http://www.oracle.com/technetwork/testcontent/o58asktom-101055.html