简体   繁体   English

我应该为这些数据使用单独的表格吗?

[英]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 实体是指以下的人,地方,事物,概念或事件:

  1. can be uniquely identified 可以唯一标识
  2. we can store information about and 我们可以存储有关和的信息
  3. is important to the business or enterprise 对企业或企业很重要

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: 理想的主键具有以下特征:

  1. simple (a single attribute which is simple native datatype, eg int) 简单(单个属性,它是简单的本地数据类型,例如int)
  2. unique (a value identifies only a single row, no two rows have the same value) 唯一(一个值仅标识一行,没有两行具有相同的值)
  3. anonymous (carries no visible or hidden information) 匿名(不携带可见或隐藏信息)
  4. immutable (once value assigned, it is not changed) 不可变的(一旦分配了值,就不会更改)

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.

相关问题 我应该为抽认卡应用程序使用单独的表格吗? - Should I use separate tables for a flashcards app? 我应该为此用例创建一个单独的表吗? - Should I create a separate table for this use case? 如果它们仅相差一列,是否应该有两个单独的表? - Should I have two separate tables if they differ by only one column? 我应该将 ACL 表插入我的数据库还是创建一个单独的数据库? - Should I insert ACL tables into my database or create a separate Database? Mysql:为什么我在定义表时使用适当的数据大小并选择最佳数据长度 - Mysql: why should I use appropriate data sizes and choose the optimal data length while defining tables 我应该使用平台还是规范化数据库? - Should I use flat tables or a normalized database? 我应该使用UNION还是其他方式从多个表中选择数据? - Should I use UNION or something else for selecting data from multiple tables? 我应该使用哪个联接,联合或选择来处理多个表中的数据? - Which join, union or select should I use to process data in multiple tables? 数据建模。 要拆分成单独的表还是使用基于角色的授权? - Data modeling. To split into separate tables or use role based authorization? 我是否应该为每个表维护一个历史表或为每个表单独维护一个表? - Should I maintain one history table or separate tables for each and every tables for auditing purpose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM