我正在建立一些应用程序,其中包括培训项目。我的问题是这样,
锻炼可能很简单,如下所示:
3 sets of 45 push ups.所以我只创建两个字段,集合/计数
但锻炼也可以是:
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米游泳。
数据不一致,一组可能是多个俯卧撑,下一组可能是时间长度等等。
发布于 2010-08-21 11:04:37
我认为这需要一个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-21workout_components表:
workout_id tabindex repeat quantity quantity_unit activity
1 1 3 45 pcs pushups
1 2 1 2 minutes rope-jumping优势:
发布于 2010-08-21 10:47:11
您可以创建一个包含以下列的表: WorkoutType、、Value、ValueType。这样你就可以像
----------------------------------
WorkoutType | Sets | Value | ValueType
----------------------------------
Pushups | 3 | 45 | nos
Run | null | 45 | minutes
Rope Jumping | null | 2 | minutes
Swimming | null | 150 | meter 发布于 2010-08-21 10:53:45
您可能需要考虑数据库架构,如以下所示:
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称上述模型为“具体表继承”在他的企业应用体系结构模式书中。比尔·卡温还在他的SQL Antipattens书“实体-属性-值”一章中描述了这个模型。他还描述了选择EAV模型来解决这种情况的缺点。
另一方面,如果您想要完全的模式灵活性,您可以考虑其他NoSQL解决方案,而不是MySQL。这些数据存储通常不需要固定的表架构。
https://stackoverflow.com/questions/3537288
复制相似问题