度量快速开发平台-专业、快速的软件定制快开平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 部件 流程 SQL
查看: 2417|回复: 1
打印 上一主题 下一主题

[分享] Oracle使用触发器和mysql中使用触发器的案例比较

[复制链接]

328

主题

3738

帖子

8566

积分

作者

Rank: 7Rank: 7Rank: 7

积分
8566
QQ
跳转到指定楼层
楼主
发表于 2020-5-29 16:31:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这篇文章主要介绍了Oracle使用触发器和mysql中使用触发器的案例比较,本文通过示例讲解,给大家介绍的非常详细,需要的朋友参考下


一、触发器
  1.触发器在数据库里以独立的对象存储,
  2.触发器不需要调用,它由一个事件来触发运行
  3.触发器不能接收参数
  --触发器的应用
    举个例子:校内网、开心网、facebook,当你发一个日志,自动通知好友,其实就是在增加日志的时候做一个出发,再向表中写入条目。
  --触发器的效率很高
    举例:论坛的发帖,每插入一个帖子都希望将版面表中的最后发帖时间,帖子总数字段进行同步更新,这时使用触发器效率会很高。
二、Oracle 使用 PL/SQL 编写触发器


1.--PL/SQL创建触发器的一般语法
1
2
3
4
5
6
7
8
create [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.使用ld 和 :new 操作符
create or replace trigger tri_update
after
update on employees
for each row
begin
  dbms_output.put_line('更新前:'||ld.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中使用触发器的案例比较,希望对大家有所帮助。


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
沙发
发表于 2020-2-17 17:23:12 | 只看该作者
sssssssss
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|玉祥公司客服-玉祥集团客服  本站关键词:快速开发平台

GMT+8, 2024-5-19 01:26 , Processed in 0.123787 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表