Table is mutating, trigger/function may not see it (stopping an average grade from dropping below 2.5)

前端 未结 6 1028
半阙折子戏
半阙折子戏 2020-12-03 18:01

Here\'s the problem:

Create a trigger that prevents any change to the taking relation that would drop the overall average grade in any particular class below 2.5. No

6条回答
  •  眼角桃花
    2020-12-03 18:28

    I think you can fix this by rewriting this as a before trigger, rather than an after trigger. However, this might be a little complicated for inserts and deletes. The idea is:

    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;  
    

提交回复
热议问题