RelationalSchema: routemk_create_and_delete.sql

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