"Connect the dots ..." the computer certainly can.
As a general rule of thumb, plan on having a database table for:
- every "thing" in your world (users, food, meals, people, dishes), describing only the properties of that "thing" and any "one-to-one" or "one-to-many" relationships it may have;
- every many-to-many relationship if you have any;
- and, perhaps, every event, such as a scheduled meal.
Use this rule-of-thumb as a starting point for your design, and if you find that certain tables are not needed you can eliminate them.
There is no "right" answer.
Now, as for how those tables can be
linked, which seems to be the crux of your present question.
Tables do not have to be linked
directly to each other
because in an SQL query I can
join any reasonable number of tables, following any "chains" of relationships that I have going in my database. For example, let's figure out how many potatoes (and while we're at it,
every other food item) that I am going to need for all the
meals that are to be served today. To do this, the query must follow this chain of table relationships:
(These relationships are not "specified in advance," but merely occur in the query I'm about to write.)- In [b]meals,[b] (which in my current concept of the database serves as the schedule) locate all the meals with date_scheduled = today().
- With an inner join to dishes, find all of the food items that are present in any of those meals.
A simple query like this would
list the food items, eliminating duplicates; not quite what we want although useful. Let's do more...
We use the
count(ingredient) clause to specify that we want to know the count of the ingredients
(obviously...) and we then use
GROUP BY ingredient to specify that we want a unique row for each ingredient and the count of occurrences for each group.
Summary:
- The database schema describes the world as the computer knows it.
- The querying capability of the SQL database provides the answers sought, figuring out how to produce each answer at the moment it is presented.
As the book of Psalms says,
"selah ..." ("ponder this")