oracle 觸發(fā)器 實(shí)現(xiàn)出入庫(kù)
更新時(shí)間:2009年07月19日 00:53:31 作者:
出庫(kù)入庫(kù)這樣的功能在許多系統(tǒng)中都有??赡芙蟹ú灰弧S械目赡苁菙?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ù)類(lèi)型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說(shuō)了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫(xiě)的不好請(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ù)類(lèi)型。
如果 eExpression1 與 eExpression2 的結(jié)果皆為 null 值,則 NVL( ) 返回 .NULL.。
這里插入和刪除就不說(shuō)了。主要是更新操作,更新操作要注意的是更新應(yīng)該是先減去舊值,在加上新值。
以上就是觸發(fā)器例子的實(shí)現(xiàn)。文章寫(xiě)的不好請(qǐng)大家諒解。
相關(guān)文章
Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細(xì)圖文教程)
這篇文章主要介紹了Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細(xì)圖文教程),需要的朋友可以參考下2017-03-03
深入探討:Oracle中如何查詢正鎖表的用戶以及釋放被鎖的表的方法
本篇文章是對(duì)Oracle中查詢正鎖表的用戶以及釋放被鎖的表的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Windows Server 2012 安裝oracle11g(圖文教程)
這篇文章主要介紹了Windows Server 2012 安裝oracle11g(圖文教程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
ORACLE數(shù)據(jù)庫(kù)事務(wù)隔離級(jí)別介紹
事務(wù)隔離級(jí)別:一個(gè)事務(wù)對(duì)數(shù)據(jù)庫(kù)的修改與并行的另一個(gè)事務(wù)的隔離程度2012-07-07
PL/SQL Dev連接Oracle彈出空白提示框的解決方法分享
第一次安裝Oracle,裝在虛擬機(jī)中,用PL/SQL Dev連接遠(yuǎn)程數(shù)據(jù)庫(kù)的時(shí)候老是彈出空白提示框,網(wǎng)上找了很久,解決方法也很多,可是就是沒(méi)法解決我這種情況的。2014-08-08
Oracle中RegExp_Like正則表達(dá)式基本用法舉例
這篇文章主要給大家介紹了關(guān)于Oracle中RegExp_Like正則表達(dá)式基本用法的相關(guān)資料,REGEXP_LIKE(x,pattern)函數(shù)的功能類(lèi)似于like運(yùn)算符,用于判斷源字符串是否匹配或包含指定模式的子串,需要的朋友可以參考下2024-02-02
Oracle 通過(guò)impdp導(dǎo)入報(bào)ORA-39002 ORA-39165錯(cuò)誤的解決方案
這篇文章主要介紹了Oracle 通過(guò)impdp導(dǎo)入報(bào)ORA-39002 ORA-39165錯(cuò)誤,本文給大家分享原因分析及解決方案,需要的朋友可以參考下2023-09-09

