与预定义类型的多对多关系

时间:2014-08-05 14:15:10

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

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

enter image description here

问题是我必须使用预定义"类型的列表"并且每个流程/操作必须属于相同的"类型"。换句话说,"类型A"不能进行" B"的操作,另一方面也是如此。

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

enter image description here

2 个答案:

答案 0 :(得分:1)

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

例如:

  

教授< - >主题< - >当然

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

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

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

有关更多信息,请参阅

  

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

来自同一本书。

另外,你可以看到:

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

答案 1 :(得分: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}