各位用户为了找寻关于Oracle使用触发器和mysql中使用触发器的案例比较的资料费劲了很多周折。这里教程网为您整理了关于Oracle使用触发器和mysql中使用触发器的案例比较的相关资料,仅供查阅,以下为您介绍关于Oracle使用触发器和mysql中使用触发器的案例比较的详细内容
一、触发器
1.触发器在数据库里以独立的对象存储,
2.触发器不需要调用,它由一个事件来触发运行
3.触发器不能接收参数
--触发器的应用
举个例子:校内网、开心网、facebook,当你发一个日志,自动通知好友,其实就是在增加日志的时候做一个出发,再向表中写入条目。
--触发器的效率很高
举例:论坛的发帖,每插入一个帖子都希望将版面表中的最后发帖时间,帖子总数字段进行同步更新,这时使用触发器效率会很高。
二、Oracle 使用 PL/SQL 编写触发器
1.--PL/SQL创建触发器的一般语法
? 1 2 3 4 5 6 7 8create
[
or
replace
]
trigger
trigger_name
{before |
after
}
{
insert
|
delete
|
update
[
of
column
[,
column
... ]]}
on
table_name
[
for
each row]
[
where
condition]
--trigger_body;
begin
end
;
2.--练习
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28--问题3.使用:old 和 :new 操作符
create
or
replace
trigger
tri_update
after
update
on
employees
for
each row
begin
dbms_output.put_line(
'更新前:'
||:old.salary||
' 更新后:'
||:new.salary);
end
;
--问题2.编写一个触发器,在向 emp 表中插入记录时 打印'hello'
create
or
replace
trigger
tri_update
after
insert
on
emp
begin
dbms_output.put_line(
'ok'
);
end
;
--问题1.一个helloworld级别的触发器
--创建一个触发器,在更新employees表的时候触发
create
or
replace
trigger
tri_update
after
update
on
employees
for
each row
--想在最后执行完打印一个ok,把这句话去掉
begin
dbms_output.put_line(
'ok'
);
end
;
--执行
update
employees
set
salary = salary+1
where
department_id = 80
三、在MySql 使用触发器
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26--假设有两张表 board 和 article
create
table
board(
id
int
primary
key
auto_increment,
name
varchar
(50),
articleCount
int
);
create
table
article(
id
int
primary
key
auto_increment,
title
varchar
(50),
bid
int
references
board(id)
);
--创建一个触发器
delimiter $$
create
trigger
insertArticle_trigger
after
insert
on
article
for
each row
begin
update
board
set
articleCount=articleCount+1
where
id = new.bid;
end
;
$$
delimiter ;
--当我们对article表执行插入操作的是后就会触发这个触发器
insert
into
board
values
(
null
,
'test_boardname'
,0);
insert
into
article
values
(
null
,
'test_title'
,1);
--执行完这条插入语句后,board表中的articleCount字段值回+1;这个操作由触发器完成。
以上所述是小编给大家介绍的Oracle使用触发器和mysql中使用触发器的案例比较,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:http://www.cnblogs.com/redirectZmh/archive/2016/12/19/6196487.html