关于触发顺序

发布于 2024-12-18 11:12:04 字数 129 浏览 0 评论 0原文

桌子上有两个触发器。当表中的每一行有插入或更新时,就会执行一个触发器。当表中的每一行有更新时,将执行第二个触发器。在ORACLE 10G中,当表中的一行有更新语句时,哪个触发器首先执行。 oracle中触发器有执行顺序吗?如果可以我该如何设置?

Have two triggers on a table. One trigger is executed when there is a insert or update for each row in the table. Second trigger is executed when there is a update for each row in the table. Which trigger gets executed first in ORACLE 10G when there is a update statement on a row in the table. Is there any order of execution for triggers in oracle? If so how can i set it?

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

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

发布评论

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

评论(4

独夜无伴 2024-12-25 11:12:04

触发器触发的顺序是任意的,您无法在 10g 中控制。我相信,从技术上讲,它是按照触发器创建的顺序进行的,但这肯定不是您想要指望的。

在11g中,您可以控制触发器的触发顺序。然而,用一个调用两个存储过程的触发器替换这两个触发器几乎总是更好。所以,比起

CREATE TRIGGER trg_1
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  <<do thing 1>>
END;


CREATE TRIGGER trg_2
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  <<do thing 2>>
END;

你会得到更好的服务,比如

CREATE PROCEDURE p1( <<arguments>> )
AS
BEGIN
  <<do thing 1>>
END;

CREATE PROCEDURE p2( <<arguments>> )
AS
BEGIN
  <<do thing 2>>
END;

CREATE TRIGGER trg
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  p1( <<list of arguments>> );
  p2( <<list of arguments>> );
END;

The order in which the triggers will fire is arbitrary and not something that you can control in 10g. I believe, technically, it goes in the order that the triggers happened to be created but that's certainly not something that you'd want to count on.

In 11g, you can control the firing order of triggers. However you are almost always better off replacing the two triggers with one trigger that calls two stored procedures. So rather than

CREATE TRIGGER trg_1
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  <<do thing 1>>
END;


CREATE TRIGGER trg_2
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  <<do thing 2>>
END;

you would be much better served with something like

CREATE PROCEDURE p1( <<arguments>> )
AS
BEGIN
  <<do thing 1>>
END;

CREATE PROCEDURE p2( <<arguments>> )
AS
BEGIN
  <<do thing 2>>
END;

CREATE TRIGGER trg
  BEFORE UPDATE ON t
  FOR EACH ROW
BEGIN
  p1( <<list of arguments>> );
  p2( <<list of arguments>> );
END;
好久不见√ 2024-12-25 11:12:04

对于 11g 之前的版本,否,顺序未指定。来自 10g 第 2 版文档

对于启用的触发器,Oracle 自动执行以下操作:

  • 当单个 SQL 语句触发多个触发器时,Oracle 会按照计划的触发序列运行每种类型的触发器。首先,触发语句级触发器,然后触发行级触发器。

  • Oracle 在设定的时间点对不同类型的触发器执行完整性约束检查,并保证触发器不会损害完整性约束。

  • Oracle 为查询和约束提供读取一致的视图。

  • Oracle 管理触发器操作代码中引用的触发器和架构对象之间的依赖关系

  • 如果触发器更新分布式数据库中的远程表,Oracle 将使用两阶段提交。

  • 如果给定语句存在多个相同类型的触发器,则 Oracle 会以未指定的随机顺序触发多个触发器;也就是说,同一语句的同一类型的触发器不保证以任何特定顺序触发。

For versions before 11g, no, the order is unspecified. From 10g Release 2 docs:

For enabled triggers, Oracle automatically performs the following actions:

  • Oracle runs triggers of each type in a planned firing sequence when more than one trigger is fired by a single SQL statement. First, statement level triggers are fired, and then row level triggers are fired.

  • Oracle performs integrity constraint checking at a set point in time with respect to the different types of triggers and guarantees that triggers cannot compromise integrity constraints.

  • Oracle provides read-consistent views for queries and constraints.

  • Oracle manages the dependencies among triggers and schema objects referenced in the code of the trigger action

  • Oracle uses two-phase commit if a trigger updates remote tables in a distributed database.

  • Oracle fires multiple triggers in an unspecified, random order, if more than one trigger of the same type exists for a given statement; that is, triggers of the same type for the same statement are not guaranteed to fire in any specific order.

━╋う一瞬間旳綻放 2024-12-25 11:12:04

在 10g 中,除了正常的 before 语句、before row、after row、after statements 顺序之外,不能依赖任何触发触发的顺序。在 11g 中,新的 FOLLOWS 子句添加到 CREATE TRIGGER 语句。

No order to trigger firing can be relied upon in 10g beyond the normal before statement, before row, after row, after statement order. In 11g a new FOLLOWS clause was added to the CREATE TRIGGER statement.

给妤﹃绝世温柔 2024-12-25 11:12:04

在 Oracle 10g 中,我们不控制在同一时间创建的触发器。它是随机执行的。所以我们不能说哪个触发器首先被触发。为了解决这个问题,Oracle 11g 引入了FOLLOWS CLAUSE。使用它我们可以控制执行顺序。

In Oracle 10g we do not control the triggers that are created on same timing. It is executed randomly. So we cannot say which trigger is fired first. To overcome this problem, Oracle 11g introduced FOLLOWS CLAUSE. Using this we can control the execution order.

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