Question

I am lost at modeling an association with a transitive dependency. Here is the corresponding ERM:

ERM

A match has one home team and one guest team (let's stick to the home team for simplicity). I would therefore include a team_id attribute in the match.

Now, how can I make sure that the team maintained in the match participates in the match's league? I'm open to introducing join tables, but do not see how to keep the information about the transitive dependency "team -> plays in -> match -> being part of -> league -> with participating -> team"

Was it helpful?

Solution

I'm assuming both teams have to be in the match's league. The solution involves overlapping constraints and overlapping foreign key references.

You show a m:n relationship between teams and leagues. Matches will reference this table.

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)
);

And there's a 1:n relationship between leagues and matches. There are overlapping constraints here.

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)
);

Whether matches.league_id also needs a foreign key reference to leagues is application-dependent. I think it should be sufficient to reference team_leagues, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top