简体   繁体   English

数据库问题,如何存储变化的数据结构

[英]Database issue, how to store changing data structure

I'm building some application, the involves training programs. 我正在构建一些应用程序,其中涉及培训计划。 my issue is like this, 我的问题是这样的

A workout could be simple as this: 锻炼可能很简单,如下所示:

3 sets of 45 push ups.

so I would just create 2 fields, sets / count 所以我只创建2个字段,设置/计数

BUT a workout could be also: 但是锻炼也可以是:

45 minutes run, 3 sets of 45 pushups, 2 minutes of rope jumping, 150 meter swimming.

so i need to build one table, that would know to store the data as it changes it structure, and later I could still translate it to real data on the gui. 因此,我需要构建一个表,该表将知道在数据更改结构时会存储数据,以后我仍然可以将其转换为gui上的真实数据。

how can i make it efficiently and wisely ? 我怎样才能有效而明智地做到这一点?

edit: 编辑:

To make it a bit clear, i want to specify to each workout what Ive done in it. 为了更清楚一点,我想为每个锻炼指定我在其中所做的工作。 so one workout could be : 3 sets, first: 45 push ups second: 32 push ups third: 30 push ups 所以一个锻炼可以是:3套,第一套:45个俯卧撑第二套:32个俯卧撑第三套:30个俯卧撑

and another workout could be: 3 sets of pushups: first: 45 push ups second:32 push ups third: 30 push ups and also 2 minutes of jumping rope 150 meter swimming 另一种锻炼方法可能是:3套俯卧撑:第一套:45个俯卧撑第二套:32个俯卧撑第三套:30个俯卧撑以及2分钟的跳绳150米游泳

the data isn't consistence, one set could be a number of push ups, the next could be a time length etc.. 数据不一致,一组可能是俯卧撑的数量,下一组可能是时间长度等。

You could create a table with following columns: WorkoutType | 您可以使用以下各列创建一个表:WorkoutType | Sets | 套装 Value | 价值| ValueType . 值类型 。 So you can store like 所以你可以像

----------------------------------
WorkoutType | Sets | Value | ValueType
----------------------------------

Pushups      | 3    | 45   | nos
Run          | null | 45   | minutes
Rope Jumping | null | 2    | minutes 
Swimming     | null | 150  | meter 

You may want to consider a database schema such as the following: 您可能需要考虑如下数据库模式:

CREATE TABLE workouts (
   workout_id  int,
   user_id     int,
   PRIMARY KEY (workout_id)
) ENGINE=INNODB;

CREATE TABLE sessions_pushups (
   started     datetime,
   workout_id  int,
   number      int,
   PRIMARY KEY (started, workout_id),
   FOREIGN KEY (workout_id) REFERENCES workouts (workout_id)
) ENGINE=INNODB;

CREATE TABLE sessions_rope_jumping (
   started          datetime,
   workout_id       int,
   duration_minutes int,
   PRIMARY KEY (started, workout_id),
   FOREIGN KEY (workout_id) REFERENCES workouts (workout_id)
) ENGINE=INNODB;

CREATE TABLE sessions_swimming (
   started    datetime,
   workout_id int,
   meters     int,
   PRIMARY KEY (started, workout_id),
   FOREIGN KEY (workout_id) REFERENCES workouts (workout_id)
) ENGINE=INNODB;

This allows you to have complex workouts that do not follow the schema of previous workouts. 这使您可以进行不遵循先前锻炼模式的复杂锻炼。 You could have something like this very easily: 您可以很容易地得到以下内容:

CREATE TABLE sessions_triathlon (
   started            datetime,
   workout_id         int,
   swimming_meters    int,
   cycling_meters     int,
   running_meters     int,
   duration_minutes   int,
   PRIMARY KEY (started, workout_id),
   FOREIGN KEY (workout_id) REFERENCES workouts (workout_id)
) ENGINE=INNODB;

Martin Fowler calls the above model "Concrete Table Inheritance" in his Patterns of Enterprise Application Architecture book. Martin Fowler在他的《企业应用程序体系结构模式》一书中将上述模型称为 “具体表继承”。 Bill Karwin also describes this model in his SQL Antipattens book, in the Entity-Attribute-Value chapter. Bill Karwin还在其《 SQL Antipattens》一书的“实体-属性-值”一章中描述了该模型。 He also describes the disadvantages in choosing an EAV model to tackle such a scenario. 他还描述了选择EAV模型来解决这种情况的缺点。

On the other hand, if you want total schema flexibility, you could consider other NoSQL solutions instead of MySQL. 另一方面,如果要总体架构具有灵活性,则可以考虑使用其他NoSQL解决方案而不是MySQL。 These data stores do not not normally require fixed table schemas. 这些数据存储区通常不需要固定的表架构。

I'd say this calls for a 1:n relationship, where there is a master "workouts" table, and one unified "components" table that contains all the activities of a workout. 我想说这需要一种1:n关系,这里有一个主“锻炼”表和一个统一的“组件”表,其中包含锻炼的所有活动。

You'd have your main table workouts : 您将进行主表workouts

id   int
participant varchar(255)
date        datetime
...... any other workout related data

Then the child table workout_components : 然后是子表workout_components

workout_id  int          // Which workout this belongs to
tabindex    int          // Which sorting order this component has in the list
repeat      int          // Number of repetitions (e.g. 3 sets)
quantity    int          // e.g. 45 push-ups or 150 meters of cycling
quentity_unit varchar    // e.g. minutes or laps
activity    varchar      // push-ups, cycling .....

an example value would look like this: 一个示例值如下所示:

workout table: 健身表:

id          participant      date
1           Harry Miller     2010-08-21

workout_components table: training_components表:

workout_id  tabindex     repeat      quantity     quantity_unit  activity
1           1            3           45           pcs            pushups
1           2            1           2            minutes        rope-jumping

Advantages: 好处:

  • Not limited to specific activities 不限于特定活动

  • Easy to query - every question related to how to get something from this kind of data structure has already been answered on SO 易于查询-与如何从这种数据结构中获取数据有关的每个问题都已经在SO上得到了解答

  • Activities can be freely added to each workout 活动可以免费添加到每个锻炼中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM