Oracle使用触发器和mysql中使用触发器的案例比较

所属分类: 数据库 / oracle 阅读数: 848
收藏 0 赞 0 分享

一、触发器

  1.触发器在数据库里以独立的对象存储,

  2.触发器不需要调用,它由一个事件来触发运行

  3.触发器不能接收参数

  --触发器的应用

    举个例子:校内网、开心网、facebook,当你发一个日志,自动通知好友,其实就是在增加日志的时候做一个出发,再向表中写入条目。

  --触发器的效率很高

    举例:论坛的发帖,每插入一个帖子都希望将版面表中的最后发帖时间,帖子总数字段进行同步更新,这时使用触发器效率会很高。

二、Oracle 使用 PL/SQL 编写触发器

1.--PL/SQL创建触发器的一般语法

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.--练习

--问题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 使用触发器

--假设有两张表 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中使用触发器的案例比较,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Oracle轻松取得建表和索引的DDL语句

Oracle轻松取得建表和索引的DDL语句
收藏 0 赞 0 分享

重新编译PLSQL中的无效对象或者指定的对象 的方法

重新编译PLSQL中的无效对象或者指定的对象 的方法
收藏 0 赞 0 分享

在OracleE数据库的字段上建立索引的方法

在OracleE数据库的字段上建立索引的方法
收藏 0 赞 0 分享

oracle下加密存储过程的方法

oracle下加密存储过程的方法
收藏 0 赞 0 分享

浅谈LogMiner的使用方法

浅谈LogMiner的使用方法
收藏 0 赞 0 分享

oracle 下WITH CHECK OPTION用法

oracle 下WITH CHECK OPTION用法
收藏 0 赞 0 分享

在Oracle中向视图中插入数据的方法

在Oracle中向视图中插入数据的方法
收藏 0 赞 0 分享

DBA_2PC_PENDING 介绍

DBA_2PC_PENDING 介绍
收藏 0 赞 0 分享

在Oracle PL/SQL中游标声明中表名动态变化的方法

在Oracle PL/SQL中游标声明中表名动态变化的方法
收藏 0 赞 0 分享

DB2和 Oracle的并发控制(锁)的比较

DB2和 Oracle的并发控制(锁)的比较
收藏 0 赞 0 分享
查看更多