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)稱入庫(kù)表)income,出庫(kù)金額表(以下簡(jiǎn)稱出庫(kù)表)outlay,余額表balance
income{
id number;
pay_amount number;(入庫(kù)金額字段)
}
outlay{
id number;
outlay_amount number;(出庫(kù)金額字段)
}
balance
{
id number;
balance number;(余額字段)
}
下面分別在入庫(kù)和出庫(kù)表中建立觸發(fā)器
入庫(kù)表(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;
出庫(kù)表(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觸發(fā)器,觸發(fā)事件分為插入,刪除,更新列三種事件,分別對(duì)應(yīng)inserting /deleting/updating關(guān)鍵字
可以用if語(yǔ)句分別實(shí)現(xiàn)
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ù)類型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說(shuō)了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫的不好請(qǐng)大家諒解。
好處:
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)稱入庫(kù)表)income,出庫(kù)金額表(以下簡(jiǎn)稱出庫(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ù)類型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說(shuō)了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫的不好請(qǐng)大家諒解。
相關(guān)文章
oracle插入字符串?dāng)?shù)據(jù)時(shí)字符串中有''單引號(hào)問(wèn)題
這篇文章主要介紹了oracle插入字符串?dāng)?shù)據(jù)時(shí)字符串中有'單引號(hào)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-04-04裝Oracle用PLSQL連接登錄時(shí)不顯示數(shù)據(jù)庫(kù)的解決
這篇文章主要介紹了裝Oracle用PLSQL連接登錄時(shí)不顯示數(shù)據(jù)庫(kù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Oracle中sql語(yǔ)句(+)符號(hào)代表連接的使用講解
今天小編就為大家分享一篇關(guān)于Oracle中sql語(yǔ)句(+)符號(hào)代表連接的使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02使用Navicat Premium連接Oracle的方法步驟
這篇文章主要介紹了使用Navicat Premium連接Oracle的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Oracle數(shù)據(jù)庫(kù)對(duì)象的使用詳解
這篇文章主要介紹了Oracle數(shù)據(jù)庫(kù)對(duì)象的使用,文章中涉及到的命令希望大家認(rèn)真學(xué)習(xí),對(duì)大家今后的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Oracle賬戶被鎖錯(cuò)誤:the?account?is?locked解決方法
the?account?is?locked意思是賬戶被鎖定了,這種情況需要大家去解鎖,這篇文章主要給大家介紹了關(guān)于Oracle賬戶被鎖錯(cuò)誤:the?account?is?locked的解決方法,需要的朋友可以參考下2023-12-12oracle 11g em重建報(bào)唯一約束錯(cuò)誤解決方法
今天在手工配置Oracle11g的EM時(shí)總是報(bào)如下錯(cuò)誤,也沒(méi)有找到解決辦法,以下是我的解決過(guò)程,希望可以幫助你們2012-11-11