如何创建具有弱实体的三元关系?

How to create a ternary relationship that has a weak entity?

我正在开发一个系统,其中有 doctorpatientdiagnosis。 我将 diagnosis 设为弱实体,因为没有医生或患者就无法诊断。

现在我想在 doctorpatientdiagnosis 之间建立一个名为 treatment 的关系,其中特定医生将治疗具有特定诊断的特定患者.

鉴于 diagnosis 是一个没有自己的主键的弱实体,如何建立关系。

我认为您对弱实体和主键的基本理解存在缺陷。

你似乎认为因为弱实体 table "includes" 是另外两个 table 的主键,这意味着它不能有自己的主键.

事实并非如此。主键可以是多列的组合,只要该组合对所有行都是唯一的。

根据您的描述,应该是这样的:

Table Doctor
Primary Key:  DoctorID

Table Patient
Primary Key:  PatientID

Table Diagnosis
Primary Key:  DoctorID, PatientID (or an Identity column to form an artificial PK)
Foreign Key:  DoctorID References Table Doctor
Foreign Key:  PatientID References Table Patient

So finally, 

Table Treatment
Primary Key:  DoctorID, PatientID (, Identity column of Table Diagnosis if you created one)
Foreign Key:  DoctorID References Table Doctor
Foreign Key:  PatientID References Table Patient

如果医生只能为每位患者诊断一次,并且也只能为每位患者提供一种治疗建议,这就足够了。如果这些组合中的任何一个可以有多个实例,那么您应该将第三个 "Line Number" 类型列添加到诊断 and/or 治疗 table 的 PK 中,以包含在 table 的 PK 中=24=] 并使其独一无二。