预防触发器(Prevention triggers)

2019-06-28 04:38发布

我有这样一个表:

StudentID  Student Name   Birthdate Student Birthplace Gender Height Weight 
--------- --------------- --------- ------------------ ------ ------ ------ 
83        Adam Stone      30-JUN-94 Towson, USA        M      193    88               
84        Stephanie Love  17-JUN-93 KL,Malaysia        F      176    67                 
85        Rachel Kim      17-FEB-92 Seoul, South Korea F      179    56   

我怎样写一个触发器,以防止15岁以下的学生被储存在学生的餐桌?

Answer 1:

你有出生日期。 所以,你需要确定的是,出生日期是今天之前至少十六年。 有这样做的各种不同的方式; 这里有一个使用间隔文字 。

create or replace trigger students_biur
     before insert or update on students for each row 
begin
    if (:new.student_birthdate + INTERVAL '15' YEAR ) < sysdate
    then 
         raise_application_error( -20000, 'This student is too young be registered.');     
    end if;
end; 

这也触发检查更新,以防止后续变化无效的学生。


触发器名students_biur只是一个约定我使用:带有后缀指示* B表名* *安伏I * nsert * U * PDATE每个* R *流。

RAISE_APPLICATION_ERROR是用于与消息投掷用户定义的异常的标准程序。 了解更多 。

甲骨文保留范围-20999 -20000到为用户定义的错误; 任何其它数目可以与oracle的定义的异常发生冲突。



文章来源: Prevention triggers