sql 触发器trigger 创建与用法

php中文网
发布: 2016-06-07 17:48:02
原创
2333人浏览过

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> * 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语句级触发器--如果存在这种触发器


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

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

php中级教程之ajax技术
php中级教程之ajax技术

AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。它不是新的编程语言,而是一种使用现有标准的新方法,最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容,不需要任何浏览器插件,但需要用户允许JavaScript在浏览器上执行。《php中级教程之ajax技术》带你快速

php中级教程之ajax技术 2114
查看详情 php中级教程之ajax技术


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上执行。例如,不能将它们作为一个整体进行赋值。只能对其中的各

个字段分别赋值。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号