I'm looking for a schema to handle optional fields. The record with the field present will be unique from the record without the field.
Let's take an ingredient as an example.
An ingredient has a name (title), category name and an optional variety name.
+---------------+-------+----------+
| ID_Ingredient | Title | Category |
+---------------+-------+----------+
An example is an apple. For some recipes, any apple will suffice. But there are different varieties of an apple such as "Granny Smith" and "Red Delicious". Some apple pie recipes call for a blend of Granny Smith and MacIntosh varieties.
In order to handle this dilemma, the variety is optional.
My present schema is to have two tables: Optional_Variety and Variety:
+------------+--------------+
| ID_Variety | Variety Name |
+------------+--------------+
+---------------+------------+
| ID_Ingredient | ID_Variety | // Optional_Variety table
+---------------+------------+
If an ingredient has a variety, the variety can be found by searching the Optional_Variety
table.
My dilemma is that if an ingredient has a variety, then it should have a unique ID_Ingredient
identifer. After all, a Granny Smith apple is different than a Red Delicious apple (both look and taste), so they should be unique ingredients.
My other solution is to have the variety field in the Ingredient record:
+---------------+-------+----------+---------+
| ID_Ingredient | Title | Category | Variety |
+---------------+-------+----------+---------+
My issue with this schema is that many ingredients don't have a variety, and I'd be wasting database space by having empty fields.
In my actual implementation, there are many optional fields for ingredients, so the amount of wasted database space for empty fields would multiply.
The question is:
What is the schema for handling records with an optional field, such that when the field is present, it is different (a.k.a. having a unique identifier) than the record without the field?
Platform: MySql on Windows 7, 64-bit; MySQL Windows Vista, 32-bit;
I'm using C++ ODB with Object Relational Mapping to interface with the database.