@@ -4,22 +4,22 @@ __TODO__ : add clear and small examples in relevance to complex queries.
4
4
5
5
## import demo database inside postgres container
6
6
7
- We are going to create a docker compose file to move the demo database into the image and
7
+ We are going to create a docker compose file to move the demo database into the image and
8
8
then open it
9
9
- download demo database from : [ https://edu.postgrespro.com/demo-small-en.zip ] ( https://edu.postgrespro.com/demo-small-en.zip )
10
- - go to terminal
11
- - ` vim .env `
12
- - paste the following and enter the valid paths:
13
- ``` text
14
- DEMO_DATABASE_PATH="<path to demo file>/demo-small-en-20170815.sql"
15
- DATABASE_BACKUP="<path to db backup"
16
- #POSTGRES_DB="flight"
17
- POSTGRES_USER="flight"
18
- POSTGRES_PASSWORD="flight"
19
- ```
20
-
21
- - `vim docker-compose.yml`
22
- - paste the following
10
+ - go to terminal
11
+ - ` vim .env `
12
+ - paste the following and enter the valid paths:
13
+ ``` text
14
+ DEMO_DATABASE_PATH="<path to demo file>/demo-small-en-20170815.sql"
15
+ DATABASE_BACKUP="<path to db backup"
16
+ #POSTGRES_DB="flight"
17
+ POSTGRES_USER="flight"
18
+ POSTGRES_PASSWORD="flight"
19
+ ```
20
+
21
+ - `vim docker-compose.yml`
22
+ - paste the following
23
23
```yaml
24
24
version: '3'
25
25
services:
@@ -36,65 +36,65 @@ then open it
36
36
- ${DEMO_DATABASE_PATH}:/demo-small-en-20170815.sql
37
37
- ${DATABASE_BACKUP}:/var/lib/postgresql/data
38
38
```
39
-
40
- - run : ` docker composer up -d `
41
- - ` docker exec -it postgresintro psql -d flight -U flight `
42
- - ` \i demo-small-en-20170815.sql `
43
- - db will automatically change to demo
44
39
45
- - to change to database demo next time,
46
- - ` docker exec -it postgresintro psql -d demo -U flight `
47
- - OR : ` \c demo ` inside psql
40
+ - run : ` docker composer up -d `
41
+ - ` docker exec -it postgresintro psql -d flight -U flight `
42
+ - ` \i demo-small-en-20170815.sql `
43
+ - db will automatically change to demo
44
+
45
+ - to change to database demo next time,
46
+ - ` docker exec -it postgresintro psql -d demo -U flight `
47
+ - OR : ` \c demo ` inside psql
48
48
49
49
___
50
50
51
51
## DEMO database
52
52
53
53
### Schema
54
54
- The demo database imported contains statistics on all the flights of an imaginary airline company of one month time-period
55
- - In postgres, schema is a named collection of database objects, including tables, views, functions, i.e, a collection
56
- logical structures of data.
57
- - The main entity in our demo database's schema is a __ booking__ (mapped to bookings table).
58
- - Each booking includes several passengers with a __ separate__ ticket issued for each passenger(tickets).
59
- - we assume each passenger is unique , there is no unique-id issued for each passenger as a person
60
- - Each ticket always contain one or more flight segments(ticker_flights).
61
- - if there are no direct flights with departure to destination, and it's not round trip ticket then a ticket can
62
- have multiple flight segments included.
63
- - it's assumed all tickets in a single booking has the same flight segment. Even though schema does not contain
64
- any such restrictions.
65
- - Each flight('flight') goes from one airport('airports') to another .
66
- - Flights with the same flight number have the same points of departure and destination but different departure
67
- dates.
68
- - At flight check-in, each passenger is issued a boarding pass (boarding_passes), where the seat number is specified.
69
- - The flight/seat combination must be unique
55
+ - In postgres, schema is a named collection of database objects, including tables, views, functions, i.e, a collection
56
+ logical structures of data.
57
+ - The main entity in our demo database's schema is a __ booking__ (mapped to bookings table).
58
+ - Each booking includes several passengers with a __ separate__ ticket issued for each passenger(tickets).
59
+ - we assume each passenger is unique , there is no unique-id issued for each passenger as a person
60
+ - Each ticket always contain one or more flight segments(ticker_flights).
61
+ - if there are no direct flights with departure to destination, and it's not round trip ticket then a ticket can
62
+ have multiple flight segments included.
63
+ - it's assumed all tickets in a single booking has the same flight segment. Even though schema does not contain
64
+ any such restrictions.
65
+ - Each flight('flight') goes from one airport('airports') to another .
66
+ - Flights with the same flight number have the same points of departure and destination but different departure
67
+ dates.
68
+ - At flight check-in, each passenger is issued a boarding pass (boarding_passes), where the seat number is specified.
69
+ - The flight/seat combination must be unique
70
70
71
71
> you can use \d+ command to understand tables within terminal too.
72
- > \d+ bookings
72
+ > \d+ bookings
73
73
> \d+ aircrafts
74
74
75
75
### Bookings
76
- - To fly with our airline, passengers book the required tickets in advance (book_date, which must be within one month
77
- before the flight). The booking is identified by its number (book_ref, a six-position combination of letters and digits).
76
+ - To fly with our airline, passengers book the required tickets in advance (book_date, which must be within one month
77
+ before the flight). The booking is identified by its number (book_ref, a six-position combination of letters and digits).
78
78
- The total_amount field stores the total price of all tickets included into the booking, for all passengers.
79
79
80
80
### Tickets
81
81
82
82
- A ticket has a unique number (ticket_no), which consists of 13 digits.
83
- - Ticker contains passenger_id, passenger_name and their contact_data
83
+ - Ticker contains passenger_id, passenger_name and their contact_data
84
84
85
85
### Flight Segments
86
86
87
- - A flight segment connects a ticket with a flight, identified by a number
87
+ - A flight segment connects a ticket with a flight, identified by a number
88
88
- Each flight segment has its price (amount) and travel class(fare_conditions).
89
89
90
90
### Flights
91
91
92
92
- natural composite key of the flights table consists of the flight number (flight_no) and the date of the departure
93
- (scheduled_departure). To make foreign keys that refer to this table a bit shorter, a surrogate key flight_id is used as
94
- the primary key.
93
+ (scheduled_departure). To make foreign keys that refer to this table a bit shorter, a surrogate key flight_id is used as
94
+ the primary key.
95
95
96
96
- A flight always connects two points: departure_airport and arrival_airport.
97
- - There's no entity like "connecting flight" , in such cases . Ticket contains all flight segments.
97
+ - There's no entity like "connecting flight" , in such cases . Ticket contains all flight segments.
98
98
- Each flight has time of scheduled_departure and scheduled_arrival, actual_departure and actual_arrival times may differ.
99
99
100
100
#### Flight status
@@ -108,8 +108,8 @@ the primary key.
108
108
### Airports
109
109
110
110
- identified by a three-letter airport_code and has an airport_name.
111
- - a city name is simply an airport attribute, which is required to identify all the airports of the same city.
112
- - The table also includes coordinates (longitude and latitude) and the timezone.
111
+ - a city name is simply an airport attribute, which is required to identify all the airports of the same city.
112
+ - The table also includes coordinates (longitude and latitude) and the timezone.
113
113
### Boarding Passes
114
114
- the boarding pass is identified by the combination of ticket and flight numbers.
115
115
- Boarding pass numbers (boarding_no) are assigned sequentially and has seat number (seat_no).
@@ -132,7 +132,7 @@ __NOTE__: View is a query stored in postgres database server.
132
132
### The “now” Function
133
133
- The demo database contains a snapshot of data
134
134
- The snapshot time is saved in the ` bookings.now ` function.
135
- - use this function in demo queries for cases that would typically require calling the ` now ` function.
135
+ - use this function in demo queries for cases that would typically require calling the ` now ` function.
136
136
- the return value of this function determines the version of the demo database: ` SELECT bookings.now(); `
137
137
138
138
#### Simple Queries
@@ -158,7 +158,7 @@ AND f.scheduled_departure::date =
158
158
bookings .now ()::date - INTERVAL ' 2 day'
159
159
AND bp .seat_no = ' 1A' ;
160
160
```
161
- __ NOTE__ : '::' is used to data from one type to another :
161
+ __ NOTE__ : '::' is used to data from one type to another :
162
162
163
163
164
164
> Problem: How many seats remained free on flight PG0404 yesterday?
@@ -221,7 +221,7 @@ GROUP BY f.flight_no,
221
221
in first for all their flights. Take into account only those
222
222
passengers who took at least two flights.
223
223
224
- > Solution : boarding pass numbers are issued in the check-in order.
224
+ > Solution : boarding pass numbers are issued in the check-in order.
225
225
``` SQL
226
226
SELECT t .passenger_name ,
227
227
t .ticket_no
@@ -277,10 +277,10 @@ WINDOW w AS ( PARTITION BY tf.ticket_no
277
277
#### Arrays
278
278
> Problem. Find the round-trip tickets in which the outbound route differs from the inbound one.
279
279
> A round-trip ticket where the outbound route differs from the inbound route is typically referred to as a "multi-city"
280
- > or "multi-destination" ticket. With this type of ticket, travelers can fly from one location to another
280
+ > or "multi-destination" ticket. With this type of ticket, travelers can fly from one location to another
281
281
> (the outbound route), and then return from a different location (the inbound route).
282
282
283
- > Solution:
283
+ > Solution:
284
284
``` SQL
285
285
SELECT r1 .departure_airport ,
286
286
r1 .arrival_airport ,
@@ -349,8 +349,8 @@ WHERE p.last_arrival = p.destination;
349
349
```
350
350
351
351
> Problem. What is the maximum number of connections that can be required to get from any airport to any other airport?
352
- > Solution: We can take the previous query as the basis for the solution. However, the first iteration must now contain
353
- > all the possible airport pairs, not just a single pair: each airport must be connected to all the other airports.
352
+ > Solution: We can take the previous query as the basis for the solution. However, the first iteration must now contain
353
+ > all the possible airport pairs, not just a single pair: each airport must be connected to all the other airports.
354
354
> For all these pairs of airports we first find the shortest path, and then select the longest of them.
355
355
356
356
``` SQL
0 commit comments