[英]Supertype & Subtypes and one to one relationship
我在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。 我该如何执行它以便医生只能在一张桌子上出现?
SQL Server中没有内置约束/功能来处理此问题。 您需要为此编写自定义登录名。 在过程中或触发。
您可以编写一个存储过程,将其插入这些表中。 在插入之前,它将验证如果任何表中已经存在医生ID(如果是),则将自定义错误,否则过程将在相应表中插入记录。
我会对此有所不同。 我将拥有3个表,一个Doctor表(如您已经拥有的),一个Specialist表和SpecialistAttributes表。
Doctor表轻松包含所有Doctors的信息。
专家表包含您的SpecialistTypeID和SpecialistDescription等。您的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 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.