`
andy54321
  • 浏览: 441457 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Before, after, each row and table level triggers

阅读更多
The goal of this page is to demonstrate the most important differences between before and after triggers as well as the differences between for each row and table level triggers.

Before / for each row trigger

A before trigger is called before because it fires before 
create table

 t_update_before_each_row (
  txt varchar2(10)
);

create table log (
  txt varchar2(20)
);


create trigger

 update_before_each_row 
  before update
 on t_update_before_each_row
  for each row

begin



  :new.txt := upper(:new.txt);

  insert into log values ('old: ' || :old.txt);
  insert into log values ('new: ' || :new.txt);

end

 update_before_each_row;
/
insert into t_update_before_each_row values('one');
insert into t_update_before_each_row values('two');
insert into t_update_before_each_row values('three');
insert into t_update_before_each_row values('four');
Updating (that is: concatenating the value with itself) the rows containing two and three :
update t_update_before_each_row set txt = txt || txt 
 where substr

(txt,1,1) = 't';
select

 * from t_update_before_each_row;
As can be seen by the output of the select statement, the trigger changed the values of the new values; they're in uppercase now:
one
TWOTWO
THREETHREE
four
The log displays the old and new values:
select

 * from log;
old: two
new: TWOTWO
old: three
new: THREETHREE
Cleaning up:
drop table t_update_before_each_row;
drop table log;

After / for each row trigger

In contrast to a before trigger, an after trigger does not allow to change :new.field_name because the value is, when the trigger fires, already written to the table.
If one tries to assign a value to :new.field_name , Oracle throws an ORA-04084: cannot change NEW values for this trigger type .
create table t_update_after_each_row (
  txt varchar2(10)
);

create table log (
  txt varchar2(20)
);


create trigger update_after_each_row 
  after
 update on t_update_after_each_row
  for each row

begin

  -- :new.txt := upper(:old.txt); -- ORA-04084
: cannot change NEW values for this trigger type

  insert into log values ('old: ' || :old.txt);
  insert into log values ('new: ' || :new.txt);

end update_after_each_row;
/
insert into t_update_after_each_row values('one');
insert into t_update_after_each_row values('two');
insert into t_update_after_each_row values('three');
insert into t_update_after_each_row values('four');
update t_update_after_each_row set txt = txt || txt 
 where substr(txt,1,1) = 't';
select * from t_update_after_each_row;
one
twotwo
threethree
four
select * from log;
As the log table shows, it is possible to use :new and :old although it's not possible to assign something to :new .
old: two
new: twotwo
old: three
new: threethree
Cleaning up:
drop table t_update_after_each_row;
drop table log;

Table level trigger

A table level trigger is a trigger that doesn't fire for each row to be changed. Accordingly, it lacks the for each row . Consequently, both, the :new and :old are not permitted in the trigger's PL/SQL block , otherwise, an ORA-04082: NEW or OLD references not allowed in table level triggers is thrown .
create table t_update_before (
  txt varchar2(10)
);

create table log (
  txt varchar2(20)
);


create trigger update_before 
  before update on t_update_before
begin

  -- :new.txt := upper(:old.txt); -- ORA-04082


  insert into log values ('update trigger');

end update_before;
/
insert into t_update_before values('one');
insert into t_update_before values('two');
insert into t_update_before values('three');
insert into t_update_before values('four');
update t_update_before set txt = txt || txt 
 where substr(txt,1,1) = 't';
select * from t_update_before;
one
twotwo
threethree
four
select * from log;
Although two rows were updated, only one record is found in the log table:
select * from log;
update trigger
An update statement that doesn't update any row:
update t_update_before set txt = txt || txt 
 where txt = 'no update';
Still, the trigger fires...
select * from log;
... which results in another row found in the log table:
update trigger
update trigger
Cleaning up:
drop table t_update_before;
drop table log;

Order of execution

Oracle allows to create multiple triggers on the same table. The order of the execution of these triggers is undeterministic (or random, if you want this word) except that all before triggers fire before the after triggers.
分享到:
评论

相关推荐

    微软内部资料-SQL性能优化3

    In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...

    stored-procedures-and-triggers.zip_SQL触发器实验_Triggers

    在“stored procedures and triggers.doc”文档中,可能会详细解释每个实验的步骤,提供示例代码,以及帮助学生理解如何将这些概念应用于实际场景。通过实践,学生不仅能掌握理论知识,还能获得宝贵的动手经验,这...

    mysql中触发器使用详解.docx

    `table_name`是你想要关联的表名,`time`可以是`BEFORE`或`AFTER`,表示触发器是在操作前还是后执行,`event`可以是`INSERT`、`UPDATE`或`DELETE`,表示触发的操作类型。`FOR EACH ROW`表示无论操作影响多少行,...

    orcle中触发器的使用

    BEFORE/ AFTER INSERT ON table_name FOR EACH ROW BEGIN -- 触发器的SQL语句 END; ``` ### 2. UPDATE触发器 UPDATE触发器在对表中的行进行更新时触发。这可以用来检查更新后的值,或者在更新后更新其他相关的行...

    循环某数据库所有表,自动创建触发器

    SET @sql = CONCAT('CREATE TRIGGER before_insert_', tbl_name, ' BEFORE INSERT ON ', tbl_name, ' FOR EACH ROW BEGIN SET NEW.CreatedAt = NOW(); END;'); PREPARE stmt FROM @sql; EXECUTE stmt; ...

    Professional SQL Server 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types

    In English. SQL CLR is an enhancement to T-SQL programming for SQL Server, is one new feature for SQL Server 2005 which let you embed C#, VB.Net code into T-SQL ... triggers, aggregates and types.

    db2 store procedure and triggers

    触发器(Triggers) 触发器是当特定事件发生时自动执行的一组 SQL 语句。触发器的主要用途是在数据插入、更新或删除之前或之后执行某些操作,例如记录日志、验证数据完整性等。DB2 支持以下几种触发器类型: - **...

    Programming Oracle Triggers And Stored Procedures 3rd ed

    Programming Oracle Triggers And Stored Procedures 英文第三版 自制CHM格式,可DropDownList选章选节阅读,可调节字体大小,大大方便了Viliv S5等手持上网设备的阅读。 注意: 由于内部使用了MS XML Parser,本...

    触发器Triggers、MultiTrggers、EventTrigger应用案例

    在Windows Presentation Foundation (WPF) 中,触发器(Triggers)是XAML中的一种关键特性,它们允许我们根据特定条件改变UI元素的属性或者执行某些行为。触发器分为多种类型,包括数据触发器(DataTriggers)、样式...

    PLSQL触发器.pdf

    BEFORE UPDATE OF sal ON scott.emp FOR EACH ROW BEGIN IF (:NEW.sal ) THEN -- 如果新的薪水小于旧薪水,则抛出错误 RAISE_APPLICATION_ERROR(-20001, '薪水不能减少'); END IF; END; / ``` 触发器还可以用于...

    11g_plsql_user_guide_and_reference.pdf

    For example, they can specify whether a trigger should be executed before or after a DML operation, or whether it should be executed for each row or for the entire statement. This level of control ...

    vagrant plugin install vagrant-triggers

    The Vagrantfile requires the plugin vagrant-triggers. To install the plugin run: $ vagrant plugin install vagrant-triggers 安装Docker的管理程序 Deis 在 vagrant,需要安装vagrant-triggers,由于网络...

    mysql触发器.pdf

    触发器的执行时机(BEFORE或AFTER)和事件(INSERT、UPDATE或DELETE)决定了触发器何时被激活。 2.2 创建触发器 创建触发器的语法如下: ```sql CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl...

    触发器

    在数据库管理领域,触发器(Triggers)是一种重要的数据库对象,它允许数据库管理员或开发者定义在特定事件(如INSERT、UPDATE、DELETE)发生时自动执行的SQL语句或存储过程。触发器通常用于实现复杂的业务规则,...

    mysql觸發器

    首先,触发器的定义包括事件(如INSERT、UPDATE或DELETE)、表名、执行时机(BEFORE或AFTER)以及触发时执行的动作(SQL语句)。例如,一个简单的触发器可能在插入新记录到表中之前检查某些条件,或者在更新记录后...

    触发器介绍

    - 整张表的所有触发器:`ALTER TABLE table_name DISABLE ALL TRIGGERS;` - **启用触发器**:与禁用类似,可以通过以下命令重新启用触发器: - 单个触发器:`ALTER TRIGGER trigger_name ENABLE;` - 整张表的所有...

    oracle

    [BEFORE | AFTER] DML_statement [OF column,...] ON table_name|view [WHEN (condition)] [FOR EACH ROW] [DECLARE declarations] BEGIN execute_statement EXCEPTION END trigger_name; ``` - `trigger_name...

    Oracle触发器.pdf

    ON table_name [FOR EACH ROW] [WHEN (condition)] BEGIN -- 触发器逻辑 -- PL/SQL代码块 END; ``` - `CREATE [OR REPLACE] TRIGGER`:创建新触发器或替换同名的现有触发器。 - `trigger_name`:触发器的名称。 - ...

    MySQL 使用手册

    - **语法**: `CREATE TRIGGER 触发器名 BEFORE|AFTER 触发事件 ON 表名 FOR EACH ROW 执行语句;` - 示例: `CREATE TRIGGER tr_insert_log AFTER INSERT ON orders FOR EACH ROW INSERT INTO log (action, timestamp...

Global site tag (gtag.js) - Google Analytics