两个外键引用一个表和null外键

时间:2016-08-28 10:11:10

标签: mysql sql-server entity-framework foreign-keys foreign-key-relationship

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

工作流程

    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.

对于上述要求,我设计了下面的表结构

方法1

RegTable

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

RegDetail

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

FK_1,FK_2是Regtable的RegId

我有两个问题

  1. 我目前的做法是否正确?
  2. 上述工作流程是否有替代方法。
  3. 请帮我解决这个问题。在此先感谢

2 个答案:

答案 0 :(得分:1)

我建议第三个表RegRecords使用字段 id,note,date。它将包含一个没有链接到RegTable的注册数据。因此,您将在RegDetail中存储仅包含两个字段的真人的链接:FK_KEY_RegRecords和FK_KEY_ RegTable。

答案 1 :(得分:1)

这里不需要2个表。你可以按照下面的说明进行操作。

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