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

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

问题描述

有人告诉我,这是坏的设计,在数据模型中有循环。我听了这么多次,但没有多加注意。例如,您有实体User,Project,Activity。项目由用户拥有,因此我们有一个从用户到项目的一对多关系。可以将活动分配给单个用户,从用户到活动的另一个一对多关系。当然一个项目是由一组活动定义的,另一个是从项目到活动的一对多关系。因此形成了一个循环。



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



我试图搜索,但我想我没有使用正确的单词,但是这似乎对我来说应该是一个基础的人试图设计一个数据库。



那么,任何人都可以指出一些关于er / db图中循环/循环的有用信息,如果应该避免呢?


<在 noreferrer>本文



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



考虑父母有很多孩子的情况(从论文)每个孩子上学。父母与子女之间有第三种关系。学校(父母在学校有孩子)。然而:你不想明确地建模第三关系;它完全可以从其他两个派生。如果你明确捕获它,你需要确保循环总是一致的。



所以在这种情况下,你想避免循环。但是:循环不是普遍不好。再次考虑上面的例子,考虑对父母是学校的州长的情况进行建模。这也将创建一个循环。在这种情况下,虽然它是有效的:不可能从其他两个关系派生父母是校长在学校的关系。



所以在总结:不模型当一个关系完全可从其他组合导出时循环。



推荐文章,但它给我一个比我能给出的更好的描述。



hth。


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.

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

解决方案

There's a really good treatment of relationship loops in chapter 3 of this paper.

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.

hth.

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

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