简体   繁体   English

两个外键引用一个表和空的外键

[英]Two foreign keys reference one table and null able foreign key

I am new to Database tables and relationships .I need some help for the below requirements 我是数据库表和关系的新手。我需要以下要求的帮助

Work flow 工作流程

    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.

For the above requirements i have designed the table structure below 针对以上要求,我设计了以下表格结构

Approach 1 方法1

RegTable RegTable

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

RegDetail RegDetail

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

FK_1,FK_2 is RegId from Regtable FK_1,FK_2是Regtable中的RegId

I have two questions 我有两个问题

  1. Is my current approach is correct or Not ? 我目前的方法正确还是不正确?
  2. Is alternative approach is there for the above work flow . 是否有替代方法可用于上述工作流程。

Kindly help me solve this . 请帮我解决这个问题。 Thanks in Advance 提前致谢

I would suggest the third table RegRecords with field id, note, date. 我建议使用字段ID,注释,日期的第三张表RegRecords It will contain a registration data without link to RegTable. 它将包含没有链接到RegTable的注册数据。 So you will store links to real people in RegDetail that will have only two fields: FK_KEY_RegRecords and FK_KEY_ RegTable. 因此,您将在RegDetail中存储指向真实人员的链接,该链接只有两个字段:FK_KEY_RegRecords和FK_KEY_ RegTable。

You don't need 2 tables here.You can do it as shown below. 您这里不需要2个表。您可以如下所示进行操作。

RegTables - this is the only table you need

Id int PK

Name string

Gender String 

PatientType tinyint 

Here you can maintain enum Type for separating Single and couple . 在这里,您可以维护enum Type以将Singlecouple分开。

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

Update : 更新:

Treatments table

Id int PK

Name string

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM