数据库问题,如何存储变化的数据结构 [英] Database issue, how to store changing data structure

查看:56
本文介绍了数据库问题,如何存储变化的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building some application, the involves training programs. my issue is like this,

锻炼可以像这样简单:

3 sets of 45 push ups.

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

so I would just create 2 fields, sets / count

但是锻炼也可以:

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

所以我需要建立一个表,它会知道在数据改变结构时存储数据,然后我仍然可以将其转换为 gui 上的真实数据.

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.

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

how can i make it efficiently and wisely ?

为了说得清楚一点,我想为每次锻炼指定我在其中做了什么.所以一种锻炼可能是:3套,第一:45个俯卧撑第二个:32个俯卧撑第三个:30个俯卧撑

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个俯卧撑并且2分钟跳绳150米游泳

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

数据不一致,一组可能是多个俯卧撑,下一组可能是时间长度等.

the data isn't consistence, one set could be a number of push ups, the next could be a time length etc..

推荐答案

我认为这需要 1:n 的关系,其中有一个主锻炼"表和一个统一的组件"表,其中包含所有锻炼的活动.

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.

你有你的主表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

workout_components 表:

workout_components table:

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

优点:

  • 不限于特定活动

  • Not limited to specific activities

易于查询 - 有关如何从此类数据结构中获取某些内容的所有问题都已在 SO 上得到解答

Easy to query - every question related to how to get something from this kind of data structure has already been answered on SO

活动可以自由添加到每次锻炼中

Activities can be freely added to each workout

这篇关于数据库问题,如何存储变化的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆