DBMS-6
DBMS-6
Relational Model
This chapter presents the relational model and three relational languages. The
rela
tional model (Section 3.1) is used extensively throughout the text as is the
relational
algebra(Section 3.2). The chapter also coversthe tuple
relationalcalculus(Section3.6)
and domain relational calculus (Section 3.7) (which is the basis of the QBE
language
described in Chapter 5). Classes that emphasize only SQL may omit the relational
calculus languages.
Our notation for the tuple relational calculus makes it easy to present the con
cept of a safe query. The concept of safety for the domain relational calculus,
though
identical to that for the tuple calculus, is much more cumbersome notationally and
requires careful presentation. This consideration may suggest placing somewhat
less
emphasis on the domain calculus for classes not planning to cover QBE.
Section 3.3 presents extended relational-algebra operations, such as outer-joins
and aggregates. The evolution of query languages such as SQL clearly indicates the
importance of such extended operations. Some of these operations, such as outer
joins can be expressed in the tuple/domain relational calculus, while extensions
are
required for other operations, such as aggregation. We have chosen not to present
such extensions to the relational calculus, and instead restrict our attention to
exten
sions of the algebra.
29
30
Chapter 3
Relational Model
address
driver-id
name
model
license
person
owns car
year
report-number
location
date
driver
Exercises
participated
damage-amount
Figure 3.38. E-R diagram.
accident
3.1 Design a relational database for a university registrar’soffice. The office
main
tains data about each class, including the instructor, the number of students
enrolled, and the time and place of the class meetings. For each student–class
pair, a grade is recorded.
Answer: Underlined attributes indicate the primary key.
student (student-id,name,program)
course (courseno, title, syllabus, credits)
course-offering (courseno,secno,year, semester, time, room)
instructor (instructor-id, name, dept, title)
enrols (student-id,courseno,secno, semester,year,grade)
teaches (courseno,secno, semester,year, instructor-id)
requires (maincourse, prerequisite)
3.2 Describethedifferencesinmeaningbetweenthetermsrelationandrelationschema.
Illustrate your answer by referring to your solution to Exercise 3.1.
Answer: A relation schema is a type definition, and a relation is an instance of
that schema. For example, student (ss#, name)isarelationschemaand
ss#
name
123-45-6789 Tom Jones
456-78-9123 Joe Brown
is a relation based on that schema.
3.3 Design a relational database corresponding to the E-R diagram of Figure 3.38.
Answer: The relational database schema is given below.
person (driver-id, name, address)
car (license, year, model)
accident (report-number, location, date)
owns (driver-id, license)
participated (report-number driver-id, license, damage-amount)
Exercises
31
employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)
manages (person-name, manager-name)
Figure 3.39. Relational database for Exercises 3.5, 3.8 and 3.10.
3.4 In Chapter 2, we saw how to represent many-to-many, many-to-one, one-to
many, and one-to-one relationship sets. Explain how primary keys help us to
represent such relationship sets in the relational model.
Answer: Suppose the primary key of relation schema R is {Ai1
, Ai2
,...,Ain
}
and the primary key of relation schema S is {Bi1
, Bi2
,...,Bim
}.Thenare
lationship between the 2 sets can be represented as a tuple (Ai1
, Ai2
,...,Ain
Bi1
,Bi2
,...,Bim
). In a one-to-one relationship, each value on {Ai1
,Ai2
,...,Ain
}
will appear in exactly one tuple and likewise for {Bi1
,Bi2
,...,Bim
}.Inamany
to-one relationship (e.g., many A-oneB), each value on {Ai1
,Ai2
,...,Ain
} will
appearonce, and each value on {Bi1
,Bi2
,...,Bin
} may appear many times. In a
many-to-manyrelationship, values on both {Ai1
,Ai2
,...,Ain
} and{ Bi1
,Bi2
, ...,
Bim
}will appearmanytimes. However, in all the above cases{Ai1
,Ai2
,...,Ain
,
Bi1
,Bi2
,...,Bim
} is a primary key, so no tuple on (Aj1
,...,Ajn
Bk1
,...,Bkm
) will
appear more than once.
3.5 Consider the relational database of Figure 3.39, where the primary keys are un
derlined. Give an expression in the relational algebra to express each of the fol
lowing queries:
a. Find the names of all employees who work for First Bank Corporation.
b. Find the names and cities of residence of all employees who work for First
Bank Corporation.
c. Find the names, street address, and cities of residence of all employees who
work for First Bank Corporation and earn more than $10,000per annum.
d. Find the names of all employees in this database who live in the same city
as the company for which they work.
e. Find the names of all employees who live in the same city and on the same
street as do their managers.
f. Find the names of all employees in this database who do not work for First
Bank Corporation.
g. Find the names of all employees who earn more than every employee of
Small Bank Corporation.
h. Assume the companies may be located in several cities. Find all companies
located in every city in which Small Bank Corporation is located.
Answer:
a. Πperson-name (σcompany-name = “First Bank Corporation” (works))
b. Πperson-name,city (employee 1
(σcompany-name = “First Bank Corporation” (works))