繁体   English   中英

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

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

我正在构建一些应用程序,其中涉及培训计划。 我的问题是这样的

锻炼可能很简单,如下所示:

3 sets of 45 push ups.

所以我只创建2个字段,设置/计数

但是锻炼也可以是:

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

因此,我需要构建一个表,该表将知道在数据更改结构时会存储数据,以后我仍然可以将其转换为gui上的真实数据。

我怎样才能有效而明智地做到这一点?

编辑:

为了更清楚一点,我想为每个锻炼指定我在其中所做的工作。 所以一个锻炼可以是:3套,第一套:45个俯卧撑第二套:32个俯卧撑第三套:30个俯卧撑

另一种锻炼方法可能是:3套俯卧撑:第一套:45个俯卧撑第二套:32个俯卧撑第三套:30个俯卧撑以及2分钟的跳绳150米游泳

数据不一致,一组可能是俯卧撑的数量,下一组可能是时间长度等。

您可以使用以下各列创建一个表:WorkoutType | 套装 价值| 值类型 。 所以你可以像

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

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

您可能需要考虑如下数据库模式:

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;

这使您可以进行不遵循先前锻炼模式的复杂锻炼。 您可以很容易地得到以下内容:

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在他的《企业应用程序体系结构模式》一书中将上述模型称为 “具体表继承”。 Bill Karwin还在其《 SQL Antipattens》一书的“实体-属性-值”一章中描述了该模型。 他还描述了选择EAV模型来解决这种情况的缺点。

另一方面,如果要总体架构具有灵活性,则可以考虑使用其他NoSQL解决方案而不是MySQL。 这些数据存储区通常不需要固定的表架构。

我想说这需要一种1:n关系,这里有一个主“锻炼”表和一个统一的“组件”表,其中包含锻炼的所有活动。

您将进行主表workouts

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

然后是子表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 .....

一个示例值如下所示:

健身表:

id          participant      date
1           Harry Miller     2010-08-21

training_components表:

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

好处:

  • 不限于特定活动

  • 易于查询-与如何从这种数据结构中获取数据有关的每个问题都已经在SO上得到了解答

  • 活动可以免费添加到每个锻炼中

暂无
暂无

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

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