Database design 如何为与具有公共外键的两个源表相关的关联表提供约束?

Database design 如何为与具有公共外键的两个源表相关的关联表提供约束?,database-design,constraints,data-integrity,Database Design,Constraints,Data Integrity,示例场景 在飞行计划系统中,有一个飞行员表,该表指的是一个飞机类型表,指示飞行员能够飞行的飞机(假设是多对一关系) 还有一个平面表格,它引用平面_类型表格来指示平面的类型 (也是多对一关系) 现在有一个关联表flight\u plan,它为给定的航班将飞行员分配给飞机 我如何确保飞行员的资格与本次航班的飞机类型相匹配 有没有可能在数据库设计中实现这一约束?多谢各位 编辑: 参考下图,如何确保pilot.plane\u type等于plane.plane\u type 平面在平面ID、平面类型ID

示例场景

在飞行计划系统中,有一个
飞行员
表,该表指的是一个
飞机类型
表,指示飞行员能够飞行的飞机(假设是多对一关系)

还有一个
平面
表格,它引用
平面_类型
表格来指示平面的类型 (也是多对一关系)

现在有一个关联表
flight\u plan
,它为给定的航班将
飞行员
分配给
飞机

我如何确保飞行员的资格与本次航班的飞机类型相匹配

有没有可能在数据库设计中实现这一约束?多谢各位

编辑:

参考下图,如何确保
pilot.plane\u type
等于
plane.plane\u type


平面
平面ID、平面类型ID

编辑

create table Pilot (PilotID integer);
alter table Pilot add constraint PK_Pilot primary key (PilotID);

create table PlaneType (PlaneTypeID integer);
alter table PlaneType add constraint PK_PlaneType primary key (PlaneTypeID);

create table PilotQualification (PilotID integer, PlaneTypeID integer);
alter table PilotQualification 
  add constraint  PK_PilotQual primary key (PilotID, PlaneTypeID)
, add constraint FK1_PilotQual foreign key (PilotID)     references Pilot(PilotID)
, add constraint FK2_PilotQual foreign key (PlaneTypeID) references PlaneType(PlaneTypeID) ;

create table Plane (PlaneID integer, PlaneTypeID integer);
alter table Plane
  add constraint  PK_Plane primary key (PlaneID)
, add constraint FK1_Plane foreign key (PlaneTypeID) references PlaneType(PlaneTypeID) ;
create unique index AK_Plane on Plane (PlaneID, PlaneTypeID) ;

create table PlanePilot (PlaneID integer, PlaneTypeID integer, PilotID integer) ;
alter table PlanePilot
  add constraint  PK_PlanePilot primary key (PlaneID, PlaneTypeID, PilotID)
, add constraint FK1_PlanePilot foreign key (PilotID, PlaneTypeID) references PilotQualification(PilotID, PlaneTypeID)
, add constraint FK2_PlanePilot foreign key (PlaneID, PlaneTypeID) references Plane(PlaneID, PlaneTypeID)
, add constraint FK3_PlanePilot foreign key (PilotID) references Pilot(PilotID) ;

平面
平面ID、平面类型ID

编辑

create table Pilot (PilotID integer);
alter table Pilot add constraint PK_Pilot primary key (PilotID);

create table PlaneType (PlaneTypeID integer);
alter table PlaneType add constraint PK_PlaneType primary key (PlaneTypeID);

create table PilotQualification (PilotID integer, PlaneTypeID integer);
alter table PilotQualification 
  add constraint  PK_PilotQual primary key (PilotID, PlaneTypeID)
, add constraint FK1_PilotQual foreign key (PilotID)     references Pilot(PilotID)
, add constraint FK2_PilotQual foreign key (PlaneTypeID) references PlaneType(PlaneTypeID) ;

create table Plane (PlaneID integer, PlaneTypeID integer);
alter table Plane
  add constraint  PK_Plane primary key (PlaneID)
, add constraint FK1_Plane foreign key (PlaneTypeID) references PlaneType(PlaneTypeID) ;
create unique index AK_Plane on Plane (PlaneID, PlaneTypeID) ;

create table PlanePilot (PlaneID integer, PlaneTypeID integer, PilotID integer) ;
alter table PlanePilot
  add constraint  PK_PlanePilot primary key (PlaneID, PlaneTypeID, PilotID)
, add constraint FK1_PlanePilot foreign key (PilotID, PlaneTypeID) references PilotQualification(PilotID, PlaneTypeID)
, add constraint FK2_PlanePilot foreign key (PlaneID, PlaneTypeID) references Plane(PlaneID, PlaneTypeID)
, add constraint FK3_PlanePilot foreign key (PilotID) references Pilot(PilotID) ;

这个解决方案不就是保证每种机型只能有一个平面吗?@CYT NO.每种机型可以有多个平面。它确保分配给飞机的飞行员具备该飞机的资格。PLanePilot上的外键是FK1
PLanePilot(PilotID,PlaneTypeID)REFERENCES PilotQualification(PilotID,PlaneTypeID)
和FK2
PLanePilot(PlaneID,PlaneTypeID)REFERENCES Plane(PlaneTypeID)
和FK3
PLanePilot(PilotID)REFERENCES PilotID(PilotID)
FK1中的
PlaneTypeID
和FK2中的
PlaneTypeID
PlanePilot
中是同一列?如果不是,数据库如何确保两列具有相同的值?如果您将“飞行员已分配[飞机飞行员]飞行计划”重述为“合格飞行员已分配飞行计划”,并删除飞行员和飞机飞行员之间不必要的FK,那么我想说这是可行的。这个解决方案不就是确保每个飞机类型只能有一个平面吗?@CYT NO。每个飞机类型可以有多个平面。它确保分配给飞机的飞行员具备该飞机的资格。PLanePilot上的外键是FK1
PLanePilot(PilotID,PlaneTypeID)REFERENCES PilotQualification(PilotID,PlaneTypeID)
和FK2
PLanePilot(PlaneID,PlaneTypeID)REFERENCES Plane(PlaneTypeID)
和FK3
PLanePilot(PilotID)REFERENCES PilotID(PilotID)
FK1中的
PlaneTypeID
和FK2中的
PlaneTypeID
PlanePilot
中是同一列?如果不是,数据库如何确保这两个列具有相同的值?如果您将“飞行员已分配[飞机飞行员]飞行计划”重述为“合格飞行员已分配飞行计划”,并删除飞行员和飞机飞行员之间不必要的FK,那么我认为这是可行的。