1

示例场景。

在航班时刻表系统中,有一个pilot表格,它指的是一个plane_type表格,指示飞行员能够驾驶的飞机(假设这是一个多对一的关系)。

还有一个plane表格,它引用plane_type表格来指示飞机的类型(也是多对一关系)。

现在有一个关联表flight_plan,它将 a 分配给pilotplane定航班的 a。

如何确保pilot' 的资格与plane此航班的 ' 类型相匹配?

是否有可能将其作为数据库设计中的约束来实现?谢谢你。

编辑:

参考下图,如何确定pilot.plane_type等于plane.plane_type

在此处输入图像描述

4

1 回答 1

1

Plane具有唯一索引 (AK)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) ;
于 2011-10-14T13:04:56.630 回答