Prevention triggers

不羁的心 提交于 2019-11-28 14:00:24

问题


I have a table like this:

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   

How do i write a trigger to prevent any student under the age of 15 from being stored in the student's table?


回答1:


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.



来源:https://stackoverflow.com/questions/12364269/prevention-triggers

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