I want to create a stored procedure that allows me to know the estimated time of the flights with Spain as destination (the incoming airport is in Spain). And I have the following tables:
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));
How can I go through this? How can I chain the incomming_airport
field in FLIGHT_PLAN
with code in AIRPORT
and then code in CITY
and code in COUNTRY
?
Should I use foreign keys or what?
You can link the tables up using
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'
Note: Oracle is ase sensitive so watch the spelling of Spain, or use
WHERE UPPER(C.name) = 'SPAIN'
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')
If you know the code for spain, you can leave out the JOIN in the sub-select and directly add WHERE ct.country_code = 42
If you need the name and city of the airport in the result, you'll have to join the main query agains airport and city:
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.