【发布时间】: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