Question

For example:

  1. Table: Bankaccount, with an id as primary key and a value.
  2. Table: Category, with an id as primary key, a name and a type (boolean, 0 for positive transactions and 1 for negative)
  3. Table: Transaction, with an id as primary key, a value (for example -10000$ for shopping) and a foreign key which references Category.

My question is if it's possible to add some transactions to the Transaction table, which causes the automatic change of the value in the Bankaccount table.

I hope someone of you could help me there!

Was it helpful?

Solution

you must use triggers: an after insert trigger Something like this (syntax depends on the used dbms):

CREATE or REPLACE TRIGGER trg_transaction
AFTER INSERT
   ON ***transaction***

DECLARE
   -- variable declarations

BEGIN
   -- trigger code
   UPDATE ***bankaccount*** SET ....

END;

Hope this will help you!!!

OTHER TIPS

I think this is exactly what I was searching for, thank you very much! I just tried it on a H2-Database and don't know what I did wrong, maybe you could help me there too. My trigger looks like this:

create trigger transaction_trig_value_ai after isert of value on transaction
declare
v_value double;
v_type_category boolean;
cursor c_value IS SELECT  value from transaction where id = :NEW.idcategory;
cursor c_type_kategorie IS SELECT type from category where id = :NEW.idcategory;
begin
open c_value;
fetch c_value into v_value;
close c_value;
open c_type_category;
fetch c_type_category into v_type_category;
close c_type_category;

if v_type_category == 0 then
update bankaccount
set value = value +  v_value;
end if;
if v_type_category == 1 then
update bankaccount
set value = value - v_value;
end if;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top