2011-10-14 1 views
1

예제 시나리오.두 개의 원본 테이블에 대한 연관 테이블에 공통 외래 키가있는 제약 조건을 제공하는 방법은 무엇입니까?

비행 스케줄 시스템에는 파일럿이 날 수있는 비행기를 나타내는 plane_type 테이블을 나타내는 pilot 테이블이 있습니다 (다 대일 관계라고 가정 할 때).

plane 테이블도 있고 plane_type 테이블을 참조하여 비행기 유형 (다 대일 관계)을 나타냅니다.

이제 주어진 비행에 대해 pilotplane에 할당하는 연관 테이블 flight_plan이 있습니다.

pilot의 자격이 해당 항공편의 plane 유형과 일치하는지 어떻게 확인합니까?

데이터베이스 디자인에서 이것을 제약 조건으로 구현할 수 있습니까? 고맙습니다.

편집 :

, 어떻게 pilot.plane_typeplane.plane_type 같다 확인하기 위해 아래 그림을 다스 려?

enter image description here

답변

1

PlanePlaneID, PlaneTypeID

enter image description here

편집

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) ; 
+0

밤은이 솔루션에 고유 인덱스 (AK)가 바로 각 PlaneType은 하나 개의 평면을 가질 수 있습니다 보장? –

+0

@CYT NO. 각 PlaneType은 많은 평면을 가질 수 있습니다. 그것은 Plane에 배정 된 Pilot이 그 비행기에 대한 자격을 가지고 있음을 보증합니다. PLanePilot의 외래 키는 FK1'PlanePilot (PilotID, PlaneTypeID) REFERENCES PilotQualification (PilotID, PlaneTypeID)'및 FK2' PlanePilot (PlaneID, PlaneTypeID) 참조 평면 (PlaneID, PlaneTypeID) 및 FK3' PlanePilot (파일럿 ID)입니다.)' –

+0

FK1의'PlaneTypeID'와 FK2의'PlaneTypeID'는'PlanePilot'에서 같은 열입니까? 데이터베이스가 두 열 모두 동일한 값을 갖는 것을 어떻게 보장합니까? –