简体   繁体   English

如何将动态数据添加到数据库表中?

[英]How do I add dynamic data to a database table?

I am trying to create a database for gym workout plans.我正在尝试为健身房锻炼计划创建一个数据库。 This would have each member's workout plan, which comprises multiple exercises.这将有每个成员的锻炼计划,其中包括多个练习。

My question is: given that one member might have 5 exercises, another member 3, another 6, and so on, how can I store this in the database?我的问题是:假设一个成员可能有 5 个练习,另一个成员 3 个,另一个 6 个,等等,我如何将其存储在数据库中?

My thought process has been around hardcoding a set number of exercises in the table, and give the possibility of them being null (for exemple 10 exercises, but if you only use 5 exercises, everything else can be stored with nothing).我的思考过程一直围绕着在表格中硬编码一组练习,并给出它们是 null 的可能性(例如 10 个练习,但如果你只使用 5 个练习,其他所有内容都可以一无所有地存储)。 But this doesn't feel very correct.但这感觉不太正确。

I am using MySQL for this.我为此使用 MySQL。

Like @Sami pointed out, you are going to work on whats called a relational database .就像@Sami 指出的那样,您将使用所谓的relational database

Below is a simple database structure that will help you get started.下面是一个简单的数据库结构,可以帮助您入门。

Members table成员表

Table Name: tbl_members表名:tbl_members

Table Structure:表结构:

| | member_id |member_name|member_contact| member_id |member_name|member_contact|

Exercise or schedule table练习或日程表

Table Name: tbl_member_schedule表名:tbl_member_schedule

Table Structure:表结构:

| | schedule_id | schedule_id | member_id |assigned_date|Schedule member_id |assigned_date|时间表

In the tbl_member_schedule you can store the entire schedule as a JSON structure or you can further normalize.在 tbl_member_schedule 中,您可以将整个时间表存储为 JSON 结构,或者您可以进一步规范化。 Since this is an example I am storing the entire schedule in JSON format inside the Schedule field.由于这是一个示例,我将整个计划以 JSON 格式存储在计划字段内。

Now when you want to retrieve the schedule of a specific member (member id: 1) you can query the database as follows:现在,当您要检索特定成员(成员 ID:1)的日程安排时,您可以按如下方式查询数据库:

select 
    tbl_members.member_id, tbl_members.member_name, 
    tbl_member_schedule.Schedule
from 
    tbl_members join
    tbl_member_schedule on tbl_members.member_id = tbl_member_schedule.member_id
where 
    tbl_members.member_id = 1;

So you JSON structure for your schedule could be因此,您的日程安排的 JSON 结构可能是

{ 
    day1: [
        {exerciseName: 'dumbbell curls', reps: '8 x 4'}, 
        {exerciseName: 'leg extension', reps: '8 x 3'}
    ],
    day2: [
        {exerciseName: 'dumbbell curls', reps: '8 x 4'}, 
        {exerciseName: 'leg extension', reps: '8 x 3'}
    ]
}

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

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