June 2, 2015R&G: Database Management Systems1 Introduction to Database Systems Key to As#1 Ex. 3 Instructor: Jiawei Han based on the notes by Raghu Ramakrishnan,

Slides:



Advertisements
Similar presentations
Exercise 2 Relational Calculus
Advertisements

Review Session Monday, Oct 8 Shipra Agrawal. Announcements New Gradiance assignment deadline Wednesday, Oct 10 Please read FAQs for assignments.
Chapter IV Relational Data Model Pemrograman Sistem Basis Data.
Relational Algebra and SQL Exercises
SQL Exercises – Part II April 11, 2017.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
Review Indra Budi Fakultas Ilmu Komputer UI 2 Database Introduction Database vs File Processing Main purpose of database Database Actors.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
1 Exercise 1  Given the following tables: employee (person_name, street, city)‏ works (person_name, company_name, salary)‏ company (company_name, city)‏
1 SQL - Select Join / Outer Join Sub queries Join Join Outer join Left outer join Right outer join.
DB HW2 B 陳彥名 1.A π person-name (σ company-name= “ First Bank Corporation ” (works) ) 1.B π person-name,city (σ company-name= “ First Bank Corporation.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Extended SQL & The Relational Calculus.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
1 Lecture 22: Query Execution Wednesday, March 2, 2005.
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
DB opgaven 2 Opgaven 2 Geert-Jan Houben.
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.
Domains, Relations & Base RelVars (Ch
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
1 Information Systems Chapter 6 Database Queries.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
IM433-Industrial Data Systems Management Lecture 5: SQL.
CS609 Introduction. Databases Current state? Future?
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4– Structured Query.
2.3 Introduction to Functions
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
SE305 Database System Technology 23/10/2014 Quiz-2.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational Mapping with Constraints &
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
1Database Management Systems Exercise II  Consider the following relational schemas:  employee (person_name, street, city)  company (company_name, city)
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Slide 8- 1 Copyright © 2010, 2007, 2004 Pearson Education, Inc. All Rights Reserved. MAT 155 Chapter 8 Hypothesis Testing The following is a brief review.
Copyright © Cengage Learning. All rights reserved. 1 Functions and Their Graphs.
Relational Algebra Sample Questions.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
Section P.2 Solving Inequalities
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
SE305 Database System Technology 25/09/2014 Quiz-1.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Differences between Microeconomics and Macroeconomics
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Relational tuple calculus.
Relational-Style XML Query Taro L. Saito, Shinichi Morishita University of Tokyo June 10 th, SIGMOD 2008 Vancouver, Canada Presented by Sangkeun-Lee Reference.
Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.
1 Relational Algebra & SQL Query Formulation Exercise.
Introduction to Database Design
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Algebra and SQL
Relational Algebra at a Glance
Relational Algebra - Select & Project
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Introduction to Functions
January 19th – Subqueries 2 and relational algebra
نجاح وفشل المنشآت الصغيرة
Dealing with Uniqueness Constraint in Query Optimization
Relational Algebra Sample Questions.
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lectures 5: Introduction to SQL 4
Relational Algebra Friday, 11/14/2003.
Lecture 23: Query Execution
Lecture 03: SQL Friday, October 3, 2003.
Syllabus Introduction Website Management Systems
Presentation transcript:

June 2, 2015R&G: Database Management Systems1 Introduction to Database Systems Key to As#1 Ex. 3 Instructor: Jiawei Han based on the notes by Raghu Ramakrishnan, UW- Madison Please observe the copyright of ©R&G. For classroom use only. Do not distribute!

June 2, 2015R&G: Database Management Systems 2 Question Consider relation database person(pname, street, city) works_for(pname, cname, salary) company(cname, city) manages (pname, manager) Write the following queries in relational algebra tuple relational calculus domain relational calculus SQL

June 2, 2015R&G: Database Management Systems 3 Find street and city of all employees who work for the Future Shop, live in Burnaby, and earn more than $40,000. Relational algebra  street, city ((  city =‘Burnaby’ Person)  (  cname =‘FutureShop’  salary > Works_for)) or,  street, city ((  city =‘Burnaby’ Person)   pname (  cname =‘FutureShop’  salary > Works_for)) Tuple relational calculus {T |  P  Person  W  Works_for (W.salary>  W.cname = ‘FutureShop’  P.pname = W.pname  P.city = ‘Burnaby’  T.street = P.street  T.city = P.city)} Domain relational calculus {  S, C  |  N  N, S, C   Person  I,Y  N, I, Y   Works_for  I = ‘FutureShop’  Y >  C = ‘Burnaby’} SQL select P.street, P.city from Person P, Works_for W where P.pname = W.pname and W.cname = ‘FutureShop’ and W.salary > 40,000 and P.city = ‘Burnaby’

June 2, 2015R&G: Database Management Systems 4 Find the names and the companies they work for, for all the people who have a higher salary than their manager Relational algebra  (T1, Works_for),  (T2, Works_for),  (M, Manages)  (TM,  M.pname, T2.salary (  M.manager = T2.pname ( M  T2)))  t1.pname, T1.cname (  TM.pname = T1.pname  T1.salary > TM.salary ( TM  T1)) Tuple relational calculus {T |  W  Works_for  M  Manages  W2  Works_for (W.salary> W2.salary  W.pname = M.pname  M.manager = W2.pname  T.pname = P.pname  T.cname = W.cname)} Domain relational calculus {  N, C  |  S  N, C, S   Works_for  M  N, M   Manages  C2, S2  M, C2, S2   Works_for  S > S2} SQL select W.pname, W.cname from Works_for W, manages M, Works_for W2 where W.pname = M.pname and M.manager = W2.pname and W.salary > W2.salary

June 2, 2015R&G: Database Management Systems 5 Assume a company may located in several cities. Find all the companies located in every city in which the FutureShop is located. Relational algebra  (T1, Works_for),  (T2, Works_for),  (M, Manages)  (TM,  M.pname, T2.salary (  M.manager = T2.pname ( Manages  T2)))  t1.pname, T1.cname (  TM.pname = T1.pname  T1.salary > TM.salary ( Manages  T1)) Tuple relational calculus {T |  W  Works_for  M  Manages  W2  Works_for (W.salary> W2.salary  W.pname = M.pname  M.manager = W2.pname  T.pname = P.pname  T.cname = W.cname)} Domain relational calculus {  N  |  N  N,C   Company  M  N, M   Manages  C2, S2  M, C2, S2   Works_for  S > S2}

June 2, 2015R&G: Database Management Systems 6 Find all the companies located in every city in which the FutureShop is located. (SQL) select distinct a.cname from company a where a.cname <> 'FS' and not exists (select distinct c.cname from company c where c.cname <> 'FS' and c.city = a.city and exists (select d.city from company d where d.cname = 'FS' and not exists (select e.city from company e where e.cname <> 'FS' and e.cname = c.cname and e.city = d.city))) Note: This is a difficult query. The key is the double negation (using not exists). This one really works in SQLServer.

June 2, 2015R&G: Database Management Systems 7 An alternative solution: Find all the companies located in every city in which the FutureShop is located. (SQL) select c.cname from company c where not exists (select c2.city from company c2 except (select C3.city from company C3 where c3.name='future shop' and c3.city=c.city) Note: This is a solution in most students’ homework which follows the textbook. However, SQLServer does not have except and we cannot judge whether it really works (although we still give the points)

June 2, 2015R&G: Database Management Systems 8 Find the number of managers who manage more than 5 employees living in Richmond select count(manager) from person P, manages M where P.pname = M.pname and city = “Richmond” group by manager having count (pname) > 5

June 2, 2015R&G: Database Management Systems 9 Thank you !!!