tutorial for sql

2
CPE303/CSC206 1 Tutorial 4 SQL (Contd.) and Functional Dependencies Classroom Exercise 1. Consider the following schema of a product database: Parts(pid: integer , pname: string, color: string) Suppliers(sid: integer , sname: string, address: string) Catalog(sid: integer, pid:integer , price: real) The Catalog records that some Supplier sid supplies Part pid at a given price. Formulate each of the following integrity constraints as an SQL assertion. (i) No Supplier may supply red and green Parts. (ii) For all Parts, no other Supplier has a lower price than the Supplier with sid” = 1. 2. Consider the following ER diagram, which describes graduate students (Grad) and courses (Course) they serve as Teaching Assistants (TA). a) For each of the following statements, write a functional dependency (FD) that best captures the statement. The studentID of each graduate student uniquely identifies the student. No two offices have the same phone number (officePhone). No two courses have the same courseID. If two courses have the same course name, their course descriptions are the same. b) From the ER diagram and the set of FDs you listed above, can you derive new FDs? If no, explain why not. If yes, derive two non-trivial FDs.

Upload: calvin-cobain

Post on 20-Feb-2015

99 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Tutorial for SQL

CPE303/CSC206

1

Tutorial 4 SQL (Contd.) and Functional Dependencies

Classroom Exercise

1. Consider the following schema of a product database:

Parts(pid: integer, pname: string, color: string) Suppliers(sid: integer, sname: string, address: string) Catalog(sid: integer, pid:integer, price: real)

The Catalog records that some Supplier sid supplies Part pid at a given price. Formulate each of the following integrity constraints as an SQL assertion.

(i) No Supplier may supply red and green Parts. (ii) For all Parts, no other Supplier has a lower price than the Supplier with

“sid” = 1. 2. Consider the following ER diagram, which describes graduate students

(Grad) and courses (Course) they serve as Teaching Assistants (TA).

a) For each of the following statements, write a functional dependency (FD) that best captures the statement.

• The studentID of each graduate student uniquely identifies the

student. • No two offices have the same phone number (officePhone). • No two courses have the same courseID. • If two courses have the same course name, their course

descriptions are the same.

b) From the ER diagram and the set of FDs you listed above, can you derive new FDs? If no, explain why not. If yes, derive two non-trivial FDs.

Page 2: Tutorial for SQL

CPE303/CSC206

2

3. Consider the following two sets of functional dependencies:

Φ = {A →C, AC → D, E → AD, E → H } and, Γ = {A →CD, E → AH }. Check whether they are equivalent. 4. Prove the following properties using Armstrong's axioms or reject it by

counterexample relations. a) A → B ⇒ AC → B b) A → C and AB → C ⇒ B → C

Critical Thinking Exercise 1. Consider a relation R(A,B,C,D,E,F) and a set of functional

dependencies FD={A->CD, C->BE, B->F, F->A} that hold on R. Using Armstrong axioms show that A ->BF hold on R.

2. Consider a relation R(A,B,C,D,E,F). The only thing you know about R is that ABCD and EF are keys. What is the maximum number of keys that R can possibly have (including ABCD and EF)? Explain your calculation.

3. Consider the relation ADDRESS having attributes Street, City, State and Zip. Assume that for any given zipcode, there is just one city and state. Also, for any given street, city, and state, there is just one zipcode.

(a) Infer all possible functional dependencies (FDs) for this relation.

(b) Which are possible minimal keys?