2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CHAPTER

2
Introdu tion to the Relational
Model

Pra ti e Exer ises


2.1 Consider the employee database of Figure 2.17. What are the appropriate pri-
mary keys?
Answer:
The appropriate primary keys are shown below:

employee(person name, street, ity)


works(person name, ompany name, salary)
ompany ( ompany name, ity)

2.2 Consider the foreign-key onstraint from the dept name attribute of instru tor to
the department relation. Give examples of inserts and deletes to these relations
that an ause a violation of the foreign-key onstraint.
Answer:

• Inserting a tuple:
(10111, Ostrom, E onomi s, 110000)

employee (ID, person name, street, ity )


works (ID, ompany name, salary)

ompany ( ompany name, ity)

Figure 2.17 Employee database.

5
6 Chapter 2 Introdu tion to the Relational Model

into the instru tor table, where the department table does not have the de-
partment E onomi s, would violate the foreign-key onstraint.
• Deleting the tuple:
(Biology, Watson, 90000)
from the department table, where at least one student or instru tor tuple
has dept name as Biology, would violate the foreign-key onstraint.
2.3 Consider the time slot relation. Given that a parti ular time slot an meet more
than on e in a week, explain why day and start time are part of the primary key
of this relation, while end time is not.
Answer:
The attributes day and start time are part of the primary key sin e a parti ular
lass will most likely meet on several di erent days and may even meet more
than on e in a day. However, end time is not part of the primary key sin e a
parti ular lass that starts at a parti ular time on a parti ular day annot end at
more than one time.
2.4 In the instan e of instru tor shown in Figure 2.1, no two instru tors have the
same name. From this, an we on lude that name an be used as a superkey
(or primary key) of instru tor?
Answer:

No. For this possible instan e of the instru tor table the names are unique, but
in general this may not always be the ase (unless the university has a rule that
two instru tors annot have the same name, whi h is a rather unlikey s enario).
2.5 What is the result of rst performing the Cartesian produ t of student and advi-
sor , and then performing a sele tion operation on the result with the predi ate

s id = ID? (Using the symboli notation of relational algebra, this query an be


written as s id=ID (student  advisor ).)
Answer:

The result attributes in lude all attribute values of student followed by all at-
tributes of advisor. The tuples in the result are as follows: For ea h student who
has an advisor, the result has a row ontaining that student's attributes, followed
by an s id attribute identi al to the student's ID attribute, followed by the i id
attribute ontaining the ID of the students advisor.
Students who do not have an advisor will not appear in the result. A student
who has more than one advisor will appear a orresponding number of times
in the result.
2.6 Consider the employee database of Figure 2.17. Give an expression in the rela-
tional algebra to express ea h of the following queries:
a. Find the name of ea h employee who lives in ity “Miami”.
Pra ti e Exer ises 7

(
bran h bran h name , bran h ity, assets )
ustomer (ID, ustomer name , ustomer street, ustomer ity )
loan(loan number, bran h name, amount)
borrower (ID, loan number)
a ount (a ount number , bran h name, balan e)

depositor (ID, a ount number )

Figure 2.18 Bank database.

b. Find the name of ea h employee whose salary is greater than $100000.


. Find the name of ea h employee who lives in “Miami” and whose salary
is greater than $100000.

Answer:

a. person name ( ity = “Miami”


(employee))
b. person name (salary > 100000
(employee Æ works))
. person name ( ity = “Miami” á salary >100000
(employee Æ works))
2.7 Consider the bank database of Figure 2.18. Give an expression in the relational
algebra for ea h of the following queries:
a. Find the name of ea h bran h lo ated in “Chi ago”.
b. Find the ID of ea h borrower who has a loan in bran h “Downtown”.

Answer:

a. bran h name (bran h ity = “Chi ago”


(bran h))
b. ID (bran h name = “Downtown”
(borrower Æborrower loan number=loan loan number
: :

loan )).
2.8 Consider the employee database of Figure 2.17. Give an expression in the rela-
tional algebra to express ea h of the following queries:
a. Find the ID and name of ea h employee who does not work for “BigBank”.
b. Find the ID and name of ea h employee who earns at least as mu h as
every employee in the database.

Answer:

a. To nd employees who do not work for BigBank, we rst nd all those
who do work for BigBank. Those are exa tly the employees not part of the
8 Chapter 2 Introdu tion to the Relational Model

desired result. We then use set di eren e to nd the set of all employees
minus those employees that should not be in the result.

ID person name ( employee )*


ID person name
,

(employee Æemployee ID=works ID ( ompany name=``


,

: : BigBank
¨¨ (works)))

b. We use the same approa h as in part a by rst nding those employess


who do not earn the highest salary, or, said di erently, for whom some
other employee earns more. Sin e this involves omparing two employee
salary values, we need to referen e the employee relation twi e and there-
fore use renaming.

ID person name ( )* employee

A ID A person name (A ( ) ÆA salary B (


,

: , :
employee
: < B:salary employee ))

2.9 The division operator of relational algebra, “Ÿ”, is dened as follows. Let r(R)
and s(S) be relations, and let S Ó R; that is, every attribute of s hema S is
also in s hema R. Given a tuple t, let t[S℄ denote the proje tion of tuple t on
the attributes in S. Then r Ÿ s is a relation on s hema R * S (that is, on the
s hema ontaining all attributes of s hema R that are not in s hema S). A tuple
t is in r Ÿ s if and only if both of two onditions hold:
• tis in R*S (r)
• For every tuple ts in s, there is a tuple tr in r satisfying both of the following:
a. tr [S℄ = t
s [S ℄
b. tr [R * S ℄ = t

Given the above denition:

a. Write a relational algebra expression using the division operator to nd


the IDs of all students who have taken all Comp. S i. ourses. (Hint:
proje t takes to just ID and ourse id , and generate the set of all Comp.
S i. ourse id s using a sele t expression, before doing the division.)
b. Show how to write the above query in relational algebra, without using
division. (By doing so, you would have shown how to dene the division
operation using the other relational algebra operations.)

Answer:

a. ID (ID ourse id (


,
takes ) Ÿ  ourse id (dept name= 'Comp. S i'
( ourse ))
b. The required expression is as follows:
Pra ti e Exer ises 9

r } ID ourse id ( )
,
takes

s }  ourse id (dept name= 'Comp. S i'


( ourse ))

ID ( takes ) * ID ((ID ( takes )  s ) * r )

In general, let r(R) and s(S) be given, with S Ó R. Then we an express


the division operation using basi relational algebra operations as follows:

r Ÿ = R*S ( ) * R*S ((R*S ( ) 


s r r s ) * R*S S ( ))
,
r

To see that this expression is true, we observe that R*S (r) gives us all
tuples t that satisfy the rst ondition of the denition of division. The
expression on the right side of the set di eren e operator

R*S ((R*S ( )  r s ) * R*S S ( )) ,


r

serves to eliminate those tuples that fail to satisfy the se ond ondition of
the denition of division. Let us see how it does so. Consider R*S (r)  s.
This relation is on s hema R, and pairs every tuple in R*S (r) with every
tuple in s. The expression R*S S (r) merely reorders the attributes of r.
Thus, (R*S (r)  s) * R*S S (r) gives us those pairs of tuples from
,

R*S (r) and s that do not appear in r. If a tuple tj is in


,

R*S ((R*S ( )  r s ) * R*S S ( )) ,


r

then there is some tuple ts in s that does not ombine with tuple tj to form
a tuple in r. Thus, tj holds a value for attributes R * S that does not appear
in r Ÿ s. It is these values that we eliminate from R*S (r).

You might also like