-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairlineCreateTables.sql
131 lines (107 loc) · 3.9 KB
/
airlineCreateTables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
DROP TABLE IF EXISTS city;
CREATE TABLE city
(
city_name TEXT NOT NULL,
city_timezone TEXT NOT NULL,
PRIMARY KEY (city_name)
);
DROP TABLE IF EXISTS aircraft;
CREATE TABLE aircraft
(
aircraft_code NUMERIC(3) UNIQUE NOT NULL,
aircraft_name VARCHAR(20) UNIQUE NOT NULL,
aircraft_capacity INTEGER NOT NULL check(aircraft_capacity >= 50 and aircraft_capacity <= 300),
aircraft_range INTEGER NOT NULL check(aircraft_range >= 7000 and aircraft_range <= 10000),
PRIMARY KEY (aircraft_code)
);
DROP TABLE IF EXISTS airport;
CREATE TABLE airport
(
airport_code CHAR(3) UNIQUE NOT NULL,
airport_name TEXT UNIQUE NOT NULL,
airport_city_name TEXT NOT NULL,
PRIMARY KEY (airport_code),
FOREIGN KEY (airport_city_name) REFERENCES city(city_name)
);
DROP TABLE IF EXISTS passenger;
CREATE TABLE passenger
(
passenger_id CHAR(10) UNIQUE NOT NULL,
passenger_firstname VARCHAR(20) NOT NULL,
passenger_lastname VARCHAR(20) NOT NULL,
passenger_phone NUMERIC(10) UNIQUE NOT NULL,
passenger_email VARCHAR(50) UNIQUE NOT NULL,
PRIMARY KEY (passenger_id)
);
DROP TABLE IF EXISTS flight;
CREATE TABLE flight
(
flight_code CHAR(6) NOT NULL,
departure_date DATE NOT NULL,
departure_airport CHAR(3),
arrival_airport CHAR(3),
distance INTEGER NOT NULL,
scheduled_departure_time TIME NOT NULL,
scheduled_arrival_time TIME NOT NULL,
scheduled_duration INTEGER NOT NULL,
actual_departure_time TIME,
actual_arrival_time TIME,
flight_status TEXT NOT NULL,
aircraft_code NUMERIC(3),
PRIMARY KEY (flight_code , departure_date),
FOREIGN KEY (departure_airport) REFERENCES airport(airport_code),
FOREIGN KEY (arrival_airport) REFERENCES airport(airport_code),
FOREIGN KEY (aircraft_code) REFERENCES aircraft(aircraft_code),
CHECK(departure_airport != arrival_airport),
CHECK(distance >= 1000 and distance <= 7000),
CHECK(flight_status= 'Scheduled' OR flight_status='On Time'OR flight_status='Delayed' OR flight_status='Departed' OR flight_status='Arrived' OR flight_status='Cancelled' )
);
DROP TABLE IF EXISTS booking;
CREATE TABLE booking
(
book_ref CHAR(6) NOT NULL,
total_amount INTEGER NOT NULL,
book_date DATE NOT NULL,
flight_code VARCHAR(6) NOT NULL,
departure_date DATE NOT NULL,
PRIMARY KEY (book_ref),
FOREIGN KEY (flight_code, departure_date) REFERENCES flight(flight_code, departure_date) ON UPDATE CASCADE ON DELETE CASCADE,
CHECK(total_amount > 0),
CHECK(book_date >= departure_date - INTERVAL '1' MONTH) -- yyyy-mm-dd
);
DROP TABLE IF EXISTS ticket;
CREATE TABLE ticket
(
ticket_no NUMERIC(13) NOT NULL,
passenger_id VARCHAR(10),
book_ref VARCHAR(6),
PRIMARY KEY (ticket_no),
FOREIGN KEY (passenger_id) REFERENCES passenger(passenger_id),
FOREIGN KEY (book_ref) REFERENCES booking(book_ref) ON UPDATE CASCADE ON DELETE CASCADE
);
DROP TABLE IF EXISTS more_flights;
CREATE TABLE more_flights
(
ticket_no NUMERIC(13),
flight_code CHAR(6) NOT NULL,
departure_date DATE NOT NULL ,
amount INTEGER NOT NULL,
fare TEXT NOT NULL,
PRIMARY KEY (flight_code , departure_date , ticket_no),
FOREIGN KEY (flight_code, departure_date) REFERENCES flight(flight_code, departure_date) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (ticket_no) REFERENCES ticket(ticket_no) ON UPDATE CASCADE ON DELETE CASCADE,
CHECK(amount > 0),
CHECK(fare ='Economy' OR fare = 'Business' OR fare = 'First Class')
);
DROP TABLE IF EXISTS boarding_pass;
CREATE TABLE boarding_pass
(
seat_no CHAR(3) NOT NULL,
flight_code CHAR(6) NOT NULL,
departure_date DATE NOT NULL,
ticket_no NUMERIC(13),
boarding_no INTEGER NOT NULL,
PRIMARY KEY (seat_no, flight_code , departure_date ),
FOREIGN KEY (flight_code, departure_date) REFERENCES flight(flight_code, departure_date) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (ticket_no) REFERENCES ticket(ticket_no) ON UPDATE CASCADE ON DELETE CASCADE
);