Assignment 1 Comp 521 Fall 2015

Assigned: 3 September 2015
Due: 17 September 2015

Answer each question below by filling in code at the point indicated. Do not modify any of the other cells.

You must run the cells in order. It looks like you can come back later after restarting and pick up where you left off but that is not the case. When restarting you always have to start at the top of the notebook and run each cell in turn.

Question 1

Use SQL to create tables for the following database schema modeling a library.

STUDENT sid(INTEGER) sname(TEXT)
BOOK bid(INTEGER) bname(TEXT) author(TEXT)
BORROWS id(INTEGER) sid(INTEGER) bid(INTEGER) date(TEXT)

All primary keys are bold. In the BORROWS table, bid is a foreign key referencing BOOK and sid is a foreign key referencing STUDENT.

Make sure you use the exact names and data types for the tables and attributes in order to pass the automatic test.

Question 2

Insert the following data into the tables you created above.

STUDENT
sidsname
1Alice
2Bob
3Cindy
BOOK
bidbnameauthor
1Database Management SystemsRamakrishnan
2Lord of the RingsTolkien
BORROWS
idsidbiddate
1218/3/2015
2329/1/2015

Question 3

Delete the record with id equal 1 from the BORROWS table.

Setup for questions 4-12

Consider the following schema for a database modeling courses at UNC.

    DEPARTMENT (did, name)  
    COURSE (cid, did, name, num, creditHours)  
    STUDENT (sid, fname, lname, did)  
    ENROLLED_IN (eid, sid, cid)

In the COURSE table, did is a foreign key referencing DEPARTMENT(did). In the ENROLLED_IN table, sid is a foreign key referencing STUDENT(sid), and cid is a foreign key referencing COURSE(cid). In STUDENT, did is a foreign key referencing DEPARTMENT(did).

Questions 4-8

Use Dee to write relational algebra queries for the following. In particular, you may find the following Dee operators most useful: Projection, Restriction (corresponds to select in relational algebra), Join, Intersection, Difference, Union, and Divide.

Your, already populated, Dee database is named DeeDB.

Question 4

Find the names of all courses below the 500 level. You can get the Dee relation for COURSE with DeeDB.COURSE.

Question 5

Find the first and last names of all students enrolled in "Files and Databases".

Question 6

Find the first and last names of all students in the ANTH department.

Question 7

List the first and last names of all students enrolled in no courses.

Question 8

List the first and last names of all students enrolled in every COMP course.

Questions 9-12

Now use the sqlite3 database courseEnrollment.db, which has the same schema as above to write SQL queries for the following. I will open it for you below. You must execute this next cell before the others!

Question 9

Find the names of all 4 credit hour courses.

Question 10

Find the first and last names of all students enrolled in COMP courses.

Question 11

Find the first and last names of all students enrolled in courses of at least 500 level.

Question 12

Find the first and last names of all students enrolled in 'Data Structures' and "Anthropology of the Body and the Subject'.

Done!

Now get your report and submit your assignment.