Advertisement
YavorGrancharov

Databases MySQL Exam Preparation I

Oct 21st, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 6.68 KB | None | 0 0
  1. 01. Table Design
  2. CREATE DATABASE airport_db;
  3. USE airport_db;
  4. CREATE TABLE towns(
  5.     town_id INT PRIMARY KEY AUTO_INCREMENT,
  6.     town_name VARCHAR(30) NOT NULL
  7. );
  8. CREATE TABLE airports(
  9.     airport_id INT PRIMARY KEY AUTO_INCREMENT,
  10.     airport_name VARCHAR(50) NOT NULL,
  11.     town_id INT,
  12.     CONSTRAINT fk_airport_town_id
  13.     FOREIGN KEY (town_id) REFERENCES towns(town_id)
  14. );
  15. CREATE TABLE airlines(
  16.     airline_id INT PRIMARY KEY AUTO_INCREMENT,
  17.     airline_name VARCHAR(30) NOT NULL,
  18.     nationality VARCHAR(30) NOT NULL,
  19.     rating INT
  20. );
  21. CREATE TABLE customers(
  22.     customer_id INT PRIMARY KEY AUTO_INCREMENT,
  23.     first_name VARCHAR(20) NOT NULL,
  24.     last_name VARCHAR(20) NOT NULL,
  25.     date_of_birth DATE NOT NULL,
  26.     gender VARCHAR(1),
  27.     home_town_id INT,
  28.     CONSTRAINT fk_customers_home_town
  29.     FOREIGN KEY (home_town_id) REFERENCES towns(town_id)
  30. );
  31. CREATE TABLE flights(
  32.     flight_id INT PRIMARY KEY AUTO_INCREMENT,
  33.     departure_time DATETIME NOT NULL,
  34.     arrival_time DATETIME NOT NULL,
  35.     `status` VARCHAR(9),
  36.     origin_airport_id INT NOT NULL,
  37.     destination_airport_id INT NOT NULL,
  38.     airline_id INT NOT NULL,
  39.     CONSTRAINT fk_airport_origin
  40.     FOREIGN KEY (origin_airport_id) REFERENCES airports(airport_id),
  41.     CONSTRAINT fk_airport_destination
  42.     FOREIGN KEY (destination_airport_id) REFERENCES airports(airport_id),
  43.     CONSTRAINT fk_airport_airline
  44.     FOREIGN KEY (airline_id) REFERENCES airlines(airline_id)
  45. );
  46. CREATE TABLE tickets(
  47.     ticket_id INT PRIMARY KEY AUTO_INCREMENT,
  48.     price DECIMAL(8,2) NOT NULL,
  49.     class VARCHAR(6),
  50.     seat VARCHAR(5) NOT NULL,
  51.     customer_id INT NOT NULL,
  52.     flight_id INT NOT NULL,
  53.     CONSTRAINT fk_customers_id
  54.     FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
  55.     CONSTRAINT fk_flights_id
  56.     FOREIGN KEY (flight_id) REFERENCES flights(flight_id)
  57. );
  58. ----------------
  59. 02. Insert
  60. INSERT INTO flights(departure_time,arrival_time,`status`,origin_airport_id,
  61. destination_airport_id,airline_id)
  62. SELECT '2017-06-19 14:00:00' AS `departure_time`,
  63. '2017-06-21 11:00:00' AS `arrival_time`,
  64.     (CASE
  65.         WHEN airline_id % 4 = 0 THEN 'Departing'
  66.         WHEN airline_id % 4 = 1 THEN 'Delayed'
  67.         WHEN airline_id % 4 = 2 THEN 'Arrived'
  68.         WHEN airline_id % 4 = 3 THEN 'Canceled'
  69.     END) AS `status`,
  70.     CEIL(SQRT(LENGTH(airline_name))) AS origin_airport_id,
  71.     CEIL(SQRT(LENGTH(nationality))) AS destination_airport_id,
  72.     airline_id AS airline_id FROM airlines AS a WHERE
  73.     airline_id BETWEEN 1 AND 10;
  74. ------------
  75. 03. Update Flights
  76. UPDATE flights SET airline_id=1
  77. WHERE `status`='Arrived';
  78. ------------
  79. 04. Update Tickets
  80. UPDATE tickets AS t
  81. INNER JOIN flights AS f ON t.flight_id=f.flight_id
  82. INNER JOIN airlines AS a ON f.airline_id=a.airline_id
  83. SET price = price * 1.5
  84. WHERE a.rating = (SELECT MAX(rating) FROM airlines);
  85. -------------
  86. 05. Tickets
  87. SELECT ticket_id,price,class,seat FROM tickets
  88. ORDER BY ticket_id ASC;
  89. -------------
  90. 06. Customers
  91. SELECT customer_id,CONCAT(first_name,' ',last_name) AS `full_name`,gender
  92. FROM customers ORDER BY `full_name` ASC,customer_id ASC;
  93. ------------
  94. 07. Flights
  95. SELECT flight_id,departure_time,arrival_time FROM flights
  96. WHERE `status`='Delayed' ORDER BY flight_id ASC;
  97. -----------
  98. 08. Top 5 Airlines - 80%
  99. SELECT a.airline_id,a.airline_name,a.nationality,a.rating
  100. FROM airlines AS a ORDER BY a.rating DESC,airline_id ASC LIMIT 5;
  101. ----------
  102. 09. First Class Tickets
  103. SELECT t.ticket_id,a.airport_name AS `destination`,CONCAT(
  104. c.first_name,' ',c.last_name) AS `customer_name` FROM tickets AS t
  105. INNER JOIN flights AS f ON f.flight_id=t.flight_id
  106. INNER JOIN airports AS a ON a.airport_id=f.destination_airport_id
  107. INNER JOIN customers AS c ON c.customer_id=t.customer_id
  108. WHERE t.price < 5000 AND t.class='First'
  109. ORDER  BY t.ticket_id ASC;
  110. ----------------
  111. 10. Home Town Customers
  112. SELECT DISTINCT c.customer_id,CONCAT(c.first_name,' ',c.last_name) AS `full_name`,
  113. town_name AS `home_town`FROM customers AS c INNER JOIN towns AS t
  114. ON t.town_id=c.home_town_id INNER JOIN tickets AS tic
  115. ON tic.customer_id=c.customer_id
  116. INNER JOIN flights AS f ON f.flight_id=tic.flight_id
  117. INNER JOIN airports AS a ON a.airport_id=f.origin_airport_id
  118. AND a.town_id=c.home_town_id
  119. WHERE f.`status`='Departing' ORDER BY c.customer_id ASC;
  120. -------------
  121. 11. Flying Customers
  122. SELECT DISTINCT c.customer_id,CONCAT(c.first_name,' ',c.last_name) AS `full_name`,
  123. TIMESTAMPDIFF(YEAR,c.date_of_birth, '2016-12-31') AS `age`
  124. FROM customers AS c INNER JOIN tickets AS t ON t.customer_id=c.customer_id
  125. INNER JOIN flights AS f ON f.flight_id=t.flight_id WHERE f.`status`='Departing'
  126. ORDER BY age ASC,c.customer_id ASC;
  127. ------------
  128. 12. Delayed Customers
  129. SELECT c.customer_id,CONCAT(c.first_name,' ',c.last_name) AS `full_name`,
  130. t.price AS `ticket_price`,airport_name AS `destination` FROM customers
  131. AS c INNER JOIN tickets AS t ON t.customer_id=c.customer_id INNER JOIN
  132. flights AS f ON f.flight_id=t.flight_id INNER JOIN airports AS a
  133. ON a.airport_id=f.destination_airport_id WHERE f.`status`='Delayed'
  134. ORDER BY t.price DESC ,c.customer_id ASC LIMIT 3;
  135. --------------
  136. 13. Last Departing Flights - 80%
  137. SELECT * FROM(SELECT f.flight_id,f.departure_time,f.arrival_time,a.airport_name AS `origin`,
  138. a2.airport_name AS `destination` FROM flights AS f INNER JOIN
  139. airports AS a ON a.airport_id=f.origin_airport_id INNER JOIN
  140. airports AS a2 ON a2.airport_id=f.destination_airport_id WHERE
  141. f.`status`='Departing' ORDER BY f.arrival_time DESC LIMIT 5) AS `result`
  142. ORDER BY departure_time ASC,flight_id ASC;
  143. ---------------
  144. 14. Flying Children
  145. SELECT DISTINCT c.customer_id,CONCAT(c.first_name,' ',c.last_name) AS `full_name`,
  146. TIMESTAMPDIFF(YEAR,date_of_birth,'2016-12-31') AS `age` FROM customers
  147. AS c INNER JOIN tickets AS t ON t.customer_id=c.customer_id INNER JOIN
  148. flights AS f ON f.flight_id=t.flight_id WHERE f.`status`='Arrived' AND
  149. TIMESTAMPDIFF(YEAR,date_of_birth,'2016-12-31') < 21
  150. ORDER BY age DESC,c.customer_id ASC;
  151. --------------
  152. 15. Airports and Passengers
  153. SELECT airport_id,airport_name,COUNT(ticket_id) AS `passengers`
  154. FROM airports AS a INNER JOIN flights AS f ON f.origin_airport_id=a.airport_id
  155. INNER JOIN tickets AS t ON t.flight_id=f.flight_id WHERE f.`status`
  156. ='Departing' GROUP BY a.airport_id,a.airport_name ORDER BY a.airport_id ASC;
  157. -------------
  158. 16. Submit Review
  159. CREATE PROCEDURE udp_submit_review(customer_id INT,review_content VARCHAR(255),
  160. review_grade INT,airline_name VARCHAR(30))
  161. BEGIN
  162.     DECLARE airline_id INT;
  163.     SET airline_id:=(SELECT a.airline_id FROM airlines AS a WHERE
  164.     a.airline_name=airline_name);
  165.     IF (airline_id IS NULL) THEN
  166.     SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Airline does not exist.';
  167.     END IF;
  168.         INSERT INTO customer_reviews(review_content,review_grade,
  169.         airline_id,customer_id) VALUES (review_content,review_grade,
  170.         airline_id,customer_id);
  171. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement