You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle中如何实现删除或特定列更新时触发数据复制触发器?

Yes, you absolutely can implement this logic in a single trigger!

Your original trigger fires on any update to tableA, which is why it's behaving broader than intended. We can fix this by adding a conditional filter directly in the trigger definition to restrict execution to only your desired scenarios.

Here's the revised trigger code:

create or replace trigger my_trigger
before delete or update on tableA
for each row
when (deleting or (updating('STATUS') and :NEW.status = 'C'))
begin
    insert into tableB(rollno, name) values(:OLD.rollno, :OLD.name);
end;
/

Let's break down the key changes:

  • The WHEN clause acts as a gatekeeper: it only allows the trigger body to run if either:

    1. A delete operation is occurring (deleting predicate), OR
    2. The STATUS column is being updated (updating('STATUS') predicate) and the new value of STATUS is exactly 'C' (:NEW.status = 'C').
  • Specifying column names in the INSERT statement (instead of relying on position) is a good practice—it makes your code more resilient to future changes in tableB's structure.

Optional extra check (if needed):

If you want to avoid inserting duplicate records into tableB when someone updates STATUS to 'C' again (when it was already 'C'), you can add an extra condition to ensure the status actually changed:

when (deleting or (updating('STATUS') and :OLD.status != 'C' and :NEW.status = 'C'))

This single trigger will now handle both your required scenarios without needing to split into separate triggers.

内容的提问来源于stack exchange,提问作者Rohit Suthar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:00:51