Is this normalization correct? (two many-to-manys connected by a many-to-one)

前端 未结 2 488
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 10:17

I have a schema for a scorekeeping database with Game, Team, Player tables.

One team has many players, each player has only on

2条回答
  •  粉色の甜心
    2020-12-20 10:39

    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.

提交回复
热议问题