oracle中触发前后的区别

发布于 2024-09-17 05:46:49 字数 48 浏览 4 评论 0原文

有人可以用一个例子解释oracle 10g中“之前”和“之后”触发器之间的区别吗?

Can somebody explain difference between "before" and "after" trigger in oracle 10g with an example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神也荒唐 2024-09-24 05:46:49

首先,我将通过定义触发器开始我的回答:触发器是一个存储过程,在添加、修改或删除行时运行。

触发器可以在执行操作之前或执行操作之后运行。

BEFORE 触发器通常用于在接受更改之前需要进行验证的情况。它们在对数据库进行任何更改之前运行。假设您为一家银行运行数据库。您有一个表accounts 和一个表transactions。如果用户从他的帐户中提款,您需要确保用户的帐户中有足够的积分用于提款。 BEFORE 触发器将允许执行此操作,并在 accounts 中的余额不足时阻止将行插入到 transactions 中。

当由于更改而需要在单独的表中更新信息时,通常使用 AFTER 触发器。它们在对数据库进行更改(不一定已提交)后运行。让我们回到后面的例子。交易成功后,您可能希望更新 accounts 表中的 balanceAFTER 触发器将允许您准确地做到这一点。

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 table accounts and a table transactions. 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. The BEFORE trigger will allow to do that and prevent the row from being inserted in transactions if the balance in accounts 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 want balance to be updated in the accounts table. An AFTER trigger will allow you to do exactly that.

无边思念无边月 2024-09-24 05:46:49

我不完全确定你想知道什么,所以我会保留这个基础知识。

触发器之前

  • 根据名称,这些触发器在表中创建行之前被触发。随后,由于该行尚未创建,您可以完全访问 :new.table_element 字段。如果尝试插入/更新不需要的/格式错误的数据,这可以实现数据清理和一致性。这只是一个基本示例,但是当您需要访问“:new”数据时,您需要使用 before 触发器。

After 触发器

  • 由于一旦创建了行,后触发器就会触发,因此当您希望因行而发生逻辑时,通常会使用这些触发器。例如,如果您有一个地址表并且用户更新了他/她的地址,那么您可能希望在创建时更新外部参照表中的地址引用 ID(如果您碰巧也保留了所有旧地址)。此外,与 before 触发器不同,您无权修改任何列值,因为该行已存在于表中。

I'm not completely sure what you're interested in knowing, so I'll keep this fundamental.

Before Triggers

  • As per the name, these triggers are fired prior to creating the row in the table. Subsequently, since the row has not yet been created you have full access to the :new.table_element field. This allows for data cleansing and uniformity if unwanted/malformed data is attempting to be inserted/updated. This is just a basic example, but you need to utilize the before trigger any time you may require access to the ":new" data.

After Triggers

  • Since the after trigger fires once the row has already been created, these triggers are typically utilized when you want logic to occur due to the row. For example, if you have an address table and a user updates his/her address, then you may want to update the address reference ids in an xref table upon creation (if you happen to be retaining all old addresses as well). Also, unlike the before trigger, you do not have access to modify any of the column values since the row already exists in the table.
乖乖兔^ω^ 2024-09-24 05:46:49

当触发器操作应确定是否应允许触发语句完成时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文