Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Last active December 15, 2015 23:39
Show Gist options
  • Save telagraphic/5342210 to your computer and use it in GitHub Desktop.
Save telagraphic/5342210 to your computer and use it in GitHub Desktop.
CREATE TABLE `accounts` (
`account_id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(30) NOT NULL,
`balance` decimal(9,2) DEFAULT NULL,
PRIMARY KEY (`account_id`)
)
CREATE TABLE `transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`amount` decimal(9,2) DEFAULT NULL,
`description` varchar(200) DEFAULT NULL,
`account_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `transacts_to_accounts_fk` (`account_id`),
CONSTRAINT `transacts_to_accounts_fk` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`account_id`)
)
when I insert a new credit or debit in the transactions table
INSERT INTO transactions
VALUES ( null, now(), 1000, 'tax rebate', 1)
then i want the account balance updated
UPDATE accounts
SET balance = +1000
WHERE account_id = 1
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment