CS 200 Fall 2012 Assignment 8 SQL Due Monday November 10 by 4:00 pm Read Notes 6, in particular Database Design and Watcom SQL User s Guide. Please follow these instructions very carefully to find the files and programs you need. Open Macintosh HD : Users : Shared 1) Within this folder you must create a new folder labeled yhc. 2) It is within this folder that you must drag the contents of the universitytables folder, in which the university.db is located. The folder contents to copy can be found in cs200 : cs200 Public : Files For SQL : For the Assignment : universitytables 3) The permissions on the yhc folder need to be changed so that you, as the owner, have both read and write privileges. In addition, change the group and others so that they have no access. Now you have a copy of the university.db on your local machine. 4) In the Applications folder on Macintosh HD, find the SQLAnywhere10 folder 5) Start DBLauncher. 6) When prompted, browse to find your university.db file and click start. Click yes on the window that follows. 7) Your database is now running. Go back to the SQLAnywhere10 folder and start Interactive SQL. 8) Under the Identification tab, input the following. UserID : dba Password : sql 9) Under the Database tab, input the name University into the database name field and browse to find your university.db file. 10) Click OK 11) The dialog box should disappear without any errors. You are now ready to send requests. When you are finished working with SQL, quit the DB launcher first and then quit the InteractiveSQL. DB Launcher starts the database server and allows you to open university.db InteractiveSQL allows you to enter and execute SQL commands from a command window. CS 200 SQL Page 1
You may work on question 1 with a partner. If you do, be certain that you both understand all of what you have done. The purpose of allowing you to work in groups is to enable you to help each other understand the assignment, not to enable you to divide up the work. 1. Copy the file cs200 : cs200 Public : Files For SQL : For the Assignment : Assign8.doc to your cs200main network disk on Oscar twice and rename one yhc_sql_q1.doc or (yhc1_yhc2_sql_q1.doc if you are working in pair) and the other yhc_sql_q2.doc (feel free to delete the parts not relevant to the specified question). Using MS Word, insert your input and/or output after each sub-question when asked. Please format your answers with the styles AnswerTop,Answer, AnswerBot, and SQL_Output (for tabular SQL output, where requested), which are already in Assign7.doc 1. Approximately 7% of the marks for this question are reserved for proper formatting of your answer within Assign8.doc. [75%] In InteractiveSQL create the appropriate statements to answer the queries below: a) What SQL statement would you use to list the courses known to the database? b) What SQL statement would you use to list the student numbers of people taking Algebra? c) What SQL statement would you use to list the student numbers and names of students taking Algebra, ordered by surname? d) What SQL statement would you use to count the number of students taking Algebra? (And what is the answer?) e) What SQL statement would you use to list the assignments (and exams) for Algebra? (Please include the output, formatted as a table.) f) What SQL statement would you use to list the student number, course code, assignment number, assignment description, assignment weight, and mark for all the Calculus marks for the student with ID 86088? (Please include the output, formatted as a table.) g) What SQL statement would you use to compute the course mark in Calculus for the student with ID 86088? (And what is the answer?) h) What SQL statement would you use to list the course marks for students in Calculus? (Please include the first ten lines of the answer, formatted as a table.) i) What SQL Select statement would you use to compute the course mark in Calculus, for students who also took Algebra? (Please include the output, formatted as a table.) Bonus (5 marks) j) What SQL statement would you use to determine if there is more than one student with the same last name in the database? List the people in alphabetical order (first name) only once. 2 1 Use AnswerTop for the first line of a prose answer it has a little more before leading and Answer for the rest except for the paragraph just before a table, for which AnswerBot is appropriate because it adds a bit of extra leading after the paragraph (and before the table). 2 Hint: Make sure that the names that are duplicated are displayed with the first initials. CS 200 SQL Page 2
The following questions must be done independently. 2. [15%] Consider the Albums database we discussed in lecture, as shown to the right. Suppose we wish to keep track of the artists who appear on the albums in our collection, and record biographical information about them. Clearly we need an Artists table like the one shown below the Albums Table. a) Explain why these two tables cannot be joined as they are and explain how a third table, with exactly two fields, could record which albums each artist appears on. You should provide a diagram of this table based on the fields of the Album and Artists tables shown here, and you should provide information about its primary and foreign keys. Your answer should also include an explanation of how each of the following problems are solved: i) one artist is likely to contribute to many albums ii) one album is likely to have many artists on it iii) in neither case do you know how many b) Given your answer to (a), supply an SQL statement that would list the artists appearing on the album (or albums!) named Fare Thee Well Love. c) Given your answer to (a), supply an SQL statement that would list the Album Title of the albums to which an artist named Jimmy Rankin has contributed. Submission Instructions For electronic submission, a folder named yhc_assign8 containing: Your formatted Word document with answers to Question 1 named yhc_sql_q1.doc or yhc1_yhc2_sql_q1.doc (if you had a partner). Your formatted Word document with answers to Question 2 named yhc_sql_q2.doc. Printout of yhc_sql_q1.doc (or yhc1_yhc2_q1.doc) and yhc_q2.doc with your name(s) and handin code(s) in the first page header. Albums Album ID Album Title Recording Group Medium Category Price Purchase Date Year Recorded Recording Label Artists Artist ID First Name Last Name Gender Birth Date Comments CS 200 SQL Page 3
The Watcom University Database Sample.db For each database table we show first the names of the fields in that table, and below them examples of the data values that may appear in each. The names of the fields comprising the primary key are shown in bold. Underlined fields are (or are part of) a foreign key; different kinds of underlining mark distinct foreign keys. (However, limitations in Microsoft Word make it impossible to so mark fields that are part of more than one foreign key, so we also describe all foreign keys textually.) Perhaps the simplest part of the database consists of the two tables that record the people involved: students and instructions. Student Employee SurName Initials Address Birthdate Sex 86036 'COUTTS' 'S.E.' '289 Maple Drive...' 8 May 1966 'M ' EmpNum EmpName Initials Address RoomNum 81494 'DIXON' 'S.R.' '91 Trailwood Drive...' 1180 Some fields are omitted to save space. Employee numbers and student numbers look the same. This is not a problem for the database system, but it is potentially confusing for people using the database. What rooms are available, both for employee offices and for the lectures/labs? Room RoomNum Type Capacity 'LR#1' 'LECT' 40 There are three kinds of rooms: lecture rooms ( LR#1 and LR#2, of type LECT ), labs ( CHLab, CSLab, and PHLab, of type LAB ), and offices (e.g. 1180, of type OFF )
The table describes what courses are available. There is a record for each offering of a course, and for each meeting of a section there is a Class record. Finally, the Lab table associates lectures with labs. Class Credit Hours Description 'ENG' 0.50 2 'Basic Report Writing (elective)' Contains the sort of information about a course that you would find in a university catalog. Both lectures (such as CS ) and labs (such as CSL ) appear in the table. The Lab table links the two (when appropriate). EmpNum 'PHYSL' 2 64050 A course of sufficient size is delivered in several sections, for each of which there is a section record; each section meets several times a week, and for each of those meetings there is a separate Class record. Day Time Hours RoomNum 'PHYS' 1 'MON' 09:00 1 'LR#1' 'PHYS' 1 'WED' 09:00 1 'LR#1' 'PHYS' 1 'FRI' 09:00 1 'LR#1' Lab Contains information about a particular meeting of a course. [, ] is a foreign key for the table. [ RoomNum ] is a foreign key for the Room table. Lab 'CS' 'CSL' Records the connection between a lecture and a lab. Both are defined in the table. There is some potential for confusion here, in that while labs are s in the sense that they appear in the table, when a lab appears in this table it should appear only as the value of the Lab field. Sample.db Page 2
Who s enrolled in what course?. Register 86075 'PHIL' 1 This table records enrollment information. Students always enroll in a particular section of a course, even if there is only one section. The primary key, however, is just (, ) because a student can t enroll in more than one section, so there can t be more than one Register record with the same student number and course code. [ ] is a foreign key for the Student table. [] is a foreign key for the table. [, ] is a foreign key for the table. Assignment Mark AssignNum Weighting Description Date Due 'ECON' 3 20.00 'Midterm' '24 Oct 1986' Contains general information about a particular assignment (or exam) in a particular course. (Information about the mark a student received on an assignment is stored in the Marks table.) [ ] is a foreign key for the table. AssignNum Mark 86005 'CALC' 5 85 Records the mark a student received for a particular assignment (or exam) in a particular course. [, ] is a foreign key for the table. [, AssignNum ] is a foreign key for the Assignment table.
Watcom's University Database Sample.db Student Employee SurName Initials Address Birthdate Sex EmpNum EmpName Initials Address HomePhone LabPay Rank RoomNum Mark Register Room AssignNum Mark EmpNum RoomNum Type Capacity Assignment Lab Class AssignNum Weighting Description Date Due Credit Hours Description Lab Day Time Hours RoomNum Fields comprising the primary key are in bold. The arrows connect foreign keys to the linked table. In these tables, fields with the same name always indicate matching foreign and primary key fields, but that is often not the case and is certainly not a requirement.