CS127 Homework #3. Due: October 11th, :59 P.M. Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E)

Similar documents
Relational Model. Nisa ul Hafidhoh

Relational Database Design (II)

CS 582 Database Management Systems II

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Relational model and basic SQL

Chapter 3: Introduction to SQL

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

CSCB20. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 8: Relational Database Design

Unit 3 : Relational Database Design

Chapter 4: Intermediate SQL

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

QQ Group

Lecture 6 - More SQL

SQL Retrieving Data from Multiple Tables

Example Examination. Allocated Time: 100 minutes Maximum Points: 250

Chapter 4: Intermediate SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Natural-Join Operation

Chapter 4: Intermediate SQL

MySQL Views & Comparing SQL to NoSQL

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Explain in words what this relational algebra expression returns:

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CS3DB3/SE4DB3/SE6M03 TUTORIAL

The Relational Model Constraints and SQL DDL

Database Management Systems

Chapter 7: Entity-Relationship Model. Chapter 7: Entity-Relationship Model

Homework 3: Relational Database Design Theory (100 points)

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

BCNF. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong BCNF

Relational Algebra. Procedural language Six basic operators

Unit 2: SQL AND PL/SQL

Chapter 2: Intro to Relational Model

Intermediate SQL ( )

COMP7640 Assignment 2

CS425 Fall 2013 Boris Glavic Chapter 7: Entity-Relationship Model!

Generating Test Data for Killing SQL Mutants: A Constraint-based Approach

ER to Relational Model. Professor Jessica Lin

Chapter 7: Entity-Relationship Model

Unit1: Introduction. Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Entity-Relationship Model

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

Chapter 2: Intro to Relational Model

CSCI1270 Introduction to Database Systems

Homework 6. Question Points Score Query Optimization 20 Functional Dependencies 20 Decompositions 30 Normal Forms 30 Total: 100

Examination examples

Roadmap of This Lecture. Weak Entity Sets Extended E-R Features Reduction to Relation Schemas Database Design UML*

Triggers- View-Sequence

arxiv: v4 [cs.db] 13 Jul 2015

Principles of Data Management

CS40 Exam #2 November 13, 2002

Database Management Systems,

Score. 1 (10) 2 (10) 3 (8) 4 (13) 5 (9) Total (50)

CSE 444 Midterm Test

Homework 2. Develop an EER model for the following situation using the EER diagram,

Chapter 6 Formal Relational Query Languages

Database Management Systems (Classroom Practice Booklet Solutions)

Relational Algebra and SQL

Chapter 7: Entity-Relationship Model

CS x265 Exam 3 Spring ALL questions 5 points, except question 11 I will not use a source other than my brain on this exam: (please sign)

Database Management Systems (Solutions for Vol 1_Classroom Practice Questions)

SQL Functions (Single-Row, Aggregate)

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Winter 2009 Lecture 4 - Schema Normalization

Name: Database Systems ( 資料庫系統 ) Midterm exam, November 15, 2006

Normalization. Murali Mani. What and Why Normalization? To remove potential redundancy in design

Chapter 1: Introduction

Chapter 6 - Part II The Relational Algebra and Calculus

CS 461: Database Systems. Final Review. Julia Stoyanovich

Chapter 7: Entity-Relationship Model

CSE 344 Midterm. Wednesday, February 19, 2014, 14:30-15:20. Question Points Score Total: 100

Chapter 7: Entity-Relationship Model

Chapter 2 Introduction to Relational Models

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

IMPORTANT: Circle the last two letters of your class account:

Database Management

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Query Optimization. Chapter 13

Chapter 6: Formal Relational Query Languages

Fall 2018 CSE 482 Big Data Analysis: Exam 1 Total: 36 (+3 bonus points)

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Lecture 3: SQL Part II

Relational Model, Relational Algebra, and SQL

Functional Dependencies CS 1270

Unit I. By Prof.Sushila Aghav MIT

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

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Entity-Relationship Modelling. Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables

The En'ty Rela'onship Model

Database Management Systems Paper Solution

The Relational Data Model

Chapter 13: Query Optimization. Chapter 13: Query Optimization

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SOLUTIONS TO THE FINAL EXAMINATION Introduction to Database Design Spring 2011

Transcription:

CS127 Homework #3 Warmup #1 Consider the following set of functional dependencies, F, for the schema R(A, B, C, D, E) 1. Find the candidate keys for the schema R. AB, C, D, and EA 2. Compute the closure, F +, for the set F. C D D EA E B Hint: you can use * to refer to any subset of attributes in R, and α to indicate the full set {A, B, C, D, E}. So A α means that A, along with any and all subsets of {B, C, D, E}, functionally determines all attributes. EB E, EB B, A A, B B, EB EB, E B, E E, E EB, and all the dependecies of the form: AB α, C α, D α, EA α Warmup #2 Using the functional dependency set from the first warmup, compute the canonical cover F c. D EA,, E B, C D Warmup #3 Consider the following schema: student(id, name, departmentname, totalcredits) professor(id, name, departmentname, salary) classroom(building, roomnumber, capacity) course(courseid, semester, year, building, roomnumber, professorid) takes(studentid, courseid, grade) Note that: the building, roomnumber attributes of course relation are references to the classroom relation professorid of course is a reference to professor studentid of takes is a reference to student 1

courseid of takes is a reference to course 1. Considering the given database schema, write SQL queries to construct this database, where the attributes name, departmentname, building are strings of various lengths (maximum length of 50), semester is only one character, and all remaining attributes are integers. CREATE TABLE student ( ID int PRIMARY KEY, name varchar(50), departmentname varchar(50), totalcredits int) CREATE TABLE professor ( ID int PRIMARY KEY, name varchar(50), departmentname varchar(50), salary int) CREATE TABLE classroom ( building varchar(50), roomnumber int, capacity int, PRI- MARY KEY(building, roomnumber)) CREATE TABLE course ( courseid int PRIMARY KEY, semester char(1), department- Name varchar(50), year int, building varchar(50), roomnumber int, professorid int, FOREIGN KEY (building, roomnumber) REFERENCES classroom, FOREIGN KEY professorid REFERENCES professor) CREATE TABLE takes ( studentid int, courseid int, grade int, PRIMARY KEY(studentID, courseid) FOREIGN KEY studentid REFERENCES student, FOREIGN KEY courseid REFERENCES course) 2. Write a SQL query to find the name of every professor in the CS department. SELECT name FROM professor WHERE departmentname = S 3. If the salary attribute of professor represents monthly salary, write a SQL query to find every possible pairing of student ID and professor ID, where professor IDs included in the result consist of the IDs of the professors whose yearly salary is greater than $10,000 and student IDs included in the result consist of IDs of students who have completed fewer than 10 credits. SELECT S.ID, P.ID FROM student AS S, professor AS P WHERE P.salary * 12 > 10000 AND S.totalCredits < 10 Warmup #4 Write a SQL query for each of the following, using the schema from warmup 3: 1. If the salary attribute of professor represents monthly salary, find the names and yearly salaries of professors who hold classes in rooms that do not exist in classroom. SELECT name, salary * 12 FROM (professor JOIN course ON professor.id = course.professorid) LEFT JOIN classroom ON course.building = classroom.building AND course.roomnumber = classroom.roomnumber WHERE building IS NULL AND roomnumber IS NULL 2. Find the name, total credits, and GPA of every student whose GPA is greater than 3.5. GPA can be calculated by summing a student s grades and dividing by total credits. SELECT name, totalcredits, SUM( grade ) / totalcredits AS GPA FROM student JOIN takes ON student.id = takes.studentid GROUP BY name, totalcredits HAVING GPA > 3.5 3. Write a SQL query for finding the ID and name of the professor(s) who earns the highest salary, without using aggregate functions. SELECT ID, name FROM professor WHERE ID NOT IN (SELECT P.ID FROM professor AS P, professor AS PP WHERE P.salary < PP.salary) Problem 5 (To Be Graded) Using the following schema, write SQL queries to answer the following questions. advisor(s ID, i ID) classroom(building, room number, capacity) course(course id, title, dept name, credits) 2

department(dept name, building, budget) instructor(id, name, dept name, salary) section(course id, sec id, semester, year, building, room number, time slot id) student(id, name, dept name, tot cred) takes(id, course id, sec id, semester, year, grade) teaches(id, course id, sec id, semester, year) 1. Find the students who have had classes in the highest number of distinct buildings. with stud_class as (select ID, count(distinct building) as building_count from student natural join takes natural join section as t group by ID) select id, from stud_class where building_count = (select max(building_count) from stud_class) 2. Find the instructor who has given the highest number of A s. with takes_student as (select takes.id as stud_id, course_id, sec_id, semester, year, grade from takes inner join student on stud_id = student.id) with count_table as (select id, count(stud_id) as grade_count from (takes_student natural join teaches) natural join instructor where grade = A group by instructor.id) select id from count_table where grade_count = (select max(grade_count) from count_table) 3. Display the names of all students along with their advisor s name. Note that not every student has an advisor. with advise as (select s_id as id, i_id from advisor) select student.name, instructor.name from student left outer join (advise inner join instructor on advise.i_id = instructor.id) 4. The university needs to determine whether a student is eligible for graduation. Here are the following requirements for graduation: the student has taken at least 130 credits the student has taken courses in at least five different departments the student has taken at least seven courses in a single department. restricted to the department associated with their student ID. This requirement is not Find all students who are eligible to graduate. 3

with credits as (select id from student where total_cred > 130) with depth as (select id, count(distinct course_id) as dept_depth from (course inner join takes on course.course_id = takes.course_id) inner join student on student.id = takes.id group by id, depart_name) with breadth as (select id, count(distinct dept_name) as dept_breadth from course inner join takes on course.course_id = takes.course_id inner join student on student.id = takes.id group by id) select id from credits inner join depth on credits.id = depth.id inner join breadth on breadth.id = depth.id where dept_depth > 6 and dept_breadth > 4 Problem 6 (To Be Graded) For each set of relations and functional dependencies, answer the following questions: 1. What are all the nontrivial functional dependencies that follow for the given functional dependencies? You should restrict yourself to FD s with single attributes on the right side. 2. What are the candidate keys of the relation? List all possible candidate keys. 3. Give a canonical cover for the given relation and functional dependencies. R(A, B, C, D) C D D A C A, AB D, AC D, BC A, BC D, BD A, BD C, CD A, ABC D, ABD C, BCD A AB, BC, and BD The given set are a canonical cover S(A, B, C, D) A B B C B D A C, A D, AC D, AD C AC B, AD B, BC D, BD C ABC D, ABD C, ACD B A A B, B CD T (A, B, C, D) 4

BC D CD A AD B ADC B AB D, AD C, BC A, CD B ABC D, ABD C, BCD A, AB, AD, BC, CD, BC D, CD A, AD B 5