oracle中触发前后的区别
有人可以用一个例子解释oracle 10g中“之前”和“之后”触发器之间的区别吗?
Can somebody explain difference between "before" and "after" trigger in oracle 10g with an example ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我将通过定义触发器开始我的回答:触发器是一个存储过程,在添加、修改或删除行时运行。
触发器可以在执行操作之前或执行操作之后运行。
BEFORE
触发器通常用于在接受更改之前需要进行验证的情况。它们在对数据库进行任何更改之前运行。假设您为一家银行运行数据库。您有一个表accounts
和一个表transactions
。如果用户从他的帐户中提款,您需要确保用户的帐户中有足够的积分用于提款。BEFORE
触发器将允许执行此操作,并在accounts
中的余额不足时阻止将行插入到transactions
中。当由于更改而需要在单独的表中更新信息时,通常使用
AFTER
触发器。它们在对数据库进行更改(不一定已提交)后运行。让我们回到后面的例子。交易成功后,您可能希望更新accounts
表中的balance
。AFTER
触发器将允许您准确地做到这一点。First, I'll start my answer by defining trigger: a trigger is an stored procedure that is run when a row is added, modified or deleted.
Triggers can run BEFORE the action is taken or AFTER the action is taken.
BEFORE
triggers are usually used when validation needs to take place before accepting the change. They run before any change is made to the database. Let's say you run a database for a bank. You have a tableaccounts
and a tabletransactions
. If a user makes a withdrawal from his account, you would want to make sure that the user has enough credits in his account for his withdrawal. TheBEFORE
trigger will allow to do that and prevent the row from being inserted intransactions
if the balance inaccounts
is not enough.AFTER
triggers are usually used when information needs to be updated in a separate table due to a change. They run after changes have been made to the database (not necessarily committed). Let's go back to our back example. After a successful transaction, you would wantbalance
to be updated in theaccounts
table. AnAFTER
trigger will allow you to do exactly that.我不完全确定你想知道什么,所以我会保留这个基础知识。
触发器之前
After 触发器
I'm not completely sure what you're interested in knowing, so I'll keep this fundamental.
Before Triggers
After Triggers
当触发器操作应确定是否应允许触发语句完成时使用 BEFORE TRIGGER。通过使用 BEFORE TRIGGERS 用户可以消除触发语句的不必要处理
但是,当触发语句应在执行触发器操作之前完成时,使用 AFTER TRIGGERS。
BEFORE TRIGGER are used when the trigger action should determine whether or not the triggering statements should be allowed to complete .by using BEFORE TRIGGERS user can eliminate unnecessary processing of the triggering statement
but,AFTER TRIGGERS are used when the triggering statements should completed before executing the trigger action.