Repository navigation
Expand file tree
/
Copy pathSchema.sql
More file actions
206 lines (177 loc) · 6.72 KB
/
Copy pathSchema.sql
File metadata and controls
206 lines (177 loc) · 6.72 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
-- ============================================================
-- CS307 Spring 2026 - Project Part 1
-- PostgreSQL DDL
-- ============================================================
-- ============================================================
-- 0. Clean slate (drop in reverse FK order)
-- ============================================================
drop table if exists ticket_order cascade;
drop table if exists ticket cascade;
drop table if exists flight cascade;
drop table if exists passenger cascade;
drop table if exists users cascade;
drop table if exists airport cascade;
drop table if exists airline cascade;
drop table if exists region cascade;
-- ============================================================
-- 1. region
-- ============================================================
create table region (
region_code char(2) not null,
region_name varchar(100) not null,
constraint pk_region primary key (region_code),
constraint uq_region_name unique (region_name)
);
comment on column region.region_code is
'ISO 3166-1 alpha-2 code. For code-less territories, use generated synthetic code.';
-- ============================================================
-- 2. airline
-- ============================================================
create table airline (
airline_id serial not null,
airline_code char(2) not null,
airline_name varchar(200) not null,
country char(2),
constraint pk_airline primary key (airline_id),
constraint uq_airline_code unique (airline_code),
constraint fk_airline_region
foreign key (country) references region (region_code)
on update cascade on delete restrict
);
-- ============================================================
-- 3. airport
-- ============================================================
create table airport (
airport_id serial not null,
iata_code char(3) not null,
airport_name varchar(200) not null,
city varchar(100) not null,
country char(2),
region_code char(2),
latitude decimal(9,6) not null,
longitude decimal(9,6) not null,
altitude int not null,
timezone_offset smallint not null,
timezone_dst char(1) not null,
timezone_region varchar(50) not null,
constraint pk_airport primary key (airport_id),
constraint uq_airport_iata unique (iata_code),
constraint fk_airport_region
foreign key (region_code) references region (region_code)
on update cascade on delete restrict
);
comment on column airport.region_code is
'Region code kept explicitly and linked to region(region_code).';
-- ============================================================
-- 4. users
-- Each app user can own multiple passengers.
-- ============================================================
create table users (
id bigserial primary key,
username varchar(50) not null unique,
email varchar(100) unique,
password varchar(255) not null,
created_at timestamp default current_timestamp
);
-- ============================================================
-- 5. passenger
-- New requirement: each passenger belongs to one user.
-- ============================================================
create table passenger (
passenger_id serial not null,
user_id bigint not null,
name varchar(100) not null,
gender varchar(10)
check (gender in ('Male', 'Female', 'Unknown')),
age smallint
check (age between 0 and 150),
phone_number varchar(20) unique,
constraint pk_passenger primary key (passenger_id),
constraint fk_passenger_user
foreign key (user_id) references users (id)
on update cascade on delete restrict
);
-- ============================================================
-- 6. flight
-- ============================================================
create table flight (
flight_id serial not null,
flight_number varchar(10) not null,
airline_id int not null,
departure_airport_id int not null,
arrival_airport_id int not null,
departure_time time not null,
arrival_time time not null,
arrival_next_day boolean not null default false,
constraint pk_flight primary key (flight_id),
constraint uq_flight_business_key unique (
flight_number,
airline_id,
departure_airport_id,
arrival_airport_id,
departure_time,
arrival_time,
arrival_next_day
),
constraint fk_flight_airline
foreign key (airline_id) references airline (airline_id)
on update cascade on delete restrict,
constraint fk_flight_departure
foreign key (departure_airport_id) references airport (airport_id)
on update cascade on delete restrict,
constraint fk_flight_arrival
foreign key (arrival_airport_id) references airport (airport_id)
on update cascade on delete restrict,
constraint chk_different_airports
check (departure_airport_id <> arrival_airport_id)
);
comment on column flight.arrival_next_day is
'True when arrival is on the next calendar day.';
-- ============================================================
-- 7. ticket
-- ============================================================
create table ticket (
ticket_id serial not null,
flight_id int not null,
flight_date date not null,
seat_class varchar(10) not null
check (seat_class in ('Business', 'Economy')),
price decimal(10,2) not null check (price >= 0),
seats_available smallint not null check (seats_available >= 0),
constraint pk_ticket primary key (ticket_id),
constraint uq_ticket unique (flight_id, flight_date, seat_class),
constraint fk_ticket_flight
foreign key (flight_id) references flight (flight_id)
on update cascade on delete cascade
);
-- ============================================================
-- 8. ticket_order
-- ============================================================
create table ticket_order (
order_id serial not null,
passenger_id int not null,
ticket_id int not null,
order_time TIMESTAMP NOT NULL DEFAULT NOW(),
constraint pk_ticket_order primary key (order_id),
constraint uq_ticket_order unique (passenger_id, ticket_id),
constraint fk_order_passenger
foreign key (passenger_id) references passenger (passenger_id)
on update cascade on delete restrict,
constraint fk_order_ticket
foreign key (ticket_id) references ticket (ticket_id)
on update cascade on delete restrict
);
-- ============================================================
-- FK dependency order (no cycles)
-- region
-- -> airline
-- -> airport
-- airport + airline
-- -> flight
-- users
-- -> passenger
-- flight
-- -> ticket
-- passenger + ticket
-- -> ticket_order
-- ============================================================