Oracle使用触发器和mysql中使用触发器的案例比较
作者:Honor5 时间:2024-01-26 02:46:49
一、触发器
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中使用触发器的案例比较网站的支持!
来源:http://www.cnblogs.com/redirectZmh/archive/2016/12/19/6196487.html
标签:oracle,mysql,触发器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MYSQL初学者扫盲
2009-02-27 13:15:00
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
2022-12-26 18:01:16
![](https://img.aspxhome.com/file/2023/0/112100_0s.png)
深入分析PHP引用(&)
2023-11-23 00:42:35
![](https://img.aspxhome.com/file/2023/1/55771_0s.jpg)
Python pyautogui模块实现鼠标键盘自动化方法详解
2023-07-25 09:16:42
scrapy处理python爬虫调度详解
2021-09-10 11:30:00
Jmeter调用Python脚本实现参数互相传递的实现
2023-10-27 23:45:01
![](https://img.aspxhome.com/file/2023/1/117941_0s.png)
Python正则表达式实现简易计算器功能示例
2021-03-24 16:11:43
Windows下mysql5.7.18安装配置教程
2024-01-17 10:23:32
![](https://img.aspxhome.com/file/2023/3/121543_0s.png)
python通过exifread模块获得图片exif信息的方法
2023-08-18 05:00:15
vue实现评论列表功能
2023-07-02 17:05:29
![](https://img.aspxhome.com/file/2023/3/139923_0s.png)
CentOS 6/7环境下通过yum安装php7的方法
2023-11-23 11:08:02
vue 开发一个按钮组件的示例代码
2024-04-30 10:27:40
django创建自定义模板处理器的实例详解
2022-07-29 19:50:00
如何捕获人家站点的页面?
2009-11-11 19:19:00
python字典进行运算原理及实例分享
2023-02-16 14:17:14
我的栅格系统实现
2008-09-21 13:50:00
![](https://img.aspxhome.com/file/UploadPic/20089/21/2008921135617161s.jpg)
Python查找算法之插补查找算法的实现
2021-03-12 08:16:57
![](https://img.aspxhome.com/file/2023/7/82607_0s.png)
Python+radar实现随机日期时间的生成
2021-04-16 23:22:57
![](https://img.aspxhome.com/file/2023/2/134682_0s.jpg)
Python实现将目录中TXT合并成一个大TXT文件的方法
2023-02-11 18:38:12
Python super()方法原理详解
2023-06-19 18:28:30