oracle 觸發(fā)器 實(shí)現(xiàn)出入庫
更新時間:2009年07月19日 00:53:31 作者:
出庫入庫這樣的功能在許多系統(tǒng)中都有。可能叫法不一。有的可能是數(shù)量,有的可能是金額。我這里以金額為例
實(shí)現(xiàn)出庫入庫也有許多方法,一種是用語言實(shí)現(xiàn),一種是用觸發(fā)器實(shí)現(xiàn)。它們各有千秋。
用語言實(shí)現(xiàn)
好處:
1、可以減少對數(shù)據(jù)庫的訪問。
2、可移植性好。
壞處:
1、操作起來考慮的東西較多,修改一處就要修改別一處。也就是說是相互關(guān)聯(lián)的。如果少改了某一處,很可能使數(shù)據(jù)不一致。
用觸發(fā)器實(shí)現(xiàn)
好處:
1、可以使程序員從復(fù)雜的相互關(guān)聯(lián)中解放出來,把精力放在復(fù)雜的業(yè)務(wù)上。
壞處:
1、可移植性差。
下面我就用一個例子實(shí)現(xiàn)一個簡單的出入庫。因?yàn)槭抢颖碇兴玫降淖侄魏苌?。這里的例子只做為拋磚引玉。
數(shù)據(jù)表為入庫金額表(以下簡稱入庫表)income,出庫金額表(以下簡稱出庫表)outlay,余額表balance
income{
id number;
pay_amount number;(入庫金額字段)
}
outlay{
id number;
outlay_amount number;(出庫金額字段)
}
balance
{
id number;
balance number;(余額字段)
}
下面分別在入庫和出庫表中建立觸發(fā)器
入庫表(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觸發(fā)器,觸發(fā)事件分為插入,刪除,更新列三種事件,分別對應(yīng)inserting /deleting/updating關(guān)鍵字
可以用if語句分別實(shí)現(xiàn)
if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;
NVL(eExpression1, eExpression2)
如果 eExpression1 的計算結(jié)果為 null 值,則 NVL( ) 返回 eExpression2。
如果 eExpression1 的計算結(jié)果不是 null 值,則返回 eExpression1。eExpression1 和 eExpression2 可以是任意一種數(shù)據(jù)類型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫的不好請大家諒解。
好處:
1、可以減少對數(shù)據(jù)庫的訪問。
2、可移植性好。
壞處:
1、操作起來考慮的東西較多,修改一處就要修改別一處。也就是說是相互關(guān)聯(lián)的。如果少改了某一處,很可能使數(shù)據(jù)不一致。
用觸發(fā)器實(shí)現(xiàn)
好處:
1、可以使程序員從復(fù)雜的相互關(guān)聯(lián)中解放出來,把精力放在復(fù)雜的業(yè)務(wù)上。
壞處:
1、可移植性差。
下面我就用一個例子實(shí)現(xiàn)一個簡單的出入庫。因?yàn)槭抢颖碇兴玫降淖侄魏苌?。這里的例子只做為拋磚引玉。
數(shù)據(jù)表為入庫金額表(以下簡稱入庫表)income,出庫金額表(以下簡稱出庫表)outlay,余額表balance
復(fù)制代碼 代碼如下:
income{
id number;
pay_amount number;(入庫金額字段)
}
outlay{
id number;
outlay_amount number;(出庫金額字段)
}
balance
{
id number;
balance number;(余額字段)
}
下面分別在入庫和出庫表中建立觸發(fā)器
入庫表(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;
出庫表(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ā)事件分為插入,刪除,更新列三種事件,分別對應(yīng)inserting /deleting/updating關(guān)鍵字
可以用if語句分別實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;
NVL(eExpression1, eExpression2)
如果 eExpression1 的計算結(jié)果為 null 值,則 NVL( ) 返回 eExpression2。
如果 eExpression1 的計算結(jié)果不是 null 值,則返回 eExpression1。eExpression1 和 eExpression2 可以是任意一種數(shù)據(jù)類型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫的不好請大家諒解。
相關(guān)文章
oracle插入字符串?dāng)?shù)據(jù)時字符串中有''單引號問題
這篇文章主要介紹了oracle插入字符串?dāng)?shù)據(jù)時字符串中有'單引號問題的相關(guān)資料,需要的朋友可以參考下2017-04-04裝Oracle用PLSQL連接登錄時不顯示數(shù)據(jù)庫的解決
這篇文章主要介紹了裝Oracle用PLSQL連接登錄時不顯示數(shù)據(jù)庫的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11使用Navicat Premium連接Oracle的方法步驟
這篇文章主要介紹了使用Navicat Premium連接Oracle的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Oracle賬戶被鎖錯誤:the?account?is?locked解決方法
the?account?is?locked意思是賬戶被鎖定了,這種情況需要大家去解鎖,這篇文章主要給大家介紹了關(guān)于Oracle賬戶被鎖錯誤:the?account?is?locked的解決方法,需要的朋友可以參考下2023-12-12