What is the best way to setup this table structure.
I have 3 tables, one table we’ll call fruit and the other two tables are properties of that fruit so fruit_detailed and fruit_basic.
fruit
id | isDetailed
fruit_detailed
id | price | color | source | weight | fruitid?
fruit_basic
id | value | fruitid?
So what I want to do is have a property in fruit called isDetailed and if true, fill the fruit_detailed table with properties like color, weight, source, etc (multiple column). If its false then store in fruit_basic table with properties written in a single row.
Storage sounds quite basic but if I want to select a fruit and get its properties, how can I determine which table to join? I could use and IF statement on the isDetailed property and then join like that but then you have two different types of properties coming back
How would you create the tables or do the join to get the properties? Am I missing something?
I would probably model this like so:
I had to guess on appropriate data types for some of the columns. I’m also not sure exactly what the “value” column is in your
Fruit_Basictable, so I’ve left that out for now.Don’t bother putting a bunch of IDs out there simply for the sake of having an ID column on every table. The Fruits->Fruit_Details relationship is a one-to-zero-or-one relationship. In other words, you can have at most one Fruit_Details row for each Fruits row. In some cases you might have no row in Fruit_Details for a particular row in Fruits.
When you’re querying you can simply
OUTER JOINfrom theFruitstable to theFruit_Detailstable. If you get back aNULLvalue forFruit_Details.fruit_idthen you know that the fruit doesn’t have any details. You can always include theFruit_Detailscolumns, they’ll just beNULLif the row doesn’t exist. That way you can always have homogeneous resultsets. As you’ve discovered, otherwise you end up having to worry about different column lists coming back depending on the row in question, which will lead to tons of headaches.If you want to include an “isDetailed” column then you can just use this:
This approach also has an advantage over putting all of the columns in one table because it lowers the number of
NULLcolumns in your database and depending on your data can substantially decrease storage requirements and improve performance.