数据库中的一对多关系-设计概念 [英] one to many relationship in database - design concept
问题描述
两个表之间的一对多关系应该由两个或三个表实现吗?
例如,我们应该拥有:
A one to many relationship between two tables should be implemented with two or three tables? For example should we have:
author(id,otherAttributtes)
books(id,authorid,otherAttributes)
或
author(id,otherAttributtes)
books(id,otherAttributes)
authorConnectsBooks(authorid,booksid)
我更喜欢第一种方法,但是很多时候我已经看到了第二种方法以及更复杂的应用程序。第一种方法有什么缺点,或者只是个人遵循哪种方法?
I like more the first approach but i have seen the second and in more complicated applications a lot of times. Is there any downside for the first method, or it's just personal which way to follow?
推荐答案
第一个示例显示了一个方法
The first example shows a one to many relationship, while the second shows a many to many relationship.
示例可以说我们使用第一个示例
Example lets say we use the first example
Author
AuthorID
Book
BookID
AuthorID
您如何表示Jane和Jon都写了《有趣的Stackoverflow》一书?在这个关系表中,您不能表达一个作者可以写很多书。因此,要么简写它,要么乔恩写它。如果只有其中一位写过书,则可以使用这种关系类型。但是,如果要证明两个人都写过这本书,则需要多对多关系。
How would you represent that both Jane and Jon wrote the book "Stackoverflow for fun"? In this relationship table you cannot, you have expressed that one author can write many books. So either Jane wrote it or Jon wrote it. If only one of them wrote the books you could use this relationship type. However, if you want to show that both wrote this book you need a many to many relationship.
现在,使用与Jane和Jon相同的类比,您可以代表两位作者这本书使用第二个例子-多对多关系。
让我们以Stackoverflow为例,以一对多关系开始,以多对多关系结束:
Now using this same analogy of Jane and Jon you can represent both authors to this one book using your second example - many to many relationship.
Lets use Stackoverflow as an example starting with a one to many relationship and ending with a many to many relationship:
Authors
Joel
Jeff
Books
Stackoverflow Joel
可怜的杰夫,他没有被上面例子中的stackoverflow认可...所以我们需要解决:
Poor Jeff, he is not credited with stackoverflow from the above example...so we need to fix that:
Author
Joel
Jeff
Books
Stackoverflow
AuthorBooks
Stackoverflow Jeff
Stackoverflow Joel
现在每个人都很高兴...
Now everyone's happy...
这篇关于数据库中的一对多关系-设计概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!