SQL - 触发检查每个学生的日期插入

SQL - Trigger to check insert of date on each student

我正在尝试创建一个触发器来检查我插入的日期是否大于 table 中每个学生的日期。控件只需要在 ID 和 DATE 上。

STUDENT_EXAMS

id_student subject    mark        date_exam
1          Chemistry  6          'May-05-2020'
2          Maths      7          'May-01-2020'

合法插入

insert into STUDENT_EXAMS (id_student, subject, mark, date_exam)
values (1, 'History', 8, 'May-06-2020');

insert into STUDENT_EXAMS (id_student, subject, mark, date_exam)
values (2, 'Biology', 8, 'May-05-2020');

非法插入

insert into STUDENT_EXAMS (id_student, subject, mark, date_exam)
values (1, 'History', 8, 'May-04-2020');

insert into STUDENT_EXAMS (id_student, subject, mark, date_exam)
values (2, 'Biology', 10, 'Apr-30-2020');

这是我尝试创建的触发器,但它不起作用,而且我不知道如何在每个 ID_STUDENT.

上插入控件
create or replace trigger check_date
before insert on STUDENT_EXAM
for each row
begin
    if (:new.date_exam > :old.date_exam) then
        insert into STUDENT_EXAM (id_student, subject, mark, date_exam)
        values (:new.id_student, :new.subject, :new.mark, :new.date_exam); 
end if;
end;

也许这是您感兴趣的 in.It 阻止插入到 STUDENT_EXAM table 当 date_Exam 被插入的行小于最大值 date_Exam 为 id.

  create or replace trigger check_date
    before insert on STUDENT_EXAM
    for each row
    DECLARE 

    lv_date_exam DATE;
    begin

         select max(date_exam) into lv_date_exam 
         from student_exam where id = :new.id;

        if (:new.date_exam < lv_date_exam) then
           raise_application_error(-20000
            , 'Cannot insert record as date_exam '||:new.date_exam||' is less than max date_exam '||lv_date_exam);
    end if;
    end;

[由 Littlefoot 编辑,以显示 为什么 它会因 变异 table 错误而失败]

Table & 触发你建议:

SQL> create table student_exam (id number, date_exam date);

Table created.

SQL> create or replace trigger check_date
  2      before insert on STUDENT_EXAM
  3      for each row
  4      DECLARE
  5
  6      lv_date_exam DATE;
  7      begin
  8
  9           select max(date_exam) into lv_date_exam
 10           from student_exam where id = :new.id;
 11
 12          if (:new.date_exam < lv_date_exam) then
 13             raise_application_error(-20000
 14              , 'Cannot insert record as date_exam '||:new.date_exam||' is less than max date_exam '||lv_date_exam);
 15      end if;
 16      end;
 17  /

Trigger created.

测试:

这个有效:

SQL> insert into student_exam (id, date_exam) values (1, sysdate);

1 row created.

但这不是:

SQL> insert into student_exam (id, date_exam)
  2    select 1, sysdate - 10 from dual union all
  3    select 1, sysdate + 20 from dual;
insert into student_exam (id, date_exam)
            *
ERROR at line 1:
ORA-04091: table SCOTT.STUDENT_EXAM is mutating, trigger/function may not see
it
ORA-06512: at "SCOTT.CHECK_DATE", line 6
ORA-04088: error during execution of trigger 'SCOTT.CHECK_DATE'


SQL>