Prevention triggers

前端 未结 1 1499
灰色年华
灰色年华 2020-12-22 13:31

I have a table like this:

StudentID  Student Name   Birthdate Student Birthplace Gender Height Weight 
--------- --------------- --------- ------------------         


        
相关标签:
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.

    0 讨论(0)
提交回复
热议问题