COP-5725 Practice Exercises

Slides:



Advertisements
Similar presentations
Classwork notes. 2 Find the names of all employees whose salary greater than all managers salary.
Advertisements

COP4540 Database Management System Midterm Review
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise.
1 Database Systems 10.8, 2008 Lecture #4. 2 Course Administration Assignment #1 is due next Wed (outside 336/338). Course slides will now have black background.
CSC343: Intro. to Databases1 Tutorial for CSC343 Introduction to Databases Fall 2006 Week 2.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Chapter (7): Advanced SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Data Model 11 After this lecture, you should be able to:  Use E-R Model effectively to design a database from requirements.  Know to convert from E-R.
1 Pertemuan > Relational Model SQL DDL, SQL DML, Aljabar dan Kalkulus Matakuliah: >/ > Tahun: > Versi: >
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
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
SQL and Relational Algebra Zaki Malik September 02, 2008.
SQL (2).
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Extended SQL & The Relational Calculus.
Practice (Suppliers, Parts, Catalog)
Employee database: Conceptual Schema in ERD Chapter 3, page 62.
COP5725 – Principles of Database Management Systems
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Work-in dept DID emp salarynames manage names EID Can be Pkey Solution for the Quiz –Problem 1.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
The Entity-Relationship Model. 421B: Database Systems - ER Model 2 Overview of Database Design q Conceptual Design -- A first model of the real world.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
Review for Midterm. Problem 2.7 HasPriPhy Patients address name ssn age Doctors exp_year name ssn specialty Pharmacies name address phone PharmCo name.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Assignements. CSC343: Intro. to Databases2 Exercise 1 Superkeys: Candidate keys: Primary key:
1 SQL: Constraints and Triggers Chapter 5,
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 From E-R Model to Relational Model. 2 Entity type sid#name address house#street pcode Student.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5,
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Software School of Hunan University Database Systems Design Part III : Mapping ER Diagram to Relational Schema.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
COMP3030 Database Management System Final Review
COP-5725 Practice Exercises
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
CS34311 Translating ER Schema to Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Advanced SQL: Aggregation and Null Values.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
DataBase - Check 01 DataBase 2 nd year Computer Science & Engineer 1.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Assignements.
SQL – Part 2.
Basic SQL Queries Go over example queries, like 10 > ALL.
SQL-lab Download sql script file sailors.txt
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
실습 4주차.
Practice Project Practice to know SQL
Presentation transcript:

COP-5725 Practice Exercises Chapter 5: Queries, Triggers, Constraints (up to, but not including aggregation) M. Amanda Crick

Exercise 5.2 Problem 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) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL:

Exercise 5.2 Problem Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) Find the pnames of parts for which there is some supplier.

Exercise 5.2 Solution for (1) SELECT DISTINCT P.pname FROM Parts P, Catalog C WHERE P.pid = C.pid

Exercise 5.2 Problem Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) Find the pnames of parts supplied by Acme Widget Suppliers and no one else.

Exercise 5.2 Solution for (4) SELECT P.pname FROM Parts P, Catalog C, Suppliers S WHERE P.pid = C.pid AND C.sid = S.sid AND S.sname = ‘Acme Widget Suppliers’ AND NOT EXISTS ( SELECT * FROM Catalog C1, Suppliers S1 WHERE P.pid = C1.pid AND C1.sid = S1.sid AND S1.sname <> ‘Acme Widget Suppliers’ )

Exercise 5.2 Problem Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) For each part, find the sname of the supplier who charges the most for that part.

Exercise 5.2 Solution for (6) SELECT P.pid, S.sname FROM Parts P, Suppliers S, Catalog C WHERE C.pid = P.pid AND C.sid = S.sid AND C.cost >= ALL (SELECT C1.cost) FROM Catalog C1 WHERE C1.pid = P.pid)

Exercise 5.2 Problem Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) Find the sids of suppliers who supply a red part and a green part.

Exercise 5.2 Solution for (8) SELECT DISTINCT C.sid FROM Catalog C, Parts P WHERE C.pid = P.pid AND P.color = ‘Red’ INTERSECT SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1 WHERE C1.pid = P1.pid AND P1.color = ‘Green’

Exercise 5.2 Problem Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) Find the sids of suppliers who supply a red part or a green part.

Exercise 5.2 Solution for (9) SELECT DISTINCT C.sid FROM Catalog C, Parts P WHERE C.pid = P.pid AND P.color = ‘Red’ UNION SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1 WHERE C1.pid = P1.pid AND P1.color = ‘Green’

Exercise 5.4 Problem Consider the following relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Write the following queries in SQL:

Exercise 5.4 Problem Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Print the names and ages of each employee who works in both the Hardware department and the Software department.

Exercise 5.4 Solution for (1) SELECT E.ename, E.age FROM Emp E, Works W1, Works W2, Dept D1, Dept D2 WHERE E.eid = W1.eid AND W1.did = D1.did AND D1.dname = ‘Hardware’ AND E.eid = W2.eid AND W2.did = D2.did AND D2.dname = ‘Software’

Exercise 5.4 Problem Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in.

Exercise 5.4 Solution for (3) SELECT E.ename FROM Emp E WHERE E.salary > ALL (SELECT D.budget FROM Dept D, Works W WHERE E.eid = W.eid AND D.did = W.did)

Exercise 5.4 Problem Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Find the enames of managers who manage the departments with the largest budgets.

Exercise 5.4 Solution for (5) SELECT E.ename FROM Emp E WHERE E.eid IN (SELECT D.managerid FROM Dept D WHERE D.budget >= ALL (SELECT D2.budget FROM Dept D2))

Exercise 5.4 Problem Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Find the enames of managers who manage the departments with the largest budgets.

Exercise 5.4 Solution for (8) SELECT M.ename FROM Emp M WHERE M.sid IN (SELECT M2.sid FROM Emp M2, Dept D WHERE M2.sid = D.managerid AND D.budget < 5000000 EXCEPT SELECT M3.sid FROM Emp M3, Dept D2 WHERE M3.sid = D2.managerid AND D2.budget <= 5000000)

This is the end of the lecture! I hope you enjoyed it.