-1

我有一张这样的桌子:

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 岁以下的任何学生被存储在学生表中?

4

1 回答 1

4

你有一个出生日期。因此,您需要确定 DoB 至少在今天之前 16 年。有多种不同的方法可以做到这一点;这是一个使用区间文字的。

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*efore *I*nsert *U*pdate 用于每个 *R*ow。

RAISE_APPLICATION_ERROR 是一个标准过程,用于通过消息引发用户定义的异常。 了解更多

Oracle 为用户定义的错误保留 -20999 到 -20000 的范围;任何其他数字都可能与 oracle 定义的异常发生冲突。

于 2012-09-11T07:21:46.700 回答