Chapter 4 The Relational Model
Chapter 4 The Relational Model
Review Questions
4.1 Discuss each of the following concepts in the context of the relational data model:
4.2 Describe the relationship between mathematical relations and relations in the relational data
model?
Any set of n-tuples from this Cartesian product is a relation on the n sets. Now let A1, A2, . . .,
An be attributes with domains D1, D2, . . . , Dn. Then the set {A1:D1, A2:D2, . . . , An:Dn}
is a relation schema. A relation R defined by a relation schema S is a set of mappings from the
attribute names to their corresponding domains. Thus, relation R is a set of n-tuples:
(A1:d1, A2:d2, . . . , An:dn) such that d1 D1, d2 D2, . . . , dn Dn
Each element in the n-tuple consists of an attribute and a value for that attribute.
4.3 Describe the term “normalized reaction”. Why are constraints so important in a relational
database?
Codd described a normalized relation as a relation that does not contain any repeating groups
or multivalued attributes. The attributes are functionally dependent on only the primary key.
Constraints enforce data integrity, consistency, and business rules.
has a name that is distinct from all other relation names in the relational schema;
the order of tuples has no significance, theoretically. (However, in practice, the order may
affect the efficiency of accessing tuples.)
4.5 Discuss the differences between the candidate keys and the primary key of a relation. Explain
what is meant by a foreign key. How do foreign keys of relations relate to candidate keys? Give
examples to illustrate your answer.
The primary key is the candidate key that is selected to identify tuples uniquely within a relation.
A foreign key is an attribute or set of attributes within one relation that matches the candidate key
of some (possibly the same) relation. Discussed in Section 4.2.5.
4.6 Define the two principal integrity rules for the relational model. Discuss why it is desirable to
enforce these rules.
Two rules are Entity Integrity (Section 4.3.2) and Referential Integrity (Section 4.3.3).
Views can be described as virtual tables that utilize one or more relational operations operating on
the base relations to produce another relation. Views data are dynamically assigned by the DBMS
through relational operations set by users. Views are useful in a number of ways including those
described in section 4.4.2. These include data security, easy data customization, data flexibility,
and simplification of complex operations on base relations.
Exercises
where Hotel contains hotel details and hotelNo is the primary key;
Room contains room details for each hotel and (roomNo, hotelNo) forms the primary key;
Booking contains details of the bookings and (hotelNo, guestNo, dateFrom) forms the primary
key;
and Guest contains guest details and guestNo is the primary key.
4.8 Identify the foreign keys in this schema. Explain how the entity and referential integrity rules
apply to these relations.
For each relation, the primary key must not contain any nulls.
Room is related to Hotel through the attribute hotelNo. Therefore, the hotelNo in Room should
either be null or contain the number of an existing hotel in the Hotel relation. In this case study, it
would probably be unacceptable to have a hotelNo in Room with a null value.
Booking is related to Hotel through the attribute hotelNo. Therefore, the hotelNo in Booking
should either be null or contain the number of an existing hotel in the Hotel relation. However,
because hotelNo is also part of the primary key, a null value for this attribute would be
unacceptable. Similarly for guestNo. Booking is also related to Room through the attribute
roomNo.
4.9 Produce some sample tables for these relations that observe the relational integrity rules.
Suggest some general constraints that would be appropriate for this schema.
Student should provide some sample tables, observing entity and referential integrity. In
particular, ensure the uniqueness for the composite primary keys of the Room and Booking tables.
Some general constraints may be:
There can be no two bookings for the same room in the same hotel on the same day.
For the Booking relation, dateFrom must be before dateTo.
Room price must be greater than 0 and less than £200.
4.10 Analyze the RDBMSs that you are currently using. Determine the support the system provides for
primary keys, alternate keys, foreign keys, relational integrity, and views.
This is a small student project, the result of which is dependent on the system analyzed.
4.11 Implement the above schema in one of the RDBMSs you currently use. Generate two user-
views that are accessible and updatable as well as two other user-views that cannot be
updated.
Students need to practice implementing the above schema. As described in section 4.4.3, it is
possible to update the views provided it fits into the specified criteria. Generated views need to
observe criteria outlined in section 4.4.3
Chapter 5 Relational Algebra and Relational Calculus
Review Questions
5.1 What is the difference between a procedural and non-procedural language? How would you
classify the relational algebra and relational calculus?
Procedural language: a language that allows user to tell the system what data is needed and
exactly how to retrieve the data.
Non-procedural language: a language that allows user to state what data is needed rather
than how it is to be retrieved.
In tuple relational calculus, we are interested in finding tuples for which a predicate is
true. The calculus is based on the use of tuple variables. A tuple variable is a variable that
“ranges over” a named relation: that is, a variable whose only permitted values are tuples
of the relation. (The word “range” here does not correspond to the mathematical use of
range, but corresponds to a mathematical domain.) For example, to specify the range of a
tuple variable S as the Staff relation, we write: Staff(S)
To express the query “Find the set of all tuples S such that F(S) is true,” we can write:
{S | F(S)}
F is called a formula (well-formed formula, or wff in mathematical logic). See section
5.2.1
domain relational calculus
Variables are used in domain relational calculus also, but in this case the variables take
their values from domains of attributes rather than tuples of relations. An expression in
the domain relational calculus has the following general form:
{d1, d2, . . . , dn | F(d1, d2, … , dm)} m $ n
Where d1, d2, . . . , dn, . . . , dm represent domain variables and F(d1, d2, . . . , dm)
represents a formula composed of atoms. See section 5.2.2.
5.3 Define the five basic relational algebra operations. Define the Join, Intersection, and Division
operations in terms of these five basic operations.
Can rewrite -Join in terms of the basic selection and Cartesian product operations:
R 3 F S = F (R S)
Can express the intersection operation in terms of the set difference operation:
R S = R - (R - S)
T1 = C(R)
T2 = C( (S x T1) - R)
T = T1 - T2
5.4 Discuss the differences between the five Join operations: Theta join, Equijoin, Natural join,
Outer join, and Semijoin. Give examples to illustrate your answer.
Theta join R ⊳ ⊲F S Produces a relation that contains tuples satisfying the predicate F from
of R and S.
Natural join R⊳⊲S An Equijoin of the two relations R and S over all common attributes
join of R with S.
5.5 There are different types of join operations that can be used to retrieve data, based on
different relations. Describe the relation between theta and equal join.
The Theta join operation (R F S) defines a relation that contains tuples satisfying the
predicate F from the Cartesian product of R and S. The predicate F is of the form R.ai u S.bi,
where u may be one of the comparison operators (<,>,=, ≤, ≥, ≠). In the case where the
predicate F contains only equality (=), the term Equijoin is used instead. Equal join is one
form of theta join.
5.6 Define the structure of a (well-formed) formula in both the tuple relational calculus and
domain relational calculus.
5.7 What is the difference between existential and universal quantifiers in relational calculus?
Give examples to elaborate how the two are applied in a statement
Exercises
For the following exercises, use the Hotel schema defined at the start of the Exercises at the end of
Chapter 4.
5.8 Describe the relations that would be produced by the following relational algebra operations:
This will produce a relation with a single attribute (hotelNo) giving the number of those
hotels with a room price greater than £50.
This will produce a join of the Hotel and Room relations containing all the attributes of
both Hotel and Room (there will be two copies of the hotelNo attribute). Essentially this
will produce a relation containing all rooms at all hotels.
This will produce a join of Hotel and those tuples of Room with a price greater than £50.
Essentially this will produce a relation containing all hotel names with a room price above
£50.
This will produce a (semi) join of Hotel and those tuples of Room with a price greater
than £50. Only those Hotel attributes will be listed. Essentially this will produce a relation
containing all the details of all hotels with a room price above £50.
This will produce a relation containing the names of all guests who have booked all hotels
in London.
5.9 Provide the equivalent tuple relational calculus and domain relational calculus expressions
for each of the relational algebra queries given in Exercise 4.8.
DRC: {hotelNo | (rNo, typ, prce) (Room (rNo, hotelNo, typ, prce) prce 50)}
DRC: {hNo, hName, cty, rNo, hNo1, typ, prce | (Hotel(hNo, hName, cty)
DRC: {guestName | (gNo, gName, gAddress, hNo, gNo1, dFrom, dTo, rNo,
5.10 Describe the relations that would be produced by the following tuple relational calculus
expressions:
This will produce a relation containing the names of all hotels in London.
50)}
This will produce a relation containing the names of all hotels that have a room price
above £50.
This will produce a relation containing the names of all hotels that have a booking for
a guest called John Smith.
This will produce a relation containing the names of guests who have more than one
booking at the same hotel, along with the hotel number and the dates of the bookings.
5.11 Provide the equivalent domain relational calculus and relational algebra expressions for each
of the tuple relational calculus expressions given in Exercise 4.10.
50)}
DRC: {hotelName | (hNo, cty, rNo, hNo1, typ, prce) (Hotel(hNo, hotelName, cty)
DRC: {hotelName | (hNo, cty, gNo, gName, gAddress, hNo1, gNo1, dFrom, dTo,
gNo, gAddress, hNo1, gNo1, dTo1, rNo1, hNo2, gNo2, dTo2, rNo2)
(dateFrom1 dateFrom2))}
RA: Hotel
TRC: {H | Hotel(H)}
(b) List all single rooms with a price below £20 per night.
(d) List the price and type of all rooms at the Grosvenor Hotel.
(f) List the details of all rooms at the Grosvenor Hotel, including the name of the guest
staying in the room, if the room is occupied.
(g) List the guest details (guestNo, guestName, and guestAddress) of all guests staying at
the Grosvenor Hotel.
(B.hotelNo = H.hotelNo) H.hotelName = ‘Grosvenor Hotel’)
5.13 Using relational algebra, produce a report of all employees from the IT and planning
departments who are born after 1990.
The report can be obtained through union operator by combining results of a query that retrieves
employees born after 1990 from IT and the query of the employees born after 1990 from
planning.
The report could also be produced by a query that uses OR (V) operator to
combine result of the employees born after 1990 from IT and the result of
employees born after 1990 and from Planning department.
((deptNo=‘IT’ dob>’31-12-1990’) (deptNo=‘PL’ dob>’31-12-1990’)) (employee)
RA: Employee
TRC: {E | Employee(E)}
RA: dob>’31-12-1990’(Employee)
sex=‘F’
DRC: {empNo, fName, lName, address, DOB, sex, position, deptNo | Employee(empNo,
dob>’31-12-1990’)}
5.16 List all employees who are not managers and are paid more than $1500.
DRC: { fName, lName, address | Employee(empNo, fName, lName, address, DOB, sex,
5.17 Produce a list of the names and addresses of all employees who work for the IT department.
DRC: {fName, lName, address | (empNo, DOB, sex, position, deptNo, deptNo1,
DRC: {fName, lName | (empNo, address, DOB, sex, position, deptNo, projNo,
(projName = ‘SCCS’))}
5.19 Produce a complete list of all managers who are due to retire this year, in alphabetical order
of surname.
depno=‘IT’(project)
5.22 Using the union operator, retrieve the list of employees who are neither managers nor
supervisors. Attributes to be retrieved are first name, last name, position, sex and department
number.
5.23 List the total number of employees in each department for those departments with more than
10 employees. Create an appropriate heading for the columns of the results table.
where Book contains details of book titles in the library and the ISBN is the key.
BookCopy contains details of the individual copies of books in the library and
copyNo is the key. ISBN is a foreign key identifying the book title.
Borrower contains details of library members who can borrow books and borrowerNo
is the key.
BookLoan contains details of the book copies that are borrowed by library members and
copyNo/dateOut forms the key. borrowerNo is a foreign key identifying
the borrower.
Formulate the additional queries in relational algebra, tuple relational calculus, and domain relational
calculus.
RA: Borrower
TRC: {B | Borrower(B)}
RA: title(year=‘2012’(Book))
DRC: {title | (ISBN, edn, yr) (Book(ISBN, title, edn, yr) yr=’2012’}
5.27 List all copies of book titles that are available for borrowing.
(BC.available=‘Y’))}
DRC: {copyNo, title | (ISBN, edn, yr, ISBN, avail) (Book(ISBN, title, edn, yr)
BookCopy(copyNo, ISBN, avail) avail=’Y’)}
5.28 List all copies of the book title “Lord of the Rings” that are available for borrowing.
5.29 List the names of borrowers who currently have the book title “Lord of the Rings” on loan.
DRC: {borrowerName | (ISBN, title, edn, yr, copyNo, avail, bNo, bAddress, dOut,
dDue) (Book(ISBN, title, edn, yr) BookCopy(copyNo, ISBN, avail)
Borrower(bNo, borrowerName, bAddress) BookLoan(copyNo, dOut,
dDue, bNo) avail=’N’ title= ‘Lord of the Rings’)}
5.33 How many times has the book title with ISBN “0-321-52306-7” been borrowed?
5.34 Produce a report of book titles that have been borrowed by “Peter Bloomfield”.
5.35 For each book title with more than 3 copies, list the names of library members who have
borrowed them.
5.36 Produce a report with the details of borrowers who currently have books overdue.
5.37 Produce a report detailing how many times each book title has been borrowed.
5.38 Analyze the RDBMSs that you are currently using. What types of relational languages does
the system provide? For each of the languages provided, what are the equivalent operations
for the eight relational algebra operations defined in Section 5.1?
This is a small student project, the result of which is dependent on the system analyzed.
However, it is likely that the supported languages will be based around SQL and QBE, in
which case, the student should attempt to map the various SQL clauses to the algebra and
calculus. See also Exercise 5.31.
Chapter 6 SQL: Data Manipulation
Review Questions
6.1 Briefly describe the four basic SQL DML statements and explain their use.
Section 6.3 describes the usage of basic data manipulation SQL statements; SELECT INSERT,
UPDATE and DELETE.
6.2 Explain the importance and application of the WHERE clause in the UPDATE and DELETE
statements.
Application
Importance
6.3 Explain the function of each of the clauses in the SELECT statement. What restrictions are
imposed on these clauses?
If the SELECT list includes an aggregate function and no GROUP BY clause is being used to
group data together, then no item in the SELECT list can include any reference to a column
unless that column is the argument to an aggregate function.
When GROUP BY is used, each item in the SELECT list must be single-valued per group.
Further, the SELECT clause may only contain:
Column names.
Aggregate functions.
Constants.
An expression involving combinations of the above.
All column names in the SELECT list must appear in the GROUP BY clause unless the name is
used only in an aggregate function.
6.4 What restrictions apply to the use of the aggregate functions within the SELECT statement? How
do nulls affect the aggregate functions?
An aggregate function can be used only in the SELECT list and in the HAVING clause.
Apart from COUNT(*), each function eliminates nulls first and operates only on the remaining
non-null values. COUNT(*) counts all the rows of a table, regardless of whether nulls or duplicate
values occur.
6.5 How can results from two SQL queries be combined? Differentiate how the INTERSECT and
EXCEPT commands work.
There are number of relational operators that can be used to combine results of two queries. Some
of them are UNION, INTERSECT, and EXCEPT. For the two results to be semanticaly
combined, order and number of columns from both results should be identical, and the combining
columns should be of the same domain.
The difference between the two is well explained in section 6.3.9 where the intersect returns the
rows that are common to both result sets, and except returns rows that are available in the first
result set and not in the second result set.
6.6 Differentiate between the three types of subqueries. Why is it important to understand the nature
of a subquery result before you write an SQL statement?
The subquery types are scalar, row, and table subquery. The main difference is on the nature of
results they return; scalar subquery returns a single column and single row that is a single value,
row subquery returns multiple columns and single row, while table subquery returns multiple
columns and multiple rows. It is so import to understand the nature of subquery result so that you
can make the best choice about the operator to be used
Exercises
For the Exercises 6.7 – 6.28, use the Hotel schema defined at the start of the Exercises at the end of
Chapter 3.
Simple Queries
6.9 List the names and addresses of all guests in London, alphabetically ordered by name.
Strictly speaking, this would also find rows with an address like: ‘10 London Avenue, New
York’.
6.10 List all double or family rooms with a price below £40.00 per night, in ascending order of price.
SELECT * FROM Room WHERE price < 40 AND type IN (‘D’, ‘F’)
ORDER BY price;
6.11 List the bookings for which no dateTo has been specified.
Aggregate Functions
6.12 How many hotels are there?
6.14 What is the total revenue per night from all double rooms?
6.15 How many different guests have made bookings for August?
6.16 List the price and type of all rooms at the Grosvenor Hotel.
6.19 What is the total income from bookings for the Grosvenor Hotel today?
6.20 List the rooms that are currently unoccupied at the Grosvenor Hotel.
6.21 What is the lost income from unoccupied rooms at the Grosvenor Hotel?
6.24 What is the average number of bookings for each hotel in August?
SELECT AVG(X)
FROM ( SELECT hotelNo, COUNT(hotelNo) AS X
FROM Booking b
WHERE (dateFrom <= DATE’2004-08-01’ AND
dateTo >= DATE’2004-08-01’) OR
(dateFrom >= DATE’2004-08-01’ AND
dateFrom <= DATE’2004-08-31’)
GROUP BY hotelNo);
6.25 What is the most commonly booked room type for each hotel in London?
SELECT MAX(X)
FROM ( SELECT type, COUNT(type) AS X
FROM Booking b, Hotel h, Room r
WHERE r.roomNo = b.roomNo AND b.hotelNo = h.hotelNo AND
city = ‘London’
GROUP BY type);
6.26 What is the lost income from unoccupied rooms at each hotel today?
Populating Tables
General
6.29 Investigate the SQL dialect on any DBMS that you are currently using. Determine the compliance
of the DBMS with the ISO standard. Investigate the functionality of any extensions the DBMS
supports. Are there any functions not supported?
This is a small student project, the result of which is dependent on the dialect of SQL being used.
6.30 Demonstrate that queries written using the UNION operator can be rewritten using the OR
operator to produce the same result.
Hint: students should explain a query that can be written in two ways by using UNION and OR
operators.
6.31 Apply the syntax for inserting data into a table.
Description of the data insert can be found in section 6.3.10. Studenst should simulate data entry
by using the provided syntax
Case Study 2
For Exercises 6.32–6.40, use the Projects schema defined in the Exercises at the end of Chapter 5.
6.32 List all employees from BRICS countries in alphabetical order of surname.
6.34 List all managers who are female in alphabetical order of surname, and then first name.
6.35 Remove all projects that are managed by the planning department.
6.36 Assume the planning department is going to be merged with the IT department. Update
employee records to reflect the proposed change.
UPDATE employee
SET deptNO=”IT”
WHERE deptNo=”PL”;
6.37 Using the UNION command, list all projects that are managed by the IT and the HR
department.
6.38 Produce a report of the total hours worked by each female employee, arranged by department
number and alphabetically by employee surname within each department.
6.39 Remove all project from the database on which no employees worked.
WHERE P.projno=w.projno);
6.40 List the total number of employees in each department for those departments with more than
10 employees. Create an appropriate heading for the columns of the results table.
FROM Employee
GROUP BY deptNo
Case Study 3
For Exercises 6.41–6.54, use the Library schema defined in the Exercises at the end of Chapter 5.
6.43 List all books titles published between 2010 and 2014.
SELECT *
FROM book
6.44 Remove all books published before 1950 from the database.
6.45 List all book titles that have never been borrowed by any borrower.
SELECT *
FROM BOOK
WHERE L.copyno=C.copyno);
6.46 List all book titles that contain the word ‘database’ and are available for loan.
SELECT b.ISBN, b.title, b.edition, b.year, c.available
SELECT borrowerName
WHERE b.ISBN=c.ISBN
Group by b.title;
SELECT COUNT(copyNo)
FROM BookCopy
6.50 How many times has the book with ISBN “0-321-52306-7” been borrowed?
SELECT COUNT(*)
6.51 Produce a report of book titles that have been borrowed by “Peter Bloomfield”.
SELECT DISTINCT title
6.52 For each book title with more than 3 copies, list the names of library members who have
borrowed them.
GROUP BY bc1.ISBN
6.53 Produce a report with the details of borrowers who currently have books overdue.
6.54 Produce a report detailing how many times each book title has been borrowed.
GROUP BY ISBN;