簡體   English   中英

如何為帶有傳遞依賴關系的關聯建模?

[英]How to model an association with a transitive dependency?

我迷失了與傳遞依賴關系的建模模型。 這是相應的ERM:

ERM

一場比賽有1個主隊和1個客隊(為簡單起見,請堅持選擇主隊)。 因此,我將在比賽中包含team_id屬性。

現在,我如何確保比賽中維持的球隊參加比賽的聯賽? 我願意介紹聯接表,但看不到如何保留有關傳遞依賴項的信息“團隊-> 參加比賽->比賽-> 加入 ->聯賽-> 與參與 ->團隊”

我假設兩支球隊都必須參加比賽。 該解決方案涉及重疊約束和重疊外鍵引用。

您顯示團隊與聯盟之間的關系。 比賽將參考此表。

create table team_leagues (
  team_id ...,
  league_id ...,
  other_columns ...,
  primary key (team_id, league_id),
  foreign key (team_id) references teams (team_id),
  foreign key (league_id) references leagues (league_id)
);

聯賽和比賽之間存在1:n的關系。 這里有重疊的約束。

create table matches (
  home_team_id ..., 
  guest_team_id ...,
  match_start_time ...,
  league_id ...,
  primary key (home_team_id, match_start_time),
  unique (guest_team_id, match_start_time),
  foreign key (home_team_id, league_id) 
    references team_leagues (team_id, league_id),
  foreign key (guest_team_id, league_id)
    references team_leagues (team_id, league_id)
);

matches.league_id是否還需要對聯賽的外鍵引用取決於應用程序。 不過,我認為參考team_leagues就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM