I have a schema for a scorekeeping database with Game
, Team
, Player
tables.
One team has many players, each player has only on
The problem to your design is that you have used surrogate identifier as the primary key for the tables, a well defined primary key will solve the problem:
Team -> pk:team_id
Player -> pk:player_id
TeamPlayer -> pk:{team_id + player_id}
Game -> pk:game_id
GameTeam -> pk:{game_id + team_id}
GamePlayer -> pk:{game_id + GameTeam_pk + TeamPlayer_pk}
= {game_id + {game_id + team_id} + {team_id + player_id} }
Having check constraints on GamePlayer
will help the problem:
GamePlayer
{
Check game_id (FK of Game) = game_id (FK of GameTeam );
Check team_id (FK of GameTeam) = team_id (FK of TeamPlayer);
}
So
player_score will be property of GamePlayer
.
team_score will (may) be SUM of GamePlayer.player_score
with specific team_id.