[英]Should I use separate tables for this data?
This is a backend for a project I've been working on for fun that stores user's scorecards of boxing matches. 这是我一直在努力工作的一个项目的后端,该项目存储了用户的拳击比赛计分卡。 Should I have a separate table for every boxing match (about 25 columns), or should I store every boxing match in the same table and add a column denoting which boxing match the row is for?
我应该为每个拳击比赛(约25列)使用一个单独的表,还是应该将每个拳击比赛存储在同一张表中,并添加一列来表示该行用于哪个拳击比赛?
What are good standards to practice when making this decision? 做出此决定时应遵循哪些好的标准? I will be accessing the table by averaging all rounds of a given match and returning the average score for all users for any given round.
我将通过平均给定比赛的所有回合并返回任何给定回合中所有用户的平均得分来访问表格。 This type of access makes me think a separate table for each match would be desirable because that way I wouldn't be searching through any unwanted matches when trying to average the scores of a certain match.
这种访问方式使我认为每个比赛都需要一个单独的表,因为那样一来,我在尝试平均某个比赛的得分时就不会搜索任何不需要的比赛。
The standard approach is to identify each "entity" within the model. 标准方法是识别模型中的每个“实体”。
An entity is a person, place, thing, concept or event that 实体是指以下的人,地方,事物,概念或事件:
An entity is implemented as a table, with each row in the table representing a single instance of the entity. 实体被实现为一个表,表中的每一行代表该实体的单个实例。
In the example, it sounds like a "boxing match" is an entity. 在该示例中,听起来像“拳击比赛”是一个实体。 The next step is figure out what the unique identifier is, whether it's a single column, or a combination of columns, and plan on having a UNIQUE INDEX defined on that.
下一步是弄清唯一标识符是什么,无论是单列还是列组合,并计划在其上定义UNIQUE INDEX。
In addition to the natural key(s), we typically introduce a surrogate id
column which serves as the primary key of each entity table. 除了自然键之外,我们通常还会引入一个代理
id
列,该列用作每个实体表的主键。 (I will note that there are two schools of thought on whether a surrogate key is desirable: there are those who have elected to use a natural key as the primary key and have been later burned by that decision, and then there are those that haven't yet been burned by their decision to use a natural key as the primary key.) (我将注意到,关于替代密钥是否可取,存在两种思想流派:有些选择使用自然密钥作为主密钥,后来又因该决定而被烧毁,然后又有一些人选择了替代密钥。尚未决定使用自然键作为主键。)
The ideal primary key has these characteristics: 理想的主键具有以下特征:
You should not put each match in its own table. 您不应该将每个匹配项放在自己的表中。 That would mean you would need an unknown number of tables, which is an indication that you have not modeled the problem correctly.
这意味着您将需要未知数量的表,这表明您没有正确建模问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.