两个外键引用一个 table 和可为空的外键

Two foreign keys reference one table and null able foreign key

我是数据库 table 和关系的新手。我需要一些帮助来满足以下要求

工作流程

    1. Hospital will have Male Patient
    2. Hospital will have Female Patient
    3. Hospital Will have Couple Patient but in RegTable it will stored as separate record for male and female.

针对以上需求我设计了下面的table结构

方法一

注册表

+-------+---------+---------+
| RegID |  Name   | Gender  |
+-------+---------+---------+
|     1 | XXX     | M       |
|     2 | XXX     | M       |
|     3 | Husband | M       |
|     4 | Wife    | F       |
+-------+---------+---------+

注册详情

+----+------+-------+
| Id | FK_1 | FK_2  |
+----+------+-------+
|  1 |    1 | Null  |
|  2 |    2 | Null  |
|  3 |    3 | 4     |
+----+------+-------+

FK_1,FK_2 是来自 Regtable

的 RegId

我有两个问题

  1. 我目前的做法是否正确?
  2. 上述工作流程是否有替代方法。

请帮我解决这个问题。提前致谢

我建议第三个 table RegRecords with field 身份证,备注,日期。它将包含没有 link 的注册数据到 RegTable。因此,您将在 RegDetail 中将 links 存储给真实的人,其中只有两个字段:FK_KEY_RegRecords 和 FK_KEY_ RegTable。

不需要2张表here.You可以如下图那样做。

RegTables - this is the only table you need

Id int PK

Name string

Gender String 

PatientType tinyint 

这里可以维护enum Type来分离Singlecouple

public enum PatientType : byte
    {
        Single=1,
        Couple =2,
    }

更新:

Treatments table

Id int PK

Name string

RegId int FK --> this is the foreign key referencing RegTables table