在建模标题/详细信息关系时消除冗余关系?
我有一个看起来像这样的模型:
一个帐户有许多分支,每个报表都是为一个生成的帐户。该模型是多余的,因为可以从事务上的 BranchID 推断出帐户(标头上的 AccountID)(一条语句始终具有一个或多个事务)。
是否应该从 StatementHeader 中删除 AccountID,或者这种级别的冗余是否可以?或者有更好的解决方案吗?
I've got a model that looks something like this:
One Account has many Branches, and each Statement is generated for one Account. The model is redundant because the Account (the AccountID on the header) can be inferred from the BranchID on a transaction (a statement will always have one or more transactions).
Should the AccountID be removed from the StatementHeader, or is this level of redundancy OK? Or is there a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有 StatementHeader,那么它应该有 AccountID 以保持引用完整性。
但是,最好完全删除 StatementHeader 并将 StatementDate 移至 Statement 记录中。这将使事情变得更清晰,并使模型更好地描述你想要的东西。
If you have the StatementHeader then it should have the AccountID to keep the referential integrity.
However it may be better to remove the StatementHeader completely and move the StatementDate into the Statement record. This would make things cleaner and make the model better describe what you want.
由于语句是历史性的,并且通常是只读的,因此数据有一些冗余是可以的。我同意 Richard Harrison 的观点,并将 [AccountID] 和 [StatementDate] 移至 [Statement] 表中;我的理由是你说一个账户有很多分支,所以你将为一个账户生成一份报表。
将所有这些数据存储在同一个地方将减少连接并加快报告速度,我认为这就是该数据库的原因。
As a statement is historical, and usually read only, data some redundency is fine. I agree with Richard Harrison and would move both [AccountID] and [StatementDate] in to the [Statement] table; my reasoning being you say that an account has many branches so you will be genereating a statement for an Account.
Storing all this data in the same place will reduce joins and speed up reporting, which i assume is the reason for this database.
有时,(真实的或感知的)冗余是业务规则的结果。在这种情况下,业务规则是:“向帐户发出的报表应仅包含属于该特定帐户的分支机构的交易。”
为了强制执行该规则,您可以尝试提出一个数据库模式,使其不可能违反该规则,或者使用约束或触发器显式强制执行该规则。使用 StatementHeader.AccountID 似乎更容易。在 Oracle 中,您可以编写如下内容:
如果 StatementHeader 中没有 AccountID,您必须编写与共享相同 StatementID 的所有其他 Statement 中的所有其他 AccountID 的比较,从而导致更复杂的语句序列。
因此,我将 AccountID 作为 StatementHeader 中的外键,并使用触发器显式强制执行业务规则。
Sometimes, (real or perceived) redundancy is a consequence of a business rule. In this case, the business rule is: "a statement that is issued to an account shall contain only transactions for branches that belong to that particular account."
To enforce that rule, you could try to come up with a database schema that makes it impossible to violate it, or enforce it explicitly with a constraint or trigger. And that seems to be easier with StatementHeader.AccountID. In Oracle, you could write something like this:
Without AccountID in StatementHeader, you'd have to write a comparison with all the other AccountIDs from all other Statements that share the same StatementID, resulting in a more complicated sequence of statements.
So i would keep AccountID as a foreign key in StatementHeader and enforce the business rule explicitly with a trigger.