-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-structure.sql
More file actions
254 lines (230 loc) · 7.5 KB
/
Copy pathdatabase-structure.sql
File metadata and controls
254 lines (230 loc) · 7.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-- Create a SQLite database for NMBS Train Data
-- This can be adapted for MySQL, PostgreSQL, or other SQL databases
-- PRAGMA foreign_keys = ON; -- Enable foreign key support (SQLite specific, uncomment if using SQLite)
-- ------------------------------------------------
-- Table structure for GTFS static data
-- ------------------------------------------------
-- Agency information
CREATE TABLE agency (
agency_id varchar(191) PRIMARY KEY,
agency_name varchar(255) NOT NULL,
agency_url varchar(255) NOT NULL,
agency_timezone varchar(255) NOT NULL,
agency_lang varchar(255),
agency_phone varchar(255),
agency_fare_url varchar(255)
);
-- Stops (stations and platforms)
CREATE TABLE stops (
stop_id varchar(191) PRIMARY KEY,
stop_code varchar(255),
stop_name varchar(255) NOT NULL,
stop_desc varchar(255),
stop_lat REAL,
stop_lon REAL,
zone_id varchar(255),
stop_url varchar(255),
location_type INTEGER,
parent_station varchar(255),
platform_code varchar(255),
wheelchair_boarding INTEGER,
stop_timezone varchar(255),
FOREIGN KEY (parent_station) REFERENCES stops(stop_id)
);
-- Create an index for searching stop names efficiently
CREATE INDEX idx_stops_name ON stops(stop_name);
-- Routes information
CREATE TABLE routes (
route_id varchar(191) PRIMARY KEY,
agency_id varchar(255),
route_short_name varchar(255),
route_long_name varchar(255),
route_desc varchar(255),
route_type INTEGER NOT NULL,
route_url varchar(255),
route_color varchar(255),
route_text_color varchar(255),
FOREIGN KEY (agency_id) REFERENCES agency(agency_id)
);
-- Create indexes for searching routes
CREATE INDEX idx_routes_short_name ON routes(route_short_name);
CREATE INDEX idx_routes_long_name ON routes(route_long_name);
-- Calendar service data (regular service patterns)
CREATE TABLE calendar (
service_id varchar(191) PRIMARY KEY,
monday INTEGER NOT NULL,
tuesday INTEGER NOT NULL,
wednesday INTEGER NOT NULL,
thursday INTEGER NOT NULL,
friday INTEGER NOT NULL,
saturday INTEGER NOT NULL,
sunday INTEGER NOT NULL,
start_date varchar(255) NOT NULL,
end_date varchar(255) NOT NULL
);
-- Calendar exceptions (holiday schedules, special service days)
CREATE TABLE calendar_dates (
service_id varchar(191) NOT NULL,
date varchar(255) NOT NULL,
exception_type INTEGER NOT NULL,
PRIMARY KEY (service_id, date),
FOREIGN KEY (service_id) REFERENCES calendar(service_id)
);
-- Trips (individual train journeys)
CREATE TABLE trips (
trip_id varchar(191) PRIMARY KEY,
route_id varchar(255) NOT NULL,
service_id varchar(255) NOT NULL,
trip_headsign varchar(255),
trip_short_name varchar(255),
direction_id INTEGER,
block_id varchar(255),
shape_id varchar(255),
wheelchair_accessible INTEGER,
bikes_allowed INTEGER,
FOREIGN KEY (route_id) REFERENCES routes(route_id),
FOREIGN KEY (service_id) REFERENCES calendar(service_id)
);
-- Create index for searching trips by route
CREATE INDEX idx_trips_route_id ON trips(route_id);
CREATE INDEX idx_trips_service_id ON trips(service_id);
-- Stop times (train schedules at each station)
CREATE TABLE stop_times (
trip_id varchar(191) NOT NULL,
arrival_time varchar(255) NOT NULL,
departure_time varchar(255) NOT NULL,
stop_id varchar(255) NOT NULL,
stop_sequence INTEGER NOT NULL,
stop_headsign varchar(255),
pickup_type INTEGER,
drop_off_type INTEGER,
shape_dist_traveled REAL,
timepoint INTEGER,
PRIMARY KEY (trip_id, stop_sequence),
FOREIGN KEY (trip_id) REFERENCES trips(trip_id),
FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
);
-- Create indexes for searching stop times
CREATE INDEX idx_stop_times_trip_id ON stop_times(trip_id);
CREATE INDEX idx_stop_times_stop_id ON stop_times(stop_id);
CREATE INDEX idx_stop_times_arrival ON stop_times(arrival_time);
CREATE INDEX idx_stop_times_departure ON stop_times(departure_time);
-- Transfers between stops
CREATE TABLE transfers (
from_stop_id varchar(255) NOT NULL,
to_stop_id varchar(255) NOT NULL,
transfer_type INTEGER NOT NULL,
min_transfer_time INTEGER,
PRIMARY KEY (from_stop_id, to_stop_id),
FOREIGN KEY (from_stop_id) REFERENCES stops(stop_id),
FOREIGN KEY (to_stop_id) REFERENCES stops(stop_id)
);
-- Translations for multilingual support
CREATE TABLE translations (
trans_id varchar(255) NOT NULL,
lang varchar(255) NOT NULL,
translation varchar(255) NOT NULL,
PRIMARY KEY (trans_id, lang)
);
-- ------------------------------------------------
-- Table structure for GTFS real-time data
-- ------------------------------------------------
-- Track/platform changes
CREATE TABLE track_changes (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
trip_id varchar(255) NOT NULL,
stop_id varchar(255) NOT NULL,
scheduled_track varchar(255),
actual_track varchar(255),
update_time TIMESTAMP NOT NULL,
FOREIGN KEY (trip_id) REFERENCES trips(trip_id),
FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
);
-- Realtime vehicle positions
CREATE TABLE vehicle_positions (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
trip_id varchar(255) NOT NULL,
vehicle_id varchar(255),
latitude REAL NOT NULL,
longitude REAL NOT NULL,
bearing REAL,
speed REAL,
current_stop_sequence INTEGER,
current_status INTEGER,
timestamp TIMESTAMP NOT NULL,
congestion_level INTEGER,
occupancy_status INTEGER,
FOREIGN KEY (trip_id) REFERENCES trips(trip_id)
);
-- Trip updates
CREATE TABLE trip_updates (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
trip_id varchar(255) NOT NULL,
route_id varchar(255) NOT NULL,
schedule_relationship INTEGER,
timestamp TIMESTAMP NOT NULL
);
-- Stop time updates
CREATE TABLE stop_time_updates (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
trip_update_id INTEGER NOT NULL,
stop_sequence INTEGER,
stop_id varchar(191) NOT NULL,
arrival_delay INTEGER,
arrival_time TIMESTAMP,
departure_delay INTEGER,
departure_time TIMESTAMP,
schedule_relationship INTEGER,
FOREIGN KEY (trip_update_id) REFERENCES trip_updates(id),
FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
);
-- Table to keep track of data update timestamps
CREATE TABLE data_updates (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
data_type varchar(255) NOT NULL,
last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
filename varchar(255),
url varchar(255),
status varchar(255)
);
-- The modification of last_updated column has been moved to the beginning of the file
ALTER TABLE data_updates MODIFY last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- Create view for station information
CREATE VIEW station_view AS
SELECT
s.stop_id,
s.stop_name,
s.stop_lat,
s.stop_lon,
s.platform_code,
COUNT(DISTINCT st.trip_id) as daily_trains
FROM
stops s
LEFT JOIN
stop_times st ON s.stop_id = st.stop_id
WHERE
s.location_type = 0
AND (s.parent_station IS NULL OR s.parent_station = '')
GROUP BY
s.stop_id, s.stop_name, s.stop_lat, s.stop_lon, s.platform_code;
-- Create view for train schedules
CREATE VIEW train_schedule_view AS
SELECT
t.trip_id,
r.route_short_name,
t.trip_headsign,
s.stop_name,
st.departure_time,
st.arrival_time,
st.stop_sequence,
s.platform_code
FROM
trips t
JOIN
routes r ON t.route_id = r.route_id
JOIN
stop_times st ON t.trip_id = st.trip_id
JOIN
stops s ON st.stop_id = s.stop_id
ORDER BY
t.trip_id, st.stop_sequence;