Guest User

khi

a guest
Oct 28th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.34 KB | None | 0 0
  1. --- Problem 6 Extract all customers departing from home
  2.  
  3. select c.customer_id,
  4. CONCAT(c.first_name,' ',c.last_name) as full_name,
  5. p.town_name FROM customers as c
  6.         join airports as air on air.town_id  = c.home_town_id
  7.         join flights as f on f.origin_airport_id = air.airport_id
  8.         join tickets as t on t.flight_id = f.flight_id
  9.         join towns as p on p.town_id = c.home_town_id
  10.         group by customer_id
  11.         order by customer_id;
  12.  
  13. ---- PROBLEM 11 EXTRACT all airports and count passengers
  14.  
  15. select air.airport_id,air.airport_name ,
  16. COUNT(ti.customer_id) as passengers
  17. from airports as air
  18.         join flights as fi on fi.origin_airport_id = air.airport_id
  19.         join tickets as ti on ti.flight_id = fi.flight_id
  20.         where fi.`status` = 'Departing'
  21.         group by air.airport_id,air.airport_name  
  22.         having COUNT(ti.customer_id) > 0
  23.         order by air.airport_id;
  24.  
  25.  
  26. --- Problem 1 Review Registering procedure
  27.  
  28. create procedure usp_submit_review (customer_id INT ,review_content VARCHAR(255),review_grade INT ,airline_name_n VARCHAR(50))
  29. begin
  30.     declare airlin_id int;
  31.    
  32.     select airline_id into airlin_id from airlines  where airline_name = airline_name_n ;
  33.    
  34.     if(airlin_id IS NULL) then
  35.      signal sqlstate'45000' set message_text = 'Airline does not exist.';
  36.      else
  37.         insert into customer_reviews(customer_id,review_content,review_grade,airline_id)
  38.         values (customer_id,review_content,review_grade,airlin_id);
  39.     end if;
  40.    
  41. end
  42.  
  43. --- Problem 2 Ticket purchase procedure
  44.  
  45.  
  46. create procedure usp_purchase_ticket(customer_id_n INT ,flight_id INT ,a_ticket_price DECIMAL,class ENUM('First','Second','Third')
  47. ,seat VARCHAR(5))
  48. begin
  49.     declare customer_balance decimal;
  50.    
  51.     select balance into customer_balance from customer_bank_accounts  where customer_id = customer_id_n;
  52.    
  53.     if((customer_balance - a_ticket_price) < 0) then
  54.         signal sqlstate '45000' set message_text = 'Insufficient bank account balance for ticket purchase.';
  55.     else
  56.         insert into tickets (price,class,seat,flight_id,customer_id)
  57.         values (a_ticket_price,class,seat,flight_id,customer_id_n);
  58.         update customer_bank_accounts as cba
  59.             set cba.balance = cba.balance - a_ticket_price
  60.         where customer_id = customer_id_n;
  61.     end if;
  62. end
Add Comment
Please, Sign In to add comment