简体   繁体   English

会计数据库 - 存储交易

[英]Accounting Database - storing a transaction

You make a gaming website where the user can buy gaming credits and the funds are deposited/credited into the user's virtual account to play some game etc...etc.. 您制作了一个游戏网站,用户可以在其中购买游戏积分,并将资金存入/存入用户的虚拟账户以进行游戏等等。

1 1

If you got an accountant to record the transaction, it would be recorded like this (maybe a bit more complex but you get the point) 如果你有一个会计师来记录交易,它将被记录为这样(可能有点复杂,但你明白了)

TRANSACTION
PK_ID1 Cash      - $10 (System)
PK_ID2 Deposit        $10 (System)

TRANSACTION
PK_ID3 Bank Account      - $10 (John)
PK_ID4 Deposit        $10 (John)

2 2

As a developer, do you really need to waste 2 extra records? 作为开发人员,您真的需要浪费2条额外的记录吗? why not just record it like this…(then you might store information where the funds came from, status in other columns under the same deposit record) 为什么不这样记录呢...(那么你可以存储资金来自哪里,在同一存款记录下的其他栏目中的状态)

TRANSACTION
PK_ID1 Cash      - $10 (system)
PK_ID2 Deposit        $10 (John)

Is there any real advantage of option #1 over option #2 and vice visa? 选项#1优于选项#2和副签证是否有任何真正的优势?

EDIT: modified question, removed CR, DR and replaced with a sign. 编辑:修改过的问题,删除了CR,DR并用符号替换。

(Answering your question, but also responding some points raised in paxdiablo's answer.) (回答你的问题,但也回应了paxdiablo的答案中提出的一些观点。)

It is nothing to do with the accountant looking inside your database. 这与查看数据库内部的会计师无关。 With Double entry, errors are easy to trace; 使用Double入口,错误很容易追踪; it is an Accounting and IRS requirement, so really, you do not have a choice, you need double entry for any system that deals with public funds. 这是一个会计国税局的要求,所以实际上,你没有选择,你需要双重进入任何处理公共资金的系统。

  • (Please do not try to tell me what "double entry" is; I have written double entry systems for banks, to Audit requirements.) Double entry is an accounting method, based on a set of accounts. (请不要试图告诉我“双重录入”是什么;我为银行编写了双重录入系统,以审计要求。)双重录入是一种基于一组帐户的会计方法。 Every financial transaction is Journal Entry; 每笔金融交易都是期刊报名; if all the transactions were re-applied from the beginning, all the accounts would at their exact same balance as they are today. 如果从头开始重新应用所有交易,那么所有账户将达到与今天完全相同的余额。

  • Double Entry means every transaction has a "To" and a "From" account; 双重输入意味着每笔交易都有“收件人”和“来自”帐户; money never leaves the system or enters the system. 金钱永远不会离开系统或进入系统。 Every Credit has a Debit attached to it. 每个信用卡都附有借方。

  • Therefore (1) is not the "double entry" version of (2), they cannot be readily compared. 因此(1)不是(2)的“复式”版本,它们不能轻易比较。 The double entry version of John's transaction is (one financial transaction), in logical accounting terms: John的交易的双重登记版本是(一个金融交易),采用逻辑会计术语:

    • From: JohnAccount To: SystemAccount Amount: 10.00 (dollars) 从: JohnAccount到: SystemAccount金额: 10.00 (美元)

    • That may well be two rows in a table, one a credit and the other a debit, the two inserts wrapped in an SQL Transaction. 这可能是表中的两行,一个是信用卡,另一个是借记,两个插入包装在SQL事务中。

  • That is it for the Accounting system, which is internal, and deals with money. 这就是会计系统,它是内部的,处理货币。 We are done. 我们完了。

  • But you are additionally marrying the accounting system to a purchase/sale system (without having explicitly declared it). 但是,您还要将会计系统与购买/销售系统结合(未明确声明)。 Of course for the ten bucks you took from John, you need to give him whatever he purchased for it, and record that. 当然,对于你从约翰那里拿走的十块钱,你需要给他任何他为它买的东西,并记录下来。 John bought ten bucks worth of gaming credits, if you are tracking that, then yes, you also need: 约翰购买了价值10美元的游戏积分,如果你跟踪它,那么是的,你还需要:

    • From: SystemGamingAccount To: JohnGamingAccount Amount: 100 (credits) 发件人: SystemGamingAccount收件人: JohnGamingAccount金额: 100 (学分)
      or,expressed in dollars: 或者,以美元表示:
    • From: SystemGamingAccount To: JohnGamingAccount Amount: 10.00 (dollars) 从: SystemGamingAccount到: JohnGamingAccount金额: 10.00 (美元)

    • That, too, may well be two rows in a table, one a credit and the other a debit, the four inserts wrapped in an SQL Transaction. 这也可能是表中的两行,一个是信用证,另一个是借记,四个插入包装在SQL事务中。

  • To be clear, if you were selling widgets instead of gaming credits, the second (widget tracking) transaction would be: 要明确的是,如果您销售小部件而不是游戏积分,则第二个(小部件跟踪)交易将是:

    • From: Warehouse To: PublicSale Amount: 1 (widgets) 从: Warehouse到: PublicSale金额: 1 (小部件)

    • and since you are tracking Units in the warehouse but not how many widgets John Q Public has in his pocket, that is two inserts plus one update ( UPDATE Part SET QtInStock = QtyInStock - 1 WHERE PartCode = "Widget" ), all wrapped in a SQL transaction. 因为你正在跟踪仓库中的单位而不是John Q Public在他口袋里有多少小部件,那就是两个插件加上一个更新( UPDATE Part SET QtInStock = QtyInStock - 1 WHERE PartCode = "Widget" ),所有这些都包含在SQL事务。

And there IS an Account for each user, right. 对于每个用户都有一个帐户,对吧。 Virtual, esoteric or physical, it is a Legal Entity, against which transactions are made. 虚拟的,深奥的或实体的,它是一个法律实体,对其进行交易。 So let's not pretend it does not exist because it is virtual. 所以我们不要假装它不存在,因为它是虚拟的。 For gaming, one dollar Account plus one gaming (credit) Account. 对于游戏,一美元账户加一个游戏(信用)账户。

Credit/Debit 信用卡/借记卡

I would put the CR/DB back in; 我会把CR / DB放回去; not CHAR (2), but boolean. 不是CHAR(2),而是布尔值。 It will help you later when the table is large, 当桌子很大时,它会帮助你

    WHERE IsCredit = 1  

is much faster than 比...快得多

    WHERE Amount >= 0.

Note that with ">=" you have to ensure that every code segment is coded the same way, not ">" sometimes. 请注意,使用“> =”,您必须确保每个代码段的编码方式相同,有时不是“>”。 Boolean or char does not have that problem. 布尔值或char没有这个问题。

In terms of the data (which is what you're asking), no. 就数据而言(这就是你所要求的),没有。 You should store it as a signed value. 您应该将其存储为签名值。 Double-entry bookkeeping is not something the mob does so it can hide the real profits from the IRS :-) 复式记账不是暴徒所做的,所以它可以隐藏美国国税局的实际利润:-)

It means transaction have to be balanced (value is never created or destroyed, just transformed). 这意味着必须平衡事务(永远不会创建或销毁值,只是转换)。 And it'll be a lot easier to balance transactions (and the books) if you just store them in one column with a sign. 如果你只是将它们存储在带有符号的一列中,那么平衡交易(和账簿)就会容易得多。

In terms of visual presentation, some accountant may like them in separate columns but the vast majority will generate reports with the "negatives" simply indicated differently (such as surrounding them with parentheses). 在视觉呈现方面,一些会计师可能会在不同的栏目中喜欢他们,但绝大多数会产生报告,其中“负面”只是用不同的方式表示(例如用括号括起来)。

It may well be that (like many other accounting things), the dual columns are carried forward from many moons ago. 很可能是(像许多其他会计事项一样),双列从许多月前开始。 It would be easier to add up two columns then subtract the negative total from the positive total to get the current position (as opposed to adding and subtracting in a intermixed fashion). 将两列相加然后从正总数中减去负总数以获得当前位置会更容易(而不是以混合方式添加和减去)。 But that's supposition on my part. 但这是我的假设。

See also here . 另见这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM