数据库设计:来自多个数据源的主键

发布于 2024-12-13 09:35:23 字数 1109 浏览 3 评论 0原文

最初,我从 食物来源 1 的 Excel 文档中导入食物,它具有 VARCHAR 类型主要*(PK 示例 #FOOD0001)* (因为只有 1当时我刚刚使用自动递增 int ID 直接导入到 food 表中)

但我需要从另一个具有完全主键类型的源 Food Source 2 导入食物( INT) (PK 示例 #25928747)

我目前有:

食物表

INT FoodId,名称

份量表

INT ServingIdFoodId、名称、大小

最好的数据库设计是什么,以便可以导入任何食物源而不影响当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖心男生 2024-12-20 09:35:23

我建议不要对同一列中两种不同类型(域)的值进行建模,特别是当相关类型映射到不同的 SQL 数据类型时。

建议:对每个源使用一个“子类型”表,包括其各自的“自然”键,并使用一个“超类型”表,以使用您的人工键 FoodId 例如来合并它们

CREATE TABLE Foods
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name IN ('Food 1', 'Food 2')), 
 UNIQUE (Name, FoodId)
);

CREATE TABLE Foods1
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name = 'Food 1'), 
 FOREIGN KEY (Name, FoodId)
    REFERENCES Foods (Name, FoodId)
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
 Food1_ID CHAR(9) NOT NULL UNIQUE 
    CHECK (Food1_ID LIKE '#FOOD[0-9][0-9][0-9][0-9]')
);

CREATE TABLE Foods2
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name = 'Food 2'), 
 FOREIGN KEY (Name, FoodId)
    REFERENCES Foods (Name, FoodId)
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
 Food2_ID INTEGER NOT NULL UNIQUE
);

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.

CREATE TABLE Foods
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name IN ('Food 1', 'Food 2')), 
 UNIQUE (Name, FoodId)
);

CREATE TABLE Foods1
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name = 'Food 1'), 
 FOREIGN KEY (Name, FoodId)
    REFERENCES Foods (Name, FoodId)
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
 Food1_ID CHAR(9) NOT NULL UNIQUE 
    CHECK (Food1_ID LIKE '#FOOD[0-9][0-9][0-9][0-9]')
);

CREATE TABLE Foods2
(
 FoodId INTEGER NOT NULL UNIQUE, 
 Name CHAR(6) NOT NULL 
    CHECK (Name = 'Food 2'), 
 FOREIGN KEY (Name, FoodId)
    REFERENCES Foods (Name, FoodId)
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
 Food2_ID INTEGER NOT NULL UNIQUE
);
宣告ˉ结束 2024-12-20 09:35:23

这似乎是一个不错的计划。另一个更常见的选择是创建一个支持表,其中 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) and VARCHAR ID. Then when you no longer need to support the imports you can just toss the table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文