sqldatabasecreate-tableinsert-statement

Changing the value of a SQL table with inserts into an other table


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!


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!!!