Mysql – a common way to save ‘debit’ and ‘credit’ information

database-designMySQL

I'm working on an accounting system, and for each transaction I need to save if this is either debit or credit. I can think of two ways (MySQL database):

METHOD 1

  • Amount (decimal)
  • Type (enum, debit/credit)

METHOD 2

  • Debit (decimal)
  • Credit

In the first setup, I save the type of transaction, but in the second way I rather save the amounts in the debit or credit column. Pros of this method are that I can more easily sum both debit and credit totals than in method 1. But I am wondering if there is a common way to do this?

Best Answer

Your Method 1 is correct. Here is how I implemented in an accounting system. This model is from "The Data Model Resource Book". How you store the data is very different from how accounting terminology models the double entry method. Credit can mean positive or negative depending on whether you're working on an asset or liability account. This schema will allow you to persist the data no matter what type of account you're posting to.

+----+-----------------------+------------+    
| PK | TransactionID         | int        |
+----+-----------------------+------------+
|    | Description           | varchar    |
|    | Date                  | datetime   |
+----+---------------+--------------------+

+----+-----------------------+------------+
| PK | TransactionID         | int        |
| PK | TransactionSequenceID | smallint   |
+----+-----------------------+------------+
|    | Amount                | decimal    |
|    | CreditDebitFlag       | char(1)    |
+----+---------------+--------------------+