数据库设计:来自多个数据源的主键
最初,我从 食物来源 1 的 Excel 文档中导入食物,它具有 VARCHAR 类型主要*(PK 示例 #FOOD0001)* (因为只有 1当时我刚刚使用自动递增 int ID 直接导入到 food 表中)
但我需要从另一个具有完全主键类型的源 Food Source 2 导入食物( INT) (PK 示例 #25928747)
我目前有:
食物表
INT
FoodId
,名称
份量表
INT
ServingId
、FoodId
、名称、大小
最好的数据库设计是什么,以便可以导入任何食物源而不影响当前的 ID或者至少有一个映射,以便可以轻松更新、删除食物等?出于性能原因,我不想将 ID 更改为 VARCHAR
我的一个想法是在我的食物表中引入一个 FoodSourceFoodId,它具有来自食物源的原始 id,这样如果食物从食物中更改/更新那么它可以很容易地在食品表中更新吗?
食品表
INT FoodId<PK>, *VARCHAR FoodSourceFoodId*, Name
1 #FOOD0001 Food 1
2 #FOOD0002 Food 2
3 25928747 Food 1
4 25928748 Food 2
类似地,我可以对服务表执行相同的操作,其中服务 ID 可能与源数据中的服务 ID 相关。
您认为这是正确的方法吗?或者你会建议其他东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要对同一列中两种不同类型(域)的值进行建模,特别是当相关类型映射到不同的 SQL 数据类型时。
建议:对每个源使用一个“子类型”表,包括其各自的“自然”键,并使用一个“超类型”表,以使用您的人工键
FoodId
例如来合并它们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.这似乎是一个不错的计划。另一个更常见的选择是创建一个支持表,其中
INT FoodID
作为外键(食品表)和VARCHAR ID
。然后,当您不再需要支持导入时,您可以扔掉该表。This seems like a fine plan. Another more common option would be to make a support table with
INT FoodID
as a foreign key (to the foods table) andVARCHAR ID
. Then when you no longer need to support the imports you can just toss the table.