0

I have to maintain account balance for customer

for that I have create table customer(id,name,address,mobno,balance);

balance field is to store amount.

then for amount deposited and withdrawl I am creating table transaction transaction(id,amount,type,adddate); type is deposit/withdrawl,

adddate is timestamp and amount is deposited/withdrawl.

I am performing calculation on amount with deposited/withdrawl amount and again storing it with balance.

with data customer (1,'Ajay'.'India',23324,400);

transaction

(1,400,d);  balance 400
(2,300,w);  balance 100
(3,700,d);  balance 800
(4,200,w);  balance 600

I am facing problem if any one wants to update his transaction. (3,500,d); balance 600 //new balance then after this other data will not show proper value.

I want to know it is proper way to store amount and transaction or is there any batter way.

8
  • 1
    dont let them update a transaction? how does that work anyway? if they deposit 700, then change their mind to only have deposited 500, that's the equivalent of a 200 withdrawal, no? If they can change their mind, and change the transaction amount, the transaction isn't final and shouldnt be counted against the balance anyway
    – pala_
    Apr 8, 2015 at 7:25
  • one or few authorize user will use system. Yes with no updation this will work properly.
    – xrcwrn
    Apr 8, 2015 at 7:29
  • you could do it with an AFTER UPDATE trigger on the transaction table then.
    – pala_
    Apr 8, 2015 at 7:30
  • @pala_ but for updation I have to calculate all other transaction and then update the balance
    – xrcwrn
    Apr 8, 2015 at 7:34
  • 1
    Proper accounting practice does not allow changing anything. It only allows adding new records to 'fix' problems. Note that this always leaves a 'history' of what was done.
    – Rick James
    Apr 8, 2015 at 13:02

1 Answer 1

0

An AFTER UPDATE trigger that looks something like this could do the job:

CREATE TRIGGER balanceUpdate AFTER UPDATE on transaction
FOR EACH ROW
BEGIN
  SET @diff = NEW.amount - OLD.amount;
  IF(NEW.type = "withdrawal") THEN
    SET @diff = -@diff;
  END IF;
  UPDATE customer SET balance = balance + @diff WHERE id = NEW.customer_id;
END

This will change a little depending on your real table structure, but the idea is correct. It will compare the value before the update to the value after the update, and then adjust the balance in the correct direction by the difference.

Here's a quick demo on some test data: http://sqlfiddle.com/#!9/3a43b/2

A little more tweaking is required if the transaction type is changed from withdrawal to deposit, and vice versa but the above should be enough of a start to get you there if necessary.

1
  • @xrcwrn i've updated the trigger slightly, and also created an sqlfiddle demo you can check out and play with.
    – pala_
    Apr 8, 2015 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.