Advertisement
vallec

Untitled

Apr 23rd, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.09 KB | None | 0 0
  1. DROP DATABASE IF EXISTS airport;
  2. CREATE DATABASE airport;
  3. USE airport;
  4.  
  5. -- Създаване на таблица за летища
  6. CREATE TABLE Airports (
  7. AirportID INT AUTO_INCREMENT PRIMARY KEY,
  8. Name VARCHAR(255) NOT NULL,
  9. Country VARCHAR(255) NOT NULL,
  10. City VARCHAR(255) NOT NULL
  11. );
  12.  
  13. -- Създаване на таблица за самолети
  14. CREATE TABLE Planes (
  15. PlaneID INT AUTO_INCREMENT PRIMARY KEY,
  16. Model VARCHAR(255) NOT NULL,
  17. Capacity INT NOT NULL
  18. );
  19.  
  20. -- Създаване на таблица за екипаж
  21. CREATE TABLE Crew (
  22. CrewID INT AUTO_INCREMENT PRIMARY KEY,
  23. Name VARCHAR(255) NOT NULL,
  24. Role VARCHAR(255) NOT NULL
  25. );
  26.  
  27. -- Създаване на таблица за полети
  28. CREATE TABLE Flights (
  29. FlightID INT AUTO_INCREMENT PRIMARY KEY,
  30. DepartureDate DATE NOT NULL,
  31. DepartureTime TIME NOT NULL,
  32. Duration TIME NOT NULL,
  33. DepartureAirportID INT NOT NULL,
  34. ArrivalAirportID INT NOT NULL,
  35. PlaneID INT NOT NULL,
  36. FOREIGN KEY (DepartureAirportID) REFERENCES Airports(AirportID),
  37. FOREIGN KEY (ArrivalAirportID) REFERENCES Airports(AirportID),
  38. FOREIGN KEY (PlaneID) REFERENCES Planes(PlaneID)
  39. );
  40.  
  41. -- Създаване на таблица за пътници
  42. CREATE TABLE Passengers (
  43. PassengerID INT AUTO_INCREMENT PRIMARY KEY,
  44. Name VARCHAR(255) NOT NULL
  45. );
  46.  
  47. -- Създаване на таблица за полети на пътници
  48. CREATE TABLE PassengerFlights (
  49. PassengerID INT,
  50. FlightID INT,
  51. PRIMARY KEY (PassengerID, FlightID),
  52. FOREIGN KEY (PassengerID) REFERENCES Passengers(PassengerID),
  53. FOREIGN KEY (FlightID) REFERENCES Flights(FlightID)
  54. );
  55.  
  56. -- Създаване на таблица за видове храна
  57. CREATE TABLE MealTypes (
  58. MealTypeID INT AUTO_INCREMENT PRIMARY KEY,
  59. Description VARCHAR(255) NOT NULL
  60. );
  61.  
  62. -- Създаване на таблица за храна за пътници
  63. CREATE TABLE PassengerMeals (
  64. PassengerID INT,
  65. FlightID INT,
  66. MealTypeID INT,
  67. PRIMARY KEY (PassengerID, FlightID, MealTypeID),
  68. FOREIGN KEY (PassengerID) REFERENCES Passengers(PassengerID),
  69. FOREIGN KEY (FlightID) REFERENCES PassengerFlights(FlightID),
  70. FOREIGN KEY (MealTypeID) REFERENCES MealTypes(MealTypeID)
  71. );
  72.  
  73. CREATE TABLE PilotSchedules (
  74. PilotID INT,
  75. FlightID INT,
  76. ScheduleDate DATE NOT NULL,
  77. DutyType ENUM('Flight', 'Standby', 'Training', 'Leave') NOT NULL,
  78. StartTime TIME NOT NULL,
  79. EndTime TIME NOT NULL,
  80. FOREIGN KEY (PilotID) REFERENCES Crew(CrewID),
  81. FOREIGN KEY (FlightID) REFERENCES Flights(FlightID),
  82. PRIMARY KEY (PilotID, ScheduleDate, StartTime)
  83. );
  84.  
  85. CREATE TABLE Reservations (
  86. ReservationID INT AUTO_INCREMENT PRIMARY KEY,
  87. PassengerID INT,
  88. FlightID INT,
  89. ReservationDate DATE NOT NULL,
  90. Status ENUM('Active', 'Cancelled', 'Completed') NOT NULL,
  91. FOREIGN KEY (PassengerID) REFERENCES Passengers(PassengerID),
  92. FOREIGN KEY (FlightID) REFERENCES Flights(FlightID)
  93. );
  94.  
  95. CREATE TABLE Tickets (
  96. TicketID INT AUTO_INCREMENT PRIMARY KEY,
  97. ReservationID INT,
  98. Price DECIMAL(10,2) NOT NULL,
  99. IssuedDate DATE NOT NULL,
  100. SeatNumber INT NOT NULL,
  101. Type ENUM('Basic', 'Regular', 'Plus', 'Flexi Plus') NOT NULL,
  102. FOREIGN KEY (ReservationID) REFERENCES Reservations(ReservationID)
  103. );
  104.  
  105. CREATE TABLE Crew_Replacements (
  106. Replacement_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  107. Original_Crew_Member_ID INT,
  108. New_Crew_Member_ID INT,
  109. Replacement_Date DATE,
  110. Reason VARCHAR(255),
  111. FOREIGN KEY (Original_Crew_Member_ID) REFERENCES Crew(CrewID),
  112. FOREIGN KEY (New_Crew_Member_ID) REFERENCES Crew(CrewID)
  113. );
  114.  
  115. INSERT INTO Airports (Name, City, Country) VALUES
  116. ('John F. Kennedy International Airport', 'New York City', 'United States'),
  117. ('Heathrow Airport', 'London', 'United Kingdom'),
  118. ('Charles de Gaulle Airport', 'Paris', 'France'),
  119. ('Dubai World Central', 'Dubai', 'United Arab Emirates'),
  120. ('Beijing Capital International Airport', 'Beijing', 'China'),
  121. ('Dubai International Airport', 'Dubai', 'United Arab Emirates'),
  122. ('Los Angeles International Airport', 'Los Angeles', 'United States'),
  123. ('Tokyo Haneda Airport', 'Tokyo', 'Japan'),
  124. ('Beijing Daxing International Airport', 'Beijing', 'China'),
  125. ('Al Maktoum Airport', 'Dubai', 'United Arab Emirates');
  126.  
  127. INSERT INTO Planes (Model, Capacity) VALUES
  128. ('Boeing 737', 189),
  129. ('Airbus A320', 180),
  130. ('Boeing 777', 396);
  131.  
  132. INSERT INTO Crew (Name, Role) VALUES
  133. ('Ivan Ivanov', 'Pilot'),
  134. ('Maria Popova', 'Co-Pilot'),
  135. ('Test Testov', 'Co-Pilot'),
  136. ('Georgi Petrov', 'Pilot'),
  137. ('Peter Georgiev', 'Pilot'),
  138. ('Georgi Georgiev', 'Flight Attendant');
  139.  
  140. INSERT INTO Crew_Replacements (Original_Crew_Member_ID, New_Crew_Member_ID, Replacement_Date, Reason) VALUES
  141. (2, 4, '2024-04-30', 'Medical leave replacement'),
  142. (3, 1, '2024-05-01', 'Vacation replacement'),
  143. (1, 3, '2024-05-02', 'Sick leave replacement'),
  144. (4, 2, '2024-05-03', 'Training replacement'),
  145. (2, 1, '2024-05-04', 'Vacation replacement');
  146.  
  147. INSERT INTO Flights (DepartureDate, DepartureTime, Duration, DepartureAirportID, ArrivalAirportID, PlaneID) VALUES
  148. ('2024-05-01', '08:00:00', '01:50:00', 1, 2, 1),
  149. ('2024-05-02', '15:30:00', '03:00:00', 2, 3, 2),
  150. ('2024-05-02', '22:00:00', '06:30:00', 3, 1, 3),
  151. ('2024-05-03', '08:00:00', '01:30:00', 1, 3, 1),
  152. ('2024-05-03', '15:00:00', '02:15:00', 1, 2, 1),
  153. ('2024-05-04', '06:00:00', '03:30:00', 2, 3, 3),
  154. ('2024-05-04', '18:00:00', '02:30:00', 1, 3, 3);
  155.  
  156. INSERT INTO Passengers (Name) VALUES
  157. ('Peter Petrov'),
  158. ('Anna Ivanova'),
  159. ('John Smith');
  160.  
  161. INSERT INTO PassengerFlights (PassengerID, FlightID) VALUES
  162. (1, 1),
  163. (2, 2),
  164. (3, 3);
  165.  
  166. INSERT INTO MealTypes (Description) VALUES
  167. ('Vegetarian'),
  168. ('Standard'),
  169. ('Vegan');
  170.  
  171. INSERT INTO PassengerMeals (PassengerID, FlightID, MealTypeID) VALUES
  172. (1, 1, 1),
  173. (2, 2, 2),
  174. (3, 3, 3);
  175.  
  176. INSERT INTO PilotSchedules (PilotID, FlightID, ScheduleDate, DutyType, StartTime, EndTime) VALUES
  177. (1, 1, '2024-05-01', 'Flight', '06:00:00', '11:00:00'),
  178. (4, 2, '2024-05-02', 'Flight', '13:30:00', '22:30:00'),
  179. (4, 3, '2024-05-02', 'Flight', '22:00:00', '03:30:00'),
  180. (1, 4, '2024-05-03', 'Flight', '08:00:00', '11:00:00'),
  181. (1, 5, '2024-05-03', 'Flight', '15:00:00', '19:00:00'),
  182. (5, 6, '2024-05-04', 'Flight', '06:00:00', '11:00:00'),
  183. (5, 7, '2024-05-04', 'Flight', '15:00:00', '20:00:00');
  184.  
  185. INSERT INTO Reservations (PassengerID, FlightID, ReservationDate, Status) VALUES
  186. (1, 1, '2024-04-20', 'Active'),
  187. (2, 2, '2024-04-22', 'Active'),
  188. (3, 3, '2024-04-25', 'Active');
  189.  
  190. INSERT INTO Tickets (ReservationID, Price, IssuedDate, SeatNumber, Type) VALUES
  191. (1, 299.99, '2024-04-20', 1, 'Basic'),
  192. (2, 450.00, '2024-04-22', 2, 'Regular'),
  193. (3, 1300.00, '2024-04-25', 3, 'Flexi Plus');
  194.  
  195. /* 2 */
  196. SELECT
  197. F.FlightID,
  198. F.DepartureDate,
  199. F.DepartureTime,
  200. F.Duration,
  201. A.Name AS AirportName,
  202. P.Model AS PlaneModel
  203. FROM
  204. Flights F
  205. JOIN
  206. Airports A ON F.DepartureAirportID = A.AirportID
  207. JOIN
  208. Planes P ON F.PlaneID = P.PlaneID
  209. WHERE
  210. A.Name = 'Sofia Airport' AND
  211. F.DepartureDate = '2024-05-01';
  212.  
  213. /* 3 */
  214. SELECT
  215. A.Name AS AirportName,
  216. COUNT(F.FlightID) AS NumberOfFlights
  217. FROM
  218. Flights F
  219. JOIN
  220. Airports A ON F.DepartureAirportID = A.AirportID
  221. GROUP BY
  222. A.Name
  223. ORDER BY
  224. NumberOfFlights DESC;
  225.  
  226. /* 4 */
  227. SELECT
  228. P.Name AS PassengerName,
  229. F.DepartureDate,
  230. F.DepartureTime,
  231. R.Status AS ReservationStatus
  232. FROM
  233. Passengers P
  234. INNER JOIN
  235. Reservations R ON P.PassengerID = R.PassengerID
  236. INNER JOIN
  237. Flights F ON R.FlightID = F.FlightID
  238. WHERE
  239. F.DepartureDate = '2024-05-01';
  240.  
  241. /* 5 */
  242. SELECT
  243. C.Name AS OriginalCrewName,
  244. C.Role AS OriginalCrewRole,
  245. CR.Replacement_Date,
  246. R.Name AS ReplacementName,
  247. R.Role AS ReplacementRole,
  248. CR.Reason
  249. FROM
  250. Crew C
  251. LEFT OUTER JOIN
  252. Crew_Replacements CR ON C.CrewID = CR.Original_Crew_Member_ID
  253. LEFT OUTER JOIN
  254. Crew R ON CR.New_Crew_Member_ID = R.CrewID
  255. ORDER BY
  256. C.Name;
  257.  
  258. /* 6 */
  259. SELECT
  260. F.FlightID,
  261. F.DepartureDate,
  262. F.DepartureTime,
  263. F.Duration
  264. FROM
  265. Flights F
  266. WHERE
  267. F.Duration > (SELECT AVG(Duration) FROM Flights);
  268.  
  269. /* 7 */
  270. SELECT
  271. C.Name AS PilotName,
  272. COUNT(PS.FlightID) AS TotalFlights
  273. FROM
  274. Crew C
  275. JOIN
  276. PilotSchedules PS ON C.CrewID = PS.PilotID
  277. WHERE
  278. C.Role = 'Pilot'
  279. GROUP BY
  280. C.Name
  281. ORDER BY
  282. TotalFlights DESC;
  283.  
  284. /* 8 */
  285. ALTER TABLE Flights
  286. ADD COLUMN arrival_time TIME AFTER Duration;
  287.  
  288. DELIMITER //
  289.  
  290. CREATE TRIGGER calculate_arrival_time
  291. BEFORE UPDATE ON Flights
  292. FOR EACH ROW
  293. BEGIN
  294. IF NEW.DepartureTime != OLD.DepartureTime THEN
  295. SET NEW.arrival_time = ADDTIME(NEW.DepartureTime, NEW.Duration);
  296. END IF;
  297. END;
  298. //
  299. DELIMITER ;
  300.  
  301. Update Flights
  302. SET DepartureTime = '09:00:00'
  303. Where FlightID in (1);
  304.  
  305. Select * from Flights;
  306.  
  307. /* 9 */
  308. DROP PROCEDURE IF EXISTS CityAirports;
  309. DROP TABLE IF EXISTS tempAirports3;
  310. DELIMITER |
  311. CREATE PROCEDURE CityAirports(IN INCountry VARCHAR(255))
  312. BEGIN
  313. DECLARE finished INT DEFAULT 0;
  314. DECLARE tempName VARCHAR(255);
  315. DECLARE tempCity VARCHAR(255);
  316.  
  317. DECLARE airportsCursor CURSOR FOR
  318. SELECT name, city
  319. FROM Airports
  320. WHERE country = INCountry;
  321.  
  322. DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
  323.  
  324. -- Check if the temporary table exists, and drop it if it does
  325. IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'tempAirports3') THEN
  326. DROP TEMPORARY TABLE tempAirports3;
  327. END IF;
  328.  
  329. CREATE TEMPORARY TABLE tempAirports3 (
  330. id INT AUTO_INCREMENT PRIMARY KEY,
  331. tempName VARCHAR(255),
  332. tempCity VARCHAR(255)
  333. ) ENGINE = Memory;
  334.  
  335. OPEN airportsCursor;
  336.  
  337. airports_loop: WHILE finished = 0 DO
  338. FETCH airportsCursor INTO tempName, tempCity;
  339.  
  340. IF finished = 1 THEN
  341. LEAVE airports_loop;
  342. END IF;
  343.  
  344. INSERT INTO tempAirports3(tempName, tempCity)
  345. VALUES (tempName, tempCity);
  346. END WHILE;
  347.  
  348. CLOSE airportsCursor;
  349.  
  350. SELECT * FROM tempAirports3;
  351.  
  352. DROP TEMPORARY TABLE tempAirports3;
  353.  
  354. END |
  355. DELIMITER ;
  356.  
  357. CALL CityAirports('United Arab Emirates');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement