Работы в рамках дисциплины:
Клиент-серверные системы управления базами данных
Следующая работа:
Лабораторная №2-5 - Управление контактами с клиентами
Полный SQL код:
- Скачать демонстрационную базу данных PostgreSQL с официального сайта - базы данных полетов за один месяц
- Установить базу данных при помощи SQL-скрипта в комплекте с базой.
- Построить ER-диаграмму демонстрационной базы данных (можно использовать сторонний софт для построения диаграмм, например datagrip)
- Составить 20 уникальных и работающих запросов к базе данных
- Предложить 5 способов оптимизации БД (нарушения НФ, оптимизация типизации, оптимизация производительности, ограничения, лишние связи и тд.)
SELECT aircrafts_data.model ->> ‘en’ as Model, COUNT(flight_id) FROM flights
JOIN aircrafts_data ON flights.aircraft_code = aircrafts_data.aircraft_code
GROUP BY aircrafts_data.model
ORDER BY COUNT(flight_id) DESC;
SELECT tickets.passenger_name, COUNT(flight_no) FROM tickets
INNER JOIN ticket_flights ON ticket_flights.ticket_no=tickets.ticket_no
INNER JOIN flights ON ticket_flights.flight_id=flights.flight_id
GROUP BY tickets.passenger_name
HAVING COUNT(flight_no)>100
ORDER BY COUNT(flight_no) DESC;
SELECT tickets.passenger_name, COUNT(flight_no) FROM tickets
INNER JOIN ticket_flights ON ticket_flights.ticket_no=tickets.ticket_no
INNER JOIN flights ON ticket_flights.flight_id=flights.flight_id
WHERE tickets.passenger_name LIKE 'IVAN %'
GROUP BY tickets.passenger_name
HAVING COUNT(flight_no)>100
ORDER BY COUNT(flight_no) DESC;
SELECT flights.aircraft_code, COUNT(aircraft_code) FROM flights
WHERE flights.departure_airport LIKE 'DME'
GROUP BY flights.aircraft_code
ORDER BY COUNT(aircraft_code) DESC;
SELECT tickets.passenger_name FROM tickets
INNER JOIN ticket_flights ON ticket_flights.ticket_no=tickets.ticket_no
INNER JOIN flights ON ticket_flights.flight_id=flights.flight_id
WHERE flights.departure_airport LIKE 'YKS' AND tickets.passenger_name LIKE 'P%';
SELECT tickets.passenger_name, COUNT(flight_no) FROM tickets
INNER JOIN ticket_flights ON ticket_flights.ticket_no=tickets.ticket_no
INNER JOIN flights ON ticket_flights.flight_id=flights.flight_id
WHERE tickets.passenger_name = 'NATALYA IVANOVA'
GROUP BY tickets.passenger_name;
SELECT tickets.passenger_name FROM tickets
INNER JOIN ticket_flights ON ticket_flights.ticket_no=tickets.ticket_no
INNER JOIN flights ON ticket_flights.flight_id=flights.flight_id
WHERE flights.departure_airport = 'VKO' AND flights.arrival_airport = 'PES'
AND flights.status = 'Arrived';
SELECT COUNT(arrival_airport) FROM flights
WHERE flights.arrival_airport = 'KZN'
AND to_char(flights.actual_departure, 'YYYY-MM') = '2017-08';
SELECT SUM(bookings.total_amount) FROM bookings
WHERE bookings.book_date BETWEEN '2017-08-10' and '2017-08-20';
SELECT count(ticket_flights.ticket_no) FROM ticket_flights
JOIN flights ON ticket_flights.flight_id = flights.flight_id
WHERE flights.departure_airport = 'DME'
AND flights.arrival_airport = 'LED'
AND flights.flight_no = 'PG0405'
AND to_char(flights.actual_departure, 'YYYY-MM-DD') = '2017-07-16';
SELECT COUNT(boarding_passes.boarding_no) FROM boarding_passes
WHERE boarding_passes.boarding_no = '1'
OR boarding_passes.boarding_no = '111'
OR boarding_passes.boarding_no = '222'
OR boarding_passes.boarding_no = '333';
SELECT flights.actual_arrival - flights.actual_departure as interval, flights.flight_no FROM flights
WHERE flights.status = ‘Arrived’
ORDER BY interval DESC
limit 1;
SELECT flights.arrival_airport, COUNT(arrival_airport) FROM flights
GROUP BY flights.arrival_airport
ORDER BY COUNT(arrival_airport) DESC
limit 1;
SELECT bookings.book_date, passenger_name FROM bookings
JOIN tickets ON bookings.book_ref = tickets.book_ref
JOIN ticket_flights ON tickets.ticket_no = ticket_flights.ticket_no
JOIN flights ON ticket_flights.flight_id = flights.flight_id
WHERE flights.flight_no = ‘PG0405’
AND to_char(flights.actual_departure, 'YYYY-MM-DD') = ‘2017-07-16’
ORDER BY bookings.book_date
limit 1;
SELECT boarding_no FROM boarding_passes
ORDER BY boarding_no DESC
limit 1;
SELECT total_amount, passenger_name, arrival_airport FROM bookings
JOIN tickets ON bookings.book_ref = tickets.book_ref
JOIN ticket_flights ON tickets.ticket_no = ticket_flights.ticket_no
JOIN flights ON ticket_flights.flight_id = flights.flight_id
ORDER BY total_amount DESC
limit 100;
SELECT SUM(total_amount)/COUNT(total_amount) as medium FROM bookings;
SELECT DISTINCT flights.departure_airport, flights.arrival_airport, COUNT(flights.departure_airport) FROM flights
GROUP BY flights.departure_airport, flights.arrival_airport
ORDER BY COUNT(flights.departure_airport) DESC
limit 1;
SELECT passenger_name, bookings.total_amount FROM tickets
JOIN bookings ON bookings.book_ref = tickets.book_ref
WHERE bookings.total_amount BETWEEN 100000 and 200000;
SELECT aircrafts_data.model ->> ‘en’, range FROM aircrafts_data
ORDER BY range DESC
limit 1;
- В
seats
иboarding_passes
:seat_no
varchar(4)
изменить наvarchar(3)
. - В
flights
:status
varchar(20)
изменить наvarchar(10)
. - В
boarding_passes
,ticket_flights
,tickets
:ticket_no
char
изменить наinteger
. - В
tickets
:passenger_name
text
изменить наvarchar(60)
. - Создать новую таблицу, назвать ее
fare
, в ней сделать два поляfare_id
иfare_conditions
, связать эту таблицу с таблицейseats
по ключуfare_id
кseats.fare_id
. Таким образом мы сохраним категориальную переменную, затратив наименьшее количество памяти.