预防触发 [英] Prevention triggers

查看:36
本文介绍了预防触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表:

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

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

推荐答案

您有出生日期.因此,您需要确定DoB至少在今天之前十六年.有多种不同的方法可以做到这一点.这是使用间隔文字的示例.. >

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.

触发器名称students_biur只是我使用的一个约定:带后缀的表名,对于每个* R * ow,* B *之前* I * nsert * U * pdate.

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是用于在消息中引发用户定义的异常的标准过程. 了解更多.

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

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

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

这篇关于预防触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆