最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

oracle 觸發(fā)器 實(shí)現(xiàn)出入庫(kù)

 更新時(shí)間:2009年07月19日 00:53:31   作者:  
出庫(kù)入庫(kù)這樣的功能在許多系統(tǒng)中都有??赡芙蟹ú灰?。有的可能是數(shù)量,有的可能是金額。我這里以金額為例 實(shí)現(xiàn)出庫(kù)入庫(kù)也有許多方法,一種是用語(yǔ)言實(shí)現(xiàn),一種是用觸發(fā)器實(shí)現(xiàn)。它們各有千秋。
用語(yǔ)言實(shí)現(xiàn)
好處:
1、可以減少對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)。
2、可移植性好。
壞處:
1、操作起來(lái)考慮的東西較多,修改一處就要修改別一處。也就是說(shuō)是相互關(guān)聯(lián)的。如果少改了某一處,很可能使數(shù)據(jù)不一致。
用觸發(fā)器實(shí)現(xiàn)
好處:
1、可以使程序員從復(fù)雜的相互關(guān)聯(lián)中解放出來(lái),把精力放在復(fù)雜的業(yè)務(wù)上。
壞處:
1、可移植性差。
下面我就用一個(gè)例子實(shí)現(xiàn)一個(gè)簡(jiǎn)單的出入庫(kù)。因?yàn)槭抢颖碇兴玫降淖侄魏苌?。這里的例子只做為拋磚引玉。
數(shù)據(jù)表為入庫(kù)金額表(以下簡(jiǎn)稱(chēng)入庫(kù)表)income,出庫(kù)金額表(以下簡(jiǎn)稱(chēng)出庫(kù)表)outlay,余額表balance
復(fù)制代碼 代碼如下:

income{
id number;
pay_amount number;(入庫(kù)金額字段)
}
outlay{
id number;
outlay_amount number;(出庫(kù)金額字段)
}
balance
{
id number;
balance number;(余額字段)
}

下面分別在入庫(kù)和出庫(kù)表中建立觸發(fā)器
入庫(kù)表(income):
復(fù)制代碼 代碼如下:

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;

出庫(kù)表(outlay):
復(fù)制代碼 代碼如下:

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觸發(fā)器,觸發(fā)事件分為插入,刪除,更新列三種事件,分別對(duì)應(yīng)inserting /deleting/updating關(guān)鍵字
可以用if語(yǔ)句分別實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;

NVL(eExpression1, eExpression2)
如果 eExpression1 的計(jì)算結(jié)果為 null 值,則 NVL( ) 返回 eExpression2。
如果 eExpression1 的計(jì)算結(jié)果不是 null 值,則返回 eExpression1。eExpression1 和 eExpression2 可以是任意一種數(shù)據(jù)類(lèi)型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說(shuō)了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫(xiě)的不好請(qǐng)大家諒解。

相關(guān)文章

最新評(píng)論

鄂伦春自治旗| 油尖旺区| 永康市| 克东县| 固阳县| 金湖县| 莱州市| 许昌县| 崇文区| 堆龙德庆县| 西盟| 盘锦市| 本溪| 仁布县| 南江县| 海门市| 奉贤区| 和林格尔县| 深州市| 宁化县| 若羌县| 高雄县| 左权县| 汉沽区| 天津市| 宜黄县| 宜章县| 黔江区| 通化县| 西乌| 河津市| 丽水市| 青州市| 湟源县| 贵州省| 曲水县| 延庆县| 沾化县| 胶州市| 利川市| 绥宁县|