I have three table here….
One is menu, and second is drink, and food.
One menu data can only have either one drink or one food.
So, the menu table have something like this….
[id][type][type_fk]
if the menu table type is “D”, it will find the drink table, and use the type_fk, to find the drink… …Here is a more detail example:
id:1,type:D, type_fk:1
id:2,type:F, type_fk:1
in the drink table, we have
id:1, name:coke, unit:ml
in the food table, we have
id:1, name:chicken
The result why I want to separate two tables is because the drink table have “unit” column, but the food table don’t have this… …So, I will achieve information from user based on the type and the type_fk. So, in the first example, I can get the “coke”, and the second one, I can get the “chicken”.
Please drop your comments on this design.
I would suggest that you have a single table instead of 2 tables:
The reason behind that is to enforce referential integrity. Menu.ItemId will be a foreign key that references Item.Id. If you have 2 tables you won’t be able to enforce referential integrity.
I believe you shouldn’t worry about sparse columns. For example store the Unit of chicken as piece or leave it empty.
Edit:
If you want to avoid having a sparse table, a table with some columns not being used, then you might consider something like:
For example
This way you are keeping referential integrity and avoiding sparse tables.
If you are using SQL Server 2008, check this SQL Server 2008 Sparse Columns