2011-01-16 4 views
0

나는 목적지가 스페인 인 항공편의 예상 시간을 알 수있는 저장 프로 시저를 만들고 싶습니다 (들어오는 공항은 스페인에 있음). 다음 표가 있습니다.이 저장 프로 시저를 만드는 방법은 무엇입니까?

FLIGHT_PLAN(plan_number NUMBER, outgoing_airport NUMBER, estimated_flight_time NUMBER, incoming_airport NUMBER); 

AIRPORT (code NUMBER, name VARCHAR(20), city_code NUMBER); 

CITY (code NUMBER,name VARCHAR(20),country_code NUMBER); 

COUNTRY (code NUMBER, name VARCHAR(20)); 

어떻게해야합니까? incomming_airport 필드를에 코드를 AIRPORT에 연결 한 다음 CITY에 코드를 입력하고 COUNTRY에 코드를 삽입하려면 어떻게해야합니까?

외래 키를 사용해야합니까?

+0

주어진 비행 시간을 추정하기 어려울 것이다을 당신을 간격 정보 또는 심지어 파생 될 kms를 포함하는 제공했습니다. – RichardTheKiwi

+0

예, 엄청난 누락입니다. 해결되었습니다. – andandandand

답변

0

당신은

FROM COUNTRY C 
inner join CITY I on I.country_code = C.code 
inner join AIRPORT A on A.city_code = I.code 
inner join FLIGHT_PLAN F on F.incoming_airport = A.code 
WHERE C.name = 'Spain' 

주를 사용하여 테이블까지를 연결할 수 있습니다 오라클은 그래서 스페인의 철자를 볼 ASE 민감하거나 스페인의 코드를 알고있는 경우

WHERE UPPER(C.name) = 'SPAIN' 
0
 
SELECT plan_number, 
     outgoing_airport, 
     incoming_airport, 
     estimated_flight_time 
FROM flight_plan 
WHERE incoming_airport IN (SELECT ap.code 
          FROM airport ap 
          JOIN city ct ON ct.code = ap.city_code 
          JOIN country co ON co.code = ct.country_code 
          WHERE co.name = 'Spain') 

를 사용 , 하위 선택에서 JOIN을 생략하고 직접 추가 할 수 있습니다. WHERE ct.country_code = 42

이름과 결과에 공항의 도시, 당신은 메인 쿼리 전년도 공항과 도시에 가입해야합니다 : 스키마에서 정보가없는

 
SELECT fp.plan_number, 
     fp.estimated_flight_time, 
     fp.outgoing_airport, 
     out_ap.name as outgoing_airport_name, 
     out_city.name as outgoing_city_name, 
     fp.incoming_airport, 
     in_ap.name as incoming_airport_name, 
     in_city.name as incoming_city_name 
FROM flight_plan fp 
    JOIN airport in_ap ON in_ap.code = fp.incoming_airport 
    JOIN city in_city ON in_city.code = in_ap.city_code 
    JOIN airport out_ap ON out_ap.code = fp.outgoing_airport 
    JOIN city out_city ON out_city.code = out_ap.city_code 
WHERE incoming_airport IN (SELECT ap.code 
          FROM airport ap 
          JOIN city ct ON ct.code = ap.city_code 
          JOIN country co ON co.code = ct.country_code 
          WHERE co.name = 'Spain') 
+0

특히 WHERE 절에서 두 개의 동일한 쿼리를 수행하는 두 번째 경우에 쿼리가 필요한 이유는 무엇입니까? 사이버 키위 (cyberkiwi)가 지적한 방식으로 해보지 않겠습니까? –

+0

첫 번째 예제에는 조인이 필요하지 않으므로 필요했습니다. 두 번째 예제에서는 단순히 lazyness를 복사하여 붙여 넣기 만합니다. 두 번째 예제에서 country 테이블을 하위 쿼리를 사용할 때와 마찬가지로 유효한 주 쿼리에 추가해야합니다. 아마 맛의 문제 –

관련 문제