DROP TABLE
IF
	EXISTS Sold;
DROP TABLE
IF
	EXISTS Transactions;
DROP TABLE
IF
	EXISTS Passbys;
DROP TABLE
IF
	EXISTS Tickets;
DROP TABLE
IF
	EXISTS Seats;
DROP TABLE
IF
	EXISTS Users;
DROP TABLE
IF
	EXISTS Passengers;
DROP TABLE
IF
	EXISTS Rides;
DROP TABLE
IF
	EXISTS Trains;
DROP TABLE
IF
	EXISTS Stations;
CREATE TABLE Passengers (
	ID BIGINT PRIMARY KEY,
	`Name` VARCHAR ( 255 ) NOT NULL,
	Phone_number BIGINT UNIQUE NOT NULL,
	CHECK ( REGEXP_LIKE ( ID, '^\\d{18}$' ) ),
	CHECK ( REGEXP_LIKE ( Phone_number, '^\\d{11}$' ) ) 
);
CREATE TABLE Users (
	ID INT AUTO_INCREMENT PRIMARY KEY,
	`Password` VARCHAR ( 255 ) NOT NULL,
	Phone_number BIGINT UNIQUE NOT NULL,
	CitizenID BIGINT UNIQUE NOT NULL,
	FOREIGN KEY ( Phone_number ) REFERENCES Passengers ( Phone_number ) ON DELETE CASCADE,
	FOREIGN KEY ( CitizenID ) REFERENCES Passengers ( ID ) ON DELETE CASCADE 
);
CREATE TABLE Stations (
	ID INT AUTO_INCREMENT PRIMARY KEY,
	`Name` VARCHAR ( 255 ) UNIQUE NOT NULL,
	Province VARCHAR ( 255 ) NOT NULL,
	City VARCHAR ( 255 ) NOT NULL 
);
CREATE TABLE Trains (
	ID VARCHAR ( 255 ) PRIMARY KEY,
	Property SMALLINT NOT NULL,
	StationID_start INT NOT NULL,
	StationID_arrive INT NOT NULL,
	FOREIGN KEY ( StationID_start ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( StationID_arrive ) REFERENCES Stations ( ID ) ON DELETE CASCADE 
);
CREATE TABLE Seats (
	ID VARCHAR ( 255 ) PRIMARY KEY,
	Type VARCHAR ( 255 ) NOT NULL,
	TrainID VARCHAR ( 255 ) NOT NULL,
	FOREIGN KEY ( TrainID ) REFERENCES Trains ( ID ) ON DELETE CASCADE 
);
CREATE TABLE Tickets (
	ID INT AUTO_INCREMENT PRIMARY KEY,
	Price DECIMAL ( 7, 2 ) NOT NULL,
	Time_start DATETIME NOT NULL,
	Time_arrive DATETIME NOT NULL,
	StationID_start INT NOT NULL,
	StationID_arrive INT NOT NULL,
	SeatID VARCHAR ( 255 ) NOT NULL,
	FOREIGN KEY ( StationID_start ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( StationID_arrive ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( SeatID ) REFERENCES Seats ( ID ) ON DELETE CASCADE,
	CHECK ( Time_start <= Time_arrive ) 
);
CREATE TABLE Transactions (
	ID BIGINT AUTO_INCREMENT PRIMARY KEY,
	`Time` DATETIME NOT NULL,
	Paid TINYINT NOT NULL,
	`User` INT NOT NULL,
	PassengerID BIGINT NOT NULL,
	TrainID VARCHAR ( 255 ) NOT NULL,
	StationID_start INT NOT NULL,
	StationID_arrive INT NOT NULL,
	Time_start DATETIME NOT NULL,
	Time_arrive DATETIME NOT NULL,
	SeatID VARCHAR ( 255 ) NOT NULL,
	FOREIGN KEY ( `User` ) REFERENCES Users ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( PassengerID ) REFERENCES Passengers ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( TrainID ) REFERENCES Trains ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( StationID_start ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( StationID_arrive ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( SeatID ) REFERENCES Seats ( ID ) ON DELETE CASCADE,
	CHECK ( Time_start < Time_arrive ) 
);
CREATE TABLE Sold (
	TransactionID BIGINT NOT NULL,
	TicketID INT NOT NULL,
	FOREIGN KEY ( TransactionID ) REFERENCES Transactions ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( TicketID ) REFERENCES Tickets ( ID ) ON DELETE CASCADE 
);
CREATE TABLE Passbys (
	StationID INT NOT NULL,
	TrainID VARCHAR ( 255 ) NOT NULL,
	arrive_time DATETIME NOT NULL,
	leave_time DATETIME NOT NULL,
	FOREIGN KEY ( StationID ) REFERENCES Stations ( ID ) ON DELETE CASCADE,
	FOREIGN KEY ( TrainID ) REFERENCES Trains ( ID ) ON DELETE CASCADE,
	CHECK ( arrive_time <= leave_time ) 
);
INSERT INTO Stations ( `Name`, Province, City )
VALUES
	( '北京南', '北京', '北京' ),
	( '沧州西', '河北', '沧州' ),
	( '济南西', '山东', '济南' ),
	( '徐州东', '江苏', '徐州' ),
	( '宿州东', '安徽', '宿州' ),
	( '蚌埠南', '安徽', '蚌埠' ),
	( '南京南', '江苏', '南京' ),
	( '无锡南', '江苏', '无锡' ),
	( '上海虹桥', '上海', '上海' );
INSERT INTO Trains ( ID, StationID_start, StationID_arrive, Property )
VALUES
	( 'G103', 1, 9, 1 );
INSERT INTO Passbys ( StationID, TrainID, arrive_time, leave_time )
VALUES
	( 1, 'G103', '2024-03-28 06:20:00', '2024-03-28 06:20:00' ),
	( 2, 'G103', '2024-03-28 07:13:00', '2024-03-28 07:15:00' ),
	( 3, 'G103', '2024-03-28 08:01:00', '2024-03-28 08:04:00' ),
	( 4, 'G103', '2024-03-28 09:07:00', '2024-03-28 09:09:00' ),
	( 5, 'G103', '2024-03-28 09:28:00', '2024-03-28 09:33:00' ),
	( 6, 'G103', '2024-03-28 09:56:00', '2024-03-28 09:58:00' ),
	( 7, 'G103', '2024-03-28 10:40:00', '2024-03-28 10:43:00' ),
	( 8, 'G103', '2024-03-28 11:27:00', '2024-03-28 11:29:00' ),
	( 9, 'G103', '2024-03-28 11:58:00', '2024-03-28 11:58:00' );
SELECT
	Passbys.TrainID,
	Stations.`Name`,
	Passbys.arrive_time,
	Passbys.leave_time,
	( Passbys.leave_time - Passbys.arrive_time ) / 100 AS Stay_time 
FROM
	Passbys
	JOIN Stations ON Passbys.StationID = Stations.ID;