Trigger example

本文通过一个具体的Oracle数据库触发器案例,详细介绍了如何使用触发器来记录表上的增删改操作,并展示这些操作如何被准确地记录到日志表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、一个例子

     表test1(id1 int,name varchar2(200);          

      表log(trigger_info)

   create or replace trigger audit_test1
  after insert or update of name or delete on test1  --只有update能到列
  for each row
  when (nvl(old.id1,0)>=0)  --当insert无old时,为old.id1 is null
declare

begin
  case --deleting、inserting、updating三个逻辑值,当相应动作时,该值为TRUE
    when deleting then
      --delete只有OLD,new为null
      insert into log
      values
        ('delete id' || :old.id1 || ',name' || :old.name);
    when inserting then
      --insert只有new,old为null
      insert into log
      values
        ('insert id' || :new.id1 || ',name' || :new.name);
    when updating then
      --既有new也有old
      insert into log
      values
        ('update oldid' || :old.id1 || ',oldname' || :old.name ||
         'to newid' || :new.id1 || ',newname' || :new.name);
  end case;
--commit 触发器里不能有提交,它与对应的DML属于同一个事务
end ;

    分析:

    a、insert

    SQL> insert into test1 values(33,'dqu');
 
1 row inserted
 
SQL> select * from log;
 
TRIGGER_INFO
--------------------
insert id33,namedqu
 
SQL> rollback;
 
Rollback complete
 
SQL> select * from log;
 
TRIGGER_INFO
--------------------
 
SQL> insert into test1 values(33,'dqu');
 
1 row inserted

SQL>commit;

SQL> select * from log;
 
TRIGGER_INFO
--------------------
insert id33,namedqu

    b、update

SQL> update test1 set id1=32;
 
1 row updated
 
SQL> select * from log;
 
TRIGGER_INFO
--------------------
insert id33,namedqu
 
SQL> commit;
 
Commit complete
 
SQL> select * from test1;
 
                                    ID1 NAME
--------------------------------------- --------------------
                                     32 dqu

SQL> select * from log;    ---未更新name列,触发器未插入日志
 
TRIGGER_INFO
--------------------------------------------------------------------------------
insert id33,namedqu

SQL> update test1 set name ='duqiang new';
 
1 row updated
 
SQL> select * from log;
 
TRIGGER_INFO
--------------------------------------------------------------------------------
insert id33,namedqu
update oldid32,oldnamedquto newid32,newnameduqiang new

   c、delete

SQL> delete test1;
 
1 row deleted
 
SQL> select * from log;
 
TRIGGER_INFO
--------------------------------------------------------------------------------
insert id33,namedqu
update oldid32,oldnamedquto newid32,newnameduqiang new
delete id32,nameduqiang new

   d、停用启用trigger

SQL> alter trigger audit_inset_into_test1 disable;
Trigger altered

SQL> alter trigger audit_inset_into_test1 enable;
Trigger altered

### Zabbix Trigger Configuration and Usage In the context of Zabbix monitoring systems, triggers play a crucial role in alerting administrators about potential issues or anomalies within monitored environments. A trigger is essentially an expression that evaluates data collected from hosts to determine whether a problem exists. #### Definition and Purpose Triggers are logical expressions based on item values or service statuses used by Zabbix to detect problems automatically. They can be configured with different severity levels (Information, Warning, Average, High, Disaster), allowing users to prioritize alerts effectively[^1]. #### Basic Structure of Triggers A typical trigger consists of several components including conditions, dependencies, and actions: - **Conditions**: These define when the trigger should fire using functions such as `last()`, `avg()`, etc., which operate on specific items. - **Dependencies**: One trigger may depend on another; this means it will only activate under certain circumstances defined by other related triggers. - **Actions**: Once triggered, these specify what happens next—such as sending notifications via email/SMS/etc.[^2] Here’s how you might set up basic configurations programmatically through API calls: ```json { "jsonrpc": "2.0", "method": "trigger.create", "params": { "description": "Too many processes running", "expression": "{Template OS Linux:proc.num[]}>500" }, "auth": "<your-auth-token>", "id": 1 } ``` This JSON snippet demonstrates creating a new trigger named *'Too many processes running'* where if more than 500 processes exist (`{Template OS Linux:proc.num[]} > 500`), then the condition becomes true triggering further action(s). Additionally, templates often come pre-configured but custom ones could also include advanced features like macro variables `${}` enhancing flexibility across multiple similar setups without redundancy per host basis alone [^3]. #### Example Scenario – Disk Space Monitoring Suppose we want to monitor disk space utilization exceeding thresholds at varying severities over time intervals dynamically adjusting according priorities accordingly : Trigger Expression : ```bash ({HOST.NAME}:vfs.fs.size[/,pfree].last())<10 And ({HOST.NAME}:vfs.fs.size[/,pfree].min(5m))<=5 ``` Explanation : If free percentage drops below ten percent AND remains less than five percent during last five minutes , raise alarm level appropriately depending upon business impact analysis results derived previously . --- §§Related Questions§§ 1. How do macros function inside template definitions while setting complex multi-level nested conditional statements ? 2. What best practices apply towards optimizing performance metrics collection utilizing proxies alongside centralized servers architecture design considerations? 3. Can there exist any limitations regarding simultaneous execution limits imposed against large scale enterprise deployments involving thousands interconnected nodes requiring real-time updates continuously ? 4. Is it possible integrate external scripts written Python directly into existing workflows managed solely within GUI interface itself rather relying third party plugins/extensions altogether? 5. Which database schema optimization techniques recommended ensuring minimal latency even peak load scenarios especially concerning historical archiving purposes long term storage requirements fulfillment aspects too .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值