database 最大基数和最小基数有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5104349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Max Cardinality and Min Cardinality?
提问by Steffan Harris
I am having a hard time understanding what is the difference between the Max and Min cardinalities when trying to design a database.
在尝试设计数据库时,我很难理解 Max 和 Min 基数之间的区别。
回答by Nix
Remember cardinality is always a relationship to another thing.
记住基数总是与另一件事的关系。
Max Cardinality(Cardinality)Always 1 or Many. Class A has a relationship to Package B with cardinality of one, that means at most there can be one occurance of this class in the package. The opposite could be a Package has a Max Cardnality of N, which would mean there can be N number of classes
Max Cardinality(Cardinality)始终为 1 或多。类 A 与包 B 的关系为基数为 1,这意味着该类最多只能在包中出现一次。相反可能是一个包的最大 Cardnality 为 N,这意味着可以有 N 个类
Min Cardinality(Optionality)Simply means "required." Its always 0 or 1. 0 would mean 0 or more, 1 ore more
Min Cardinality(Optionality) 仅表示“必需”。它总是 0 或 1。0 表示 0 或更多,1 或更多
There are tons of good articles out there that explain this, including some that explain how to even property "diagram". Another thing you can search for is Cardinality/Optionality (OMG Terms) which explains the same thing, Optionality is "Min" Cardinality is "Max",
有很多很好的文章可以解释这一点,包括一些解释如何甚至属性“图表”的文章。您可以搜索的另一件事是 Cardinality/Optionality(OMG 术语),它解释了同样的事情,Optionality 是“Min” Cardinality 是“Max”,
From http://www.databasecentral.info/FAQ.htm
来自http://www.databasecentral.info/FAQ.htm
Q: I can see how maximum cardinality is used when creating relationships between data tables. However, I don't see how minimal cardinality applies to database design. What am I missing?
A: You are correct in noticing that maximum cardinality is a more important characteristic of a relationship than minimum cardinality is. All minimum cardinality tells you is the minimum allowed number of rows a table must have in order for the relationship to be meaningful. For example, a basketball TEAM must have at least five PLAYERS, or it is not a basketball team. Thus the minimum cardinality on the PLAYER side is five and the minimum cardinality on the TEAM side is one.
One can argue that a person cannot be a player unless she is on a team, and thus the minimum cardinality of TEAM is mandatory. Similarly an organization cannot be a basketball team unless it has at least five players. The minimum cardinality of PLAYERS is mandatory also. One could argue in the opposite direction too. When a player quits a team, does it cease to be a team until a replacement is recruited? It cannot engage in any games, but does it cease to be a team? This is an example of the fact that each individual situation must be evaluated on its own terms. What is truth in THIS particular instance? The next time a similar situation arises, the decision might be different, due to different circumstances.
问:我可以看到在数据表之间创建关系时如何使用最大基数。但是,我没有看到最小基数如何应用于数据库设计。我错过了什么?
答:您注意到最大基数是比最小基数更重要的关系特征是正确的。所有最小基数都告诉您表必须具有的最小行数才能使关系有意义。例如,一个篮球队必须至少有五名球员,否则就不是篮球队。因此,PLAYER 端的最小基数是 5,TEAM 端的最小基数是 1。
有人可能会争辩说,除非她在团队中,否则一个人不能成为一名球员,因此 TEAM 的最低基数是强制性的。同样,一个组织不能是一支篮球队,除非它至少有五名球员。PLAYERS 的最小基数也是强制性的。也可以反其道而行之。当一名球员退出球队时,在招募替代者之前,它是否不再是一支球队?它不能参与任何游戏,但它是否不再是一个团队?这是一个事实,即必须根据自己的条件评估每种情况。在这个特定的例子中什么是真理?下次出现类似情况时,由于情况不同,决定可能会有所不同。
回答by sfinnie
Agree with other answers, here's a slightly different view. Think in terms of optionalityand multiplicity. Take an example: Person
has Address
.
同意其他答案,这里的观点略有不同。考虑可选性和多样性。举个例子:Person
有Address
。
Optionality asks: Does every Person
need to have an Address
? If so the relationship is unconditional - which means minimum cardinality is 1. If not, then min cardinality is 0.
可选性要求:是否每个人都Person
需要有一个Address
? 如果是,则关系是无条件的 - 这意味着最小基数为 1。如果不是,则最小基数为 0。
Multiplicity asks: Can any given Person
have more than one Address
? If not, the maximum cardinality is 1. If so the maximum cardinality is >1. In most cases it's unbounded, usually denoted N
or *
.
多重性问:任何给定的Person
都可以有多个Address
吗?如果不是,则最大基数为 1。如果是,则最大基数 >1。在大多数情况下,它是无界的,通常表示为N
或*
。
Both are important. Non-optional associations make for simpler code since there's no need to test for existence before de-referencing: e.g.
两者都很重要。非可选关联使代码更简单,因为在取消引用之前不需要测试是否存在:例如
a=person.address()
instead of
代替
if (person.address !=null) {
a=person.address()
}
Addresses are a good example of why Multiplicity is important. Too many business applications assume each person has exactly one address - and so can't cope when people have e.g. holiday homes.
地址是一个很好的例子,说明为什么多重性很重要。太多的业务应用程序假设每个人只有一个地址——因此当人们拥有度假屋等时就无法应对。
It is possible to further constrain the cardinality, e.g. a car engine has between 2 and 12 cyclinders. However those constraints are often not very stable (Bugatti now offers a 16 cylinder engine). So the important questions are optionality and multiplicity.
可以进一步限制基数,例如汽车发动机具有 2 到 12 个气缸。然而,这些限制通常不是很稳定(布加迪现在提供 16 缸发动机)。所以重要的问题是可选性和多样性。
hth.
嗯。
回答by MIS Graduate
Maximum Cardinality:
1 to 1, 1 to many, many to many, many to 1
最大基数:
1对1、1对多、多对多、多对1
Minimum Cardinality:
Optional to Mandatory, Optional to Optional, Mandatory to Optional, Mandatory to Mandatory
最小基数:
可选到强制、可选到可选、强制到可选、强制到强制
回答by Tony Puthenveettil
To your question, 'what is the use of optionality in database design?': It becomes very helpful in the scenarios like the following.
对于您的问题,“可选性在数据库设计中的用途是什么?”:它在以下场景中变得非常有用。
When you design 2 tables with 1-to-1 relation, you will be confused to decide where (in which table) to have the foreign key. It's very easy to decide it, if you have optionality 1 for one table and 0 for the other table. The foreign key should be present in the former. There are many other uses for it as well.
当您设计 2 个具有 1 对 1 关系的表时,您会困惑于决定在哪里(在哪个表中)有外键。决定它很容易,如果你有一个表的可选性 1 和另一个表的可选性 0。外键应该存在于前者中。它还有许多其他用途。
Hope it helps.
希望能帮助到你。
回答by akshat sinha
回答by Mahesh
Let's work with an example -
让我们举个例子——
Students
takes Class
. Here both Students
and Class
are entities.A School may or may not have students enrolled in a particular semester. Think of a school offering courses in summer semester but no student is interested to join in. So, student's cardinality can be (0,N). But if a Class
is going on means, it should have at least 1 student registered. So, its cardinality should be (1,N). So, you should check whether the entity participating in the relation is partialor total, which decides it's cardinality in the relation.
Students
需要Class
. 在这里,Students
和Class
都是实体。学校可能有也可能没有学生在特定学期注册。想想一所学校在夏季学期开设课程,但没有学生有兴趣加入。因此,学生的基数可以是(0,N)。但如果Class
是一个意思,它应该至少有1名学生注册。所以,它的基数应该是(1,N)。因此,您应该检查参与关系的实体是partial还是total,这决定了它在关系中的基数。
Hope it helps.
希望能帮助到你。