Advertisement
Guest User

Final Project 2

a guest
Oct 13th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.51 KB | None | 0 0
  1. SET search_path = dadade;
  2. --By David Adade and Mirjam Brielmaier
  3. --in following we have a hotel with guests, employees, rooms, amenities and appointments
  4. --DDL
  5. CREATE TABLE room (
  6.     id serial PRIMARY KEY,
  7.     name text NOT NULL CHECK (LENGTH (name)>0),
  8.     smoke_free BOOLEAN NOT NULL,
  9.     wifi BOOLEAN NOT NULL
  10. );
  11.  
  12. COMMENT ON TABLE room IS 'All rooms in the hotel';
  13. COMMENT ON COLUMN room.id IS 'The room id';
  14. COMMENT ON COLUMN room.name IS 'The hotel rooms name';
  15. COMMENT ON COLUMN room.smoke_free IS 'Some hotel rooms are smoke free while others are not';
  16. COMMENT ON COLUMN room.wifi IS 'Some hotel rooms have wifi while others do not';
  17.  
  18.  
  19. CREATE TABLE employee (
  20.     id serial PRIMARY KEY,
  21.     name text NOT NULL CHECK (LENGTH(name)>0),
  22.     hiring_date DATE NOT NULL CHECK (hiring_date BETWEEN '1900-01-01' AND CURRENT_DATE)
  23. );
  24.  
  25. COMMENT ON TABLE employee IS 'A list of the employees currently working at the hotel';
  26. COMMENT ON COLUMN employee.id IS 'The id of the employee working at the hotel';
  27. COMMENT ON COLUMN employee.name IS 'The name of the employee of the hotel';
  28. COMMENT ON COLUMN employee.hiring_date IS 'The date when the employee was hired';
  29.  
  30. CREATE TABLE guest (
  31.     id serial PRIMARY KEY,
  32.     name text NOT NULL CHECK(LENGTH (name) > 0),
  33.     age SMALLINT NOT NULL CHECK(age > 0),
  34.     address text NOT NULL CHECK (LENGTH (address) > 0),
  35.     credit_card_number BIGINT NOT NULL CHECK (credit_card_number BETWEEN 1.000000000000000 AND 9999999999999999),
  36.     employee_id INTEGER NOT NULL REFERENCES employee(id)
  37.        ON DELETE cascade ON UPDATE cascade
  38. );
  39.  
  40. COMMENT ON TABLE  guest IS 'The guest booking of the hotel with the following information listed';
  41. COMMENT ON COLUMN guest.id IS 'The ID of the guest who is booking the hotel';
  42. COMMENT ON COLUMN guest.name IS 'Name of the guest who is booking the hotel room';
  43. COMMENT ON COLUMN guest.age IS 'Age of the guest who is booking the hotel room';
  44. COMMENT ON COLUMN guest.address IS 'Address of the guest who is booking the hotel room';
  45. COMMENT ON COLUMN guest.credit_card_number IS 'Credit Card Number from the guest who is booking the hotel room';
  46. COMMENT ON COLUMN guest.employee_id IS 'Every guest has a representative employee who responds to the guests needs';
  47.  
  48. CREATE TABLE amenities(
  49.     id serial PRIMARY KEY,
  50.     name text NOT NULL CHECK(LENGTH (name) > 0),
  51.     price money NOT NULL CHECK (price > '0'::money),
  52.     min_age SMALLINT NOT NULL CHECK (min_age > 0)
  53. );
  54.  
  55. COMMENT ON TABLE  dadade.amenities IS 'The amenities of the hotel the guests can use';
  56. COMMENT ON COLUMN amenities.id IS 'The ID of the amenity in the hotel';
  57. COMMENT ON COLUMN amenities.name IS 'The name of the amenity in the hotel ';
  58. COMMENT ON COLUMN amenities.price IS 'The rental of the amenity being used';
  59. COMMENT ON COLUMN amenities.min_age IS 'The minimum age required to be able to use the amenities in the hotel';
  60.  
  61. CREATE TABLE appointment(
  62.     guest_id INTEGER NOT NULL REFERENCES guest(id)
  63.        ON DELETE cascade ON UPDATE cascade,
  64.     check_in_date DATE NOT NULL CHECK (check_in_date BETWEEN '1900-01-01' AND CURRENT_DATE),
  65.     room_id INTEGER NOT NULL REFERENCES room(id)
  66.        ON DELETE cascade ON UPDATE cascade,
  67.     number_of_guests SMALLINT NOT NULL CHECK (number_of_guests > 0),
  68.     extra INTEGER NULL REFERENCES amenities(id)
  69.        ON DELETE cascade ON UPDATE cascade
  70. );
  71.  
  72. COMMENT ON TABLE  appointment IS 'The appointment booked by the guest for the hotel allocating a room to the guest with optional amenity bookings';
  73. COMMENT ON COLUMN appointment.guest_id IS 'The information of the guest booking';
  74. COMMENT ON COLUMN appointment.check_in_date IS 'The check in date of the guest for the hotel booking';
  75. COMMENT ON COLUMN appointment.room_id IS 'The room where the guest is staying';
  76. COMMENT ON COLUMN appointment.number_of_guests IS 'How many guests are staying in the room booked';
  77. COMMENT ON COLUMN appointment.extra IS 'Guest who booked the room can also choose to book the amenities';
  78.  
  79.  
  80. --DML
  81. INSERT INTO amenities (name, price, min_age) VALUES
  82.     ('gym', '20.00', 12),
  83.     ('pool', '5.00', 1),
  84.     ('theatre room', '3.00', 6);
  85.  
  86. INSERT INTO employee (name, hiring_date) VALUES
  87.     ('David', '2014-07-07'),
  88.     ('Mirjam', '2013-08-03'),
  89.     ('Greg', '2015-04-22');
  90.  
  91. INSERT INTO room (name, smoke_free, wifi) VALUES
  92.     ('Ocean View', '1', '1'),
  93.     ('Street View', TRUE, FALSE),
  94.     ('Penthouse', 't', 'f');
  95.  
  96. INSERT INTO guest (name, age, address, credit_card_number, employee_id) VALUES
  97.     ('Anna', 32, '372 Street View Thunder Bay ON', 3782473950028432, (SELECT employee.id FROM employee WHERE employee.name = 'David')),
  98.     ('John', 48, '142 Balmoral Street Toronto ON', 3792749302947828, (SELECT employee.id FROM employee WHERE employee.name = 'David')),
  99.     ('Lauren', 22, '282 Oliver Road Thunder Bay ON', 3407300472847346, (SELECT employee.id FROM employee WHERE employee.name = 'Mirjam'));
  100.  
  101. INSERT INTO appointment (guest_id, check_in_date, room_id, number_of_guests, extra) VALUES
  102.     ((SELECT guest.id FROM guest WHERE guest.name = 'Anna'), '2015-10-07', (SELECT room.id FROM room WHERE room.name = 'Street View'), 3, (SELECT amenities.id FROM amenities WHERE amenities.name='gym')),
  103.     ((SELECT guest.id FROM guest WHERE guest.name = 'John'), '2015-10-05', (SELECT room.id FROM room WHERE room.name = 'Ocean View'), 3, (SELECT amenities.id FROM amenities WHERE amenities.name='gym')),
  104.     ((SELECT guest.id FROM guest WHERE guest.name = 'Lauren'), '2015-10-01', (SELECT room.id FROM room WHERE room.name = 'Penthouse'), 3, NULL);
  105.  
  106. DELETE FROM amenities WHERE name = 'theatre room';
  107.  
  108. UPDATE amenities SET price = 7.00 WHERE name = 'pool';
  109.  
  110. --TEST QUERIES
  111. --WILL PASS
  112. INSERT INTO amenities (name, price, min_age) VALUES
  113.     ('lounge', 1.00, 1);
  114. --WILL FAIL
  115. INSERT INTO amenities (name, price, min_age) VALUES
  116.     ('pool game', -2.00, 6); -- fails because the price is negative and it is supposed to be greater than 0
  117.  
  118. --WILL PASS
  119. INSERT INTO room (name, smoke_free, wifi) VALUES
  120.     ('suite', TRUE, TRUE);
  121. --WILL FAIL
  122. INSERT INTO room (name, smoke_free, wifi) VALUES
  123.     ('', TRUE, FALSE); -- fails because room.name is an empty string and the length is supposed to be greater than 0
  124.  
  125. --WILL PASS
  126. INSERT INTO employee (name, hiring_date) VALUES
  127.     ('Michelle', '2014-12-07');
  128. --WILL FAIL
  129. INSERT INTO employee (name, hiring_date) VALUES
  130.     ('Aldo', '2016-12-07'); --fails because hiring_date is in the future and it is supposed to be in the past or in the present
  131.  
  132.  
  133. SELECT AVG(min_age)
  134. FROM amenities;
  135.  
  136. SELECT MIN(price) AS minimum_price
  137. FROM amenities;
  138.  
  139.  
  140. SELECT avg(age)
  141. FROM guest;
  142.  
  143. SELECT guest_id, SUM(number_of_guests) AS Total_Guest
  144. FROM appointment
  145. GROUP BY guest_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement