I am trying to create a good model for a database that I need for an application. The way that I have tought about realizing it involves creating a new table for each new element that is added, but I am unsure if that really is such a good idea. I found a related question that used a garden as an analogy, and I quite liked that, so I will try to exemplify using that below. Note that each gardener only works with one garden. The gardeners doesn't need to be able to be at several gardens.
I have a set of Gardereners as such:
PrimaryKey | Name | Garden
1 | Bob | Garden1
2 | James| Garden2
3 | Ian | Garden3
Each Garden will point to a new table containing the flowers. The Garden contains all the flowers in that garden.
FlowerNumber | Type
1 | Holly
2 | Lilach
3 | Larch
The question is then, would it be "right" to create a new table Garden for each new Gardener that is created? I tought it kind of wasteful to create new tables all the time, but I am not sure of any other ways to do it. I think object-oriented thinking might have perverted my way of thinking about databases.
Best Answer
No. This will cause maintenance problems: when you want to update the structure of
Garden
, then changes (adding a new column, removing an old column, changes to constraints, changes to triggers, whatever) will have to be propagated to everyGarden_*
table. It is also bad data modelling. You should aim to have one table per entity/relation in your system. Having multipleGarden
tables would imply that they are fundamentally different.I'd suggest a structure more like this:
The data for this schema could look like this:
This will let a gardener be assigned to one garden. If you need more flexibility such that gardeners can be assigned to multiple gardens, and gardens can have multiple gardeners assigned to them, you'd need a separate garden assignment table, and remove the
garden_id
field fromgardener
: