在本章中,我们将讨论和学习pl/sql中的触发器。 触发器是存储的程序,在发生某些事件时会自动执行或触发。事实上,触发器是为了响应以下任何事件而被执行的 -
delete
,insert
或update
)create
,alter
或drop
)。servererror
,logon
,logoff
,startup
或shutdown
)。可以在事件关联的表,视图,模式或数据库上定义触发器。
使用触发器的好处
触发器可以用于以下目的 -
创建触发器的语法是 -
create [or replace ] trigger trigger_name
{before | after | instead of }
{insert [or] | update [or] | delete}
[of col_name]
on table_name
[referencing old as o new as n]
[for each row]
when (condition)
declare
declaration-statements
begin
executable-statements
exception
exception-handling-statements
end;
其中,
create [or replace] trigger trigger_name
- 使用trigger_name
创建或替换现有的触发器。{before | after | instead of}
- 指定何时执行触发器。instead of
子句用于在视图上创建触发器。{insert [or] | update [or] | delete}
- 这指定了dml操作。[of col_name]
− 这指定了将要更新的列名称。[on table_name]
- 这指定了与触发器关联的表的名称。[referencing old as o new as n]
- 这允许各种dml语句(如insert
,update
和delete
)引用新值和旧值。[for each row]
- 这指定了一个行级别的触发器,即触发器将被执行的每一行受到影响。否则触发器将在执行sql语句时执行一次,这称为表级触发器。when(condition)
- 这为触发器触发的行提供了一个条件。该子句仅对行级触发器有效。首先,将使用前面章节中创建和使用的customers
表,表的定义和数据如下 -
create table customers(
id int not null,
name varchar (20) not null,
age int not null,
address char (25),
salary decimal (18, 2),
primary key (id)
);
-- 插入示例数据
insert into customers (id,name,age,address,salary)
values (1, 'ramesh', 32, 'ahmedabad', 2000.00 );
insert into customers (id,name,age,address,salary)
values (2, 'khilan', 25, 'delhi', 1500.00 );
insert into customers (id,name,age,address,salary)
values (3, 'kaushik', 23, 'kota', 2000.00 );
insert into customers (id,name,age,address,salary)
values (4, 'chaitali', 25, 'mumbai', 6500.00 );
insert into customers (id,name,age,address,salary)
values (5, 'hardik', 27, 'bhopal', 8500.00 );
insert into customers (id,name,age,address,salary)
values (6, 'komal', 22, 'mp', 4500.00 );
下面的程序为customers
表创建一个行级触发器,该触发器将触发在customers
表上执行的insert
,update
或delete
操作。这个触发器将显示旧值和新值之间的工资差异 -
set serveroutput on size 999999;
create or replace trigger display_salary_changes
before delete or insert or update on customers
for each row
when (new.id > 0)
declare
sal_diff number;
begin
sal_diff := :new.salary - :old.salary;
dbms_output.put_line('old salary: ' || :old.salary);
dbms_output.put_line('new salary: ' || :new.salary);
dbms_output.put_line('salary difference: ' || sal_diff);
end;
/
当上面的代码在sql提示符下执行时,它会产生以下结果 -
这里需要考虑以下几点 -
old
和new
引用不可用于表级触发器,而是可以将它们用于记录级触发器。after
关键字,因为触发器只能在应用初始更改并且表返回一致状态后才能查询表或进行更改。delete
或insert
或update
操作之前触发,但是可以在一个或多个操作上编写触发器,例如before delete
,当表中的一条记录被删除时,自动触发。触发一个触发器
现在,在customers
表上执行一些dml操作。这里以执行一个insert
语句作为示例,它将在表中创建一个新记录 -
insert into customers (id,name,age,address,salary)
values (7, 'hinew', 23, 'oracle', 9500.00 );
当在customers
表中创建一条记录时,上面的创建触发器display_salary_changes
将被触发,并且将显示以下结果 -
sql> insert into customers (id,name,age,address,salary)
2 values (7, 'hinew', 23, 'oracle', 9500.00);
old salary:
new salary: 9500
salary difference:
已创建 1 行。
sql>
因为这是一个新的记录,旧的薪水(salary
)列是不可用的,上述结果为空。下面再向customers
表上执行另一个更多的dml操作。这次使用update
语句来更新表中的现有记录 -
update customers
set salary = salary + 500
where id = 2;
执行上面示例代码,得到以下结果 -
sql> update customers
2 set salary = salary + 500
3 where id = 2;
old salary: 1500
new salary: 2000
salary difference: 500
已更新 1 行。