简体   繁体   English

SQL如何找出关系是否多对多

[英]SQL How to find out if relationship is many to many

I am trying to understand many to many relationship. 我试图了解多对多的关系。 I have read articles on this topic but I am still not getting it. 我已经阅读了有关该主题的文章,但仍然不了解。

Let's take team and player as an example. 让我们以团队和球员为例。 One team can have multiple players, but one player can only play in one team? 一队可以有多个球员,但一个球员只能在一个队中比赛? I know that it's not correct but I can not understand why. 我知道这是不正确的,但我不明白为什么。

What is the proper way to determine relationship between tables? 确定表之间关系的正确方法是什么? What questions should I ask (from side of player and from team) to find out relationships? 我应该问哪些问题(从球员和团队的角度)以找出关系?

Not sure whether my second question is clear enough. 不知道我的第二个问题是否足够清楚。

Thanks for help 感谢帮助

Perhaps the best way to look at it is to draw all teams on one side, all players on the other, and draw the relationships between them as lines. 也许最好的观察方法是在一侧画所有团队,在另一侧画所有玩家,并将它们之间的关系画成线。

If no player is (nor can be) connected to multiple teams, ie there is at most one line leaving from each player, then it has a to-one relationship with the teams. 如果没有(也不能)与多支球队建立联系,即每位球员最多只有一条线,那么它与各队之间是一对一的关系。

If teams are (or can be) connected to multiple players, ie there can be multiple lines leaving from each team joining it to multiple players, then it has a to-many relationship with the players. 如果团队被(或可以)连接到多个玩家,即每个团队可以有多条线将其连接到多个玩家,则它与玩家之间具有一对多的关系。

So typically, teams have a one-to-many relationship with players. 因此,通常情况下,团队与球员之间是一对多的关系。 One team shares many players, and players are part of only one team. 一队共享许多球员,而球员只是一队的一部分。

Here is a good many-to-many relationship example: a car type can be sold at many dealerships and a dealership can sell many car types. 这是一个很好的多对多关系示例:可以在许多经销店出售汽车类型,而经销店可以出售许多汽车类型。

The following is a good one-to-many relationship using the same objects as an example: a car type with a unique VIN# can be sold at one dealership and a dealership can sell many different car types with a unique VIN#. 以下是一个很好的一对多关系,并使用相同的对象作为示例:具有唯一VIN#的汽车类型可以在一个经销商处出售,而经销商可以出售具有唯一VIN#的许多不同的汽车类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM