How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

huangapple go评论61阅读模式
英文:

How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

问题

I'm here to provide the Chinese translation of the text you provided. Here it is:

我正在尝试根据他们表中的ID键查询与他们自身关系(作为玩家与玩家玩)的比赛表中的两个外键的玩家名称。

我正在使用MS Access,表格和字段如下:

玩家

玩家ID(主键)

玩家姓名

比赛

比赛ID(主键)

玩家姓名ID(从玩家ID复合外键)

对手姓名ID(从玩家ID复合外键)

我只添加了比赛ID字段,因为拥有一个ID以标识比赛非常重要,但真正的标识是两个不同玩家的组合,这意味着玩家不能与自己比赛,因为他们是每个玩家与其他所有人对战。

这两个表中都有注册的玩家。

我不确定根据外键在查询中将比赛ID与玩家姓名ID和对手姓名ID字段一起提取的SQL指令应该是什么。

为了在MS Access中在表格之间建立自关联,我找到的方法是打开同一张玩家表两次,并建立以下关系:

建立的自关联

正如你上面所看到的,表格玩家被镜像打开为玩家_1。我不得不以这种方式建立自关联,因为每当我尝试从玩家ID到比赛表中的两个字段建立两个关系作为外键时,Access在进行第二个关系时会取消一个关系,因此我也不完全确定这是否正确。然而,在查询中,两个关系显示为它们应该的方式,如下所示:

查询中的自关联

在进行了大量关于SQL指令的研究后,我发现了一种称为自引用的东西,似乎在这里是适用的,但我找到的只在同一表中使用。但根据我所学,似乎我应该使用两个不同的别名两次地址MATCH表,以便它以与对手姓名ID不同的方式从玩家姓名ID中提取玩家姓名。

所以我想SQL指令应该看起来像这样:

SELECT Match.Match_ID, p.Player_name, o.Player_name FROM Match p INNER JOIN Match o ON (Player.Player_ID = Match.Player_name_ID) AND (Match.Opponent_name_ID = Player.Player_ID);

经过多次试验后,我得到的结果要么是查询正确,但是除了按预期显示玩家名称外,我只能看到他们的ID号码,要么我只能获取玩家与自己对战的数据行,这是不允许的。

我在查询中想要的结果是这样的:

预期的查询表

有人能告诉我我是否正确地在Access中建立了自关联,以及我寻求的结果的正确SQL指令是什么吗?提前感谢。

英文:

I'm trying to query the player names according to their ID keys in their table from the two foreign keys in the match table of their self-relationship (as player plays with player).

I'm using MS Access, and the tables and fields are these:

PLAYER

player_id (PK)

player_name

MATCH

match_id (PK)

player_name_id (COMPOSITE FK from player_id)

opponent_name_id (COMPOSITE FK from player_id)

I added the match_id field only because it's important to have an id to identity the match, but the real identification is the combination of two different players, meaning a player mustn't play vs themselves, as it's each player vs everyone else.

There are players registered in the fields of both tables.

I'm not sure what the SQL instruction should be in order to bring the match_id along with the respective player names in the player_name_id and opponent_name_id fields in a query according to their foreign keys.

In order to establish the self-relationship in MS Access between the tables, the way I found was to open the same PLAYER table twice and establish the following relationships:

self-relationship established

As you can see above, the table PLAYER was mirror-opened as PLAYER_1. I had to establish the self-relationship this way because whenever I try to establish two relationships coming from player_id to two fields in the MATCH table as foreign keys, Access undoes one relationship when doing the second, so I'm also not completely sure this is correct. In the query, however, the two relationships are shown as they should as you can see below:

self-relationship in the query

After doing a lot of research for the SQL instruction, I found something called self-referencing, which seems to be the case here, but the ones I found were only used in a same table. But according to what I learned, it seems that I should address the MATCH table twice using two different aliases so that it brings the player name from the player_name_id differently than the opponent_name_id.

So I imagine the SQL instruction should look something like this:

SELECT Match.Match_ID, p.Player_name, o.Player_name FROM Match p INNER JOIN Match o ON (Player.Player_ID = Match.Player_name_ID) AND (Match.Opponent_name_ID = Player.Player_ID);

The results I got after much trial and error was that I'd either get the query right, except that instead of showing the player names as intended, I'd only see their ID numbers or I'd get only the rows of data which had players playing vs themselves, which shouldn't be allowed.

What I wanted in my query was a result like this:

expected query table

Could someone tell me if I made the self-relationship in Access properly and what the right SQL instruction is for the result I'm seeking? Thanks in advance.

答案1

得分: 2

是的,这是一个自连接,并且如果您的SQL技能足够强,您可以打开设计师的SQL窗格并编写它。关键在于让设计师编写它:

我使用关系工具正确设置了关系,但当我打开查询设计师时,出现了以下情况:

How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

无论您的查询设计师显示什么,都可以通过将玩家表拖动到设计表面的第二次并将第二个表格连接起来来设置查询,如下所示:
How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

'生成的SQL
SELECT Matches.MatchID, Players.PlayerName, Matches.PlayerID, Matches.OpponentID, Players_1.PlayerName
FROM Players AS Players_1 INNER JOIN (Players INNER JOIN Matches ON Players.PlayerID = Matches.PlayerID) ON Players_1.PlayerID = Matches.OpponentID;
'输出
----------------------------------------------------------------------------------------------------------
|      MatchID       | Players.PlayerName |      PlayerID      |     OpponentID     | Players_1.PlayerNa |
----------------------------------------------------------------------------------------------------------
|                  1 | Marcos             |                  1 |                  5 | Daniel             |
----------------------------------------------------------------------------------------------------------
|                  2 | John               |                  2 |                  4 | Carlos             |
----------------------------------------------------------------------------------------------------------
|                  3 | Jane               |                  3 |                  6 | George             |
----------------------------------------------------------------------------------------------------------
|                  4 | John               |                  2 |                  5 | Daniel             |
----------------------------------------------------------------------------------------------------------
|                  5 | Carlos             |                  4 |                  6 | George             |
----------------------------------------------------------------------------------------------------------

备注:我无需完成Players_1的关系设置。对于这些类型的查询,编辑SQL通常比调整设计师更容易,但如果查询复杂,请小心。当在设计师和SQL窗格之间切换时,Access会重新编写查询,如果查询复杂,可能会导致翻译出现问题。

英文:

Yes this is a self Join and you can open the sql pane of the designer and write it if your sql skills are up to the task. The trick is getting the designer to write it:

I set up the relationship properly with the relationships tool but when I opened the query designer I got:

How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

Whatever your Query designer shows set up the query to dupe the players table by dragging the players table onto the design surface a second time and hooking the second table up like so:
How is a many to many self-relationship in data modeling represented in database tables and how is the query SQL instruction?

'resulting sql
SELECT Matches.MatchID, Players.PlayerName, Matches.PlayerID, Matches.OpponentID, Players_1.PlayerName
FROM Players AS Players_1 INNER JOIN (Players INNER JOIN Matches ON Players.PlayerID = Matches.PlayerID) ON Players_1.PlayerID = Matches.OpponentID;
'output
----------------------------------------------------------------------------------------------------------
|      MatchID       | Players.PlayerName |      PlayerID      |     OpponentID     | Players_1.PlayerNa |
----------------------------------------------------------------------------------------------------------
|                  1 | Marcos             |                  1 |                  5 | Daniel             |
----------------------------------------------------------------------------------------------------------
|                  2 | John               |                  2 |                  4 | Carlos             |
----------------------------------------------------------------------------------------------------------
|                  3 | Jane               |                  3 |                  6 | George             |
----------------------------------------------------------------------------------------------------------
|                  4 | John               |                  2 |                  5 | Daniel             |
----------------------------------------------------------------------------------------------------------
|                  5 | Carlos             |                  4 |                  6 | George             |
----------------------------------------------------------------------------------------------------------

Notes:There was no need for me to finish setting up the relationship for Players_1. Also it is often easier to edit the sql than to adjust the designer for these types of queries but if the query is complicated beware. Access rewrites the query when it switches between the designer and sql panes and it can screw up the translation when the query is complicated

huangapple
  • 本文由 发表于 2023年6月8日 05:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定