Review for Midterm. Problem 2.7 HasPriPhy Patients address name ssn age Doctors exp_year name ssn specialty Pharmacies name address phone PharmCo name.

Slides:



Advertisements
Similar presentations
ER Modeling Case Studies
Advertisements

Exercise 1 Consider the ER diagram below. Assume that an employee may work in up to two departments or may not be assigned to any department. Assume that.
COP4540 Database Management System Midterm Review
Database Design The process of finding user requirement
CH4 EXERCISES 4.2. Given two relations R1 and R2, where R1 contains N1 tuples, R2 contains N2 tuples, and N2 > N1 > 0, give the min and max possible sizes.
Relational Algebra and SQL Exercises
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
COP-5725 Practice Exercises
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
(Uses slides from Fernando Farfan and Eduardo J. Ruiz
Entity-Relationship Models
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
ENTITY RELATIONSHIP MODELLING
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.
E/R – ODL – UML CS145 Monday November 26 Sanaz Motahari-Asl.
D ATABASE S YSTEMS I A DMIN S TUFF. 2 Mid-term exam Tuesday, Oct 2:30pm Room 3005 (usual room) Closed book No cheating, blah blah No class on Oct.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
COP5725 – Principles of Database Management Systems
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Entity/Relationship Modelling
Database Systems Lecture 5 Natasha Alechina
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Instructor :Huda Al-Omair
Introduction to Database Systems
Entity-Relationship Model Ch. 3
Database Management COP4540, SCS, FIU SQL (Continued) Querying Multiple Tables.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
Lecture 15: Relational Algebra
Assignements. CSC343: Intro. to Databases2 Exercise 1 Superkeys: Candidate keys: Primary key:
3 & 4 1 Chapters 3 and 4 Drawing ERDs October 16, 2006 Week 3.
Principles of Database Systems Yunsheng Liu. YSLiu-DBS-Exercise 2 Exercise 1 1.Please give at least four main differences between a DBMS and a file system.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Advanced Relational Algebra & SQL (Part1 )
WXGE 6101 DATABASE CONCEPTS & IMPLEMENTATIONS. Lesson Overview The Relational Model Terminology of relational model. Properties of database relations.
COMP3030 Database Management System Final Review
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
COP-5725 Practice Exercises
Introduction to Database Systems
Howard Paul. Sequential Access Index Files and Data File Random Access.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Assignements.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
SQL – Part 2.
Relational Schemas Classroom (building, room-number, capacity) Department (dept-name, building, budget) Course (course-id, title, dept-name, credits) Instructor.
Database Systems 10/13/2010 Lecture #4.
Question Which of the following plays an important role in representing information about the real world in a database? Explain briefly 1)DDL.
LECTURE 3: Relational Algebra
Foreign key (FK) is defined as follows:
CS4222 Principles of Database System
ER Modeling Case Studies
1. Explain the following concepts: (a) superkey (b) key
1.(5) Describe the working process with a database system.
실습 4주차.
Presentation transcript:

Review for Midterm

Problem 2.7 HasPriPhy Patients address name ssn age Doctors exp_year name ssn specialty Pharmacies name address phone PharmCo name phone Makes stDate endDate txt super isContracted dateqty Sell prescribes price Drugs tradeName formula

Relational Database Schema Doctors(ssn:integer,name:string, specialty:string,year_exp:integer) PK: ssn Patients(ssn:integer,name:string,age:integer,address:string,ssnDoctor:integer) PK: ssnFK: ssnDoctor references Doctors Pharmacies(name:string, phone:integer, address:string) PK: name PharmCo(name:string, phone:integer) PK: name Drugs(tradeName: string,formula:string,pharmCoName: string) PK: pharmCoName, tradeNameFK: pharmCoName references PharmCo Prescribes( ssnPatient:integer, ssnDoctor:integer, pharmCoName: string, tradeName:sring, date:date, qty:integer ) PK: FK: ssnPatient references Patients ssnPatient,ssnDoctor,pharCoName,tradeName FK: ssnDoctor references Doctors FK: (pharCoName,tradeName) referencesDrugs Sells(pharmCoName: string, tradeName: string, pharmacyName: string, price:float) PK: pharmCoName,tradeName,pharmacyName FK: (pharmCoName,tradeName) references Drugs FK: pharmacyName references Pharmacies Contracts(pharmCoName: string,pharmacyName: string, startDate:date, endDate:date, text:string, supervisor:string) PK: pharCoName,pharmacyName FK: pharmCoName references PharmCo FK: pharmacyName references Pharmacies

Each faculty is assigned to exactly one office room. An office room is assigned to at least one faculty. Is the following E-R diagram correct? If your answer is NO, indicate what is wrong in this E-R diagram. FacultyOffice AssignedTo Answer: Correct!

A faculty teaches at most three classes, but a class is taught by exactly one faculty. Is the following E-R diagram correct? If your answer is NO, indicate what is wrong in this E-R diagram. Faculty Classes teaches  Total participation of Faculty w.r.t teaches.  Key Constraint on Faculty w.r.t teaches.  Partial participation of Classes w.r.t teaches. Answer: NO

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints. Faculty Office scheduled Time WeekDay

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints. Faculty Office scheduled Time WeekDay

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints. Faculty Office scheduled Time WeekDay

Given two relations R1 and R2, where R1 contains N1 tuples and R2 contains N2 tuples, with N2>N1>0, give the minimum and maximum possible sizes (in tuples) for the resulting relation produced by each of the following relational algebra expressions. In each case, state any assumptions about the schemas for R1 and R2 needed to make the expression meaningful. Problem 4.2

Expression AssumptionMinMax

 Suppose R1 and R2 are multisets of tuples (rows);  N1 = number of copies of a tuple t in R1;  N2 = number of copies of a tuple t in R2. What’s the number of copies of t in R1 UNION ALL R2 ? Answer: N1+N2

 Suppose R1 and R2 are multisets of tuples (rows);  N1 = number of copies of a tuple t in R1;  N2 = number of copies of a tuple t in R2. What’s the number of copies of t in R1 INTERSECT ALL R2 ? Answer: min{N1,N2}

 Suppose R1 and R2 are multisets of tuples (rows);  N1 = number of copies of a tuple t in R1;  N2 = number of copies of a tuple t in R2. What’s the number of copies of t in R1 EXCEPT ALL R2 ? Answer:

Consider the following schema: Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) State what the following queries compute. Problem 4.4 The Catalog relation lists the prices charged for Parts by Suppliers.

Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) Answer: Find the Supplier names of the suppliers who supply a red part that costs less then 100 dollars.

Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) Answer: This Relational Algebra statement does not return anything because of the sequence of projection operators. Once the sid is projected, it is the only field in the set. Therefore, projecting on sname will not return anything.

Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) Answer: Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) Answer: Find the Supplier ids of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Suppliers(sid:integer, sname:string, address:string) Parts(pid:integer, pname:string, color:string) Catalog(sid:integer, pid:integer, cost:real) Answer: Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Department (D-code, D-Name, Chair-SSn)Faculty (Ssn, F-Name, D-Code, Rank) Course (D-code, C-no, Title, Units)Student (Ssn, S-Name, Major, Status) Prereq (D-code, C-no, P-code, P-no)Enrollment (Class-no, Student-Ssn) Class (Class-no, D-code, C-no, Instructor-SSn)Transcript (Student-Ssn, D-Code, C-no, Grade) select F.SSn from Class Co, Faculty F where F.Ssn = Co.Instructor- SSn and not exists (select * from Class C, Faculty F1 where Co.D-code = C.D-code and Co.C-no = C.C-no and C.Instructor-SSn = F1.Ssn and F1.D-code != F.D-Code); (1) Find the ssn of faculty who are currently teaching courses taught by faculty of several departments. (2) Find the ssn of faculty who are not currently teaching any courses of his/her department. (3) Find the ssn of faculty who are currently teaching courses taught only by faculties of his/her department. (4) Find the ssn of faculty who are currently teaching some course of his/her department. (5) Find the ssn of faculty who are currently teaching exactly one course.

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer )  Employees relation describes pilots and other kinds of employees as well;  Every pilot is certified for some aircraft (otherwise, he/she would not qualify as a pilot);  Only pilots are certified to fly. Write the following queries in relational algebra and SQL.

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Find the aids of all aircrafts that can be used on non-stop flights from Bonn to Madrid. SELECT A.aid FROM Aircraft A, Flight F WHERE F.from=‘Bonn’ AND F.to=‘Madrid’, AND A.cruisingrange >=F.distance

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Identify the flights (flno) that can be piloted by each pilot whose salary is more than $100,000. SELECT F.flno FROM Aircraft A, Certified C, Employees E, Flight F WHERE E.eid=C.eid AND C.aid=A.aid AND F.distance 100,000

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any Boeing aircraft.

SELECT E.ename FROM Employees E WHERE E.eid IN ((SELECT C1.eid FROM Certified C1, Aircraft A1 WHERE A1.aid=C1.aid AND A1.cruisingrange>3000) EXCEPT (SELECT C2.eid FROM Certified C2, Aircraft A2 WHERE A2.aid=C2.aid AND A2.aname=‘Boeing’)) Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any Boeing aircraft. Another solution using EXCEPT:

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Find the eids of employees who make the highest salary (RA only). The approach to take is: first find all employees who do not have the highest salary. Subtract these from the original list of employees and what is left is the highest paid employees.

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Find the eids of employees who make the second highest salary (RA only). The approach is similar to the previous one. First find all employees who do not have the highest salary. Remove these from the original list of employees and what is left is the highest paid employees. Remove the highest paid employees from the original list. What is left is the second highest paid employees together with the rest of the employees. Then find the highest paid employees of this new list. This is the list of the second highest paid employees.

Flight(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time ) Aircraft(aid: integer, aname: string, cruisingrange: integer ) Certified(eid: integer, aid: integer ) Employees(eid: integer, ename: string, salary: integer ) Find the eids of employees who make the second highest salary (RA only).

 Sample Midterm (with solutions);  Solutions of HW1, HW2, HW3;  You should:  Review the lectures’ slides and hws;  Do the readings in the textbook;  Do exercises at the end of chapters. Material for you to get ready for Midterm…