简体   繁体   English

如何创建此存储过程?

[英]How to create this stored procedure?

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 ? 如何将FLIGHT_PLANincomming_airport字段与AIRPORT代码FLIGHT_PLAN在一起,然后在CITY编写代码并在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 注意:Oracle是区分大小写的,因此请注意西班牙的拼写或使用

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 如果您知道西班牙的代码,则可以在子选择中省略JOIN并直接添加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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM