我想弄清楚如何正确存储外键,当关键点指向基于类型的不同表格时。交易目的地的在线信用系统表格布局外键
我searched 但nothing 我found seemed 到help at all。
有一个表,将存储最基础知识的学分:
user_accounting
--------------------
user_accounting_id (PK)
user_id (FK)
amount
type (+deposit, +credit, -transfer, -purchase)
credit (bool, so I don't have to always check if amount is <or> 0)
void (in case it was canceled and repaired with some other transaction)
date
details (notes)
类型 S:
deposit - for outsite money being put into the system.
credit - are for money being transfered into their account from another
transfer - for putting money into someone elses account
purchase - for purchasing a site item
到目前为止好。接下来的一部分我有点困惑。它还必须存储钱的来源或来源。
我想存储任何外键类型将表明它来自。 因此,如果是购买,它将存储invoice_id的FK,如果它是存款,它将存储来自商家提供商的transaction_id,如果它是转账,它将存储信用的user_accounting_id,如果这是信用,将存储传输的user_accounting_id。
我会是不错的一列存储的是:
user_accounting (con't)
-----------------------------
source_or_destination_id (FK)
但我知道,我不能有一列是一个外键连接到基于类型不同的表。所以我可以将它作为一个(int)存储,但它会是一个巨大的痛苦试图做与基于类型与不同的表与该id的JOIN。很久以前试图做到这一点,很大的错误。
所以不是,我能做到这一点,而不是:
user_accounting (con't)
-----------------------------
invoice_id (FK)
transaction_id (FK)
credit_user_accounting_id (FK)
transfer_user_accounting_id (FK)
但再次,一个很大的问题,因为我创造一个独特的弧形这是不好的。
我也可以对类型使用many_to_many_through关系,但数据透视表仍然存在将外键存储在多个表的相同位置的问题。
也许我可以简单地存储不同类型的交易在不同的表完全, 我可以有:
user_accounting_deposit, user_accounting_credit, user_accounting_transfer, user_accounting_purchase.
然后是外键将不会是一个问题。当然,要弄清楚任何人的帐户余额,我现在必须从一堆表格中加入和汇总。
也许有一个完全不同的更好的方式来做到这一点。我不在乎需要多少桌子。机会是我在某个地方复杂化某事。
感谢
双记录簿不处理外键。如果一个会计人员需要它的双重输入格式,只要数据以某种形式存在,我就可以对其进行处理。即使我将一个陈旧的会计系统移植到数据库中,我仍然需要指向其他位置的来源。 此外,数据库答案是我特别链接到的网站之一没有答案。我查看了他们所有的会计系统表格布局。它们太基本,不支持全面的数据参考。 – phazei