oracle 觸發(fā)器 實現出入庫
更新時間:2009年07月19日 00:53:31 作者:
出庫入庫這樣的功能在許多系統(tǒng)中都有。可能叫法不一。有的可能是數量,有的可能是金額。我這里以金額為例
實現出庫入庫也有許多方法,一種是用語言實現,一種是用觸發(fā)器實現。它們各有千秋。
用語言實現
好處:
1、可以減少對數據庫的訪問。
2、可移植性好。
壞處:
1、操作起來考慮的東西較多,修改一處就要修改別一處。也就是說是相互關聯(lián)的。如果少改了某一處,很可能使數據不一致。
用觸發(fā)器實現
好處:
1、可以使程序員從復雜的相互關聯(lián)中解放出來,把精力放在復雜的業(yè)務上。
壞處:
1、可移植性差。
下面我就用一個例子實現一個簡單的出入庫。因為是例子表中所用到的字段很少。這里的例子只做為拋磚引玉。
數據表為入庫金額表(以下簡稱入庫表)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ā)事件分為插入,刪除,更新列三種事件,分別對應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.。
這里插入和刪除就不說了。主要是更新操作,更新操作要注意的是更新應該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實現。文章寫的不好請大家諒解。
好處:
1、可以減少對數據庫的訪問。
2、可移植性好。
壞處:
1、操作起來考慮的東西較多,修改一處就要修改別一處。也就是說是相互關聯(lián)的。如果少改了某一處,很可能使數據不一致。
用觸發(fā)器實現
好處:
1、可以使程序員從復雜的相互關聯(lián)中解放出來,把精力放在復雜的業(yè)務上。
壞處:
1、可移植性差。
下面我就用一個例子實現一個簡單的出入庫。因為是例子表中所用到的字段很少。這里的例子只做為拋磚引玉。
數據表為入庫金額表(以下簡稱入庫表)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ā)事件分為插入,刪除,更新列三種事件,分別對應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.。
這里插入和刪除就不說了。主要是更新操作,更新操作要注意的是更新應該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實現。文章寫的不好請大家諒解。
相關文章
使用Navicat Premium連接Oracle的方法步驟
這篇文章主要介紹了使用Navicat Premium連接Oracle的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
Oracle賬戶被鎖錯誤:the?account?is?locked解決方法
the?account?is?locked意思是賬戶被鎖定了,這種情況需要大家去解鎖,這篇文章主要給大家介紹了關于Oracle賬戶被鎖錯誤:the?account?is?locked的解決方法,需要的朋友可以參考下2023-12-12

