http://dev.mysql.com/doc/refman/5.7/en/create-table.html says in part:
The DEFAULT clause specifies a default value for a column. With one
exception, the default value must be a constant; it cannot be a
function or an expression. This means, for example, that you cannot
set the default for a date column to be the value of a function such
as NOW() or CURRENT_DATE. The exception is that you can specify
CURRENT_TIMESTAMP as the default for a TIMESTAMP or DATETIME column.
(emphasis mine)
So you can't set use the DEFAULT
syntax to define a column as the result of a subquery.
As an alternative, you could create triggers on the transactions table you want to monitor, so that any change to the transactions updates your balance column.
You should create a trigger for each action: insert, delete, and update.
CREATE TRIGGER trx_ins_balance ON transactions AFTER INSERT
FOR EACH ROW
UPDATE accounts SET accounts.balance = accounts.balance + NEW.amount
WHERE accounts.accountid = NEW.accountid;
CREATE TRIGGER trx_del_balance ON transactions AFTER DELETE
FOR EACH ROW
UPDATE accounts SET accounts.balance = accounts.balance - OLD.amount
WHERE accounts.accountid = NEW.accountid;
CREATE TRIGGER trx_upd_balance ON transactions AFTER UPDATE
FOR EACH ROW
UPDATE accounts SET accounts.balance = accounts.balance - OLD.amount + NEW.amount
WHERE accounts.accountid = NEW.accountid;
The above is just an example. Don't ask me for a more tailored example, because you shouldn't be using code verbatim from Stack Overflow anyway. You should understand how to write your own triggers if you're going to use them.
Another solution is to create a VIEW so that your balance column is actually an expression to read the transactions table. But this won't be pre-calculated. It will re-query the transactions every time you query the view, which might be an expensive solution.