oracle 触发器 实现出入库
来源:asp之家 发布时间:2009-07-23 14:56:00
用语言实现
好处:
1、可以减少对数据库的访问。
2、可移植性好。
坏处:
1、操作起来考虑的东西较多,修改一处就要修改别一处。也就是说是相互关联的。如果少改了某一处,很可能使数据不一致。
用触发器实现
好处:
1、可以使程序员从复杂的相互关联中解放出来,把精力放在复杂的业务上。
坏处:
1、可移植性差。
下面我就用一个例子实现一个简单的出入库。因为是例子表中所用到的字段很少。这里的例子只做为抛砖引玉。
数据表为入库金额表(以下简称入库表)income,出库金额表(以下简称出库表)outlay,余额表balance
代码如下:
income{
id number;
pay_amount number;(入库金额字段)
}
outlay{
id number;
outlay_amount number;(出库金额字段)
}
balance
{
id number;
balance number;(余额字段)
}
下面分别在入库和出库表中建立触发器
入库表(income):
代码如下:
CREATE TRIGGER "AA"."TRI_ADD" AFTER
INSERT
OR DELETE ON "INCOME" FOR EACH ROW begin
if deleting then
update balance set balance = nvl(balance,0) - :old.pay_amount;
elsif updating then
update balance set balance = nvl(balance,0) - :old.pay_amount + :new.pay_amount;
else
update balance set balance = nvl(balance,0) + :new.pay_amount;
end if;
end;
出库表(outlay):
代码如下:
CREATE TRIGGER "AA"."TRI_CUT" AFTER
INSERT
OR DELETE
OR UPDATE ON "OUTLAY" FOR EACH ROW begin
if deleting then
update balance set balance = nvl(balance,0) + :old.outlay_amount;
elsif updating then
update balance set balance = nvl(balance,0) + :old.outlay_amount - :new.outlay_amount;
else
update balance set balance = nvl(balance,0) - :new.outlay_amount;
end if;
end;
下面我解释一下
oracle触发器,触发事件分为插入,删除,更新列三种事件,分别对应inserting /deleting/updating关键字
可以用if语句分别实现
代码如下:
if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;
NVL(eExpression1, eExpression2)
如果 eExpression1 的计算结果为 null 值,则 NVL( ) 返回 eExpression2。
如果 eExpression1 的计算结果不是 null 值,则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。
如果 eExpression1 与 eExpression2 的结果皆为 null 值,则 NVL( ) 返回 .NULL.。
这里插入和删除就不说了。主要是更新操作,更新操作要注意的是更新应该是先减去旧值,在加上新值。
以上就是触发器例子的实现。文章写的不好请大家谅解。
猜你喜欢
- 1.创建空字典>>> dic = {}>>> type(dic)<type 'dict
- 1.在myblog中的urls.py中from django.urls import includefrom django.conf.url
- 前言以下是我对python中编写脚本最重要的库之一pyautogui的学习整理,分享给大家希望有所帮助提示:我在初步使用pyautogui的
- 最近在做压力测试嘛,需要逐步增加用户量做验证,每个用户单独创建数据库进行连接,就要不断去创建数据库,这个报错也很容易理解,mysql连接数不
- 本文实例讲述了Python实现小数转化为百分数的格式化输出方法。分享给大家供大家参考,具体如下:比如将 0.1234 转化为 12.34%
- OL是有序列表,但给list-style-type:decimal 定义,在IE中却显示的全是1,在火狐、Opera、谷歌、Safari等中
- ThinkPHP模板的empty标签用于判断模板变量是否为空值。ThinkPHP模板empty标签用来判断模板变量是否为空值,其功能相当于P
- 前言在awesomes上寻找移动端框架的时候意外发现了vux的页面切换效果,后面由于其他考虑没有选用vuex但是这个切换效果确实感觉很有新意
- 在我们想要对不同变量进行判断的时候,会分析其中的之间的联系。这种理念同样也被用在实例生活中,最常见到的是做一个地理的热力图。很多人对画热力图
- 前言为了简化并更好地标识异步IO,从Python 3.5开始引入了新的语法async和await,可以让coroutine的代码更简洁易读。
- 我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不
- 一般而言下面的就可以完成需求了。def convertToDic(data): jsonDic=json.loads(data) retur
- 以GraphViz为例:下载安装好的路径名字为C:/Program Files (x86)/Graphviz2.38import osos.
- 在学会了SELECT语句的构造和用途之后你就该学习如何使用它了。在你所掌握的数据库工具下,这可能意味着你得按下某个写着“执行”字样的按钮。在
- datetime 时间包认识 datetime 时间包:date:日期;time:时间;所以 datetime 就是 日期与时间的结合体使用
- python实现的对文件夹中的图像进行连续的重命名方法:import osclass BatchRename(): def __init__
- 在网站或软件的策划和设计过程中,我们经常听到这样的讨论:“这个功能设计得太重了”又或“我们希望能够处理得轻一些”。似乎轻设计是时下炙手可热的
- 项目地址是:https://www.chenshiyang.com/dytk接下来我们分析下源码简要看下实现原理。实现原理该项目不需要使用模
- 我就废话不多说了,直接上代码吧!import numpy as npimport matplotlib.pyplot as pltdef g
- 身体是革命的本钱,身体健康了我们才有更多精力做自己想做的事情,追求女神,追求梦想。然而程序员是一个苦比的职业,大部分时间都对着电脑,我现在颈