Personal finance app database design -
after searching around minimalistic money tracking/budgeting app, decided build 1 own personal use.
however i'm unsure part of database design. @ moment, have entries table stores data each transaction, whether credit or debt etc.
the dilemma have is, don't know if should create table store current balance of each account or if should populate dynamically subtracting debits credits.
part of me saying entries table grows ability generate balance each account slower (yes premature optimization supposedly evil), seems unnecessary add table when can calculate data existing tables.
thanks
edit: sorry may not have been clear, understand how implement either method of creating account balance. more looking advantages/disadvantages of either method 'best practice'. replies!
if design minimalistic accounting application, like
ledger ------------- key int(12) primary key account_id int(10) category_id int(10) trans_type char(3) amount numeric(10,2) account ------------ account_id int(10) primary key created datetime name varchar(32) ... category ------------ category_id int(10) name varchar(32) ...
the column key
consist of date , zero-padded numeric value (i.e. 201102230000
) last 4 digits daily transaction id. useful track transactions , return range, etc. daily transaction id 0000
account balance @ beginning (or end) of day, , id 0001
, other transactions.
the column trans_type
hold transaction codes, such "deb" (debit), "cre" (credit), "tra" (transfer) , "bal" (balance), etc.
with setup that, can perform kind query, getting "credit" transactions between given date, account balance @ given date, or date range.
example: fetch credit , debit transactions between 2011-01-01
, 2011-02-23
select ledger.*, account.name, category.name ledger join account on ledger.account_id = account.account_id join category on ledger.category_id = category.category_id (ledger.trans_type = "cre" or ledger.trans_type = "deb") , ledger.key between 201101010000 , 201102239999 order ledger.key asc
example: fetch transactions (except balances) between 2011-01-01
, 2011-02-23
account #1
(ex: mortgage)
select ledger.*, account.name, category.name ledger join account on ledger.account_id = account.account_id join category on ledger.category_id = category.category_id ledger.trans_type <> "bal" , ledger.key between 201101010000 , 201102239999 , account.id = 1 order ledger.key asc
so there go, flexibility , extensibility.
Comments
Post a Comment