将(几乎)相似的表合并到一张表中

问题描述 投票:0回答:1

我广泛使用了搜索功能,但到目前为止对答案不满意。到目前为止,我找到的最佳答案是在this线程中找到的。但我的问题略有不同。

我想创建一个包含多个组件的数据库。有些组件在某些属性上有所不同,有些则没有。

我的(严重倾倒的)结构:

create table component_1 (
  id integer,
  component_name text,
  component_material integer,
  coating_concept_id integer
);

create table component_2 (
  id integer,
  component_name text,
  component_material integer
);

create table component_3 (
  id integer,
  component_name text,
  component_material integer
);

COMPONENT_1

id 组件名称 组件_材料 coating_concept_id
1 我的_组件_1 1 随机涂层概念

COMPONENT_2

id  组件名称  组件_材料
 1 我的_组件_2 2

COMPONENT_3

id  组件名称  组件_材料
 1 我的_组件_3 3

如您所见,

component_2
component_3
相似,而
component_1
有一些该组件特有的额外属性。它们都有一个
component_material
属性,该属性是
material
表的外键,可以在其中输入更多详细信息,例如
serial_id
manufacturer
详细信息。

这个

material
表就是这篇文章的原因:

我应该:

  • 创建一个(大)材质表并合并所有必要的属性。为了区分组件,我将引入一个
    component_type
    属性。
  • 为每个组件创建材料表
  • 创建两个材质表:一张用于
    component_1
    ,一张用于
    component_2
    component_3
    ,因为它们具有相同的属性。

解决方案1:

MATERIAL_TABLE

材质_id  制造商 序列号  coating_concept_id
1  制造商_1  serial_id_1  随机涂层概念
2 制造商_2 序列号_2  空
3 制造商_3 serial_id_3  空

解决方案2:

COMPONENT1_MATERIAL_TABLE

材质_id  制造商 序列号  coating_concept_id
1  制造商_1  serial_id_1  随机涂层概念

OTHER_COMPONENTS_MATERIAL_TABLE

材质_id  制造商 序列号
1 制造商_2 序列号_2
2 制造商_3 serial_id_3

解决方案3:

COMPONENT1_MATERIAL_TABLE

材质_id  制造商 序列号  coating_concept_id
1  制造商_1  serial_id_1  随机涂层概念

COMPONENT2_MATERIAL_TABLE

材质_id  制造商 序列号
1 制造商_2 序列号_2

COMPONENT3_MATERIAL_TABLE

材质_id  制造商 序列号
1 制造商_3 serial_id_3

想法:

从另一个线程中,我得知解决方案 1 会违反 NF2。但是,我不确定,因为每个属性都依赖于 PK (material_id) 并且我没有看到部分依赖关系(但我也很愚蠢)。

解决方案 2 看起来有点混乱。

解决方案 3 看起来有点冗长,因为感觉有很多重复的代码。

请帮帮我!预先感谢您!

postgresql database-design normalization
1个回答
0
投票

第一个表有 id 和 ranmid

id randomcoatingconceptid

还有第二个

material_id      manufacturer   serial_id

因此您可以添加您想要和需要的任意数量的制造商

© www.soinside.com 2019 - 2024. All rights reserved.