Initially I was importing foods from an excel document from Food Source 1 and it had VARCHAR type primary*(PK example #FOOD0001)* (Because there was only 1 source at the time I just imported directly into the foods table with auto incrementing int ID)
But I have a need to import food from another source Food Source 2 which has completely primary key type (INT) (PK example #25928747)
I currently have:
Foods table
INT
FoodId<PK>
, Name
Servings table
INT
ServingId<PK>
,FoodId<FK>
, Name, Size
What is the best database design so that any food source could be imported that won't affect the current ids or at least has a mapping so that foods can easily be updated, deleted etc? I don't want to change the ID to a VARCHAR for performance reasons
One idea I have is to introduce a FoodSourceFoodId in my foods table that has the original id from the food source, that way if a food gets changed/updated from the food source then it can easily be updated in the foods table?
Foods table
INT FoodId<PK>, *VARCHAR FoodSourceFoodId*, Name
1 #FOOD0001 Food 1
2 #FOOD0002 Food 2
3 25928747 Food 1
4 25928748 Food 2
Similarly I could do the same thing to the servings table where the serving id might relate to the serving id in the source data
Do you think this is the way to go? Or would you suggest something else?
I would recommend against modelling values from two distinct types (domains) in the same column, especially when the types in question map to distinct SQL data types.
Suggestion: use a 'subtype' table, including their respective 'natural' key, for each source and a single 'supertype' table to consolidate them using your artifical key
FoodId
e.g.