一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

sql 触发器trigger 创建与用法

时间:2011-04-26 编辑:简简单单 来源:一聚教程网

sql 触发器trigger 创建与用法

发器也是一种带名的pl/sql块。触发器类似于过程和函数,因为它们都是拥有声明

、执行和异常处理过程的带名pl/sql块。与包类似,触发器必须存储在数据库教程中并

且不能被块进行本地化声明。

对于触发器而言,当触发事件发生的时候就会显式地执行该触发器,并且触发器不

接受参数

 

create table employee(
2>     id          int,
3>     name        nvarchar (10),
4>     salary      int,
5>     start_date  datetime,
6>     city        nvarchar (10),
7>     region      char (1))
8> go
1>
2> insert into employee (id, name,    salary, start_date, city,      

region)
3>               values (1,  'jason', 40420,  '02/01/94', 'new york',

'w')
4> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (2,  'robert',14420,  '01/02/95',

'vancouver','n')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (3,  'celia', 24020,  '12/03/96', 'toronto', 

'w')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (4,  'linda', 40620,  '11/04/97', 'new york',

'n')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (5,  'david', 80026,  '10/05/98',

'vancouver','w')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (6,  'james', 70060,  '09/06/99', 'toronto', 

'n')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (7,  'alison',90620,  '08/07/00', 'new york',

'w')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (8,  'chris', 26020,  '07/08/01',

'vancouver','n')
3> go

(1 rows affected)
1> insert into employee (id, name,    salary, start_date, city,      

region)
2>               values (9,  'mary',  60020,  '06/09/02', 'toronto', 

'w')
3> go

(1 rows affected)
1>
2> select * from employee
3> go
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----
          1 jason            40420 1994-02-01 00:00:00.000 new york   w
          2 robert           14420 1995-01-02 00:00:00.000 vancouver  n
          3 celia            24020 1996-12-03 00:00:00.000 toronto    w
          4 linda            40620 1997-11-04 00:00:00.000 new york   n
          5 david            80026 1998-10-05 00:00:00.000 vancouver  w
          6 james            70060 1999-09-06 00:00:00.000 toronto    n
          7 alison           90620 2000-08-07 00:00:00.000 new york   w
          8 chris            26020 2001-07-08 00:00:00.000 vancouver  n
          9 mary             60020 2002-06-09 00:00:00.000 toronto    w

(9 rows affected)
1>
2> -- creating and using triggers
3>
4>
5> create trigger mytrigger
6> on employee
7> for update, insert, delete
8> as
9> select * from inserted
10> select * from deleted
11> go
1>
2>
3> select 'before insert'
4> insert employee (id, name) values (31, 'rick')
5> go

-------------
before insert

(1 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----
         31 rick              null                    null null      

null

(1 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----

(0 rows affected)
1>
2> select 'before update'
3> update employee
4> set name = 'rickie'
5> where id = 3
6> go

-------------
before update

(1 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----
          3 rickie           24020 1996-12-03 00:00:00.000 toronto    w

(1 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----
          3 celia            24020 1996-12-03 00:00:00.000 toronto    w

(1 rows affected)
1>
2> select 'before delete'
3> delete from employee where id = 3
4> go

-------------
before delete

(1 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----

(0 rows affected)
id          name       salary      start_date              city      

region
----------- ---------- ----------- ----------------------- ---------- -

-----
          3 rickie           24020 1996-12-03 00:00:00.000 toronto    w

(1 rows affected)
1>
2>
3> drop table employee
4> go
1>

dml触发器的激活顺序

1)执行before语句级触发器—如果存在这种触发器

2)对受该语句影响的每一行记录

执行before行级触发器—如果存在这种触发器

执行该语句本身

执行after行级触发器--如果存在这种触发器

3)执行after语句级触发器--如果存在这种触发器


同一种类型的触发器的点火次序没有经过定义。如果该次序很重要的话,那么建议

将所有这些操作组合到一个触发器当中。


3、行级触发器中的关联标识符

触发器的激活语句每处理一行数据,行级触发器就会激活一次。可以在这种行级触

发器内部,访问正被处理的记录行中的数据。这是通过两个关联标识符--:old

和:new—实现的。关联标识符也是pl/sql的一种特殊的绑定变量。标识符前面的冒

号,既说明这二者都是绑定变量,同时也说明它们不是一般的pl/sql变量。pl/sql

编译器会将它们看作下面这个类型的记录:


triggering_table%rowtype


其中triggering_table是在其上定义触发器的表名。于是,下面这种引用


:new.field


就只有当其中的field是该触发表中的字段名时才会有效。


触发语句
:old
:new

insert
未定义—所有字段均为null
触发语句完成的时候,要插入的值

update
更新以前相应记录行的原始值
触发语句完成的时候,要更新的值

delete
删除以前相应记录行的原始值
未定义—所有字段均为null

 

注意:insert语句上没有定义:old标识符,delete语句上也没有定义:new标识符。

如果再insert语句上使用:old标识符,或者在delete语句上使用:new标识符,

pl/sql并不会产生错误,但是这两个字段值都会为null。


伪记录

虽然在语法构成上,会将:new和:old看作triggering_table%rowtype类型的记录,

但是,实际上它们并不是记录。因此,那些能够在记录上正常执行的操作,并不能

在:new和:old上执行。例如,不能将它们作为一个整体进行赋值。只能对其中的各

个字段分别赋值。

热门栏目