为什么在为数据库设计关系时应该避免循环? [英] Why should I avoid loops when designing relationships for a database?

查看:21
本文介绍了为什么在为数据库设计关系时应该避免循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我在数据模型中有循环是糟糕的设计.我之前听说过几次,但没有太注意.例如,您有实体用户、项目、活动.一个项目归一个用户所有,所以我们有一个从用户到项目的一对多关系.一个活动可以分配给一个用户,另一个从用户到活动的一对多关系.当然,一个项目是由一组活动定义的,另一个是从项目到活动的一对多关系.这样就形成了一个循环.

Someone told me that it was bad design to have loops in the datamodel. I have heard this before a couple of times but didn't pay much attention. For example you have entities User, Project, Activity. A project is owned by a User, so we have a one-to-many relationship from user to Project. An activity can be assigned to a single User, another one-to-many relationship from User to Activity. Of course a project is defined by a set of activities, another one-to-many relationship from Project to Activity. Thus a loop is formed.

我问这个人为什么设计不好,但他告诉我他也不知道,他也被告知,猴子学习是最好的.

I asked this guy why is it bad design but he told me he didn't know either, he was told so too, monkey learning at it's best.

我尝试搜索,但我想我没有使用正确的词,但是在我看来,这对于尝试设计数据库的人来说应该是基础.

I tried searching but I guess I didn't use the proper words, however this seems to me something that should be fundamental for someone trying to design a DB.

那么,谁能告诉我一些关于 er/db 图中循环/循环的有用信息,应该避免它们吗?

So, can anyone point me to some useful info about loops/cycles in er/db diagrams, should they be avoided?

推荐答案

这篇论文(archive.org).

然而,一般来说,循环最常见的问题是冗余信息的一致性.

Generally however, the most common issue with loops is consistency of redundant information.

考虑(来自论文)父母有很多孩子的情况;每个孩子都上一所学校.父级与父级之间存在第三种关系.学校('父母有孩子在学校').但是:您不想明确地对第三个关系建模;它完全可以从其他两个派生出来.如果您确实明确捕获了它,则需要确保循环始终一致.

Consider the case (from the paper) where a parent has many children; each child attends a school. There is a third relationship between parent & school ('parent has child at school'). However: you don't want to model the 3rd relationships explicitly; it's completely derivable from the other two. If you did capture it explicitly, you'd need to ensure the loop was always consistent.

因此,在这种情况下,您需要避免循环.然而:循环并不是普遍坏的.再次以上面的例子为例,考虑对父母是学校校长的情况进行建模.这也将创建一个循环.在这种情况下,虽然它是有效的:不可能从其他两种关系中推导出父母是学校的管理者"关系.

So in that case you'd want to avoid the loop. However: loops are not universally bad. Taking the above example again, consider modelling the case where a parent is a governor at a school. That would also create a loop. In this case though it's valid: it's not possible to derive the 'parent is governor at school' relationship from the other two relationships.

总而言之:当一种关系完全可以从其他关系的组合中推导出来时,不要对循环进行建模.但是当它们不可推导时创建循环是可以的.

So in summary: don't model loops when one relationship is completely derivable from the others combined. But it's OK to create loops when they're not derivable.

不过还是推荐这篇论文;它给出的描述比我在这里给出的要好得多.

Would recommend the paper though; it gives a much better description than I can give here.

这篇关于为什么在为数据库设计关系时应该避免循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆