Question

I have a question about database that I'm currently trying to design, I also would like to mention that I am a beginner so please keep that in mind.

So, the DB that I'm currently designing will be used for my application that I'm developing for school project. The application will be a fitness app which will have number of features but the main focus will be on creating diets based on some information provided by the user. All the data to generate such diet will be taken from the DB such as food and its nutrients etc.

The way I want to construct this diet is so that it will create be a sample diet for a period of 7 days. On each day there will be 5 meals, and each meal will contain number of products e.g 100g chicken breast, 100g brown rice, 100g broccoli.

I've made a ERD diagram to help me model this which you can see at the following link

http://imgur.com/0ivcM5x

As you can see from the picture I've created Food table which will hold all sort of food items that can be used to create the meals, and This is where I get stuck. In my model I have broken it down into separate tables but I don'k know whether that is correct and will work.

I'm also not sure how to create that "MealTable" so far I've got meal_id as PK and food_id as a FK but will I be able to to create Meals with multiple food items or will it be 1 meal 1 item from "food" table.

Similar with the "DietTable" and "dayOfTheWeek" I am trying to take similar approach here so I've got diet_id as PK and day_id as FK but will that allow me to have multiple "day" instances within the same diet.

I know this questions are not really specific, but I'm just trying to understand how to model this and whether this approach is correct. Will it work and are there any other alternative ways to model similar problems.

Any help or suggestions would be appreciated.

Was it helpful?

Solution

I think your ERD diagram looks pretty good, but I noticed a couple things:

  • It looks like your MealTable table will be having more than one row per meal, so I would rename it to MealFood to list all the food items in each meal. If you need to add fields to each meal (such as the name of the meal), then make a separate table called Meal that will have just one row per meal
  • I would remove day_id from DietTable and instead have a diet_id column in the dayOfTheWeek table. If you pair this with a mealOfTheDay column, you could make a unique key across diet_id, day_id, mealOfTheDay to make sure each diet only has one meal for each meal of the day

Example dayOfTheWeek table:

| diet_id | day_id | mealOfTheDay | meal_id |
---------------------------------------------
| 1       | 1      | 1            | 999     |
| 1       | 1      | 2            | 642     |
| 1       | 1      | 3            | 242     |
| 1       | 1      | 4            | 298     |
| 1       | 1      | 5            | 322     |
| 1       | 1      | 5            | 111     |  <- Unique key will not allow 
                                                  this row as it's the second 
                                                  fifth meal on same day for 
                                                  diet 1
  • Also, try to keep you naming consistent. Some of you table names end with Table and some don't.

OTHER TIPS

I've been looking to your database model, and there's some things that are missing and other that needs to be change (my opinion). I've already disigned a similar model, not for fitness, but I need to follow meals protein for a PKU disease. Your final model is (I think) really near than what I've designed :

Here are the list of tables : FOOD : As you've done, except that in my case I'm using it to describe the proteins for 100g of this food. FOOD_UNIT : mass units (mg, g, ..., Kg), volume (ml, cl, ..., L) FOOD_CATEGORY : Vegetables, fruits, ... CALENDAR : Date, day by day... I think it would be more usable for you than using a simple day of week ID and a meal ID. If soneine start the diet on monday and another one on Friday, they will eat exactly the same meal but in fact maybe the first person needs more or less food than the other one. Actually you have a big problem on this. MEAL_PERIOD : Morning, ... Evening. Use time here helps to precise if meal was taken in early morning or nearby from lunch PERSON : For who you are editing the meal. Several persons with differents needs in proteins, fat, ... MEAL : I think you know what to put here (primary key on person_id, calendar_id, meal_period_id). Add all food and quantity in this table. Don't forget to add the foreign key on food_unit_id, and in your case your diet_id

Can't draw a schema now, but normally you have all the elements to draw a cool diagram that will normally fit your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top