IS 340 SQL Project Name: Nicholas Baccus ID: A25315659 Score: I pledge that I neither gave nor received help on this project other than help received from the instructor. I also understand that the instructor, in order to ensure fairness, will NOT directly help individual student on this project. Instead, the instructor will make every effort to clarify the requirements of this project and answer relevant questions to the class as a whole. Please save your completed file as a pdf file and submit it through UAH Canvas. Instructions: You are requested to design a database for a movie rental company. The following two tables contain the unnormalized data that you will use in the database. Please first draw the ER diagram to identify 4 entities and their relationships. Then normalize the data and create the database in your Oracle SQL account. Finally, write six SQL statements according to the detailed instructions given in Part II. Movie Rental Database Description and its Unnormalized Data: UNNORMALIZED TABLE 1. MOVIE_ID MOVIE_TITLE MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID 10 5 1 SciFi 8 3 1 SciFi 15 11 1 SciFi Texas Chainsaw Masacre 7 2 2 Horror 5 Jaws 7 1 2 Horror 6 The good, the bad and the ugly 7 2 3 Western 7 Silverado 7 1 3 Western 8 Duck Soup 5 1 4 Comedy 9 Planes, trains and automobiles 5 3 4 Comedy 1 Alien 2 Bladerunner 3 Star Wars 4 MOVIE_CATEGORY 10 Waking Ned Devine 12 4 4 Comedy 11 Deep Blue Sea 14 3 5 Drama 12 The Fifth Element 15 5 5 Drama Columns Descriptions ----------------------------------------------------movie_id Movie ID movie_title Movie Title movie_value The value (cost) of each movie movie_qty The number of copies available for each movie movie_cat_id Movie Category ID movie_category The category for each movie UNNORMALIZED TABLE 2. Columns Descriptions -----------------------------------------------Member_id Member's ID Last Member's last name First Member's first name License# Member's driver license number St Member's driver license issuing state Credit_card Member's credit card number Rental_id Rental transaction ID Rent_date Movie rent date Return_date Movie return date Pay_method Member's payment method for each rental transaction Part I: Datebase Design (30 Points) 1. Please draw the Entity-Relationship Diagram for your database below (15 points) Hint: you should identify 4 entities if you correctly normalize the data, and please name them as movie, movie_type, rental and member respectively. 2. Create the database in your SQL account based upon your ER diagram and normalized data. Display the content (SELECT * FROM table_name) and structure (DESCRIBE table_name) of the 4 tables (movie, movie_type, rental, and member) in your database. (15 Points) TABLE NAME TABLE CONTENT TABLE STRUCTURE MOVIE MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------1 Alien 10 5 1 2 Bladerunner 8 3 1 MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID Name Null? Type ----------------------------------------- ----------------------------------MOVIE_ID NOT NULL NUMBER MOVIE_TITLE VARCHAR2(255) MOVIE_VALUE NUMBER MOVIE_QTY NUMBER MOVIE_CAT_ID NUMBER TABLE NAME TABLE CONTENT TABLE STRUCTURE ----------- ---------- -----------3 Star Wars 15 11 1 4 Texas Chainsaw Masacre MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------7 2 2 5 Jaws 7 1 2 6 MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------The Good, The Bad, and The Ugly 7 2 3 7 Silverado 7 1 3 MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------8 Duck Soup 5 1 4 9 Planes, Trains, and Automobiles 5 3 4 MOVIE_ID ---------MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------10 Waking Ned Devine 12 4 11 Deep Blue Sea MOVIE_ID ---------- 4 TABLE NAME TABLE CONTENT TABLE STRUCTURE MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_QTY MOVIE_CAT_ID ----------- ---------- -----------14 3 5 12 The Fifth Element 15 5 MOVIE_TYPE RENTAL 5 12 rows selected. MOVIE_CAT_ID MOVIE_CATEGORY ------------ -------------------------------------------------1 SciFi 2 Horror 3 Western 4 Comedy 5 Drama RENTAL_ID MEMBER_ID MOVIE_ID RENT_DATE RETURN_DA ---------- ---------- ---------- --------- --------PAY_METHOD -------------------------------------------------1 10 11 18-SEP-09 01-OCT-09 Credit 2 Credit 10 8 18-SEP-09 03-OCT-09 3 Check 12 6 11-OCT-09 19-OCT-09 RENTAL_ID MEMBER_ID MOVIE_ID RENT_DATE RETURN_DA ---------- ---------- ---------- --------- --------PAY_METHOD -------------------------------------------------4 13 3 28-OCT-09 31-OCT-09 Debit 5 13 5 28-OCT-09 05-NOV-09 6 13 11 29-OCT-09 11-NOV-09 Debit Debit RENTAL_ID MEMBER_ID MOVIE_ID RENT_DATE RETURN_DA ---------- ---------- ---------- --------- --------PAY_METHOD -------------------------------------------------7 14 10 01-NOV-09 11-NOV-09 Credit 8 Credit 14 12 02-NOV-09 10-NOV-09 9 12 4 05-NOV-09 Cash RENTAL_ID MEMBER_ID MOVIE_ID RENT_DATE RETURN_DA ---------- ---------- ---------- --------- --------- Name Null? Type ----------------------------------------- ----------------------------------MOVIE_CAT_ID NOT NULL NUMBER MOVIE_CATEGORY VARCHAR2(50) Name Null? Type ----------------------------------------- ----------------------------------RENTAL_ID NOT NULL NUMBER MEMBER_ID NUMBER MOVIE_ID NUMBER RENT_DATE DATE RETURN_DATE DATE PAY_METHOD VARCHAR2(50) TABLE NAME TABLE CONTENT TABLE STRUCTURE PAY_METHOD -------------------------------------------------10 12 12 08-NOV-09 18-NOV-09 Cash MEMBER 11 Cash 12 3 14-NOV-09 12 Debit 13 11 18-NOV-09 12 rows selected. MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------LICENSE# ST CREDIT_CARD -------------------- -- -------------------10 Tangier Tim 1111111111 VA 123456789111 MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------LICENSE# ST CREDIT_CARD -------------------- -- -------------------12 Maulder Fox 3333333333 FL 3333333333 MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------LICENSE# ST CREDIT_CARD -------------------- -- -------------------13 Wild Coyote 4444444444 VA 4444444444 MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------LICENSE# ST CREDIT_CARD -------------------- -- -------------------14 Castel Joan 5555555555 VA 5555555555 Name Null? Type ----------------------------------------- ----------------------------------MEMBER_ID NOT NULL NUMBER LAST VARCHAR2(255) FIRST VARCHAR2(255) LICENSE# VARCHAR2(20) ST CHAR(2) CREDIT_CARD VARCHAR2(20) Part II: SQL and PL/SQL Questions Use only ONE SQL statement for each question (10*7=70 Points) 1. Display the ID, lastname and firstname of those members who have not returned the movies they rented. SQL Statement: SELECT m.member_id, m.last, m.first 2 FROM member m 3 JOIN rental r ON m.member_id = r.member_id 4 WHERE r.return_date IS NULL; Query Result Generated By ORACLE: MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------12 Maulder Fox 12 Maulder Fox MEMBER_ID ---------LAST -------------------------------------------------------------------------------FIRST -------------------------------------------------------------------------------13 Wild Coyote 2. Display the title, value (cost) and category of those movies whose titles contain "and". SQL Statement: SELECT mov.movie_title, mov.movie_value, mt.movie_category 2 FROM movie mov 3 JOIN movie_type mt ON mov.movie_cat_id = mt.movie_cat_id 4 WHERE LOWER(mov.movie_title) LIKE '%and%'; Query Result Generated By ORACLE: MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_VALUE MOVIE_CATEGORY ----------- -------------------------------------------------The Good, The Bad, and The Ugly 7 Western Planes, Trains, and Automobiles 5 Comedy 3. Display the rental transaction ID, member ID, the number of weeks between the movie rent date and return date (call this column "Delay_weeks", use "December 1, 2009" as the return date if a movie's return date is not yet available). The values in the "Delay_weeks" column should be rounded to integers. Please also display another new column with the name "Status" that shows whether the movie in each rental transaction was "Returned" or "Not Returned". SQL Statement: SELECT r.rental_id, 2 r.member_id, 3 ROUND( 4 (NVL( 5 r.return_date, 6 TO_DATE('2009-12-01', 'YYYY-MM-DD') 7 ) - r.rent_date) / 7 8 ) AS Delay_weeks, 9 CASE 10 WHEN r.return_date IS NULL THEN 'Not Returned' 11 ELSE 'Returned' 12 END AS Status 13 FROM rental r; Query Result Generated By ORACLE: RENTAL_ID MEMBER_ID DELAY_WEEKS STATUS ---------- ---------- ----------- -----------1 10 2 Returned 2 10 2 Returned 3 12 1 Returned 4 13 0 Returned 5 13 1 Returned 6 13 2 Returned 7 14 1 Returned 8 14 1 Returned 9 12 4 Not Returned 10 12 1 Returned 11 12 2 Not Returned 12 13 2 Not Returned 4. Display the movie category that generated more rental transactions than any other movie categories. Also display the number of rental transactions generated by this category. SQL Statement: SELECT mt.movie_category, COUNT(*) AS rental_transactions 2 FROM rental r 3 JOIN movie m ON r.movie_id = m.movie_id 4 JOIN movie_type mt ON m.movie_cat_id = mt.movie_cat_id 5 GROUP BY mt.movie_category 6 ORDER BY rental_transactions DESC 7 FETCH FIRST 1 ROWS ONLY; Query Result Generated By ORACLE: MOVIE_CATEGORY RENTAL_TRANSACTIONS -------------------------------------------------- ------------------Drama 5 5. Display the title, category, movie_qty and the average movie_value of those movies that were rented by customers using cash as payment. SQL Statement: SELECT m.movie_title, mt.movie_category, m.movie_qty, AVG(m.movie_value) AS average_movie_value 2 FROM rental r 3 JOIN movie m ON r.movie_id = m.movie_id 4 JOIN movie_type mt ON m.movie_cat_id = mt.movie_cat_id 5 WHERE r.pay_method = 'Cash' 6 GROUP BY m.movie_title, mt.movie_category, m.movie_qty; Query Result Generated By ORACLE: MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_CATEGORY MOVIE_QTY -------------------------------------------------- ---------AVERAGE_MOVIE_VALUE ------------------Star Wars SciFi 11 15 Texas Chainsaw Masacre Horror 7 2 MOVIE_TITLE -------------------------------------------------------------------------------MOVIE_CATEGORY MOVIE_QTY -------------------------------------------------- ---------AVERAGE_MOVIE_VALUE ------------------The Fifth Element Drama 15 5 6. Create an anonymous PL/SQL block containing a WHILE loop to display the top four movies with the greatest number of copies available (movie_qty). Please display all fields from the movies table in the output. Anonymous PL/SQL block: DECLARE 2 CURSOR movie_cursor IS 3 SELECT movie_title, movie_qty 4 FROM movie 5 ORDER BY movie_qty DESC 6 FETCH FIRST 4 ROWS ONLY; 7 8 BEGIN 9 FOR movie_rec IN movie_cursor LOOP 10 DBMS_OUTPUT.PUT_LINE('Title: ' || movie_rec.movie_title || ' - Quantity: ' || movie_rec.movie_qty); 11 END LOOP; 12 END; Output of the PL/SQL block: Title: Star Wars - Quantity: 11 Title: Alien - Quantity: 5 Title: The Fifth Element - Quantity: 5 Title: Waking Ned Devine - Quantity: 4 7. Create an anonymous PL/SQL block updating the pay_method of a member (whose member_id is 30) to “Credit”. However, no such a member exists in the database. Please use a user-defined exception to trap this error and return the message “This is an invalid member ID” as the output of this anonymous block. Anonymous PL/SQL block: DECLARE 2 v_member_id NUMBER := 30; 3 member_not_found EXCEPTION; 4 BEGIN 5 UPDATE member 6 SET pay_method = 'Credit' 7 WHERE member_id = v_member_id; 8 9 IF SQL%ROWCOUNT = 0 THEN 10 RAISE member_not_found; 11 END IF; 12 13 COMMIT; 14 EXCEPTION 15 WHEN member_not_found THEN 16 DBMS_OUTPUT.PUT_LINE('This is an invalid member ID'); 17 END;