INFO 1103 Homework Project 2 February 15, 2019 Due March 13, 2019, at the end of the lecture period. 1 Introduction In this project, you will design and create the appropriate tables for a version of the system used in Homework 1. Although your ERD solution for Homework 1 might be as good or better than the one in the diagram below, please use the provided one. insert some data into each table. write SQL queries against this data. Remember, this is to be individual work, unlike the labs. 1
Figure 1: Starting ERD 2 Details Details follow about table creation, data insertion and the required queries. 2.1 Table Creation Refer to Figure 1, which is a subset of the ERD from Project 1. (The names of attributes probably don t meet the guidelines from the textbook.) In the given design, Song Recording is an associative entity that replaces a many-to-many relationship between Album and Song. Its multivalued attribute time played gives the times (date and time of day) when the song was played by the station. Both original lyrics and modified lyrics are optional. There are two sets of lyrics because sometimes a recording artist will sing something slightly changed from the original lyrics. Don t forget to enforce that only certain rating score values are allowed. Since I have not provided metadata definitions for entities, relationships and attributes, you will have to rely on your own best guesses based on attribute 2
names as to the appropriate types. Use some form of whole numbers for unique identifiers. Ask me questions if you wish. Create a.sql script in the Oracle tool that contains the commands necessary to build the tables. Include all appropriate constraints for domain values, foreign keys and primary keys. Follow the steps for generating tables from ER models (in Chapter 4). 2.2 Data Creation Continue your.sql script to contain the necessary SQL INSERT commands to add several rows to each table. Some of the rows will be of your own choosing, but you need to make sure that the rows added also include the following: 1. at least 4 users. The first user has user id = 1 and has 2 past payments recorded. One of the other users has user id=2 and has 1 past payment recorded. 2. at least 6 songs. One song (with song id=1) has been recorded 3 times and one song (with song id = 2) has not been recorded at all. The rest have been recorded once each. We have original lyrics for one song that was then recorded with different lyrics. 3. at least 4 albums. None of them have the cover photo (since we don t want to get into BLOBs with images). 4. at least 8 comments from user id=1 and at least 2 comments from user id=2. At least 2 song recordings have been commented on. For one song recording, user id=1 commented, user id=2 did a comment in response, and then user id=1 commented in response to the response. At least one user has no comments. 5. Three users upvoted one comment and downvoted another comment. One of comments was upvoted by the comment s author, and (strangely) another comment was downvoted by its author. 6. at least 4 ratings. Two users are responsible for all the ratings. In at least one case, a user has rated a recording but not commented on it. In at least one case, a user has commented on a recording but not rated it. And in at least one case, a user has both commented on and rated a song recording. The song with song id=1 has had all its recordings rated by user id=1, who loved one of the recordings and hated the other two recordings. 7. Attempt to insert a rating whose score is outside of the allowed range. The attempt should fail. 8. Two recordings have been played 3 times each and another has been played 2 times. Use non-null dates when recording this information. 3
2.3 Queries Continue your.sql script with some SELECT and CREATE VIEW commands. Some of the queries can be answered from a single table, although many of them require joining tables. Don t take advantage of the fact that you know what is in each of your tables. The grader should be free to add additional rows into your tables, if he wishes. Give SQL statements for the following. The first 11 are worth 2 points each and the remaining 6 are worth 3 points each. 1. Determine the number of users. 2. For each song, give its title and id, as well as the album name and artist of each recording of it. Sort the result by song id. 3. For each song, give its title and id, as well as the album name and artist and average rating of each recording of it. 4. For each user, determine the total value of the past payments they have made. 5. For each different song title, give the number of distinct artists who have recorded it. If two different songs have the same title, their results should be combined. 6. For each album (by id and name), determine the total number of comments about recordings on it. 7. For song id 1 and user id=1, list the album names where they rated at a recording of the song, and show the rating they gave. 8. For each album (by id and name), give the number of recordings on it and the average rating for the recordings on it. Your result should be sorted by album and ranking. 9. Give the name and id of each user and also the number of comments made by the user. Make sure that users who have never made a comment are listed as having 0 comments. 10. List the songs, by title, where some recording has different (non-null) lyrics from the song s original lyrics. 11. For each recording (give album name and song title) that has been played at least 2 times, give the number of times it has been played and the date when it was most recently played. 12. Show the comments (id and words) and their authors names when the person who wrote the comment also voted on the comment. Show the direction of each such vote. 4
13. (a) Show the comments (ids and words) that are responses to original (non-response) comments. You probably will want to do a self join. (b) Repeat, except you want comments that are responses to responses to original comments. 14. Show the recordings (by album name and song title) that nobody has rated. The minus or except operator is likely to be helpful. 15. Create (and show) two views (a) One gives, for each user id, the highest rating the user has given. (b) One gives, for each rating given, the user id, song title and album name that goes with it. 16. Using the views from the previous question, provide a list, sorted by user name, showing each user s name and the recordings (by song title and album name) that have received that user s top rating (also show the rating received). Users who have submitted no ratings should not be listed. 17. For each user, show their most highly rated album (taking the average of the ratings they have given recordings on the album). In the event of ties, you may either list several albums or just choose one of them. Users who have never rated anything should not be shown. The queries are generally listed in order of my perception of their difficulty. My perception might not match your reality; if one query is stumping you, go on and try the next. However, your script should have the queries in the given order. Please use SQL comments (eg -- query 3) in the script to indicate which query you are giving. 3 Grading Submit your.sql script that creates the tables, populates them, and then answers the queries. It should be possible for whoever grades the assignment to run your script in Oracle SQL Developer and get results, without errors. Submit this on paper and in D2L. The table creations are worth 40%, the insert commands are worth 20%, and the queries (and view creation) are worth 40%. Also, submit (paper and D2L) the transcript of what happens if you play the script (i.e., the contents of the output window in Oracle SQL Developer). 5