1

シナリオ例。

フライトスケジュールシステムには、パイロットが飛行できる飛行機を示すpilotテーブルを参照するテーブルがあります(これは多対1の関係であると想定しています)。plane_type

テーブルもあり、プレーンのタイプ(多対1の関係)を示すためplaneにテーブルを参照します。plane_type

これで、特定のフライトのにflight_planを割り当てる連想テーブルができました。pilotplane

の資格がこのフライトののタイプと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 に答える