· 8 years ago · Jun 14, 2018, 11:46 AM
1sum of final_value column where account_id = specific value
2
3te dali ne nji tabele tjeter me balance as that sum
4
5DROP TABLE IF EXISTS `red_trans`;
6CREATE TABLE `red_trans` (
7 `red_trans_id` INT(32) PRIMARY KEY AUTO_INCREMENT,
8 `account_id` INT(32) NOT NULL,
9 `final_value` double(32,10) NOT NULL,
10 FOREIGN KEY (`account_id`) REFERENCES red_balance(`account_id`)
11 ON DELETE no action
12 ON UPDATE no action
13) ENGINE=InnoDB DEFAULT CHARSET=latin1;
14
15
16
17DROP TABLE IF EXISTS `red_balance`;
18CREATE TABLE `red_balance` (
19 `account_id` INT(32) PRIMARY KEY AUTO_INCREMENT,
20 `final_value` double(32,10) NOT NULL
21) ENGINE=InnoDB DEFAULT CHARSET=latin1;
22
23
24select * from red_trans;
25select * from red_balance;
26
27DROP TRIGGER IF EXISTs new_transaction;
28CREATE TRIGGER new_transaction AFTER INSERT ON red_trans
29FOR EACH ROW
30 UPDATE red_balance
31 SET final_value = (select sum(final_value) from red_trans a where a.account_id = NEW.account_id)
32 where account_id = NEW.account_id;