代码之家  ›  专栏  ›  技术社区  ›  Tim Sullivan

一对一体育数据库设计

  •  1
  • Tim Sullivan  · 技术社区  · 16 年前

    假设我正在开发一个跟踪拳击比赛结果的应用程序。最好的方法是用两张桌子来跟踪这些,比如:

    Boxers
    ======
    id
    name
    
    Matches
    =======
    id
    match_number
    boxer_id
    opponent_id
    result
    

    …然后每场比赛有两个记录,一个是胜者,一个是失败者,类似这样:

    id match_number boxer_id opponent_id result
    -- ------------ -------- ----------- ------
    1  1            1001     2001        WIN
    2  1            2001     1001        LOSS
    

    …还是我错过了更好的方法?

    如果没有结果存储,只是记录了两个对手的匹配结果呢?

    谢谢!

    4 回复  |  直到 16 年前
        1
  •  4
  •   James Curran    16 年前

    好吧,对于一个完全标准化的问题,我想说:

    拳击手-引用。

    比赛:

    id  / match_number   (Don't think these need to be distinct anymore)
    winner (FK-Boxers; Nullable, for for matches scheduled, but not decided yet)
    

    反对者:

    id  (meaningless PK value.  Optional. 
         you could use the other two fields as the PK)
    match_id
    boxer_id
    

    对手两排,每场比赛一排。

    (更新以添加其他答案的建议)

        2
  •  2
  •   Sampson    16 年前

    存储 winner_id 在同一个记录中。
    这允许您显示即将发生的战斗,而不是建议存储的解决方案 温尼尔斯德 loser_id 只有当没有赢家或输家的时候,战斗才会发生。

    比赛

    • 火柴盒
    • 火柴日期
    • 温尼尔斯德

    火柴战士

    • 火柴盒
    • 战斗机

    战斗机

    • 战斗机
    • 战士名字
        3
  •  0
  •   Scott Vander Molen    16 年前

    存储获胜者ID会将匹配表中的记录数减半。否则,存储对手ID是没有意义的。

        4
  •  0
  •   devio    16 年前

    像问题中的拳击手

    匹配:

    match_id int pk
    winner_id int fk
    loser_id int fk
    result(_id) int/bool nullable
    date
    ...
    

    如果结果只能是“win”或“unknown”,则可以为空的布尔值就足够了。

    如果有不同的结果类型(例如导致相扑赢/输的技术名称),请使用一个可以为空的int。

    当你存储数据时,你应该经常问自己:我的数据应该能够回答什么样的问题。