简体   繁体   English

在 oracle 中执行触发器时保持两个表之间的依赖关系

[英]Maintain dependency between two tables while executing triggers in oracle

How we can maintain dependency between two tables while executing triggers in oracle??我们如何在 oracle 中执行触发器时保持两个表之间的依赖关系? As we know we can use follows clause if we have single table and multiple triggers based on that table but my concern is that is there any way if we have 2 different tables and there are different triggers based on these table so how can we maintain some proper sequence in this case.正如我们所知,如果我们有单个表和基于该表的多个触发器,我们可以使用 follows 子句,但我担心的是,如果我们有 2 个不同的表并且有基于这些表的不同触发器,那么我们如何维护一些在这种情况下的正确顺序。

For example: --Table1:例如:--表 1:

CREATE TABLE emp(
empId number PRIMARY KEY,
FirstName varchar2(20),
LastName varchar2(20),
Email varchar2(25),
PhoneNo varchar2(25),
Salary number(8)
);

--Table 2: --表2:

CREATE TABLE emp_1(
empId number PRIMARY KEY,
FirstName varchar2(20),
LastName varchar2(20),
Email varchar2(25),
PhoneNo varchar2(25),
Salary number(8)
);

Trigger on EMP : EMP触发:

CREATE OR replace TRIGGER TRIGGER_emp 
BEFORE INSERT OR AFTER UPDATE ON emp
FOR EACH ROW 
BEGIN  
  
    dbms_output.put_line ('MY EXECUTE ORDER FOR EMP IS SECOND -EXECUTED');  
END; 
/

Trigger on EMP1 :EMP1上触发:

CREATE OR replace TRIGGER TRIGGER_emp1
BEFOR INSERT OR AFTER UPDATE ON emp_1 
FOR EACH ROW 
BEGIN  
  
    dbms_output.put_line ('MY EXECUTE ORDER FOR EMP IS FIRST -EXECUTED');  
END; 
/

Now i want this trigger TRIGGER_emp1 will execute first and then this trigger will execute TRIGGER_emp last.现在我希望这个触发器 TRIGGER_emp1 首先执行,然后这个触发器最后执行 TRIGGER_emp。 Is it possible to do so in oracle oracle可以吗

Please guide me in this.请指导我。

You seem to be a clone of user who recently posted two questions regarding the same subject.您似乎是最近发布了关于同一主题的两个问题的用户的克隆。 It was said that you can't do that.有人说你不能那样做。 Triggers "belong" to a single table, which means that触发器“属于”单个表,这意味着

  • you insert (or update) values in emp table, which then您在emp表中插入(或更新)值,然后
  • fires trigger trigger_emp which does something触发触发器trigger_emp做某事
    • in your case, it just displays a message (you might, or might not see; that depends on a tool you use)在你的情况下,它只是显示一条消息(你可能会,也可能不会看到;这取决于你使用的工具)
  • as nothing happened to table emp1 , trigger trigger_emp1 won't fire - why would it?由于表emp1没有发生任何事情,触发器trigger_emp1不会触发 - 为什么会这样?
    • if you want it to fire, then let the first trigger ( trigger_emp ) affect rows in emp1 table (insert a row or update existing values)如果你想让它触发,那么让第一个触发器( trigger_emp )影响emp1表中的行(插入一行或更新现有值)
      • once you do that, trigger_emp1 will do whatever it does一旦你这样做了, trigger_emp1就会做它所做的一切

Maybe, just maybe you should consider creating a stored procedure which contains code that works with rows in both emp and emp1 table.也许,只是也许您应该考虑创建一个存储过程,其中包含与empemp1表中的行一起使用的代码。 Then, instead of relying on triggers and worry whether they will fire, when will they fire and in which sequence, your main transaction would actually call the procedure which would then do the job (and affect rows in emp and emp1 , in any order you want - that's easy to do as you'd just rearrange statements within the procedure).然后,而不是依赖触发器并担心它们是否会触发,它们何时会触发以及以什么顺序触发,您的主事务实际上会调用该过程,然后执行该工作(并影响empemp1中的行,以您的任何顺序想要 - 这很容易做到,因为您只需在过程中重新排列语句)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM