MySQL Double Entry Accounting System Database Design?

Jignesh Manek picture Jignesh Manek · Nov 19, 2011 · Viewed 8.3k times · Source

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??

Answer

ruakh picture ruakh · Nov 19, 2011

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).