Prevention triggers

陌路散爱 提交于 2019-11-29 18:21:37

You have a Date of Birth. So you need to determine that the DoB is at least sixteen years before today. There are various different ways of doing this; here's one using an interval literal.

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; 

This trigger also checks for updates, to prevent subsequent changes invalidating an student.


The trigger name students_biur is just a convention I use: the table name with a suffix indicating *B*efore *I*nsert *U*pdate for each *R*ow.

RAISE_APPLICATION_ERROR is a standard procedure for throwing user-defined exceptions with a message. Find out more.

Oracle reserves the range -20999 to -20000 for user-defined errors; any other number may clash with a oracle-defined exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!