Sql 超型&;子类型和一对一关系
我在SQL Server中有以下超类型/多个子类型表 超类型:医生和亚类型:儿科医生、骨科医生和牙医Sql 超型&;子类型和一对一关系,sql,sql-server,database-design,Sql,Sql Server,Database Design,我在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 (
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的医生,然后转到牙医和整形外科表,并在这两个表中指定DoctorId值1。如何强制执行它,使医生只能出现在一个表中?SQL server中没有内置约束/功能来处理此问题。您需要为它编写自定义登录名。在程序或触发器中
您可以编写一个存储过程,负责在这些表中插入。在插入之前,它将验证如果任何表中已经存在医生id,如果是,则将自定义引发错误,否则程序将在相应的表中插入记录。我将以不同的方式安排此位。我会有3张桌子,一张医生桌(就像你们已经有的),一张专家桌和一张专家桌 医生表包含所有医生的信息,简单 专家表包含您的SpecialistTypeID和SpecialistDescription等。 您的3个示例专家将在此表中各为一行 SpecialistAttributes表包含专家所需的所有属性。在医生表中,您有一个外键来查找SpecialistTypeID,因此只能有1个,那么SpecialistType就有许多可以链接到的SpecisLaiStattId
以这种方式组织数据的另一个好处是,您需要添加任何专家角色或属性,无需更改数据库的结构,只需添加更多行即可
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 |
在所有三个表(即儿科医生、骨科医生、牙医)上添加/Before Insert触发器,并在插入之前检查DoctorID是否已经存在。我会以不同的方式安排这一点。我会有三张桌子,一张医生桌,一张专家桌和一张专家桌。医生表包含所有医生的信息,简单。专家表包含您的SpecialistTypeID和SpecialistDescription等。您的3位专家示例将在此表中各为一行。SpecialistAttributes表包含专家所需的所有属性。在医生表中,您有一个外键来查找SpecialistTypeID,因此只能有1个,然后SpecialistTypeID可以链接到多个SpecisLaistAttribute。以这种方式组织数据的另一个好处是,您需要添加任何专家角色或属性,而无需更改数据库的结构,只需添加更多行。您的问题可能有答案。@WalterMitty您的链接确实非常有用。尝试应用第三种技术,但在尝试使用sql server时出错