MySQL Double Entry Accounting System Database Design?
-
04-03-2021 - |
Question
i am going to create a database for double-entry accounting system in MySQL.
i recently read the article: http://homepages.tcp.co.uk/~m-wigley/gc_wp_ded.html
i found in this article that it would be convenient to have three tables ACCOUNT, JOURNAL and POSTING:
ACCOUNT(accountID, accountName)
JOURNAL(journalID, journalType)
POSTING(postingID, journalID, accountID, amount)
the article described that if an account is debited, value of the 'amount' field will be positive, else it will be negative.
now for the POSTING table above, i have other two options as well..
(1) POSTING(postingID, journalID, accountID, isDr, amount)
in this option, 'isDr' field is a boolean. if the account is debited, isDr field will contain value "true", otherwise "false". and the 'amount' field will always have a positive value.
(2) POSTING(postingID, journalID, accountID, debitAmount, creditAmount)
here, if the account is debited, i will store amount in 'debitAmount' field, else i will store it in 'creditAmount' field.
so, exactly which option from above three is better to use??
Solution
This is subjective, but I think option #0 (the one where a single amount
field is either positive or negative) is best. Option #1 will cause you a lot of pain whenever you want to do math that includes both credits and debits, since you'll have to consult the isDr
field to determine whether to multiply by -1
. Option #2 is not well-normalized, since the expressions debitAmount IS NULL
and creditAmount IS NOT NULL
will be equivalent (and if they ever differ, then your database is in an inconsistent state).
OTHER TIPS
Option #0 has a draw back which I recently faced with:
There are some cases which require the amount written/recorded and also seen as minus(-) on the credit side on the ledgers, instead of showing its amount as positive(+) at the debit side. (also vice versa)
Option #0 fails in that (if not flagged as debit or credit, or not solved in some another way)
You need to ask this your accountant. (if any minus(-) amount needs to be recored and seen on the credit side (and vice versa).
I recommend option #1. Option #2 will resultl in a lot of unused fields (50% of the total quantity of debitAmount and creditAmount fields). Also, Option #1 allows you to easily derive current balances.
Lastly (or perhaps, firstly), option #1 adheres to proper normalization.