Designing a Double-Entry Financial Ledger Schema in SQL: Debits, Credits & Audit Trails
When business applications track customer invoices, vendor expenses, wallet top-ups, and GST tax liabilities inside simple balance = balance + amount columns, subtle bugs eventually creep in: deleted invoices leave stale totals, refunds corrupt tax reports, and finance teams cannot explain discrepancies during audits. Adopting true double-entry ledger principles solves financial drift permanently.
1. Core Entities of a Relational Accounting Schema
Every financial movement must record balanced entries inside an ACID database transaction (see SQL ACID Transaction Principles):
accounts(Chart of Accounts): Categorized into Assets, Liabilities, Equity, Revenue, and Expenses with unique account codes.journal_entries(Header): Captures transaction date, source entity reference (for example,invoice #1042), description, and creator audit ID.journal_lines(Immutable Postings): Individual debit and credit lines whereSUM(debit) == SUM(credit)must always hold true before committing the transaction.
| Business Event | Debit Account | Credit Account |
|---|---|---|
| Tax Invoice Issued (₹10,000 + ₹1,800 GST) | Accounts Receivable (₹11,800) | Software Revenue (₹10,000) + Output GST Payable (₹1,800) |
| Customer Payment Received via Gateway | Bank / Clearing Account (₹11,800) | Accounts Receivable (₹11,800) |
| Credit Note / Partial Refund | Sales Returns (Debit) + GST Output Adjustment | Accounts Receivable / Bank Refund |
2. Mandatory Engineering Rules for Financial Precision
- Never use
FLOATorDOUBLE: IEEE-754 floating-point binary rounding introduces silent penny/paise drift. Always store monetary amounts inDECIMAL(15, 2)(or integer minor units like paise/cents). - Append-only reversals instead of
DELETE: Once a journal entry is posted, prohibit updates or hard deletes; record a reversing journal entry linked viareverses_entry_idso financial audit history remains intact.
Mayur Kadam
Principal Software Architect · SaaS, Laravel & Distributed Systems
Leads software architecture, multi-tenant database design, and cloud security reviews for B2B web platforms, ERPs, and high-throughput APIs.