CSE132A Solutions HW 1



Similar documents
DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Proposed solutions Project Assignment #1 (SQL)

Chapter 5. SQL: Queries, Constraints, Triggers

Relational Algebra and SQL

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL: Queries, Programming, Triggers

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Introduction to Microsoft Jet SQL

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL THIRD EDITION

6 QUERY-BY-EXAMPLE (QBE)

3. Relational Model and Relational Algebra

Databases and BigData

Lecture 6. SQL, Logical DB Design

CMU - SCS / Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

LABSHEET 1: creating a table, primary keys and data types

Database Management Systems. Chapter 1

Overview of Database Management Systems

The Relational Model. Why Study the Relational Model?

CS GATE Paper

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Review: Participation Constraints

Information Systems SQL. Nikolaj Popov

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

CS 338 Join, Aggregate and Group SQL Queries

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Θεµελίωση Βάσεων εδοµένων

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them.

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Database Security. Chapter 21

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

How To Improve Performance In A Database

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

DataBase Management Systems Lecture Notes

The core theory of relational databases. Bibliography

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Data Modeling. Database Systems: The Complete Book Ch ,

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Querying Combined Cloud-Based and Relational Databases

Exercise 1: Relational Model

Databases 2011 The Relational Model and SQL

2. Basic Relational Data Model

Object Oriented Databases (OODBs) Relational and OO data models. Advantages and Disadvantages of OO as compared with relational

Database Sample Examination

TYPICAL QUESTIONS & ANSWERS

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Comp 3311 Database Management Systems. 2. Relational Model Exercises

Database Application Development. Overview. SQL in Application Code. Chapter 6

2. Conceptual Modeling using the Entity-Relationship Model

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

D B M G Data Base and Data Mining Group of Politecnico di Torino

Database Management Systems,

The Relational Data Model and Relational Database Constraints

There are five fields or columns, with names and types as shown above.

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

Advance DBMS. Structured Query Language (SQL)

Introduction to Databases

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

ECS 165A: Introduction to Database Systems

DATA CUBES E Jayant Haritsa Computer Science and Automation Indian Institute of Science. JAN 2014 Slide 1 DATA CUBES

Migrate Topaz databases from One Server to Another

10CS54: DATABASE MANAGEMENT SYSTEM

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

Relational Database: Additional Operations on Relations; SQL

Secure Cooperative Data Access in Multi-Cloud Environment

Managing E-Commerce Catalogs in a DBMS with Native XML Support

CSE 562 Database Systems

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 16 - Data Warehousing

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Online shopping cart. Tarik Guelzim Graduate school of computer science at Monmouth University. CS517 Database management systems Project 2

TAP into NAU. TAP is a single tool. How it works. Transfer Academic Plan nau.edu/tap


CS54100: Database Systems

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

SQL Database queries and their equivalence to predicate calculus

Using Temporary Tables to Improve Performance for SQL Data Services

Mini User's Guide for SQL*Plus T. J. Teorey

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

Decision Support. Chapter 23. Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke 1

Summary on Chapter 4 Basic SQL

Introduction to SQL: Data Retrieving

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

CSE 132A. Database Systems Principles

CA ERwin Data Modeler. ODBC Reporting Guide

CS143 Notes: Views & Authorization

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql

Transcription:

CSE132A Solutions HW 1 Problem 2. [60pts] 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 key fields are underlined, and the domain of each field is listed after the field name. The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in (a) relational algebra, (b) tuple relational calculus, (c) domain relational calculus and (d) SQL. 1. (3 pts): Find the names of suppliers who supply some red part. π sname (π sid ((π pid σ color= red Parts) Catalog) suppliers) {T :< sname > T1 Suppliers( X Parts(X[color] = red Y Catalog(Y [pid] = X[pid] Y [sid] = T1[sid])) T[sname] = T1[sname]} {Y : sname X Z Suppliers(X, Y, Z) P Q R(Parts(P, Q, R) R = red K Catalog(X, P, K))} S.sname, Parts P, Catalog C P.color= red AND C.pid=P.pid AND C.sid= 2. (3 pts): Find the sids of suppliers who supply some red part or are at 221 Packer Street. π sid ((π pid σ color= red Parts) Catalog) π sidσ address= 221 Packer Str Suppliers {T :< sid > T1 Catalog( X Parts(X[color] = red X[pid] = T1[pid]) T[sid] = T1.sid]) T2 Suppliers(T2[address] = 221 Packer Str T[sid] = T2[sid])} {(X : sid) Y Z Catalog(X, Y, Z) A B (Parts(Y, A, B) B = red ) P Q Suppliers(X, P, Q) Q = 221 Packer Str } 1

( UNION ( S.address = 221 Packer Str ) C.sid Parts P, Catalog C P.color = red AND P.pid = C.pid) 3. (3 pts): Find the sids of suppliers who supply some red part and some green part. π sid ((π pid σ color= red Parts) Catalog) π sid((π pid σ color= green Parts) Catalog) {T :< sid > T1 Catalog( X Parts(X[color] = red X[pid] = T1[pid]) T2 Catalog( Y Parts(Y [color] = green Y [pid] = T2[pid]) T1[sid] = T2[sid] T[sid] = T1[sid]) {S : sid P1 C1 N1 L1(Catalog(S, P1, C1) Parts(P1, N1, L1) L1 = red ) P2 C2 N2 L2(Catalog(S, P2, C2) Parts(P2, N2, L2) L2 = green )} C1.sid Catalog C1, Parts P1, Catalog C2, Parts P2 C1.pid=P1.pid AND P1.color= red AND C2.pid=P2.pid AND P2.color= green AND C1.sid=C2.sid 4. (6 pts): Find the sids of suppliers who supply every part. (π sid,pid (Suppliers Catalog)) (π pid Parts) {T :< sid > S Suppliers P Parts C Catalog (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} {(X : sid) SN A Suppliers(X, SN, A) P PN L (Parts(P, PN, L) C Catalog(X, P, C))} ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts) 5. (6 pts): Find the sids of suppliers who supply every red or green part. (π sid,pid (Suppliers Catalog)) (π pid σ color= red color= green Parts) {T :< sid > S Suppliers P Parts (P[color] = red P[color] = green ) C Catalog (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} 2

{(X : sid) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) (L = red L = green )) C Catalog(X, P, C))} ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = red OR color = green ) 6. (6 pts): Find the sids of suppliers who supply every red part or supply every green part. (π sid,pid (Suppliers Catalog)) (π pid σ color= red Parts) (π sid,pid (Suppliers Catalog)) (π pid σ color= green Parts) {T :< sid > S Supp P Parts(P[color] = red C Cat (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid]) S Supp P Parts(P[color] = green C Cat (C[sid] = S[sid] P[pid] = C[pid] T[sid] = S[sid])} {(X : sid) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) L = red ) C Catalog(X, P, C)) SN A Suppliers(X, SN, A) P PN L ((Parts(P, PN, L) L = green ) C Catalog(X, P, C))} ( UNION ( ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = red )) ( C.pid Catalog C C.sid = ) CONTAINS ( pid Parts color = green )) 7. (9 pts): Find pairs of sids such that the supplier with the first sid charges more for some part than the supplier with the second sid. π sid,sid σ cost>cost (Catalog δ sid,cost sid,cost Catalog) {T :< sid, sid > C Catalog C Catalog C[pid] = C [pid] C[cost] > C [cost] T[sid] = C[sid] T[sid ] = C [sid]} {(S, S ) P C C Catalog(S, P, C) Catalog(S, P, C ) C > C } 3

C.sid, C.sid AS sid Catalog C, Catalog C C.pid = C.pid AND C.cost > C.cost 8. (6 pts): Find the pids of parts supplied by at least two different suppliers. π pid σ sid sid (Catalog δ sid,cost sid,cost Catalog) {T :< pid > C Catalog C Catalog C[pid] = C [pid] C[sid] C [sid] T[pid] = C[pid]} {(P) S S C C Catalog(S, P, C) Catalog(S, P, C ) S S } DISTINCT C.pid Catalog C, Catalog C C.pid = C.pid AND C.sid <> C.sid 9. (9 pts): Find the pids of the most expensive parts supplied by suppliers named Yosemite Sham. temp π pid,cost σ sname= Y osemite Sham (Catalog Suppliers) π pid temp \ π pid σ cost<cost (temp δ pid,cost pid,cost temp) } {{ } not most expensive {T :< pid > T1 Catalog( X Suppliers (X.sname = Y osemite Sham X.sid = T1.sid) ( Z Catalog(Z.sid = T1.sid Z.cost > T1.cost))) T[pid] = T1[pid]} {(P : pid) S C Catalog(S, P, C) S N ASuppliers(S, N, A) N = Y osemite Sham ( P C Catalog(S, P, C ) C > C)} CREATE VIEW TEMP(pid,cost) AS C.pid, C.cost Catalog C, C.sid= AND S.sname = Yosemite Sham T.pid TEMP T T.pid NOT IN ( T1.pid TEMP T1, TEMP T2 T1.cost < T2.cost) 10. (9 pts): Find the pids of parts supplied by every supplier at less than $200. (If any supplier either does not supply the part or charges more than $200 for it, the part is not selected). 4

TooExpensive π pid σ cost>$200 Catalog SuppliedByAll π pid,sid Catalog π sid Suppliers SuppliedByAll \ T ooexpensive {T :< pid > C Catalog S Supplier C Catalog C [sid] = S[sid] C [pid] = C[pid] ( C Catalog C [pid] = C[pid] C [cost] > $200)} {(P : pid) S C Catalog(S, P, C) S N A Supplier(S, N, A) C Catalog(S, P, C ) ( S C Catalog(S, P, C ) C > $200)} CREATE VIEW TooExpensive AS pid Catalog cost > $200 CREATE VIEW SuppliedByAll AS C.pid Catalog C ( C1.sid Catalog C1 C1.pid = C.pid) CONTAINS ( sid Supplier) pid SuppliedByAll pid NOT IN TooExpensive 5