我正在开发一个系统,其中有doctor
,patient
和diagnosis
。
我使diagnosis
成为一个弱实体,因为没有医生或患者就不会有诊断。
现在,我希望在treatment
和doctor
以及patient
之间建立一个名为diagnosis
的关系,其中特定的医生会对具有特定诊断的特定患者进行治疗。< / p>
如果diagnosis
是一个没有它自己的主键的弱实体,如何建立关系。
答案 0 :(得分:1)
我认为您对弱实体和主键的基本理解是有缺陷的。
您似乎认为,因为弱实体表“包含”了另外两个表的主键,这意味着它不能拥有自己的主键。
事实并非如此。主键可以是多列的组合,只要该组合对于所有行都是唯一的。
根据你描述的内容,你应该有这样的东西:
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
如果医生只能诊断每位患者一次,并且每位患者只建议一次治疗,这就足够了。如果这些组合中的任何一个可以有多个实例,那么您应该将第三个“行号”类型列添加到诊断和/或治疗表的PK中,以包含在该表的PK中并使其唯一。