oracle trigger implements inbound and outbound

  • 2020-06-15 10:24:51
  • OfStack

In language
Benefits:
1, can reduce the access to the database.
2. Good portability.
The bad:
1, the operation to consider more things, modify 1 to modify another 1. In other words, it's interrelated. If you change less than one point, the data is likely to be distorted.
Implement with flip-flop
Benefits:
1. Frees programmers from complex interrelationships to focus on complex businesses.
The bad:
1. Poor portability.
Below I will use 1 example to achieve a simple inbound and outbound. Because there are very few fields used in the sample table. The examples here serve only as a starting point.
The data table is the incoming amount table (hereinafter referred to as the incoming table) income, the outgoing amount table (hereinafter referred to as the outgoing table) outlay, and the balance table balance
 
income{ 
id number; 
pay_amount number;( Input amount field ) 
} 
outlay{ 
id number; 
outlay_amount number;( Outbound amount field ) 
} 
balance 
{ 
id number; 
balance number;( The balance ) 
} 

The triggers are set up in the inbound and outbound tables, respectively
Library Table (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; 

Outbound list (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; 

So let me explain 1
oracle trigger, the trigger event is divided into insert, delete, update column three events, corresponding to the inserting /deleting/updating keyword
These can be implemented separately using the if statement
 
if inserting then 
----- 
elsif updating then 
----- 
elsif deleting then 
------ 
end if; 

NVL(eExpression1, eExpression2)
NVL() returns eExpression2 if eExpression1 computes an null value.
Returns eExpression1 if eExpression1 does not calculate an null value. eExpression1 and eExpression2 can be any one of the data types.
If the results of eExpression1 and eExpression2 are null values, NVL() returns.NULL.
I'm not going to say insert and delete here. It is mainly an update operation. The update operation should note that the update should subtract the old value and add the new value.
This is the implementation of the trigger example. Please understand that the article is not well written.

Related articles: