【问题标题】:Many-to-many relationship with predefined types与预定义类型的多对多关系
【发布时间】:2014-09-28 05:52:44
【问题描述】:

每个进程都有多个操作。每个操作都有一个唯一的持续时间(秒),具体取决于它所属的进程,一个操作可能属于多个进程。

问题是我必须使用预定义的“类型”列表,并且每个进程/操作必须属于相同的“类型”。也就是说,“A 类”的进程不能有“B 类”的操作,另一边也是一样。

我尝试了以下方法但没有成功。有什么线索吗?

【问题讨论】:

    标签: database-design many-to-many relationship entity-relationship rdbms


    【解决方案1】:
    -- type common to process and operation 
    --
    po_type {type_id, type_name}
         PK {type_id}
         AK {type_name}
    
    
    operation {operation_id, operation_type}
           PK {operation_id}
           SK {operation_id, operation_type}
           FK {operation_type} REFERENCES po_type {type_id}
    
    
    process {process_id, process_type}
         PK {process_id}
         SK {process_id, process_type}
         FK {process_type} REFERENCES po_type {type_id} 
    
    
    -- operation_process
    -- process_operation_no is an integer (1,2,3 ..) for each process_id
    --
    op_proc {process_id, process_operation_no, operation_id, the_type, duration}
         PK {process_id, process_operation_no}
        FK1 {process_id, the_type}   REFERENCES process   {process_id, process_type}    
        FK2 {operation_id, the_type} REFERENCES operation {operation_id, operation_type}
    

    Notes: PK = primary key 
           AK = alternate key (use unique constraint/index)
           SK = superkey      (use unique constraint/index)
           FK = foreign key
    

    我在这里允许在此过程中重复操作,但不确定这在您的模型中是否有意义——如果不是简单地删除 process_operation_no 并在 PK 中使用 operation_id


    编辑

    在任何地方保留type_id 名称会更好一些;也没有process_operation_no——不允许操作在进程中重复。

    po_type {type_id, type_name}
         PK {type_id}
         AK {type_name}
    
    
    operation {operation_id, type_id}
           PK {operation_id}
           SK {operation_id, type_id}
           FK {type_id} REFERENCES po_type {type_id}
    
    
    process {process_id, type_id}
         PK {process_id}
         SK {process_id, type_id}
         FK {type_id} REFERENCES po_type {type_id} 
    
    
    op_proc {process_id, operation_id, type_id, duration}
         PK {process_id, operation_id}
        FK1 {process_id,   type_id} REFERENCES process   {process_id,   type_id}    
        FK2 {operation_id, type_id} REFERENCES operation {operation_id, type_id}
    

    【讨论】:

    • 这正是我所期待的。谢谢!
    【解决方案2】:

    我认为这看起来像是三元关系的经典案例。

    例如:

    教授 学科 课程

    来源:Data Modeling and Database Design By Richard W. Scamell, Narayan S. Umanath

    现在,Process、Operation 和 Type 是三个具有三元关系的实体。

    逻辑模式将具有这种三元关系。它将分解为多个 m:n 或 1:n 关系(取决于域约束)。之后,您可以将它们建模为架构中的任何其他 m:n。

    更多内容可以参考

    第 5.5.1 节分解三元和高阶关系

    来自同一本书。

    另外,你可以看到这个:

    Analysis of Binary/Ternary Cardinality Combinations in Entity-Relationship Modeling

    【讨论】:

    • 这将帮助我提高我的理论知识。谢谢!
    猜你喜欢
    • 2022-01-12
    • 2019-02-09
    • 2015-02-03
    • 2022-01-22
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多