gpt4 book ai didi

sql - 表正在发生变化,触发器/函数可能看不到它(阻止平均成绩低于 2.5)

转载 作者:行者123 更新时间:2023-12-03 21:53:39 25 4
gpt4 key购买 nike

这是问题所在:

创建一个触发器,以防止任何会导致任何特定类(class)的总体平均成绩低于 2.5 的录取关系发生变化。注意:此触发器并非旨在解决任何给定学生的平均 GPA,而是应解决特定类(class)中分配的所有成绩的平均成绩。

这是架构:

Student-schema =(studentnum, name, standing, gpa, major)
Class-schema = (schedulenum, semester, department, classnum, days, time, place, enrollment)
Instructor-schema = (name, department, office)
Teaches-schema = (name, schedulenum, semester)
Taking-schema = (studentnum, schedulenum, semester, grade)

这些触发器让我度过了一段糟糕的时光,但这是我尝试进行的工作:
CREATE OR REPLACE TRIGGER stopChange
AFTER UPDATE OR INSERT OR DELETE ON taking
REFERENCING OLD AS old
NEW AS new
FOR EACH ROW
DECLARE

grd_avg taking.grade%TYPE;

BEGIN
SELECT AVG(grade)
INTO grd_avg
FROM taking
WHERE studentnum = :new.studentnum
AND schedulenum = :new.schedulenum
AND semester = :new.semester;

IF grd_avg < 2.5 THEN
UPDATE taking
SET grade = :old.grade
WHERE studentnum = :old.studentnum
AND schedulenum = :old.schedulenum
AND semester = :old.semester;
END IF;

END;
/

我显然做错了什么,因为当我去更新或删除一个元组时,我得到了错误:
ERROR at line 1:
ORA-04091: table TAKING is mutating, trigger/function may not see it
ORA-06512: at "STOPCHANGE", line 6
ORA-04088: error during execution of trigger 'STOPCHANGE'

有什么建议吗?我正在使用甲骨文。

最佳答案

我认为您可以通过将其重写为前触发器而不是后触发器来解决此问题。但是,这对于插入和删除可能有点复杂。这个想法是:

CREATE OR REPLACE TRIGGER stopChange
BEFORE UPDATE OR INSERT OR DELETE ON taking
REFERENCING OLD AS old
NEW AS new
FOR EACH ROW
DECLARE

grd_avg taking.grade%TYPE;

BEGIN
SELECT (SUM(grade) - oldgrade + new.grade) / count(*)
INTO grd_avg
FROM taking
WHERE studentnum = :new.studentnum
AND schedulenum = :new.schedulenum
AND semester = :new.semester;

IF grd_avg < 2.5 THEN
new.grade = old.grade
END IF;
END;

关于sql - 表正在发生变化,触发器/函数可能看不到它(阻止平均成绩低于 2.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com