Plane
上PlaneID, PlaneTypeID
编辑
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) ;
心不是该解决方案具有独特的指数(AK)只是保证了每个PlaneType只能有一个平面? –
@CYT NO。每个PlaneType可以有多个Planes。它确保分配给飞机的飞行员具有该飞机的资格。 PLanePilot上的外键是FK1'PlanePilot(PilotID,PlaneTypeID)REFERENCES PilotQualification(PilotID,PlaneTypeID)'和FK2' PlanePilot(PlaneID,PlaneTypeID)REFERENCES Plane(PlaneID,PlaneTypeID)'和FK3' PlanePilot(PilotID)REFERENCES Pilot(PilotID )' –
FK1中的PlaneTypeID和FK2中的PlaneTypeID是PlanePilot中的同一列?如果不是数据库如何确保两列具有相同的值? –