-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTracksnRoutes.sql
34 lines (29 loc) · 959 Bytes
/
TracksnRoutes.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
CREATE TABLE Routes (
Route_ID INT PRIMARY KEY,
Route VARCHAR(255),
From_station_ID INT,
To_station_ID INT
);
Drop trigger if exists check_route_before_insert;
DELIMITER $$
CREATE TRIGGER check_route_before_insert
BEFORE INSERT ON Routes
FOR EACH ROW
BEGIN
DECLARE track VARCHAR(2);
DECLARE next_track VARCHAR(2);
DECLARE i INT;
DECLARE valid_route BOOLEAN DEFAULT TRUE;
SET i = 1;
WHILE i < CHAR_LENGTH(NEW.Route) DO
SET track = SUBSTRING(NEW.Route, i, 2);
SET next_track = SUBSTRING(NEW.Route, i + 2, 2);
IF FIND_IN_SET(track, '37,73,76,67,46,64,14,41,45,54,52,25') = 0 OR
(next_track != '' AND FIND_IN_SET(CONCAT(RIGHT(track, 1), LEFT(next_track, 1)), '37,73,76,67,46,64,14,41,45,54') = 0) THEN
SET valid_route = FALSE;
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid route';
END IF;
SET i = i + 2;
END WHILE;
END$$
DELIMITER ;