RelationalSchema: RouteMK.2.sql

File RouteMK.2.sql, 9.6 KB (added by 221514, 3 weeks ago)
Line 
1DROP TABLE IF EXISTS child_ticket CASCADE;
2DROP TABLE IF EXISTS ticket_relations CASCADE;
3DROP TABLE IF EXISTS ticket CASCADE;
4DROP TABLE IF EXISTS payment CASCADE;
5DROP TABLE IF EXISTS review CASCADE;
6DROP TABLE IF EXISTS student_ticket CASCADE;
7DROP TABLE IF EXISTS student CASCADE;
8DROP TABLE IF EXISTS trip_stops CASCADE;
9DROP TABLE IF EXISTS trip_days_active CASCADE;
10DROP TABLE IF EXISTS trip CASCADE;
11DROP TABLE IF EXISTS route CASCADE;
12DROP TABLE IF EXISTS favorite CASCADE;
13DROP TABLE IF EXISTS driver_drives_on_trip CASCADE;
14DROP TABLE IF EXISTS driver_vehicle_operation CASCADE;
15DROP TABLE IF EXISTS driver CASCADE;
16DROP TABLE IF EXISTS transport_organizer CASCADE;
17DROP TABLE IF EXISTS admin CASCADE;
18DROP TABLE IF EXISTS account CASCADE;
19DROP TABLE IF EXISTS location CASCADE;
20DROP TABLE IF EXISTS train CASCADE;
21DROP TABLE IF EXISTS van CASCADE;
22DROP TABLE IF EXISTS bus CASCADE;
23DROP TABLE IF EXISTS automobile CASCADE;
24DROP TABLE IF EXISTS vehicle CASCADE;
25
26CREATE TABLE account
27(
28 account_id SERIAL PRIMARY KEY,
29 email VARCHAR(100) NOT NULL UNIQUE,
30 name VARCHAR(50) NOT NULL,
31 surname VARCHAR(50) NOT NULL,
32 password VARCHAR(60) NOT NULL
33);
34
35CREATE TABLE admin
36(
37 admin_id SERIAL PRIMARY KEY,
38 account_id INT NOT NULL,
39 CONSTRAINT fk_admin_account_id FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE
40);
41
42CREATE TABLE payment
43(
44 payment_id SERIAL PRIMARY KEY,
45 account_id INT NOT NULL,
46 date DATE NOT NULL,
47 total_price DOUBLE PRECISION NOT NULL CHECK (total_price >= 0),
48 n_tickets INT NOT NULL CHECK (n_tickets > 0),
49 CONSTRAINT payment_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE
50);
51
52CREATE TABLE location
53(
54 location_id SERIAL PRIMARY KEY,
55 latitude DOUBLE PRECISION NOT NULL,
56 longitude DOUBLE PRECISION NOT NULL,
57 name VARCHAR(100) NOT NULL
58);
59
60CREATE TABLE transport_organizer
61(
62 transport_organizer_id SERIAL PRIMARY KEY,
63 account_id INT NOT NULL,
64 company_name VARCHAR(100) NOT NULL,
65 company_embg VARCHAR(50) NOT NULL,
66 CONSTRAINT transport_organizer_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE
67);
68
69CREATE TABLE route
70(
71 route_id SERIAL PRIMARY KEY,
72 transport_organizer_id INT NOT NULL,
73 from_location_id INT NOT NULL,
74 to_location_id INT NOT NULL,
75 CONSTRAINT route_transport_organizer_id_fkey FOREIGN KEY (transport_organizer_id) REFERENCES transport_organizer (transport_organizer_id) ON DELETE CASCADE,
76 CONSTRAINT route_from_location_id_fkey FOREIGN KEY (from_location_id) REFERENCES location (location_id),
77 CONSTRAINT route_to_location_id_fkey FOREIGN KEY (to_location_id) REFERENCES location (location_id)
78);
79
80CREATE TABLE trip
81(
82 trip_id SERIAL PRIMARY KEY,
83 base_price DOUBLE PRECISION,
84 transport_organizer_id INT NOT NULL,
85 route_id INT NOT NULL,
86 free_seats INT CHECK (free_seats >= 0),
87 date DATE NOT NULL,
88 status VARCHAR(30),
89 CONSTRAINT trip_transport_organizer_id_fkey FOREIGN KEY (transport_organizer_id) REFERENCES transport_organizer (transport_organizer_id),
90 CONSTRAINT trip_route_id_fkey FOREIGN KEY (route_id) REFERENCES route (route_id) ON DELETE CASCADE
91);
92
93CREATE TABLE ticket
94(
95 ticket_id SERIAL PRIMARY KEY,
96 trip_id INT NOT NULL,
97 gets_on_location_id INT NOT NULL,
98 gets_off_location_id INT NOT NULL,
99 account_id INT NOT NULL,
100 date_purchased DATE NOT NULL,
101 time_purchased TIME WITHOUT TIME ZONE NOT NULL,
102 price DOUBLE PRECISION CHECK (price >= 0),
103 seat VARCHAR(10),
104 payment_id INT NOT NULL,
105 ticket_related_with_id INT,
106 CONSTRAINT gets_on_location_fkey FOREIGN KEY (gets_on_location_id) REFERENCES location (location_id) ON DELETE CASCADE,
107 CONSTRAINT gets_off_location_fkey FOREIGN KEY (gets_off_location_id) REFERENCES location (location_id) ON DELETE CASCADE,
108 CONSTRAINT ticket_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE,
109 CONSTRAINT ticket_payment_id_fkey FOREIGN KEY (payment_id) REFERENCES payment (payment_id) ON DELETE CASCADE,
110 CONSTRAINT trip_id_fkey FOREIGN KEY (trip_id) REFERENCES trip (trip_id) ON DELETE CASCADE,
111 CONSTRAINT ticket_related_with_id_fk FOREIGN KEY (ticket_related_with_id) REFERENCES ticket(ticket_id) ON DELETE CASCADE
112);
113
114CREATE TABLE review
115(
116 review_id SERIAL PRIMARY KEY,
117 account_id INT NOT NULL,
118 ticket_id INT NOT NULL,
119 description TEXT,
120 rating INT CHECK (rating >= 1 AND rating <= 5),
121 CONSTRAINT review_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE,
122 CONSTRAINT review_ticket_id_fkey FOREIGN KEY (ticket_id) REFERENCES ticket (ticket_id) ON DELETE CASCADE
123);
124
125CREATE TABLE vehicle
126(
127 transport_organizer_id INT NOT NULL,
128 vehicle_id SERIAL PRIMARY KEY,
129 model VARCHAR(30) NOT NULL,
130 brand VARCHAR(30) NOT NULL,
131 capacity VARCHAR(20) NOT NULL,
132 year_manufactured VARCHAR(10),
133 CONSTRAINT vehicle_transport_organizer_id_fkey FOREIGN KEY (transport_organizer_id) REFERENCES transport_organizer (transport_organizer_id) ON DELETE CASCADE
134
135);
136
137CREATE TABLE automobile
138(
139 automobile_id SERIAL PRIMARY KEY,
140 vehicle_id INT NOT NULL UNIQUE,
141 CONSTRAINT automobile_vehicle_id_fkey FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicle_id) ON DELETE CASCADE
142);
143
144CREATE TABLE bus
145(
146 bus_id SERIAL PRIMARY KEY,
147 vehicle_id INT NOT NULL UNIQUE,
148 CONSTRAINT bus_vehicle_id_fkey FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicle_id) ON DELETE CASCADE
149);
150
151CREATE TABLE van
152(
153 van_id SERIAL PRIMARY KEY,
154 vehicle_id INT NOT NULL UNIQUE,
155 CONSTRAINT van_vehicle_id_fkey FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicle_id) ON DELETE CASCADE
156);
157
158CREATE TABLE train
159(
160 train_id SERIAL PRIMARY KEY,
161 vehicle_id INT NOT NULL UNIQUE,
162 CONSTRAINT train_vehicle_id_fkey FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicle_id) ON DELETE CASCADE
163);
164
165CREATE TABLE driver
166(
167 driver_id SERIAL PRIMARY KEY,
168 account_id INT NOT NULL,
169 years_experience INT NOT NULL,
170 transport_organizer_id INT,
171 CONSTRAINT driver_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE,
172 CONSTRAINT driver_transport_organizer_id_fkey FOREIGN KEY (transport_organizer_id) REFERENCES transport_organizer (transport_organizer_id) ON DELETE CASCADE
173);
174
175CREATE TABLE driver_vehicle_operation
176(
177 driver_vehicle_operation_id SERIAL PRIMARY KEY,
178 driver_id INT NOT NULL,
179 vehicle_id INT NOT NULL,
180 CONSTRAINT driver_vehicle_operation_driver_id_fkey FOREIGN KEY (driver_id) REFERENCES driver (driver_id) ON DELETE CASCADE,
181 CONSTRAINT driver_vehicle_operation_vehicle_id_fkey FOREIGN KEY (vehicle_id) REFERENCES vehicle (vehicle_id) ON DELETE CASCADE
182);
183
184CREATE TABLE driver_drives_on_trip
185(
186 driver_drives_on_trip_id SERIAL PRIMARY KEY,
187 driver_id INT NOT NULL,
188 trip_id INT NOT NULL,
189 CONSTRAINT driver_drives_on_trip_driver_id_fkey FOREIGN KEY (driver_id) REFERENCES driver (driver_id) ON DELETE CASCADE,
190 CONSTRAINT driver_drives_on_trip_trip_id_fkey FOREIGN KEY (trip_id) REFERENCES trip (trip_id) ON DELETE CASCADE
191);
192
193CREATE TABLE favorite
194(
195 favorite_id SERIAL PRIMARY KEY,
196 route_id INT NOT NULL,
197 account_id INT NOT NULL,
198 CONSTRAINT favorite_route_id_fkey FOREIGN KEY (route_id) REFERENCES route (route_id) ON DELETE CASCADE,
199 CONSTRAINT favorite_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE
200);
201
202CREATE TABLE trip_days_active
203(
204 trip_days_active_id SERIAL PRIMARY KEY,
205 route_id INT NOT NULL,
206 day VARCHAR(20) NOT NULL,
207 CONSTRAINT trip_days_active_route_id_fkey FOREIGN KEY (route_id) REFERENCES route (route_id) ON DELETE CASCADE
208);
209
210CREATE TABLE trip_stops
211(
212 trip_stop_id SERIAL PRIMARY KEY,
213 trip_id INT NOT NULL,
214 location_id INT NOT NULL,
215 stop_time TIME WITHOUT TIME ZONE NOT NULL,
216 CONSTRAINT trip_stops_trip_id_fkey FOREIGN KEY (trip_id) REFERENCES trip (trip_id) ON DELETE CASCADE,
217 CONSTRAINT trip_stops_location_id_fkey FOREIGN KEY (location_id) REFERENCES location (location_id) ON DELETE CASCADE
218);
219
220CREATE TABLE student
221(
222 student_id SERIAL PRIMARY KEY,
223 account_id INT NOT NULL,
224 university VARCHAR(100) NOT NULL,
225 index_number VARCHAR(20) NOT NULL,
226 CONSTRAINT student_account_id_fkey FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE CASCADE
227);
228
229CREATE TABLE student_ticket
230(
231 student_ticket_id SERIAL PRIMARY KEY,
232 ticket_id INT NOT NULL,
233 discount DOUBLE PRECISION,
234 CONSTRAINT student_ticket_ticket_id_fkey FOREIGN KEY (ticket_id) REFERENCES ticket (ticket_id) ON DELETE CASCADE
235);
236
237CREATE TABLE child_ticket
238(
239 child_ticket_id SERIAL PRIMARY KEY,
240 ticket_id INT NOT NULL,
241 discount DOUBLE PRECISION,
242 embg VARCHAR(13) NOT NULL,
243 parent_embg VARCHAR(13) NOT NULL,
244 CONSTRAINT child_ticket_ticket_id_fkey FOREIGN KEY (ticket_id) REFERENCES ticket (ticket_id) ON DELETE CASCADE
245);
246