发布于 2017-01-07 00:56:24 | 137 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Mysql教程,程序狗速度看过来!

Mysql关系型数据库管理系统

MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。


这篇文章主要介绍了Oracle使用触发器和mysql中使用触发器的案例比较,本文通过示例讲解,给大家介绍的非常详细,需要的朋友参考下

一、触发器

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务