【问题标题】:Supertype & Subtypes and one to one relationship超类型和子类型以及一对一的关系
【发布时间】:2019-02-14 12:19:01
【问题描述】:

我在 SQL Server 中有以下超类型/多个子类型表 超类型:医生和子类型:儿科医生、骨科和牙医

    create table Doctor
(
    DoctorID int primary key,
    Name varchar(100),
    -- add some other common attributes (all of vendor, sponsor, volunteer have) here.
)

create table Paediatrician
(
    PaediatricianId int primary key,
    DoctorID int foreign key references Doctor(DoctorID)
    -- add some other attributes related to Paediatrician here.
)

create table Orthopedic
(
    OrthopedicId int primary key,
    DoctorID int foreign key references Doctor(DoctorID)
    -- add some other attributes related to Orthopedic here.
)

create table Dentist
(
    DentistId int primary key,
    DoctorID int foreign key references Doctor(DoctorID)
    -- add some other attributes related to Dentisthere.
)

我的业务逻辑是,医生可以是儿科医生、牙医或骨科医生。不能超过一种子类型。基于上述设计,这不是强制的。我可以创建 Id = 1 的 Doctor,然后转到 Dentist 和 Orthopedictables 并在两个表中分配 DoctorId 值 1。如何强制执行,以便医生只能在一张桌子旁?

【问题讨论】:

  • 在所有三个表(即儿科、骨科、牙医)上添加 On/Before Insert 触发器,并在插入之前检查 DoctorID 是否已经存在。
  • 我会以不同的方式安排这一点。我将有 3 个表,一个医生表,一个专家表和一个专家属性表。医生表包含所有医生的信息,很简单。专家表包含您的专家类型 ID 和专家描述等。您的 3 位示例专家将在此表中各占一行。 SpecialistAttributes 表包含专家所需的所有属性。在您的 Doctor 表中,您有一个外键来查找 SpecialistTypeID,因此只能有 1 个,然后 SpecialistType 有许多可以链接到的 SpecislaistAttibutes。
  • 以这种方式组织数据的另一个好处是您需要添加任何专家角色或属性,您不需要更改数据库的结构,只需添加更多行。跨度>
  • 你的问题可能会有答案here
  • @WalterMitty 您的链接确实很有帮助。尝试应用第三种技术,但尝试使用 sql server 时出现错误

标签: sql sql-server database-design


【解决方案1】:

我会以不同的方式安排这一点。我将有 3 个表,一个 Doctor 表(就像你已经拥有的),一个 Specialist 表和一个 SpecialistAttributes 表。

Doctor 表包含所有 Doctors 的信息,很简单。

专家表包含您的专家类型 ID 和专家描述等。 您的 3 位示例专家将在此表中各占一行。

SpecialistAttributes 表包含专家所需的所有属性。在您的 Doctor 表中,您有一个外键来查找 SpecialistTypeID,因此只能有 1 个,然后 SpecialistType 有许多可以链接到的 SpecislaistAttibutes。

以这种方式组织数据的另一个好处是您需要添加任何专家角色或属性,您无需更改数据库的结构,只需添加更多行。

Doctor Table
    | ID | Name     | Specialist_FK |
    ---------------------------------
    | 1  | Smith    | 2             |
    | 2  | Davies   | 3             |
    | 3  | Jones    | 3             |

Specialist Table
    | ID | Speciality    |
    ----------------------
    | 1  | Paediatrician |
    | 2  | Orthopedic    |
    | 3  | Dentist       |

SpecialistAttribute Table
    | ID | SpecialityID+FK | Description          | Other      |
    ------------------------------------------------------------
    | 1  | 1               | Paediatrician Info 1 | Other Info |
    | 2  | 1               | Paediatrician Info 2 | Other Info |
    | 3  | 2               | Orthopedic Info 1    | Other Info |
    | 4  | 2               | Orthopedic Info 1    | Other Info |
    | 5  | 3               | Dentist Info 1       | Other Info |
    | 6  | 4               | Dentist Info 1       | Other Info |

【讨论】:

    【解决方案2】:

    SQL Server 中没有内置约束/功能来处理这个问题。您需要为其编写自定义登录。在过程或触发器中。

    您可以编写一个存储过程来负责插入这些表。在插入之前,它将验证医生 ID 是否已存在于任何表中,如果是,则将自定义引发错误,否则程序会将记录插入相应的表中。

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 2013-07-18
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多