Homework Assignment 2. Due Date: October 17th, CS425 - Database Organization Results

Similar documents
Quiz 2. Due November 26th, CS525 - Advanced Database Organization Solutions

Exam 1. March 12th, CS525 - Midterm Exam Solutions

Database Systems SQL. A.R. Hurson 323 CS Building

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL---Part 1

Correlated subqueries. Query Optimization. Magic decorrelation. COUNT bug. Magic example (slide 2) Magic example (slide 1)

L13: Normalization. CS3200 Database design (sp18 s2) 2/26/2018

Carnegie Mellon University Department of Computer Science /615 - Database Applications C. Faloutsos & A. Pavlo, Fall 2016

Sub-Queries in SQL SQL. 3 Types of Sub-Queries. 3 Types of Sub-Queries. Scalar Sub-Query Example. Scalar Sub-Query Example

b. hurricanescale.xml - Information on the Saffir-Simpson Hurricane Scale [3].

Design Theory: Functional Dependencies and Normal Forms, Part I Instructor: Shel Finkelstein

b. states.xml Listing of states and their postal abbreviations.

Chapter 8: Relational Database Design

Math 110 (S & E) Textbook: Calculus Early Transcendentals by James Stewart, 7 th Edition

MATH 137 : Calculus 1 for Honours Mathematics Instructor: Barbara Forrest Self Check #1: Sequences and Convergence

Languages. Theory I: Database Foundations. Relational Algebra. Paradigms. Projection. Basic Operators. Jan-Georg Smaus (Georg Lausen)

COM S 330 Homework 08 Solutions. Type your answers to the following questions and submit a PDF file to Blackboard. One page per problem.

CS 347 Parallel and Distributed Data Processing

Chapter 7: Relational Database Design

Relational Database Design

Chapter 7: Relational Database Design. Chapter 7: Relational Database Design

GUIDED NOTES 2.5 QUADRATIC EQUATIONS

Query Processing. 3 steps: Parsing & Translation Optimization Evaluation

CS145 Midterm Examination

Databases 2011 The Relational Algebra

Schema Refinement. Feb 4, 2010

Relational Design Theory I. Functional Dependencies: why? Redundancy and Anomalies I. Functional Dependencies

Relations. Relations of Sets N-ary Relations Relational Databases Binary Relation Properties Equivalence Relations. Reading (Epp s textbook)

download from

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #15: BCNF, 3NF and Normaliza:on

Page Points Score Total: 100

Schema Refinement and Normal Forms. Chapter 19

Logic and Databases. Phokion G. Kolaitis. UC Santa Cruz & IBM Research Almaden. Lecture 4 Part 1

Introduction to Database Systems CSE 414. Lecture 20: Design Theory

FUNCTIONAL DEPENDENCY THEORY II. CS121: Relational Databases Fall 2018 Lecture 20

High School Students Working with Rates of Change

CS246 Final Exam. March 16, :30AM - 11:30AM

SCHEMA NORMALIZATION. CS 564- Fall 2015

P Q1 Q2 Q3 Q4 Q5 Tot (60) (20) (20) (20) (60) (20) (200) You are allotted a maximum of 4 hours to complete this exam.

EECS-3421a: Test #2 Electrical Engineering & Computer Science York University

INTRODUCTION TO RELATIONAL DATABASE SYSTEMS

Databases Exam HT2016 Solution

Math , Fall 2014 TuTh 12:30pm - 1:45pm MTH 0303 Dr. M. Machedon. Office: Math Office Hour: Tuesdays and

Page Points Score Total: 100

CSE 132B Database Systems Applications

Databases 2012 Normalization

Constraints: Functional Dependencies

Number Theory: Representations of Integers

Relational Algebra on Bags. Why Bags? Operations on Bags. Example: Bag Selection. σ A+B < 5 (R) = A B

MATH 137 : Calculus 1 for Honours Mathematics. Online Assignment #2. Introduction to Sequences

FALL 2018 MATH 4211/6211 Optimization Homework 4

Work easier problems first. Write out your plan for harder problems before beginning them.

CHEMICAL INVENTORY ENTRY GUIDE

Dictionary: an abstract data type

Information Systems (Informationssysteme)

Mathematical Induction

Schema Refinement and Normal Forms. Why schema refinement?

Assignment 8: Functional Dependencies

Administering your Enterprise Geodatabase using Python. Jill Penney

Geographical Databases: PostGIS. Introduction. Creating a new database. References

Section 1: Sets and Interval Notation

CS322: Database Systems Normalization

NEW RIVER COMMUNITY COLLEGE DUBLIN, VIRGINIA COURSE PLAN

Oracle Designer. Report : TABLE DEFINITION Filename : s:\documents\pdfs\fept.pdf. 12 May Total Pages : 9

Wednesday, 10 September 2008

Outline. Wednesday, 10 September Schedule. Welcome to MA211. MA211 : Calculus, Part 1 Lecture 2: Sets and Functions

Functional Dependencies. Chapter 3

Entity-Relationship Diagrams and FOL

Lecture 6 Relational Database Design

Calculus Volume I: Introduction, With Vectors And Analytic Geometry By Tom M. Apostol

. ) =-, *+ 7 4* 8 ) 0 *+ 7 (c. 0 A #B (d

Homework Set 2, ECO 311, Fall 2014

Constraints: Functional Dependencies

Relationships between elements of sets occur in many contexts. Every day we deal with

MASTERING CHEMISTRY ACCESS PDF

Honors Geometry Summer Packet NHHS and VHS

MULTIVARIABLE CALCULUS BRIGGS PDF

Outline. Approximation: Theory and Algorithms. Application Scenario. 3 The q-gram Distance. Nikolaus Augsten. Definition and Properties

MULTIVARIABLE CALCULUS SOLUTIONS MANUAL 7TH DOCUMENT

Math 51 First Exam October 19, 2017

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

Use the Rational Zero Theorem to list all the possible rational zeros of the following polynomials. (1-2) 4 3 2

Natural Numbers: Also called the counting numbers The set of natural numbers is represented by the symbol,.

The Meaning of Entity-Relationship Diagrams, part II

Database Design Issues

Shuigeng Zhou. April 6/13, 2016 School of Computer Science Fudan University

Midterm, Fall 2003

A2 MATHEMATICS HOMEWORK C3

But RECAP. Why is losslessness important? An Instance of Relation NEWS. Suppose we decompose NEWS into: R1(S#, Sname) R2(City, Status)

Mathematics Without Calculations It s a Beautiful Thing!

Math 70 and 95 Master Syllabus Beginning and Intermediate Algebra, 5th ed., Miller, O Neill, Hyde with ALEKS.

MATH-0955: BEGINNING ALGEBRA

MATH 152 FINAL EXAMINATION Spring Semester 2014

FALL 2018 MATH 4211/6211 Optimization Homework 1

MAT01A1. Appendix E: Sigma Notation

Query answering using views

Geodatabase Programming with Python John Yaist

Functional Dependencies. Getting a good DB design Lisa Ball November 2012

Instructor: Sudeepa Roy

GAV-sound with conjunctive queries

/home/thierry/columbia/msongsdb/tutorials/tutorial4/tutorial4.py January 25,

Transcription:

Name CWID Homework Assignment 2 Due Date: October 17th, 2017 CS425 - Database Organization Results Please leave this empty! 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.15 2.16 2.17 2.18 2.19 Sum

Instructions Try to answer all the questions using what you have learned in class When writing a query, write the query in a way that it would work over all possible database instances and not just for the given example instance! Some questions are marked as bonus. You do not have to answer these questions to get full points for the assignment. However, you can get bonus points for these questions! Please submit the homework electronically using blackboard Consider the following library database schema and example instance storing: Hints: book bookid title price total_copies 1 Introduction of Algorithms 84.66 4 2 Database System Concepts 74.99 5 3 Stochastic Calculus for Finance I 41.02 3 4 Stochastic Calculus for Finance II 55.22 3 course courseid title instructorid textbookid 1 Algorithms 1 1 2 DB Organization 2 2 3 Advanced DB Organization 3 2 4 Math Finance I 1 3 5 Math Finance II 4 4 student studentid name gpa 1 Tom 3.3 2 John 3.8 3 Mary 3.0 4 Kris 3.6 5 Alex 3.5 faculty facultyid name salary 1 James 70000 2 Sarah 60000 3 Jay 80000 4 Rachel 70000 5 Paul 85000 book_checkout date bookid studentid 2017-08-29 1 1 2017-09-02 4 4 2017-09-07 1 4 enroll studentid courseid 1 1 1 2 2 1 4 3 4 4 5 5 All the attributes that have integer values are of type INT; numbers with decimal point are of type NUMERIC; the attribute date of book_checkout relation is of type DATE; others are of type VARCHAR Attributes with black background form the primary key of an relation The attribute instructorid of relation course is a foreign key to relation faculty, and textbookid is a foreign key to relation book. DB - Fall 2017: Page 2 (of 20)

The attribute studentid of relation enroll is a foreign key to relation student, and courseid is a foreign key to relation course. The attribute bookid of relation book_checkout is a foreign key to relation book, and studentid is a foreign key to relation student. DB - Fall 2017: Page 3 (of 20)

Part 2.1 SQL DDL (Total: 14 Points) Question 2.1.1 (7 Points) Write an SQL statement that adds an advisorid attribute to relation student, and sets it to be a foreign key to facultyid in relation faculty. In case a faculty s id is changed, the change would be reflected on advisorid attribute; in case a student s advisor left the school, advisorid would be set to NULL. ALTER TABLE student ADD [ COLUMN ] a d v i s o r i d INT REFERENCES f a c u l t y ( f a c u l t y i d ) ON UPDATE CASCADE ON DELETE SET NULL ; OR ALTER TABLE student ADD [ COLUMN ] a d v i s o r i d INT, ADD FOREIGN KEY ( a d v i s o r i d ) REFERENCES f a c u l t y ( f a c u l t y i d ) ON UPDATE CASCADE ON DELETE SET NULL ; DB - Fall 2017: Page 4 (of 20)

Question 2.1.2 (7 Points) Write an SQL statement that adds a constraint to the student relation to make sure that the gpa attribute cannot be NULL, and that the value of this attribute has to be between 0 and 4. Furthermore, the default value for this attribute should be 3. ALTER TABLE student ALTER [ COLUMN ] gpa SET NOT NULL, ALTER [ COLUMN ] gpa SET DEFAULT 3, ADD CHECK ( gpa >= 0 AND gpa <= 4 ) ; DB - Fall 2017: Page 5 (of 20)

Part 2.2 SQL Queries (Total: 56 + 10 BONUS Points) Question 2.2.1 (5 Points) Write an SQL query that returns the studentid and name of students who have overdue books (assume a book is due after 30 days), use construct CURRENT_DATE to access the current date in your query. Do not return any duplicate tuples. SELECT DISTINCT studentid, name FROM student NATURAL JOIN book_checkout WHERE CURRENT_DATE > date + 3 0 ; Natural join could be replaced by other correct forms, same below. DB - Fall 2017: Page 6 (of 20)

Question 2.2.2 (5 Points) Write an SQL query that returns the studentid and name of the student(s) whose gpa is higher than the average gpa of all students. SELECT studentid, name FROM student WHERE gpa > ( SELECT AVG( gpa ) FROM student ) ; DB - Fall 2017: Page 7 (of 20)

Question 2.2.3 (7 Points) Write an SQL query that returns the facultyid and name of any faculty that does not teach any course but has a salary that is higher than 80,000. SELECT f a c u l t y i d, name FROM f a c u l t y WHERE s a l a r y > 80000 AND f a c u l t y i d NOT IN ( SELECT i n s t r u c t o r i d FROM course ) ; DB - Fall 2017: Page 8 (of 20)

Question 2.2.4 (7 Points) Write an SQL query that returns the bookid and title of books that are used as textbooks for more than one course. SELECT b. bookid, b. t i t l e FROM book b WHERE b. bookid IN ( SELECT c. textbookid FROM course c GROUP BY c. textbookid HAVING count ( ) > 1 ) ; DB - Fall 2017: Page 9 (of 20)

Question 2.2.5 (7 Points) Write an SQL query that returns the studentid and name of students who have checked out books that are worth more than $100 in total. SELECT studentid, name FROM student NATURAL JOIN book_checkout NATURAL JOIN book GROUP BY studentid, name HAVING sum ( p r i c e ) > 100 DB - Fall 2017: Page 10 (of 20)

Question 2.2.6 (8 Points) Write an SQL query that returns the studentid and name of students who checked out the textbook of a course that they did not enroll in. WITH target_record AS ( ( SELECT b. FROM book_checkout b, course c WHERE b. bookid = c. textbookid ) EXCEPT ( SELECT date, bookid, s t u d e n t i d FROM book_checkout b NATURAL JOIN e n r o l l e NATURAL JOIN course c WHERE textbookid=bookid ) ) SELECT DISTINCT studentid, name FROM student NATURAL JOIN target_record ; Alternatives include using NOT IN DB - Fall 2017: Page 11 (of 20)

Question 2.2.7 (9 Points) Suppose a student can check out as many books as the number of courses he/she is enrolled in. Write an SQL query that returns studentid and name of students who can not check out any more books. Your answer should include students who did not enroll in any course. DB - Fall 2017: Page 12 (of 20)

WITH allowedbooks AS ( SELECT studentid, count ( ) AS allowed FROM e n r o l l GROUP BY s t u d e n t i d ), checkedbooks AS ( SELECT studentid, count ( ) AS checked FROM book_checkout GROUP BY s t u d e n t i d ) SELECT studentid, name FROM student NATURAL JOIN allowedbooks LEFT NATURAL OUTER JOIN checkedbooks WHERE allowed COALESCE ( checked, 0 ) = 0 UNION SELECT studentid, name FROM student WHERE s t u d e n t i d NOT IN ( SELECT s t u d e n t i d FROM e n r o l l ) ; Alternatively, using outer join: WITH allowedbooks AS ( SELECT studentid, count ( ) AS allowed FROM e n r o l l GROUP BY s t u d e n t i d ), checkedbooks AS ( SELECT studentid, count ( ) AS checked FROM book_checkout GROUP BY s t u d e n t i d ) SELECT studentid, name FROM student s LEFT OUTER JOIN ( allowedbooks a LEFT NATURAL JOIN checkedbooks c ) ON ( s. s t u d e n t i d = a. s t u d e n t i d ) WHERE allowed COALESCE ( checked, 0 ) = 0 OR allowed IS NULL Note that the COALESCE is not necessary here, but only added for clarity since any student that has not checked out any books and is registered for at least one couse should not be returned anyways. DB - Fall 2017: Page 13 (of 20)

Question 2.2.8 (8 Points) Write an SQL query that returns the bookid and title of books that have 3 or more available copies. SELECT bookid, t i t l e FROM book b LEFT OUTER JOIN ( SELECT bookid, count ( ) AS num_checked FROM book_checkout GROUP BY bookid ) c ON b. bookid=c. bookid WHERE t o t a l _ c o p i e s num_checked >=3 OR ( t o t a l _ c o p i e s >=3 AND num_checked IS NULL ) Alternatively, use COALESCE: SELECT bookid, t i t l e FROM book b LEFT OUTER JOIN ( SELECT bookid, count ( ) AS num_checked FROM book_checkout GROUP BY bookid ) c ON b. bookid=c. bookid WHERE t o t a l _ c o p i e s COALESCE ( num_checked, 0 ) >= 3 DB - Fall 2017: Page 14 (of 20)

DB - Fall 2017: Page 15 (of 20)

Question 2.2.9 BONUS (5 Points) Write an SQL query which returns courseid and title of the course(s) that has/have the most expensive textbook. SELECT c. courseid, c. t i t l e FROM book b, course c WHERE b. bookid = c. textbookid AND p r i c e = ( SELECT max( p r i c e ) FROM book ) ; DB - Fall 2017: Page 16 (of 20)

Question 2.2.10 BONUS (5 Points) Write an SQL query that returns the studentid and name of students that enrolled in all of the finance courses. A course is considered a finance course if the title of the course contains the string Finance. WITH f i n a n c e AS ( SELECT FROM course WHERE t i t l e LIKE %Finance% ) SELECT s. studentid, s. name FROM student s NATURAL JOIN e n r o l l e NATURAL JOIN f i n a n c e f GROUP BY s. studentid, s. name HAVING count ( ) = ( SELECT count ( ) FROM f i n a n c e ) ; The query returns no tuple in the sample instance. Alternatively, you can use a nested subquery here. DB - Fall 2017: Page 17 (of 20)

Part 2.3 SQL Updates (Total: 30 + 5 BONUS Points) Question 2.3.1 (7 Points) Delete all courses that no one is enrolled in. DELETE FROM course WHERE c o u r s e i d NOT IN ( SELECT c o u r s e i d FROM e n r o l l ) ; Question 2.3.2 (8 Points) 4 copies of a new book Distributed and Cloud Computing has been added to the library. The price is $50.00 for each copy. Add the information to the book relation. Assume that bookid is automatically maintained by the system. INSERT INTO book ( t i t l e, p r i c e, t o t a l _ c o p i e s ) VALUES ( D i s t r i b u t e d and Cloud Computing, 5 0. 0 0, 4 ) ; DB - Fall 2017: Page 18 (of 20)

Question 2.3.3 (6 Points) One of the checkout records is incorrect. It turns out, the student with id 4 never checked out the book with id 1. He checked out the book with id 2. Update the information in book_checkout relation. UPDATE book_checkout SET bookid=2 WHERE bookid=1 AND s t u d e n t i d =4; Question 2.3.4 (9 Points) Update the gpa in the student relation according to this rule: if it is negative, set it to 0 if it is larger than 4, then set it to 4 if it is NULL, set it to 3 if none of the above applies do not change the gpa Note that we expect you to write a single statement that implements this. UPDATE student SET gpa=(case WHEN gpa<0 THEN 0 WHEN gpa>4 THEN 4 WHEN gpa IS NULL THEN 3 END ) WHERE gpa<0 OR gpa>4 OR gpa IS NULL ; or UPDATE student SET gpa=(case WHEN gpa<0 THEN 0 WHEN gpa>4 THEN 4 WHEN gpa IS NULL THEN 3 ELSE gpa END ) ; DB - Fall 2017: Page 19 (of 20)

Question 2.3.5 BONUS (5 Points) Update the salaries of faculty as their current salary + 10000 (the number of courses they are teaching). UPDATE f a c u l t y SET s a l a r y = s a l a r y + 10000 ( SELECT count ( ) FROM course WHERE i n s t r u c t o r i d=f a c u l t y i d ) ; DB - Fall 2017: Page 20 (of 20)