个人理财应用程序数据库设计

发布于 2024-10-18 20:49:56 字数 371 浏览 0 评论 0原文

在寻找一款简约的资金跟踪/预算应用程序后,我决定构建一个供我个人使用。

但是我不确定数据库设计的一部分。基本上目前,我有一个条目表,它显然存储有关每笔交易的数据,无论是信用还是债务等。

我面临的困境是,我不知道是否应该创建另一个表来存储每笔交易的当前余额或者我是否应该通过从贷方中减去借方来动态填充它。

我的一部分意思是,随着条目表的增长,为每个帐户生成余额的能力将会变慢(是的,过早的优化被认为是邪恶的),但当我可以从现有表计算数据时,似乎也没有必要添加另一个表。

谢谢

编辑:抱歉,我可能不清楚,我了解如何实现创建帐户余额的任一方法。我更多地关注这两种方法的优点/缺点以及什么是“最佳实践”。非常感谢您的回复!

After searching around for a minimalistic money tracking/budgeting app, I decided to build one for my own personal use.

However I'm unsure with part of the database design. Basically at the moment, I have an entries table which obviously stores data about each transaction, whether is credit or debt etc.

The dilemma that I have is, I don't know if I should create another table to store the current balance of each account or if I should populate it dynamically by subtracting the debits from the credits.

Part of me is saying that as the entries table grows the ability to generate the balance for each account will get slower (yes premature optimization is supposedly evil), but it also seems unnecessary to add another table when I can calculate the data from existing tables.

Thanks

EDIT: Sorry I may not have been clear, I understand how to implement either method of creating the account balance. I was more looking the advantages/disadvantages of either method as well as what would be the 'best practice'. Thanks very much for the replies!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温折酒 2024-10-25 20:49:56

如果我要设计一个简约的会计应用程序,我可能会这样做,

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

key 列将包含一个日期和一个以零填充的数值(即 201102230000),其中最后 4 位数字是每日交易 ID。这对于跟踪交易并返回范围等非常有用。每日交易 ID 0000 可以是当天开始(或结束)时的账户余额,ID 0001 及以上是其他交易。

trans_type 列将保存交易代码,例如“DEB”(借方)、“CRE”(贷方)、“TRA”(转账)和“BAL”(余额)等。

通过设置这样,您可以执行任何类型的查询,从获取任何给定日期之间的所有“信用”交易,到仅获取任何给定日期或日期范围内的帐户余额。

示例:获取 2011-01-012011-02-23 之间的所有贷记和借记交易

SELECT ledger.*, account.name, category.name
  FROM ledger
  JOIN account
    ON ledger.account_id = account.account_id
  JOIN category
    ON ledger.category_id = category.category_id
 WHERE (ledger.trans_type = "CRE"
     OR ledger.trans_type = "DEB")
   AND ledger.key BETWEEN 201101010000 AND 201102239999
 ORDER BY ledger.key ASC

示例:获取帐户 #12011-01-012011-02-23 之间的所有交易(余额除外)(例如:抵押贷款)

SELECT ledger.*, account.name, category.name
  FROM ledger
  JOIN account
    ON ledger.account_id = account.account_id
  JOIN category
    ON ledger.category_id = category.category_id
 WHERE ledger.trans_type <> "BAL"
   AND ledger.key BETWEEN 201101010000 AND 201102239999
   AND account.id = 1
 ORDER BY ledger.key ASC

这就是灵活性和可扩展性。

If I were to design a minimalistic accounting application, I would probably do something 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 would consist of a date and a zero-padded numeric value (i.e. 201102230000) where the last 4 digits would be the daily transaction id. This would be useful to track the transactions and return a range, etc. The daily transaction id 0000 could be the account balance at the beginning (or end) of the day, and the id 0001 and up are other transactions.

The column trans_type would hold transaction codes, such as "DEB" (debit), "CRE" (credit), "TRA" (transfer) and "BAL" (balance), etc.

With a setup like that, you can perform any kind a query, from getting all the "credit" transactions between any given date, to only the account balance at any given date, or date range.

Example: fetch all credit and debit transactions between 2011-01-01 and 2011-02-23

SELECT ledger.*, account.name, category.name
  FROM ledger
  JOIN account
    ON ledger.account_id = account.account_id
  JOIN category
    ON ledger.category_id = category.category_id
 WHERE (ledger.trans_type = "CRE"
     OR ledger.trans_type = "DEB")
   AND ledger.key BETWEEN 201101010000 AND 201102239999
 ORDER BY ledger.key ASC

Example: fetch all transactions (except balances) between 2011-01-01 and 2011-02-23 for the account #1 (ex: Mortgage)

SELECT ledger.*, account.name, category.name
  FROM ledger
  JOIN account
    ON ledger.account_id = account.account_id
  JOIN category
    ON ledger.category_id = category.category_id
 WHERE ledger.trans_type <> "BAL"
   AND ledger.key BETWEEN 201101010000 AND 201102239999
   AND account.id = 1
 ORDER BY ledger.key ASC

So there you go, flexibility and extensibility.

匿名。 2024-10-25 20:49:56

对于个人财务数据库,当今的关系数据库系统足够快,可以动态计算多个帐户的余额。您不需要列来保存当前余额。甚至 Microsoft Access 也足够快。我知道这一点是因为我在 Access 中构建并使用了个人财务数据库。它甚至可能是您最初寻找的东西。您可以在 http://maiaco.com/software/ledger/index 阅读并下载它。 php

For a personal financial database today's relational database systems are plenty fast enough to calculate the balance of multiple accounts dynamically. You don't need a column to hold the current balance. Even Microsoft Access is fast enough. I know this because I built and use a personal financial database in Access. It might even be what you were originally looking for. You can read about it and download it at http://maiaco.com/software/ledger/index.php

ぃ弥猫深巷。 2024-10-25 20:49:56

实际上,我现在正在研究这个网站的想法,我设置数据库的方式是:

TABLE account
    id
    account_name
    current_balance

TABLE transaction
    id
    account_id
    payee
    date
    amount
    category

每当添加新交易时,我都会更新帐户的当前余额。

仅供参考,我希望在一个月内启动我的网站,如果您有兴趣使用某人的网站,只需查看我的个人资料即可。

I am actually working on just this website idea right now and the way I've setup my database is:

TABLE account
    id
    account_name
    current_balance

TABLE transaction
    id
    account_id
    payee
    date
    amount
    category

And whenever a new transaction is added I update the account's current balance.

FYI, I hope to launch my site within a month and if you're interested in using person's site, just check out my profile.

往事随风而去 2024-10-25 20:49:56

我认为单个表读取会更好,并且将来可以提供更大的灵活性。您最终可以跟踪余额、贷项和借项的平均值。

I would think a single table read would be better and allow for more flexibility in the future. You could eventually track averages for balance, credits and debits.

你在看孤独的风景 2024-10-25 20:49:56

除非出于性能原因需要,否则不要将计算值存储在表中。
我会使用视图来公开计算值。

Don't store calculated values in tables unless you need to for performance reasons.
I would use a View to exposes the calculated values instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文