Database Systems Solutions
Database Systems Solutions
Lewis
Solutions Manual
Contents
5
5
13
15
57
57
39
39
25
25
15
71
71
iv
Contents
77
77
81
83
10
11
14
15
16
17
163
201
201
145
163
Distributed Databases
Exercises
127
145
125
127
Object Databases
Exercises
115
115
13
103
103
Database Tuning
Exercises
95
95
12
83
207
207
Contents
217
18
219
19
Models of Transactions
Exercises
20
22
247
247
231
231
225
225
Implementing Isolation
Exercises
21
219
261
261
267
23
269
24
25
285
285
301
301
275
275
Web Services
Exercises
26
269
A-1
A-1
vi
Contents
B-1
B-1
C-1
C-1
PART ONE
Introduction
1
Overview of Databases
and Transactions
EXERCISES
This chapter has no exercises.
2
The Big Picture
EXERCISES
2.1
Design the following two tables (in addition to that in Figure 2.1) that might be used
in the Student Registration System. Note that the same student Id might appear in
many rows of each of these tables.
a. A table implementing the relation CoursesRegisteredFor, relating a students
Id and the identifying numbers of the courses for which she is registered
Solution:
Id
CrsCode
111111111
111111111
111111111
666666666
666666666
111223344
987654321
023456789
123454321
CSE515
CSE505
CSE532
CSE532
CSE541
CSE504
CSE504
CSE515
CSE505
Id
CrsCode
Grade
111111111
CSE501
CHAPTER 2
111111111
666666666
666666666
111223344
987654321
023456789
123454321
CSE533
CSE505
CSE541
CSE533
CSE515
CSE505
CSE532
B+
AC
BB+
A
B+
SELECT S.Id
FROM Student
WHERE S.Status = senior
DELETE
FROM Student S
WHERE S.Status = senior
UPDATE Student S
SET S.Status = senior
WHERE S.Status = junior
2.3
Exercises
2.4
DELETE
FROM Transcript
WHERE StudId = 123456789
AND CrsCode = CS305 AND Semester = F2001
b. Changes to an A the grade assigned to the student with Id = 123456789 for the
course CS305 taken in the fall of 2000
Solution:
UPDATE Transcript
SET Grade = A
WHERE StudId = 123456789
AND CrsCode = CS305 AND Semester =F2000
c. Returns the Id of all students who took CS305 in the fall of 2000
Solution:
SELECT StudId
FROM Transcript
WHERE CrsCode = CS305 AND Semester = F2000
2.5
Given the relation Married that consists of tuples of the form a, b, where a is the
husband and b is the wife, the relation Brother that has tuples of the form c, d ,
where c is the brother of d , and the relation Sibling, which has tuples of the form
e, f , where e and f are siblings, use SQL to dene the relation Brother-In-Law,
where tuples have the form x, y with x being the brother-in-law of y.
(Hint: This query can be represented as a union of three separate SQL queries. SQL
provides the operator UNION to achieve this eect.)
CHAPTER 2
Solution:
The rst SQL query, below, describes the situation where someone is the brother of the
wife and hence the brother-in-law of the husband. The second disjunct describes the
situation where someone is the brother of the husband and hence the brother-in-law
of the wife. The third disjunct describes the situation where, someone is the husband
and hence the brother-in-law of all the wifes brothers and sisters.
2.6
Write an SQL statement that returns the names (not the Ids) of all students who
received an A in CS305 in the fall of 2000.
Solution:
SELECT Name
FROM Student, Transcript
WHERE StudId = Id AND Grade = A
AND CrsCode = CS305 AND Semester = F2000
2.7
State whether or not each of the following statements could be an integrity constraint of
a checking account database for a banking application. Give reasons for your answers.
a. The value stored in the balance column of an account is greater than or equal to
$0.
Solution:
Yes. It describes a constraint on a snapshot of the database.
b. The value stored in the balance column of an account is greater than it was last
week at this time.
Solution:
No. It does not describe a snapshot.
c. The value stored in the balance column of an account is $128.32.
Solution:
No. The balance will change.
Exercises
d. The value stored in the balance column of an account is a decimal number with
two digits following the decimal point.
Solution:
Yes. It is a domain constraint.
e. The social_security_number column of an account is dened and contains a
nine-digit number.
Solution:
Yes. It is a domain constraint.
f. The value stored in the check_credit_in_use column of an account is less than or
equal to the value stored in the total_approved_check_credit column. (These
columns have their obvious meanings.)
Solution:
Yes. It describes a constraint on a snapshot
2.8
State ve integrity constraints, other than those given in the text, for the database in
the Student Registration System.
Solution:
1.
2.
3.
4.
5.
6.
2.9
The courses for which the student enrolled (registered) must be oered this
semester(next semester).
An instructor cannot be assigned to two courses taught at the same time in the
same semester.
Two courses are not taught in the same room at the same time in a given semester.
No student must be registered (enrolled) in two courses taught at the same hour.
No student must be allowed to register for more than 20 credits in a given semester.
The room assigned to a course must have at least as many seats as the maximum
allowed enrollment for the course.
Give an example in the Student Registration System where the database satises the
integrity constraints IC0IC3 but its state does not reect the state of the real world.
Solution:
We register a student, but do not change the database.
2.10
The ight for which a person makes a reservation must be on the schedule.
The number of reservations on each ight must not exceed the number of seats on
the plane.
A passenger cannot order two meals
The number of meals ordered must equal the number of passengers who ordered
meals
The same seat on the plane must not be reserved for two passengers.
10
CHAPTER 2
2.11
2.12
Describe informally in what ways the following events dier from or are similar to
transactions with respect to atomicity and durability.
a. A telephone call from a pay phone (Consider line busy, no answer, and wrong
number situations. When does this transaction commit?)
Solution:
Commit occurs when caller hangs up. Billing information is durable. For line busy or
no answer, the transaction aborts. For a wrong number the transaction commits, but
later is compensated for by returning the callers money (Read about compensation
later in the book,)
b. A wedding ceremony (Suppose that the groom refuses to say I do. When does
this transaction commit?)
Solution:
Commit occurs when license is signed. Marriage is durable (hopefully).
c. The purchase of a house (Suppose that, after a purchase agreement is signed, the
buyer is unable to obtain a mortgage. Suppose that the buyer backs out during
the closing. Suppose that two years later the buyer does not make the mortgage
payments and the bank forecloses.)
Solution:
Various levels of commit; Every time someone signs something. For example, when
purchaser makes an oer to purchase and includes a deposit, he is committed to
Exercises
either purchase the house at that price (assuming he is approved for the mortgage) or
forfeit the deposit. If he is not approved for the mortgage, he is no longer committed
to purchase the house and gets his deposit back. If he does not pay his mortgage
payment the transaction is compensated for when the bank forecloses.
d. A baseball game (Suppose that it rains.)
Solution:
Commit occurs after game is ocial. If it rains before the game is ocial, the game
is aborted.
2.13
Assume that, in addition to storing the grade a student has received in every course he
has completed, the system stores the students cumulative GPA. Describe an integrity
constraint that relates this information. Describe how the constraint would be violated
if the transaction that records a new grade were not atomic.
Solution:
The integrity constraint is that the GPA stored in the database is the GPA of the
course grades stored. That constraint could be violated if a transaction updated a
course grade and aborted before it could update the GPA.
2.14
Explain how a lost update could occur if, under the circumstances of the previous
problem, two transactions that were recording grades for a particular student (in
dierent courses) were run concurrently.
Solution:
The rst transaction reads the course grades before the second updated its grade and
then updates the GPA. The second transaction reads the course grades before the rst
updated its grade and then updates the GPA. The rst update of the GPA is lost and
the nal one is incorrect.
11
PART TWO
Database Management
3
The Relational Data Model
EXERCISES
3.1
3.2
3.3
16
CHAPTER 3
d. Superkey
Solution:
A superkey is a set of attributes in a relation that contains a key of that relation
3.4
Dene
a. Integrity constraint
Solution:
An integrity constraint is an application-specic restriction on the tuples in one or
several relations.
b. Static, as compared with dynamic, integrity constraint
Solution:
Static integrity constraints restrict the legal instances of a database. Examples
of static ICs are domain constraints, key constraints, foreign-key constraints, etc.
Dynamic integrity constraints restrict the evolution (over time) of legal instances of
a database, for instance, a salary increase should not exceed 5%.
c. Referential integrity
Solution:
A referential integrity constraint is a requirement that the referenced tuple must
exist.
d. Reactive constraint
Solution:
A reactive constraint is a static constraint with a trigger attached. The trigger
species what to do if the constraint is violated by an update.
e. Inclusion dependency
Solution:
), which states that
references T(K
An inclusion dependency is a statement S(F)
for every tuple s s, there is a tuple t t that has the same values over the attributes
as does s over the corresponding attributes in F .
in K
f. Foreign-key constraint
Solution:
A foreign-key constraint is an inclusion dependency in which the set of attributes
referred to is a candidate key in the referenced relation.
3.5
Looking at the data that happens to be stored in the tables for a particular application
at some particular time, explain whether or not you can tell
a. What the key constraints for the tables are
Solution:
No, in one particular instance of the table, a particular attribute might uniquely
identify the rows (and thus appear to be a key), but in other instances it might not.
b. Whether or not a particular attribute forms a key for a particular table
Exercises
Solution:
You cannot tell whether a particular attribute is a key (for the reasons mentioned
in (a)), but you can sometimes tell that a particular attribute cannot form a key by
itself (if two distinct rows have the same value over that attribute).
c. What the integrity constraints for the application are
Solution:
No. Again, one cannot determine integrity constraints just by looking at database
instances.
d. Whether or not a particular set of integrity constraints is satised
Solution:
Yes. Simply check if every constraint in the set is satised in the given instance.
3.6
We state in the book that once constraints have been specied in the schema, it is the
responsibility of the DBMS to make sure that they are not violated by the execution
of any transactions. SQL allows the application to control when each constraint is
checked. If a constraint is in immediate mode, it is checked immediately after the
execution of any SQL statement in a transaction that might make it false. If it is in
deferred mode, it is not checked until the transaction requests to commit. Give an
example where it is necessary for a constraint to be in deferred mode.
Solution:
Suppose the constraint states that the value of one attribute, A, is the sum of the
values of two other attributes, B and C . If we want to increment the value of B, we
must also increment the value of A in the same transaction. But no matter in what
order we do the incrementing, the constraint will be false between the two statements
that do the incrementing, and if we used immediate mode checking, the transaction
would abort when the rst increment statement was attempted.
3.7
Suppose we do not require that all attributes in the primary key are non-null and
instead request that, in every tuple, at least one key (primary or candidate) does not
have nulls in it. (Tuples can have nulls in other places and the non-null key can be
dierent for dierent tuples.) Give an example of a relational instance that has two
distinct tuples that might become one once the values for all nulls become known (that
is, are replaced with real values). Explain why this is not possible when one key (such
as the primary key) is designated to be non-null for all tuples in the relation.
Solution:
Let the relation have attributes A and B, each of which is a key. The tuples can be
a, NULL and NULL, b. If the rst NULL becomes b and the second a then these
tuples become the same.
If all tuples are non-NULL over the same key, then they must dier over that key
somewhere and thus they cannot become the same regardless of what is substituted
for nulls in other places.
3.8
Use SQL DDL to specify the schema of the Student Registration System fragment
shown in Figure 3.4, including the constraints in Figure 3.6 and Example 3.2.2. Specify
SQL domains for attributes with small numbers of values, such as DeptId and Grade.
17
18
CHAPTER 3
Solution:
CREATE TABLE
Id
Name
Address
Status
Student (
INTEGER,
CHAR(20),
CHAR(50),
CHAR(10)
CREATE TABLE
ProfId
Name
DeptId
Professor (
INTEGER,
CHAR(20),
Departments,
CREATE TABLE
CrsCode
DeptId
CrsName
Descr
Course (
CHAR(6),
Departments,
CHAR(20),
CHAR(100),
CREATE TABLE
StudId
CrsCode
Semester
Grade
Transcript (
INTEGER,
CHAR(6),
Semesters,
Grades,
Exercises
CREATE TABLE
Teaching (
ProfId
CrsCode
Semester
INTEGER,
CHAR(6),
Semesters,
3.9
CREATE TABLE
Id
Name
Address
Supplier (
Suppliers,
CHAR(20),
CHAR(50),
CREATE TABLE
Id
Name
Customer (
Customers,
CHAR(20),
19
20
CHAPTER 3
Address
CHAR(50),
PRIMARY KEY (Id) )
CREATE TABLE
Product (
Products,
PartNumber
Name
CHAR(50),
PRIMARY KEY (PartNumber) )
CREATE TABLE
Customer
Supplier
Product
Quantity
Price
Contract (
Customers,
Suppliers,
Products,
INTEGER,
INTEGER,
Exercises
3.10
You have been hired by a video store to create a database for tracking DVDs and
videocassettes, customers, and who rented what. The database includes these relations:
RentalItem, Customer, and Rentals. Use SQL DDL to specify the schema for this
database, including all the applicable constraints. You are free to choose reasonable
attributes for the rst two relations. The relation Rentals is intended to describe who
rented what and should have these attributes: CustomerId, ItemId, RentedFrom,
RentedUntil, and DateReturned.
Solution:
3.11
You are in a real estate business renting apartments to customers. Your job is to dene
an appropriate schema using SQL DDL. The relations are Property(Id, Address,
NumberOfUnits), Unit(ApartmentNumber, PropertyId, RentalPrice, Size),
Customer (choose appropriate attributes), Rentals (choose attributes; this relation
should describe who rents what, since when, and until when), and Payments (should
describe who paid for which unit, how much, and when). Assume that a customer can
rent more than one unit (in the same or dierent properties) and that the same unit
can be co-rented by several customers.
Solution:
The students must recognize that the key of Unit is ApartmentNumber,
PropertyId and not just ApartmentNumber. The students should also recognize that both Rentals and Payments should include attributes that reference
ApartmentNumber, PropertyId. In addition, neither ApartmentNumber,
PropertyId nor customer should be a key in Payments.
3.12
You love movies and decided to create a personal database to help you with trivia
questions. You chose to have the following relations: Actor, Studio, Movie, and
PlayedIn (which actor played in which movie). The attributes of Movie are Name,
Year, Studio, and Budget. The attributes of PlayedIn are Movie and Actor. You
are free to choose the attributes for the other relations as appropriate. Use SQL DDL
to design the schema and all the applicable constraints.
Solution:
3.13
You want to get rich by operating an auction Web site, similar to eBay, at which
students can register used textbooks that they want to sell and other students can bid
on purchasing those books. The site is to use the same proxy bidding system used by
eBay (http://www.ebay.com).
Design a schema for the database required for the site. In the initial version of the
system, the database must contain the following information:
1.
2.
For each book being auctioned: name, authors, edition, ISBN number, bookId
(unique), condition, initial oering price, current bid, current maximum bid,
auction start date and time, auction end date and time, userId of the seller, userId
of the current high bidder, and an indication that the auction is either currently
active or complete
For each registered user: name, userId (unique), password, and e-mail address
21
22
CHAPTER 3
Solution:
3.14
You want to design a room-scheduling system that can be used by the faculty and
sta of your department to schedule rooms for events, meetings, classes, etc. Design
a schema for the database required for the system. The database must contain the
following information:
1.
2.
For each registered user: name, userId (unique), password, and e-mail address
For each room: room number, start date of the event, start time of the event,
duration of the event, repetition of the event (once, daily, weekly, monthly,
mon-wed-fri, or tues-thurs), and end date of repetitive event
Solution:
3.15
Design the schema for a library system. The following data should either be contained
directly in the system or it should be possible to calculate it from stored information:
1.
2.
3.
4.
About each patron: name, password, address, Id, unpaid nes, identity of each
book the patron has currently withdrawn, and each books due date
About each book: ISBN number, title, author(s), year of publication, shelfId,
publisher, and status (on-shelf, on-loan, on-hold, or on-loan-and-on-hold). For
books on-loan the database shall contain the Id of the patron involved and the
due date. For books on hold the database shall contain a list of Ids of patrons who
have requested the book.
About each shelf: shelfId and capacity (in number of books)
About each author: year of birth
The system should enforce the following integrity constraints. You should decide
whether a particular constraint will be embedded in the schema, and, if so, show how
this is done or will be enforced in the code of a transaction.
1.
2.
3.
Solution:
3.16
Suppose that the fragment of the Student Registration System shown in Figure 3.4 has
two user accounts: Student and Administrator. Specify the permissions appropriate
for these user categories using the SQL GRANT statement.
Solution:
An administrator should be able to read and modify all information in Student,
Professor, Course, Transcript, and Teaching if your university is like ours,
only administrators can change information. Here is an example:
GRANT SELECT, INSERT, DELETE, UPDATE ON Transcript
TO Administrator WITH GRANT OPTION
Exercises
Assuming that Ids and addresses are considered private, a student should be able
to see names and student status in the Student relation, professors and departments in
the Professor relation, everything in the Course relation, nothing in Transcript,
and, perhaps, CrsCode and Semester in Teaching. Here is an example:
GRANT SELECT (CrsCode, Semester) ON Teaching
TO Student
3.17
Suppose that the video store of Exercise 3.10 has the following accounts: Owner,
Employee, and User. Specify GRANT statements appropriate for each account.
Solution:
This question is formulated with a pitfall: the student is supposed to recognize that
users should not be granted any privileges, because a user should be allowed to see
only the data pertaining to that particular user.
Employee and Owner can have the same permissions. In any case, Employee
should not be allowed to delete records. Owner might be allowed all privileges.
3.18
23
4
Conceptual Modeling of Databases
with Entity-Relationship Diagrams
and the Unified Modeling
Language
EXERCISES
4.1
Suppose that you decide to convert IsA hierarchies into the relational model by adding
a new attribute (such as Status in the case of Student entities, as described on
page 91the second option for representing IsA hierarchies). What kind of problems
exist if subentities are not disjoint (e.g., if a secretary can also be a technician)? What
problems exist if the covering constraint does not hold (e.g., if some employees are not
classied as either secretary or technician)?
Solution:
If sub-entities are not disjoint then some tuples would be duplicated (except for the
value of the new attribute that simulates the hierarchy). If the covering constraint does
not hold, then we would have to use NULs to represent the tuples that do not belong
to any one of the subentities.
4.2
Construct your own example of an E-R or UML diagram whose direct translation into
the relational model has an anomaly similar to that of the Person entity (see the
discussion regarding Figure 4.13 on page 86).
Solution:
An example is the Employee relation, depicted below, in which an employee can have
several dependents. The E-R diagram is depicted in Figure 4.1.
SSN
Name
111111111
111111111
111111111
555666777
555666777
987654321
John
John
John
Mary
Mary
Bart
Doe
Doe
Doe
Doe
Doe
Simpson
Address
Dependent
Joan
Kim
Kathy
Peter
Tom
Mary
26
CHAPTER 4
FIGURE 4.1
Address
Name
SSN
EMPLOYEE
Dependent
4.3
Represent the IsA hierarchy in Figure 4.6, page 80, in the relational model. For
each IsA relationship discuss your choice of the representation technique Discuss the
circumstances in which an alternative representation (to the one you have chosen)
would be better.
Solution:
To represent the relationship IsA Employee, we would add an additional status
attribute to the schema of Employee. This is because the entity types Secretary and
Technician are likely to be disjoint. Similar considerations apply to IsA Student.
The downside of this representation is that some tuples will have null values over
the attribute Specialization (in case of secretaries) and Major and Advisor (in
case, say, of freshmen).
The relationship IsA Person is dierent from the above two in that its subentities,
Employee and Student, can have fairly large intersection. Thus, to avoid the
duplication associated with the use of the Status attribute, we would use three
relations, Person(SSN,Name,DOB), Employee(SSN,Department,Salary), and
Student(SSN,GPA,StartDate).
4.4
Suppose, in Figure 4.8, the Professor entity did not participate in the relationship
WorksIn, but the arrow between them was still present. Would it make sense to
merge Professor with WorksIn during translation into the relational model? What
kind of problems can arise here? Are they serious problems?
Solution:
The problem with such a merge is that there might be null values over the attributes
of WorksIn. This is most likely not a serious problem in this example.
4.5
Translate the brokerage example of Section 4.7 into an SQL schema. Use the necessary
SQL machinery to express all constraints specied in the E-R model.
Solution:
We will start with the diagram in Figure 4.30. In translating this E-R diagram,
we choose to combine IsHandledBy with Account, WorksIn with Broker, and
Office with ManagedBy. The rationale for these combinations is that in each
case the entity type is a key role in the corresponding relationship. The reason why
ManagedBy is combined with Office rather than Broker (which also happens to
be a key role in the ManagedBy relationship) is that it is more likely for an oce to
have a manager than it is for a broker to be a manager. Therefore, combining Office
Exercises
with ManagedBy into one table is unlikely to lead to many null values being stored
with the Office table. In contrast, combining ManagedBy with Broker is likely
to cause a situation where the value of the attribute Manages of the table Broker
would be mostly NULL.
CREATE TABLE Client (
Name CHAR(20) NOT NULL,
Id
INTEGER,
WorkPhone# CHAR(10),
PRIMARY KEY (Id)
)
CREATE TABLE Account (
Account#
INTEGER,
DateOpened DATE,
Status
CHAR(1),
)
CREATE TABLE Office (
Phone#
CHAR(10),
Address CHAR(50) NOT NULL,
Manager INTEGER,
ManagedSince DATE,
PRIMARY KEY (Phone#),
UNIQUE (Address),
FOREIGN KEY Manager REFERENCES Broker(Id)
ON UPDATE CASCADE
ON DELETE SET NULL
)
CREATE TABLE HasAccount (
Client INTEGER NOT NULL,
Account INTEGER NOT NULL,
FOREIGN KEY Client REFERENCES Client(Id)
ON UPDATE CASCADE
27
28
CHAPTER 4
ON DELETE NO ACTION,
FOREIGN KEY Account REFERENCES Account(Account#)
ON UPDATE CASCADE
ON DELETE CASCADE
)
CREATE TABLE Broker (
Id
INTEGER,
Name
CHAR(20) NOT NULL,
WorksIn CHAR(10) NOT NULL,
WorksSince DATE,
PRIMARY KEY (Id)
FOREIGN KEY WorksIn REFERENCES Office(Phone#)
ON UPDATE CASCADE
ON DELETE CASCADE
)
CREATE TABLE BrokerPhone (
Broker
INTEGER NOT NULL,
PhoneExtension CHAR(10) NOT NULL,
FOREIGN KEY Broker REFERENCES Broker(Id)
ON UPDATE CASCADE
ON DELETE CASCADE
)
Note that there is a reason why we split o the PhoneExtension attribute from the
Broker relation. If we join this attribute to Broker, then Id would no longer be
a candidate key, so one of the foreign key constraints in Accounts would not be
possible.
We still need to write a constraint to ensure that a client cannot have two separate
accounts in the same oce. We will use an SQL assertion for that. This last constraint
might be a little hard to write with the amount of knowledge of SQL that the students
have at this point, so this part can be used for extra credit or it can be postponed
until Chapter 5.
CREATE ASSERTION OneAcctPerClientOffice
CHECK NOT EXISTS (
SELECT C.Id
FROM Client C,
Exercises
)
We now turn to the representation of the trading information in Figure 4.31.
Because Transaction is a key role in Trade, it is convenient to combine the two.
Thus, we end up with the following tables:
CREATE TABLE Transaction (
Id
INTEGER,
Date
DATE NOT NULL,
Time
TIME NOT NULL,
PricePerShare DECIMAL(6) NOT NULL,
NumberOfShares INTEGER NOT NULL,
StockSymbol
CHAR(5) NOT NULL,
Account
INTEGER NOT NULL,
PRIMARY KEY (Id),
FOREIGN KEY StockSymbol REFERENCES Stock
ON UPDATE NO ACTION
ON DELETE NO ACTION,
FOREIGN KEY Account REFERENCES Account(Account#)
ON UPDATE CASCADE
ON DELETE NO ACTION
)
CREATE TABLE Position (
Account
INTEGER,
StockSymbol CHAR(5),
Amount
INTEGER NOT NULL,
PRIMARY KEY (Account, Stock),
FOREIGN KEY Account REFERENCES Account(Account#)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY StockSymbol REFERENCES Stock
ON UPDATE CASCADE
ON DELETE CASCADE
)
CREATE TABLE Stock (
StockSymbol CHAR(5),
CompanyName CHAR(50) NOT NULL,
CurrentPrice DECIMAL(6) NOT NULL,
PRIMARY KEY (StockSymbol)
)
4.6
Identify the navigation traps present in the diagram of Figure 4.29, page 107.
29
30
CHAPTER 4
CName
ItemName
Address
CUSTOMER
ITEM
ORDER
Qty
Description
SUPPLIER
SUPPLIEDBY
Price
SName
FIGURE 4.2
Solution:
One navigation trap is when we follow the links Account to IsHandledBy to
Office to WorksIn to Broker, which can associate accounts with wrong brokers.
Another trap occurs when we travel through ManagedBy instead of WorksIn.
4.7
Price
Exercises
CName
ItemName
Address
CUSTOMER
Qty
Description
ITEM
Order
SUPPLIEDBY
Price
SUPPLIER
SName
FIGURE 4.3
this constraint. Also show how this new diagram can be translated back to
the relational model.
Solution:
See the E-R diagram in Figure 4.3.
One possible relational tables corresponding to this E-R diagram:
CREATE TABLE
Customer (
CName
Address
PRIMARY KEY(CName));
CREATE TABLE
Supplier (
SName
Item (
ItemName
Description
Price
SName
31
32
CHAPTER 4
Perform conceptual design of the operations of your local community library. The
library has books, CDs, tapes, and so forth, which are lent to library patrons. The
latter have accounts, addresses, and so forth. If a loaned item is overdue, it accumulates
penalty. Some patrons are minors, so they must have sponsoring patrons who are
responsible for paying penalties (or replacing a book in case of a loss).
a. Use the E-R approach.
b. Use UML.
Solution:
a. See the E-R diagram in Figure 4.4.
4.9
A real estate rm keeps track of the houses for sale and customers looking to buy
houses. A house for sale can be listed with this rm or with a dierent one. Being
listed with a rm means that the house owner has a contract with an agent who
works for that rm. Each house on the market has price, address, owner, and a list
of features, such as the number of bedrooms, bathrooms, type of heating, appliances,
size of garage, and the like. This list can be dierent for dierent houses, and some
features can be present in some houses but missing in others. Likewise, each customer
has preferences that are expressed in the same terms (the number of bedrooms,
bathrooms, etc.). Apart from these preferences, customers specify the price range of
houses they are interested in. Perform conceptual design for this enterprise.
a. Use the E-R approach.
b. Use UML.
Solution:
a. See the E-R diagram in Figure 4.5.
4.10
Exercises
FIGURE 4.4
Account#
Address
PATRON
SPONSORDBY
LOAN
ReturnDate
Date
Id#
Type
ITEM
Title
PenaltyRate
ISA
BOOK
CD
TAPE
Modify the E-R diagram for the Student Registration System in Figure 4.33 on
page 114 to include co-requisite and prerequisite relationships that exist over multiple
periods of time. Each period begins in a certain semester and year and ends in a
certain semester and year, or it continues into the present. Modify the translation into
the relational model appropriately.
Solution:
Instead of the relationship Requires with a single attribute EnforcedSince, we
use two attributes, EnforcedSince and EnforcedUntil. If the latter is NULL,
then the prerequisite is in force at present time. A similar relationship is needed
for co-requisites. The corresponding relational representation requires that we add the
attribute EnforcedUntil to the denition of the table Requires and create a similar
table for co-requisites.
EnforcedUntil DATE,
Note that unlike EnforcedSince, we do not use the NOT NULL specication, because
NULL is meaningful it represents prerequisites that are valid at present time.
33
34
CHAPTER 4
FIGURE 4.5
Price
Address
AgentName
Name
Owner
HOUSE
LISTING
CUSTOMER
Features
REALESTATEFIRM
Preferences
PriceRange
4.12
Modify the E-R diagram for the Student Registration System in Figure 4.33 on
page 114 to include information about the student majors and the majors allowed in
courses. A student can have several majors (which are codes of the various programs in
the university, such as CSE, ISE, MUS, ECO). A course can also have several admissible
majors, or the list of admissible majors can be empty. In the latter case, anyone
is admitted into the course. Express the constraint that says that a course with
restrictions on majors can have only those students who hold one of the allowed
majors.
Alas, in full generality this constraint can be expressed only as an SQL assertion
(introduced in Section 3.3) that uses features of Section 5.2 (which we have yet to
study). However, it is possible to express this constraint under the following simplifying
assumption: when a student registers for a course, she must declare the major toward
which the course is going to be taken, and this declared major is checked against the
admissible majors.
Modify the relation schema in Figures 4.34 and 4.35 to reect this simplifying
assumption and then express the aforesaid integrity constraint.
Solution:
The Student entity type gets a new set-valued attribute Major, and similarly for
Courses.
Under the simplifying assumption mentioned above, the relations Student,
Courses, and Transcript each gets a new attribute, Major. The constrain then can
be expressed as a foreign-key constraint on the Transcript relation:
FOREIGN KEY (StudId, Major)
REFERENCES Student(Id, Major)
4.13
Redo the E-R diagram of the Student Registration System (Figure 4.33) in UML.
Exercises
Address
SUPERMARKET
LOCATEDIN
GEOGRAPHICAREA
PARTOF
ISA
SALE
Date
STATE
PRODUCT
Name
Category
CITY
REGION
TIMEPERIOD
INCLUDEDIN
ISA
DAY
MONTH
YEAR
FIGURE 4.6
Solution:
4.14
Make the necessary modications to the schema of the Student Registration System
to reect the design that uses the SQL domain Semesters, as discussed at the end of
Section 4.8. Express the constraint that a class can be taught only during the semesters
in which the corresponding course is oered. For instance, if the value of the attribute
SemestersOffered for the course CS305 is Both, then the corresponding classes can
be taught in the spring and the fall semesters. However, if the value of that attribute
is Spring then these classes can be taught only in the spring.
Solution:
The change to the schema was described at the end of Section 4.8: We no longer need
the relation WhenOffered. Instead, Course gets the following attribute:
SemestersOffered
Semesters
35
36
CHAPTER 4
Design an E-R model for the following enterprise. Various organizations make business
deals with various other organizations. (For simplicity, let us assume that there
are only two parties to each deal.) When negotiating (and signing) a deal, each
organization is represented by a lawyer. The same organization can have deals with
many other organizations, and it might use dierent lawyers in each case. Lawyers and
organizations have various attributes, like address and name. They also have their own
unique attributes, such as specialization and fee, in the case of a lawyer, and budget,
in the case of an organization.
Show how information loss can occur if a relationship of degree higher than two is
split into a binary relationship. Discuss the assumption under which such a split does
not lead to a loss of information.
Solution:
Two types of entities: Organization and Lawyer. Deal is a quadruple relationship
with roles Party1, Party2, Lawyer1 and Lawyer2.
We can split this into three binary relationships: Two representing a contract
between an Organization entity and a Lawyer entity (one for each party in the
deal) and one that represents negotiations between the two lawyers. The information
loss here is obvious, nothing connects the three binary relationships to indicate that
they are part of the same deal.
One assumption that ensures that information loss will not occur is that no
organization uses the same lawyer twice.
4.16
Design an E-R model for the library system described in Exercise 3.15. Do the same
with UML.
Solution:
4.17
Consider the E-R diagram depicted in Figure 4.38. Write down the corresponding
relational schema using SQL. Include all keys and other applicable constraints.
Solution:
CREATE TABLE
Student
Id
Name
Major
INTEGER,
CHAR(20),
CHAR(3),
PRIMARY KEY (Id, Major) )
CREATE TABLE
Class
Exercises
Id
Majors
STUDENT
Semester
TAKES
Name
CrsCode
CLASS
Room
Semester CHAR(5),
CrsCode CHAR(6),
Room
CHAR(15),
PRIMARY KEY (Semester, CrsCode) )
Takes (
StudId
INTEGER NOT NULL,
Semester CHAR(5) NOT NULL,
CrsCode CHAR(6) NOT NULL,
FOREIGN KEY (Semester,CrsCode) REFERENCES Class,
FOREIGN KEY (StudId) REFERENCES Student (Id) )
CREATE TABLE
4.18
Consider the partial translation of an E-R participation constraint into UML (shown
in Figure 4.24). Show that this is, indeed, an imprecise translation in that there are
instances of the database (i.e., collections of objects and associations) that comply
with the E-R constraint in Figure 4.24(b) such that it does not satisfy the multiplicity
constraint in UML.
37
5
Relational Algebra and SQL
EXERCISES
5.1
Assume that R and S are relations containing nR and nS tuples, respectively. What
is the maximum and minimum number of tuples that can possibly be in the result of
each of the following expressions (assuming appropriate union compatibilities)?
a. R S
Solution:
Maximum number of tuples: (nR + nS );
Minimum number of tuples: max(nR , nS ).
b. R S
Solution:
Maximum number of tuples: min(nR , nS );
Minimum number of tuples: 0.
c. R S
Solution:
Maximum number of tuples: nR ;
Minimum number of tuples: 0.
d. R S
Solution:
Maximum number of tuples: nR nS ;
Minimum number of tuples: nR nS .
e. R S
Solution:
Maximum number of tuples: nR nS ;
Minimum number of tuples: 0.
f. R / S
Solution:
Maximum number of tuples: nR/nS;
Minimum number of tuples: 0.
40
CHAPTER 5
Assume that R and S are tables representing the relations of the previous exercise.
Design SQL queries that will return the results of each of the expressions of that
exercise.
Solution:
a.
SELECT * FROM R
UNION
SELECT * FROM S
b.
SELECT * FROM R
INTERSECT
SELECT * FROM S
c.
SELECT * FROM R
EXCEPT
SELECT * FROM S
d.
SELECT * FROM R,S
e.
SELECT
FROM
WHERE
A1, ..., An
Exercises
FROM
WHERE
R r
NOT EXISTS (
(SELECT
EXCEPT
(SELECT
FROM
WHERE
* FROM
S)
R r2
r.A1 = r2.A1 AND ...
AND r.An = r2.An ) )
5.3
R, S
R.s = 4
5.4
Verify that selections commutethat is, for any relation r and any pair of selection
conditions cond1 and cond2 , cond1 (cond2 (r)) = cond2 (cond1 (r)).
Solution:
cond1 (cond2 (r)) = cond1 ANDcond2 (r))
cond2 (cond1 (r)) = cond1 ANDcond2 (r))
Thus, cond1 (cond2 (r)) = cond2 (cond1(r)).
5.5
Verify that, for any pair of relations r and s, cond (r s) = r cond (s) if the selection
condition cond involves only the attributes mentioned in the schema of relation s.
Solution:
cond (r s) = {< r , s > |r r, s s, < r , s > satises cond }. Since cond involves only
the attribute in s, the requirement that < r , s > satises cond can be replaced with
s satises cond . By denition of selection and cross product, r cond (s) = {< r , s >
|r r, s s and s satises cond }. Hence, the two expressions are equivalent.
5.6
41
42
CHAPTER 5
Since r and s are relations over A1 , . . . , An , it means that only the tuples that
are both in r and s survive the selection, i.e., the result is the intersection of the two
relations.
5.7
Using division, write a relational algebra expression that produces all students who
have taken all courses oered in every semester (this implies that they might have
taken the same course twice).
Solution:
StudId,CrsCode,Semester (Transcript) / CrsCode,Semester (Teaching)
5.8
Using division, write a relational algebra expression that produces all students who
have taken all courses that have been oered. (If a course has been oered more than
once, they have to have taken it at least once.)
Solution:
StudId,CrsCode (Transcript) / CrsCode (Teaching)
5.9
Construct a relational algebra query that produces the same result as the outer join
r outer
cond s using only these operators: union, dierence, Cartesian product, projection,
general join (not outer join). You can also use constant relations, that is, relations with
xed content (e.g., one that has tuples lled with nulls or other predened constants).
Solution:
Let nullR be the relation over R that contains only one tuple, which entirely consists
of NULLs. Similarly, let nullS be a relation over S that has a single NULL-tuple.
Then r outer
cond s is:
r cond s
(r R(r cond s)) nullS
nullR (s S (r cond s))
5.10
Express each of the following queries in (i) relational algebra and (ii) SQL using the
Student Registration System schema of Figure 3.4.
a. List all courses that are taught by professors who belong to the EE or MGT
departments.
Solution:
Algebra:
SQL:
SELECT
FROM
WHERE
T.CrsCode, T.Semester
Teaching T,
T.ProfId=P.Id
Professor P
Exercises
AND (P.DeptId = EE OR P.DeptId = EE)
b. List the names of all students who took courses both in spring 1997 and fall 1998.
Solution:
Algebra: Let Sems be a relation with Semester as the only column and
S1997 and F1998 as the only tuples. Then the answer is:
SQL:
SELECT
FROM
WHERE
S.Name
Student S
NOT EXISTS (
(SELECT * FROM Sems)
EXCEPT
(SELECT T.Semester
FROM Teaching T
WHERE T.StudId = S.Id ) )
Note that if the query required only the students who took courses in either of the
two semesters, then the solution would not have required the division operator and
NOT EXISTS and it would have been much simpler.
c. List the names of all students who took courses from at least two professors in
dierent departments.
Solution:
Name(Student Id=StudId
DeptId
=DeptId2(StudDept StudDept[StudId,DeptId2]) )
SQL: The SQL solution is also in two stages, for clarity. First, we create an
auxiliary view:
CREATE VIEW
StudDept(StudId, DeptId) AS
43
44
CHAPTER 5
R.StudId, P.DeptId
Transcript R, Teaching T, Professor P
R.CrsCode = T.Crscode
AND R.Semester = T.Semester
AND T.ProfId = P.Id
SELECT
FROM
WHERE
S.Name
Student S, StudDept SD1, StudDept SD2
d. List all courses that are oered by the MGT Department and that have been taken
by all students.
Solution:
Algebra:
SQL:
SELECT
FROM
WHERE
C.CrsCode
Course C
e.
S.StudId
Student S)
T.StudId
Transcript T
C.CrsCode = T.CrsCode) )
Find every department that has a professor who taught all courses ever oered by
that department.
Solution:
Algebra: The algebraic solution is trickier than SQLs. Let ProfCrsAux(ProfId, CrsCode) be dened as follows:
Exercises
A tuple, p, c here means that c is a course that is oered by a department
other than the one where p works. Next, consider ProfCrs(ProfId, CrsCode),
which is dened as follows:
ProfCrsAux ProfId,CrsCode(Teaching)
A tuple, p, c in this relation means that c is a course that was either taught
by p or it is oered by a department other than the one where p works.
Finally, the expression for the requisite query can be constructed as follows:
ProfCrs / CrsCode(Course)
Note that constructing ProfCrsAux was essential. The expression
ProfId,CrsCode(Teaching) / CrsCode(Course)
is not correct, because it nds every professor who have taught every course in
every department rather than the courses that are oered by the department
where that professor works.
The solution given above uses the division operator. There is a more
straightforward solution where one would rst nd professors who did not teach
one of the courses oered by the professors department, and then take the
complement.
SQL:
SELECT
FROM
WHERE
P.DeptId
Professor P
EXISTS (
( SELECT P2.Id
FROM
WHERE
Professor P2
NOT EXISTS (
(SELECT C.CrsCode
FROM
Course C
WHERE C.DeptId = P2.DeptId )
EXCEPT
(SELECT T.CrsCode
FROM
Teaching T
WHERE T.ProfId = P2.Id ) )
AND P.Id = P2.Id
P.DeptId
Professor P
NOT EXISTS (
45
46
CHAPTER 5
5.11
(SELECT
C.CrsCode
FROM
WHERE
EXCEPT
(SELECT
FROM
WHERE
Course C
C.DeptId = P.DeptId )
T.CrsCode
Teaching T
T.ProfId = P.Id ) )
Use the relational algebra to nd the list of all problematic classes (i.e., coursesemester pairs) where the failure rate is higher than 20%. (Assume, for simplicity, that
grades are numbers between 1 and 4, and that a failing grade is anything less than 2.)
Because the relational algebra does not have aggregate operators, we must add
them to be able to solve the above problem. The additional operator you should use is
countA/B (r).
The meaning of this operator is as follows: A and B must be lists of attributes
in r. The schema of countA/B (r) consists of all attributes in B plus one additional
attribute, which represents the counted value. The contents of countA/B (r) are dened
as follows: for each tuple, t B (r) (the projection on B), take A (B=t (r)) and count
the number of tuples in the resulting relation (where B=t (r) stands for the set of all
tuples in r whose value on the attributes in B is t). Let us denote this number by c(t).
Then the relation countA/B (r) is dened as {< t, c(t) >| t B (r)}.
You should be able to recognize the above construction as a straightforward
adaptation of GROUP BY of SQL to the relational algebra.
Solution:
First compute the total number of students in each course and the number of failures:
AuxCounts =
countStudId/{CrsCode,Semester}(Transcript)
[CrsCode,Semester,TotCount]
countStudId/{CrsCode,Semester}(Grade<2(Transcript))
[CrsCode,Semester,FailCount]
Computing the problematic classes is now easy:
CrsCode(FailCount/TotCount>0.2(AuxCounts))
5.12
State the English meaning of the following algebraic expressions (some of these queries
are likely to yield empty results in a typical university, but this is beside the point):
a. CrsCode,Semester (Transcript)/ CrsCode (Transcript)
Solution:
Semesters in which all courses (that were ever taught) were oered.
b. CrsCode,Semester (Transcript)/ Semester (Transcript)
Solution:
Courses oered in every semester.
Exercises
c. CrsCode,StudId (Transcript)/ (Id (Student))[StudId]
Solution:
Courses taken by every student.
d. CrsCode,Semester,StudId (Transcript)/ (Id (Student))[StudId]
Solution:
Classes (courses + semester) taken by all students.
5.13
S.Name
Student S, Transcript T
S.Id = T.StudId
AND T.CrsCode IN (CS305,CS315)
What does this query mean (express the meaning in one short English sentence)?
Write an equivalent SQL query without using the IN operator and the set construct.
Solution:
The equivalent SQL query to query (7.33) is:
SELECT
FROM
WHERE
S.Name
Student S, Transcript T
S.Id = T.StudId
AND (T.CrsCode = CS305 OR T.CrsCode = CS315)
The meaning of the query is: the names of all students who took CS305 or CS315.
5.14
Explain the conceptual dierence between views and bulk insertion of tuples into base
relations using the INSERT statement with an attached query.
Solution:
Tuples in a view are not materialized, i.e., they are computed on demand. In contrast,
when tuples are inserted into a relation using bulk INSERT they physically exist on the
disk.
5.15
5.16
Write query (5.38) on page 175 without the use of the views.
Solution:
SELECT
A.Dept
47
48
CHAPTER 5
FROM
WHERE
5.17
(SELECT
Express the following queries using SQL. Assume that the Student table is augmented
with an additional attribute, Age, and that the Professor table has additional
attributes, Age and Salary.
a. Find the average age of students who received an A for some course.
Solution:
Note: the following is incorrect, because some students ages might be counted more
than once:
SELECT
FROM
WHERE
AVG(S.Age)
Student S, Transcript T
SELECT
FROM
WHERE
T.CrsCode, MIN(S.Age)
Student S, Transcript T
GROUP BY T.CrsCode
c. Find the minimum age among straight A students per course among the students
who have taken CS305 or MAT123. (Hint: a HAVING clause might help.)
Solution:
Add the following HAVING clause to the above query:
Exercises
HAVING
T.CrsCode IN (CS305,MAT123)
d. Raise by 10% the salary of every professor who is now younger than 40 and who
taught MAT123 in the spring 1997 or fall 1997 semester. (Hint: Try a nested subquery
in the WHERE clause of the UPDATE statement.)
Solution:
UPDATE
SET
WHERE
Professor
Professor P, Teaching T
P.Id = T.ProfId
P.Age < 40
T.CrsCode = MAT123
(T.Semester = S1997
OR T.Semester = F1997) )
e. Find the professors whose salaries are at least 10% higher than the average salary
of all professors. (Hint: Use views, as in the HardClass example (5.39), page 183.)
Solution:
CREATE VIEW
SELECT
FROM
AvgSal(Avgsal) AS
AVG(P.Salary)
Professor P
P.Id
Professor P, AvgSal A
f. Find all professors whose salaries are at least 10% higher than the average salary of
all professors in their departments. (Hint: Use views, as in (5.39).)
Solution:
CREATE VIEW
SELECT
FROM
DeptAvgSal(DeptId, Avgsal) AS
P.DeptId, AVG(P.Salary)
Professor P
49
50
CHAPTER 5
P.Id
SELECT
FROM
WHERE
5.18
Professor P, DeptAvgSal D
OR DeptId= EE (Professor))
Name(CrsCode=CS305
5.19
Solution:
Id (Student) StudId (Transcript)
Write an equivalent expression in relational algebra for the following SQL query:
SELECT
FROM
WHERE
P.Name, C.Name
Professor P, Course C, Taught T
Solution:
Professor.Name,Course.Name (
Semester=S2002(
Professor Id=ProfId Taught CrsCode=CrsCode Course))
Exercises
5.20
Algebra:
StudId,ProfId (Transcript Teaching) / ProfId (Dept= MUS (Professor))
SQL:
SELECT
FROM
WHERE
5.21
R.StudId
Transcript R
NOT EXISTS (
(SELECT
FROM
WHERE
EXCEPT
(SELECT
FROM
WHERE
P.ProfId
Professor P
P.Dept = MUS)
T.ProfId
Teaching T
T.CrsCode = R.CrsCode
AND T.Semester = R.Semester)
Dene the above query as an SQL view and then use this view to answer the following
query: For each student who has taken a course from every professor in the MUS
Department, show the number of courses taken, provided that this number is more than
10.
Solution:
CREATE VIEW
SELECT
FROM
WHERE
Musician(StudId) AS
R.StudId
Transcript R
NOT EXISTS (
(SELECT
FROM
WHERE
EXCEPT
(SELECT
FROM
P.ProfId
Professor P
P.Dept = MUS)
T.ProfId
Teaching T
51
52
CHAPTER 5
T.CrsCode = R.CrsCode
AND T.Semester = R.Semester))
5.22
M.StudId = T.StudId
M.StudId
COUNT(*) > 10
Broker(Id, Name)
Write the following query in relational algebra and in SQL: Find the names of all
brokers who have made money in all accounts assigned to them (i.e., Gain > 0).
Solution:
Algebra: The algebraic solution has a pitfall and is harder than the SQL-based
solution. Note that the following is incorrect:
LoosingAccounts =
Acct#,BrokerId(Account) Acct#,BrokerId(Gain>0(Account))
The requisite query is then
SQL:
SELECT
FROM
WHERE
B.Name
Broker B
NOT EXISTS (
SELECT
FROM
WHERE
A.Acct#
Account A
Exercises
5.23
Write an SQL statement (for the database schema given in Exercise 5.22) to re all
brokers who lost money in at least 40% of their accounts. Assume that every broker
has at least one account. (Hint: Dene intermediate views to facilitate formulation of
the query.)
Solution:
BrokerLoosingNumber B2
WHERE
B1.Id = B2.Id
AND B2.Number >= 0.4 * B1.Number)
5.24
Consider the following schema that represents houses for sale and customers who are
looking to buy:
53
54
CHAPTER 5
a. Find all customers who are interested in every house listed with the agent with Id
007.
Solution:
SELECT
FROM
WHERE
C.Id
Customer C
NOT EXISTS (
(SELECT
FROM
WHERE
EXCEPT
(SELECT
FROM
WHERE
P.Feature
Preference P
P.CustId = C.Id)
A.Feature
Amenity A, House H
A.Address = H.Address
AND H.AgentId = 007) )
b. Using the previous query as a view, retrieve a set of tuples of the form
feature, number of customers, where each tuple in the result shows a feature
and the number of customers who want this feature such that
Only the customers who are interested in every house listed with Agent 007 are
considered.
The number of customers interested in feature is greater than three. (If this
number is not greater than three, the corresponding tuple feature, number of
customers is not added to the result.)
Solution:
CREATE VIEW
SELECT
FROM
WHERE
ClientOf007(CustId) AS
C.Id
Customer C
NOT EXISTS (
(SELECT
FROM
WHERE
EXCEPT
(SELECT
FROM
WHERE
P.Feature
Preference P
P.CustId = C.Id)
A.Feature
Amenity A, House H
A.Address = H.Address
AND H.AgentId = 007 )
SELECT
FROM
P.Feature, COUNT(*)
Preference P, ClientOf007 C
Exercises
WHERE
P.CustId = C.CustId
GROUP BY P.Feature
HAVING
COUNT(*) > 3
5.25
Consider the schema Person(Id, Name, Age). Write an SQL query that nds the
100th oldest person in the relation. A 100th oldest person is one such that there are
99 people who are strictly older. (There can be several such people who might have
the same age, or there can be none.)
Solution:
5.26
The last section of this chapter presented four main rules that characterize updatable
views in SQL. The third rule states that if the WHERE clause contains a nested
subquery, none of the tables mentioned in that subquery (explicitly or implicitly) can
be the table used in the FROM clause of the view denition.
Construct a view that violates condition 3 but satises conditions 1, 2, and 4
for updatability, such that there is an update to this view that has two dierent
translations into the updates on the underlying base relation.
Solution:
Consider two relation schemas, R(Att1,Att2) and S(Att1,Att2), and a view:
CREATE VIEW V(Att1)
SELECT
A.Att1
FROM
V A
WHERE
EXISTS (
SELECT *
FROM
S B
WHERE B.Att1 = A.Att1 AND B.Att2 = A.Att2 )
Here the base table of the view, R, is implicitly mentioned in the subquery (through
the tuple variable A). Now, suppose we want to delete a tuple of the form a, b from
V. There are two translations: to delete all tuples of the form a, from R or from S.
5.27
55
56
CHAPTER 5
Solution:
The view SmythCourses allows Professor Smyth to see only those rows of
Transcript corresponding to courses he has taught. However, since the view involves
several tables it would not be updatable although a change to a grade visible in the
view uniquely corresponds to a change in Transcript.
6
Database Design with the
Relational Normalization Theory
EXERCISES
6.1
The denition of functional dependencies does not preclude the case in which the lefthand side is emptythat is, it allows FDs of the form { } A. Explain the meaning
of such dependencies.
Solution:
This means that all tuples have the same value for the attribute A.
6.2
Give an example of a schema that is not in 3NF and has just two attributes.
Solution:
(AB; {} A, B A). Here B is a key, but {} A does not satisfy the 3NF
requirements.
6.3
6.4
CREATE ASSERTION FD
CHECK
(1 >= ALL (
SELECT COUNT(DISTINCT *)
FROM Abc
GROUP BY A ) )
6.5
Prove that every 3NF relation schema with just two attributes is also in BCNF. Prove
that every schema that has at most one nontrivial FD is in BCNF.
58
CHAPTER 6
Solution:
a. Suppose the schema R is in 3NF and that its attributes are AB. Then for every FD
X A F , either (1)A X , or (2) X is a superkey of R, or (3) A
X , X is not a
superkey, and A K , where K is a key of R. We will show that (3) is impossible in
our situation, which implies that R is in BCNF.
Indeed, if (3) holds, then either {A} = K or X is empty and K = AB. The rst
case is not possible because X A and A being a key implies that X is a superkey,
contrary to the assumption that it is not (this assumption is part of (3)). The second
case is impossible because { } A implies that B A, contrary to the assumption
that AB is a key (because it means that B alone is a key).
b. If R has exactly one non-trivial FD, X Y , X must be a superkey of R (if it is
not, then by augmentation it is possible to make another non-trivial FD, violating
the assumption). If R has no non-trivial FDs, the set of all attributes is its key and
the schema is, again, in BCNF.
6.6
6.7
Can a key be the set of all attributes if there is at least one nontrivial FD in a schema?
Solution:
No. Suppose a key k contained all attributes and there is a non-trivial FD X Y .
k contains X and Y . But the FD implies that X determines Y . Hence we can delete
Y (X Y ) from k and the remaining attributes still form a key. This means that k
was not minimal, a contradiction.
6.8
The following is an instance of a relation schema. Can you tell whether the schema
includes the functional dependencies A B and BC A?
1
2
1
4
2
2
3
2
3
2
2
3
Solution:
A B is not a FD; we cannot determine whether BC A is a FD.
6.9
Prove that Armstrongs transitivity axiom is soundthat is, every relation that
satises the FDs X Y and Y Z must also satisfy the FD X Z .
Solution:
Consider a relation r and an arbitrary pair of its tuples t, s r.
If X Y and t, s agree on every attribute of X , then t, s agree on every attribute
of Y . Because of Y Z and since t, s agree on every attribute of Y , they must agree
on every attribute of Z . Thus, every pair of tuples that agrees on X must also agree
on Z , so X Z holds.
Exercises
6.10
Using the argument that directly appeals to the denition of FDs, as in Section 6.4
Solution:
Let t, s be arbitrary tuples in a relation that satises the above conditions, and
suppose t, s agree on X . Then, due to X Y , they agree on Y and thus also on
Z . But then, due to Z W , t, s must agree on W .
Therefore, any pair of tuples that agrees on X in such a relation must also
agree on W , i.e., X W must hold.
(1)
(2)
(3)
(4)
(5)
(6)
6.11
Z W
Y WY
X Y
X WY
WY W
X W
Given
By Augmentation of (1) and because Z Y
Given
From (3),(2) by transitivity
By the Reflexivity rule
From (4),(5) by Transitivity
We have shown the soundness of the algorithm in Figure 6.3that if A closure then
+
+
A X F . Prove the completeness of this algorithm; that is, if A X F , then A closure
at the end of the computation. Hint: Use induction on the length of derivation of
X A by Armstrongs axioms.
Solution:
+
Since A X F and because of the completeness of the Armstrongs axioms, there must
be a derivation of X A. We prove that A closure by induction on the length, n,
of this derivation. More specically, the induction will show a more general claim: if
W V , where W closure, can be derived by Armstrongs axioms, then V closure.
Since X closure, the above would imply that if X A F+ then A closure.
Case n = 1: This is only possible if the rule used in such a 1-step derivation is
the Reexivity axiom or is an FD from F. In case of the Reexivity axiom, we must
already have V W , for some W closure, and so we conclude that V closure. In
case W V F, where W closure, V must be a subset of closure by construction:
during the iteration when W was determined to be in closure, the right-hand side of
the FD (i.e., V ) will be added.
Inductive step: Assume that, for any set V , if W V (where W closure) can
be derived via n steps, then V closure.
Suppose W V , where W closure, was derived using n + 1 derivation steps
(and no less). If the last rule in the derivation was Reexivity, then V W and, since
W closure, it follows that V closure.
If the last step was Augmentation, then W U (U
= ) was derived at step n or
earlier, and then augmented with a nonempty set of attributes Z , such that W Z = W
59
60
CHAPTER 6
6.13
R (r) . . . Rn (r) =
1
Clearly, every tuple in r satises the above conditions and thus is in the above set.
6.14
1
3
0
0
2
4
Clearly, this relation satises the conditions in the statement of the problem. It is easy
to see that the join R (r) R (r) contains (among others) the tuple 1, 0, 4, which
1
2
is not in r.
6.15
Exercises
Solution:
Proof by induction on n.
6.16
Prove that the loop in the BCNF decomposition algorithm of Figure 6.9 has the
property that the database schema at each subsequent iteration has strictly fewer FDs
that violate BCNF than has the schema in the previous iteration.
Solution:
Let S = (S ; F) be a schema that is not in BCNF. First, we have to clarify which sets
of FDs possess the properties stated in the exercise. Here we are talking about the set
of all entailed FDs, not the FDs in F. It is easy to nd examples where the number of
violating FDs can increase temporarily if we were just looking at the FDs in F rather
than F+ .
To prove the required result, observe that during each iteration, when there is an
FD X Y , which violates BCNF, the algorithm decomposes the original schema S =
(S ; F) into S1 = (XY ; F1 ) and S2 = (S (Y X ); F2 ). The set of FDs in F1+ F2+ is
a subset of F+ , so it does not have any additional FDs that violate BCNF. Moreover,
X Y is in F1 , but not in F2 , and it no longer violates BCNF, since X is a superkey
in S1. Therefore the number of violating FDs has decreased.
Note that both S1 and S2 can have other FDs that still violate BCNF, so the
decomposition process must continue until none is left.
6.17
Prove that the algorithm for synthesizing 3NF decompositions in Section 6.8.2 yields
schemas that satisfy the conditions of 3NF. (Hint: Use the proof-by-contradiction
technique. Assume that some FD violates 3NF and then show that this contradicts
the fact that the algorithm synthesized schemas out of a minimal cover.)
Solution:
Suppose, to the contrary, that one of the schemas, say (X A; {X A, . . .}), which
results from the synthesis using the FD set F, is not in 3NF. Then there must be
Y B F+ , where Y B X A, which violates the 3NF conditions.
If A = B, then Y X . However, this means that the FD X A cannot be in the
minimal cover of F, because Y X and so Y A holds and has a smaller left-hand
side.
If A
= B, then B X and B but it cannot be part of a key (because otherwise
Y B would not have violated 3NF). Thus, there is a key, W , of X A such that
61
62
CHAPTER 6
6.19
Consider a relation schema with attributes ABCGWXYZ and the set of dependencies F = {XZ ZYB, YA CG, C W , B G, XZ G}. Solve the following
problems using the appropriate algorithms.
a. Find a minimal cover for F.
Solution:
Minimal cover: After splitting the right-hand sides, we get XZ G plus the following
set:
XZ Y
XZ B
YA C
YA G
C W
B G
It is easy to see that the left-hand sides XZ and YA (as well as C and B) cannot be
reduced because there are no FDs of the form X . . ., Y . . ., Z . . ., A . . .,
or { } . . ..
Then, we notice that XZ G is redundant and none of the other FDs is, so
the minimal cover consists of the above siz FDs.
Exercises
b. Is the dependency XZA YB implied by F?
Solution:
Yes, because XZA+ = XZAYBCWG, which includes YB.
c. Is the decomposition into XZYAB and YABCGW lossless?
Solution:
Yes, because XZYAB YABCGW = YAB and YAB + = YAMCGW .
d. Is the above decomposition dependency preserving?
Solution:
Yes, because every FD in the above minimal cover is embedded into either XZYAB
or YABCGW .
6.20
Consider the following functional dependencies over the attribute set ABCDEFGH :
AE
BE D
AD BE
BDH E
AC E
FA
EB
DH
BG F
CD A
Find a minimal cover, then decompose into lossless 3NF. After that, check if all the
resulting relations are in BCNF. If you nd a schema that is not, decompose it into a
lossless BCNF. Explain all steps.
Solution:
Minimal cover: First split AD BE into AD B and AD E. The next step is to
reduce the left-hand sides of the FDs. Since A B and A E are implied by the
given set of FDs, we can replace AD B, AD E, and AC E in the original set
with A B (A E already exists in the original set). Likewise E D and BD E
can be derived from the original set so we can replace BE D and BDH E with
E D and BD E.
In the next step, we remove redundant FDs, of which we nd only A B. The
nal result is therefore
AE
EB
BG F
ED
BD E
FA
DH
CD A
Lossless 3NF:
(AE; {A E}), (EBD ; {BD E, E D , E B}),
(BGF ; {BG F }), (FA; {F A}), (DH ; {D H }), (CDA; {CD A).
63
64
CHAPTER 6
Since BGF is a superkey of the original schema, we do not need to add anything
to this decomposition.
The schema (BGF ; {BG F }) is not in BCNF because F B is entailed by the
original set of FDs and (CDA; {CD A) is not in BCNF because A D is entailed.
We decompose BGF with respect to F B into (BF ; {F B}) and (GF ; {}),
thereby loosing the FD BG F .
Similarly CDA is decomposed using A D into (AD ; {A D }) and (AC ; {}),
loosing CD A.
6.21
E HG
C FG
GA
Solution:
AF
E AF
6.22
Consider the schema with the attribute set ABCDEFH and the FDs depicted in (6.12),
page 224. Prove that the decomposition (AD ; A D ), (CE; C E), (FA; F A),
(EF ; E F ), (BHE; BH E) is not lossless by providing a concrete relation instance
over ABCDEFH that exhibits the loss of information when projected on this schema.
Solution:
A B
D E
a
a
a
a
a
b1
a
b3
b4
b5
a
a
a
a
a
b1
a
a
a
a
b1
b2
b3
b4
a
b1
b2
b3
b4
a
b1
a
b3
a
a
This relation satises all the FDs. It is easy to verify, however, that the tuple a a a
a a a a belongs to the join of the projections of this relation on the above schemas,
but it is not one of the tuples in this relation. Therefore, this relation exhibits a case
where information loss occurs due to the decomposition.
6.23
Exercises
C G (and is eliminated, since it is a duplicate), CD H reduces to C H , and
BG D reduces to G D . Thus, after the second step we end up with
BG C
GD
GF
CH
CF
CG
FD
The last stage eliminates the redundant FDs and we end up with:
BG C
GF
CH
CG
FD
A lossless, dependency-preserving decomposition into 3NF: (BGC ; {BG
C , C G}), (GF ; {G F }), (CGH ; {C GH }), (FD ; {F D }).
The schema (BGC ; {BG C , C G}) is not in BCNF. A decomposition with
respect to C G into (CG; {C G}) and (CB; {}) brings it into BCNF, but looses
the FD BG C .
6.24
Prove that all rules for inferring FDs and MVDs given in Section 6.10 are sound. In
other words, for every relation, r, which satises the dependencies in the premise of
any rule, R, the conclusion of R is also satised by r (e.g., for the augmentation rule,
Y holds in r then X Z
Y also holds in r).
prove that if X
Solution:
The proof uses a technique, which we used for proving the soundness of Armstrongs
axioms. However, the reasoning is much more involved. To illustrate, we prove the
soundness of the transitivity axiom
X
Y and Y
Z entail X
Z Y.
Most of the other proofs are every bit as tedious.
Let R be the set of all attributes in the schema. Then we have the following binary
join dependencies:
JD1:
JD2:
R = XY X (R Y )
R = YZ Y (R Z )
We need to show that every relation, r, that satises these two JDs also satises the
following JD:
65
66
CHAPTER 6
X (Z Y ) X (R (Z Y ))
To see this, let t be a tuple over R such that X (Z Y ) (t) X (Z Y )(r) and
X (R(Z Y )) (t) X (R(Z Y ))(r). To prove the soundness of the inference rule we
need to show that t r.
The proof relies on the result of exercise 6.31(a). According to this result, the
projection of JD1 on XYZ holds in XYZ (r).
Now notice that the intersection of X (Z Y ) and XYZ is X (Z Y ) and the
intersection of X (R (Z Y )) with XYZ is XY . Also notice that X (Z Y ) is the
intersection of XYZ with X (R Y ).
This means that X (Z Y ) (t) X (Z Y )(r) (as we saw earlier) and XY (t)
XY (r). Since the projection of JD1 on XYZ holds in XYZ (r), it means that
XYZ (t) XYZ (r). In particular, YZ (t) YZ (r).
Since X (R(Z Y ))(t) X (R(Z Y ))(r) (by assumption) and R (Z Y ) =
Y (R Z ), it follows that both YZ (t) YZ (r) and Y (RZ ) (t) Y (RZ )(r). Because
JD2 holds in r, it follows that t r, which completes the proof.
The above proof (and the proofs for other rules) can be simplied somewhat by
making the left and right hand sides of the MVDs disjoint. This is possible because it
is easy to show (directly from the denitions) that the MVD X
Y is equivalent to
X
(Y X ).
6.25
Prove that if a schema, S = (S , F), is in 3NF, then every FD in F+ (not only those
that are in F) satises the 3NF requirements.
Solution:
Suppose to the contrary, that there is an FD X A (F+ F) such that X is not a
superkey, A does not belong to any key of S, and the FD is non-trivial, i.e., A
X .
+
+
As we know, it must be the case that A X F and X F should be computable by the
attribute closure algorithm in Figure 6.3. The main step in that algorithm hinges on
+
being able to nd an FD Z V F such that Z closure. Since closure X F and X
is not a superkey, none of the FDs in F whose left-hand side is a superkey can be used
in that algorithm. Therefore, Z V must be one of those FDs where the attributes
of V belong to the various keys of S. But in order to add A to closure, it must belong
to V for some such FD, i.e., A itself must be an attribute of one of the keys in S a
contradiction.
6.26
6.27
(a) AB C
(b) A BE
(c) C D
Exercises
give a complete derivation of the FD A D .
Solution:
(1)
(2)
(3)
(4)
(5)
(6)
6.28
A BE using (b)
BE B using Reflexivity
A B using Transitivity on (1) and (2)
A AB using Augmentation on (3)
A C using Transitivity on (4) and (a)
A D using Transitivity on (5) and (c)
6.29
67
68
CHAPTER 6
Solution:
The decomposition is dependency preserving because each of the FDs of R is a FD
of one of the component relations.
6.30
6.31
Exercises
6.32
Consider a relation schema over the attributes ABCDEFG and the following MVDs:
ABCD DEFG
CD ABCEFG
DFG ABCDEG
Find a lossless decomposition into 4NF.
Solution:
Use the rst MVD to obtain the following decomposition: ABCD , DEFG. The second
MVD still applies to ABCD and decomposes it into CD , ABC . The third MVD applies
to DEFG and decomposes it into DFG, DGE. Thus, the result is CD , ABC , DFG,
DGE.
6.33
6.34
6.35
This exercise relies on a technique explained in the optional Section 6.10. Consider a
relation schema over the attributes ABCDEFGHI and the following MVDs and FDs:
D AH
D
BC
69
70
CHAPTER 6
C
B
G
ABCE
D AH
G I
DAH
BC
C
B
GI
ABCE
The 4NF decomposition algorithm rst yields ABCDH , ADHEFGI . Then ABCDH
is split into CB, CADH . ADHEFGI is split by the third MVD into GIAE, GIDHF .
Thus, after the 4NF decomposition stage we get: CB, CADH , GIAE, GIDHF .
The last three schemas are not in 3NF, due to the FDs. A lossless, dependencypreserving 3NF decomposition of CADH is DAH , DC . A 3NF decomposition of GIAE
is GI , GAE, and A 3NF decomposition of GIDHF is DH , GI , DGF . Since DAH and
DH have the same key, we can eliminate DH and obtain the following nal result: CB,
DC , DAH , GI , GAE, DGF .
6.36
Solution:
1.
2.
7
Triggers and Active Databases
EXERCISES
7.1
Explain the semantics of the triggers that are available in the DBMS that is used for
your course project. Describe the syntax for dening these triggers.
Solution:
Depends on the system. For instance, IBMs DB/2 has the same semantics as the SQL
standard.
7.2
Give the exact syntactic rules for constructing the triggering graphs from the sets of
SQL triggers and foreign-key constraints.
Solution:
For each SQL trigger the activating event is an update, deletion, or insertion
of/from/into the relation specied in the CREATE TRIGGER . . . BEFORE/AFTER . . .
ON Table clause. Whether the actually event must be an update, deletion or insertion
depends on what is specied in the BEFORE/AFTER clause. The events produced by the
trigger are updates, insertions, and deletions on the relations specied in the action
part of the trigger. (The exact type of the event depends on the form of the action
specied.)
Note that a trigger with the FOR EACH STATEMENT clause produces only one
event, while a trigger with the FOR EACH ROW clause produces one event per
updated/inserted/deleted tuple (whichever applies).
For a foreign-key constraint, the activating event is any update or deletion that
involves the relation mentioned in the REFERENCES clause and the events produced
are updates or deletions on the relation mentioned in the CREATE TABLE statement,
which this foreign key is part of.
The triggering graph then consists of the edges among all these triggers and
foreign-key constraints. An edge is drawn from one trigger/constraint T to another,
S , if T produces an event that activates S .
7.3
72
CHAPTER 7
CrsCode = O.CrsCode ) )
ROLLBACK
7.4
CrsCode = O.CrsCode ) )
DELETE FROM Teaching
WHERE Semester = O.Semester AND
CrsCode = O.CrsCode
7.5
Dene the trigger WatchCourseHistory that uses a table Log to record all changes
that transactions make to the various courses in the Course relation.
Solution:
Exercises
SELECT CURRENT_DATE, NEW, * FROM NewT
END
7.6
Dene triggers that re when a student drops a course, changes her major, or when
her grade average drops below a certain threshold. (For simplicity, assume that there
is a function, grade_avg(), which takes a student Id and returns the student average
grade.)
Solution:
The action part of these triggers is uninterestingjust an insertion of a record into
the log table, as we saw in earlier problems. We give an example of the last trigger.
CREATE TRIGGER WatchGPA
AFTER UPDATE, DELETE, INSERT ON Transcript
FOR EACH STATEMENT
WHEN (EXISTS (
SELECT StudId FROM Transcript T
WHERE grade_avg(StudId) < 3.0 ) )
INSERT INTO Log
SELECT DISTINCT StudId, grade_avg(StudId)
FROM Transcript T
WHERE grade_avg(StudId) < 3.0
7.7
73
74
CHAPTER 7
7.8
Consider a brokerage rm database with relations Holdings(AccountId, StockSymbol, CurrentPrice, Quantity) and Balance(AccountId, Balance). Write the
triggers for maintaining the correctness of the account balance when stock is bought
(a tuple is added to Holdings or Quantity is incremented), sold (a tuple is deleted
from Holdings or Quantity is decremented), or a price change occurs.
Solve the problem using both row-level and statement-level triggers. Give an
example of a situation when row-level triggers are more appropriate for the above
problem and when statement-level triggers are more appropriate.
Solution:
The above trigger is appropriate for real-time updates of Holdings, so the balances
are also updated in real time. If Holdings is updated only periodically (e.g., every
day), then a statement level trigger would be more ecient. This trigger can work by
erasing the old contents of Balance and then recomputing it from scratch:
CREATE TRIGGER UpdateBalanceAllAtOnce
BEFORE AFTER INSERT, DELETE, UPDATE ON Holdings
FOR EACH STATEMENT
BEGIN
-- Erase
DELETE FROM Balance;
INSERT INTO Balance
SELECT DISTINCT H.AccountId,SUM(H.CurrentPrice*H.Quantity)
FROM Holdings H
GROUP BY H.AccountId
END
7.9
Exercises
Solution:
CREATE TABLE
Contract
PartId
SupplierId
ProjectId
PRIMARY KEY
FOREIGN KEY
FOREIGN KEY
FOREIGN KEY
FOREIGN KEY
INTEGER,
INTEGER,
INTEGER,
In addition, we have a table Supplies, which tells which supplier supplies what,
and the relations Part, Project, and Supplier, which describe the corresponding
entities.
CREATE TRIGGER ProjectChangedSupplier
BEFORE UPDATE OF SupplierId ON Contract
REFERENCING NEW AS N
FOR EACH ROW
Log
INSERT INTO
VALUES
(CURRENT_DATE, Supplier Change, ProjectId,
75
76
CHAPTER 7
7.10
Consider triggers with immediate consideration and deferred execution. What do OLD
AS and NEW AS refer to during consideration and during execution?
Solution:
They refer to the state of the updated tuple just before the triggering event and just
after it. It does not matter whether the execution is immediate or deferred.
7.11
Give an example of an application where SQL triggers could be used for a purpose
other than just maintaining integrity constraints.
Solution:
When something unusual happens during the execution of a transaction, a trigger can
be used to initiate another transaction, which will report the unusual event to the
system administrator or take a corrective action.
8
Using SQL in an Application
EXERCISES
8.1
Explain why a precompiler is needed for embedded SQL and SQLJ but not for ODBC
and JDBC.
Solution:
In embedded SQL and SQLJ, SQL statements are included as statements that would
not be recognized by the host language compiler. In ODBC and JDBC, SQL statements
are included as arguments to procedure calls, which can be processed by the host
language compiler.
8.2
Since the precompiler for embedded SQL translates SQL statements into procedure
calls, explain the dierence between embedded SQL and call-level interfaces, such as
ODBC and JDBC, where SQL statements are specied as the arguments of procedure
calls.
Solution:
With embedded SQL, the actual SQL statements to be executed are known at compile
time while with the call-level interfaces, the SQL statement might not be known
at compile time. They are just string variables that can be computed at run time
and passed as arguments to host language procedures provided by the CLI. Thus in
embedded SQL, the syntax of the SQL statements can be checked at compile time,
and host language variables can be used as parameters of the SQL statements.
8.3
8.4
78
CHAPTER 8
8.5
8.6
Stored procedures can reduce the communication between server and client
because only the arguments and the results need be communicated.
The statements within a stored procedure will have been prepared before execution
of that procedure and will execute more eciently.
The checking of authorization privileges can be performed by the DBMS at the
level of the stored procedure.
The application programmer need not know the details of the database schema,
since all database accesses can be encapsulated within the procedure body.
Maintenance of the system is simplied since only one copy of the procedure,
stored on the server, need be maintained and updated.
The physical security of the code for the procedure is higher because it is stored
on the server rather than with the application program.
A possible disadvantage is that since preparation is done in advance, the database
might have changed and the selected query execution plan might be inecient.
However, most systems allow preparation to be done over again at the discretion
of the programmer or system administrator.
8.7
8.8
Exercises
b. What are the advantages of using host language variables as parameters in embedded
SQL compared with ? placeholders?
Solution:
Syntax checking can be done at compile time, and less communication is required
at run time.
8.9
Explain the advantages and disadvantages of using dynamic SQL compared with
ODBC and JDBC.
Solution:
1.
2.
8.10
Advantages: Dynamic SQL can use all the proprietary extensions to SQL provided
by the special DBMS, while JDBC and ODBC can not;
Disadvantages: In ODBC, the SQL statements use a generic dialect. Likewise, in
JDBC, a common core of SQL is guaranteed to be supported by all drivers. If the
application is moved to a dierent DBMS, only the the driver need be changed. (In
JDBC, the application should be restricted to the common SQL core). In contrast,
with dynamic SQL the actual SQL statements might have to be rewritten.
8.11
Write a Java program that executes in your local browser, uses JDBC to connect to
your local DBMS, and makes a simple query against a table you have created.
Solution:
This is a hands-on problem for which the solution is not meant to be given in this
manual.
8.12
Suppose that, at compile time, the application programmer knows all of the details of
the SQL statements to be executed, the DBMS to be used, and the database schema.
Explain the advantages and disadvantages of using embedded SQL as compared with
JDBC or ODBC as the basis for the implementation.
Solution:
Embedded SQL is more ecient than JDBC and ODBC because the syntax can be
checked at compile time and parameter passing is more ecient. Also there is less
communication involved.
8.13
79
80
CHAPTER 8
to those tuples. Also KEYSET_DRIVEN cursers can be used to update data while
STATIC cursors cannot.
b. Give an example of a schedule in which these cursors give dierent results even
when the transaction is executing in isolation.
Solution:
OPEN a cursor, UPDATE not through the cursor, FETCH through the cursor
If the cursor is STATIC, the FETCH cannot see the eect of the UPDATE. If the cursor
is KEYSET_DRIVEN. the FETCH could see the eect of the UPDATE.
c. Explain why updates and deletes can be made through KEYSET_DRIVEN cursors.
Solution:
The cursor returns a set of pointers to the rows in the result set. Hence updates and
deletes can be made through these pointers.
8.14
Give an example of a transaction program that contains a cursor, such that the value
returned by one of its FETCH statements depends on whether or not the cursor was
dened to be INSENSITIVE. Assume that this transaction is the only one executing. We
are not concerned about any eect that a concurrently executing transaction might
have.
Solution:
Since the cursor is INSENSITIVE, it does not see the eect of the INSERT. If it were not
INSENSITIVE, it could see the eect.
8.15
PART THREE
9
Physical Data Organization
and Indexing
EXERCISES
9.1
State the storage capacity, sector size, page size, seek time, rotational latency, and
transfer time of the disk
a. On your local PC
b. On the server provided by your university
Solution:
This is a hands-on problem. Its solution is not meant to be included in the manual.
9.2
9.3
a. Give an upper bound on the number of pages that must be fetched to perform a
binary search for a particular name in the phone book for your city or town.
Solution:
If the phone book has 1,000 pages, an upper bound on the number of pages that
would have to be fetched is log2 1000 = 10.
b. By how much is this number reduced if an index is prepared giving the name of the
rst entry on each page of the phone book? (Does that index t on one page of the
phone book?)
Solution:
Our local phone book has approximately 625 names on each page (in 5 columns),
so if the phone book has 1,000 pages, the index would t on 2 (phone book) pages.
Hence, 2 page fetches (at most) would be needed to search the index and one
additional fetch to get the desired page.
84
CHAPTER 9
c. Conduct an experiment using your usual (informal) method to search for the name
John Lewis in your local phone book, and compare the number of pages you look
at with the number in Exercise 3(a).
Solution:
This is a hands-on problem. Its solution is not meant to be included in the manual.
9.4
9.5
9.6
Does the nal structure of a B+ tree depend on the order in which the items are added
to it? Explain your answer and give an example.
Solution:
Yes, because when an insertion causes a split of a leaf page, the entry to be inserted
in the parent node depends on the order in which items have been added to the tree.
9.7
Starting with an empty B+ tree with up to two keys per node; show how the tree
grows when the following keys are inserted one after another:
18, 10, 7, 14, 8, 9, 21
Solution:
See Figure 9.1.
9.8
9.9
Consider the B+ tree in Figure 9.35. Suppose that it was obtained by inserting a key
into a leaf node of some other tree, causing a node split. What was the original tree
and the inserted key? Is the solution unique? Explain your answer.
Exercises
1. insert 18:
2. insert 10:
18
3. insert 7:
18
18
18
10
18
4. insert 14:
7 10
5. insert 8:
14 18
7 10
14
14
18
10
6. insert 9:
9 10
18
10
14
18
6. insert 21:
14
18
10
14
18
9 10
14
18
10
18
14
18 21
FIGURE 9.1
Solution:
The solution is not unique. The inserted key could be 17, or 19. The possible original
trees are depicted in Figures 9.5 and 9.6.
9.10
Describe a search algorithm for a B+ tree in which the search key is not a candidate
key. Assume that overow pages are not used to handle duplicates.
Solution:
Descend through the tree using the target key value, k0 . At each level (including
leaf) multiple entries in the page can contain the same key value that is ki =
ki +1 = . . . = ki +r .
85
86
CHAPTER 9
bb
acc
abc
bce
acc
bb
bcd
bce
bxy
FIGURE 9.2
bb
acc
abc
bcd bce
acc
bb
bbb
bcd
bce
bxy
FIGURE 9.3
When the leaf level is reached, scan forward using sibling pointers until a key, k , is
reached such that k > k0 .
9.11
Exercises
bcd
bb
bce
acc
bb
bbb
bcd
bce
bxy
FIGURE 9.4
16
10
15
19
20
17
20
FIGURE 9.5
10
15
16
FIGURE 9.6
hash function that is not partitioned would require that all buckets be searched. The
same benet applies to range searches, although the range information for a particular
attribute cannot be used.
9.12
Starting with the B+ tree shown in Figure 9.22 show the successive B+ trees that
result from deleting pete, phil, mike and nan.
Solution:
Figure 9.7 shows the portion of the tree that has been changed at various stages in the
87
88
CHAPTER 9
mike
judy
karen
nan
mike
nan
(a)
mike
judy
karen
nan
(b)
karen
judy
karen
(c)
FIGURE 9.7 Various stages of the portion of the B+ tree shown in Figure 9.22 that is
process: after pete and phil have been deleted (a), after mike has been deleted (b),
and after nan has been deleted (c).
9.13
Give the pseudocode for the procedure redistributeright called by the procedure
Exercises
Solution:
Express the algorithm for insertion and deletion of index entries in the extendable
hashing schema using pseudocode.
Solution:
proc insert(ent )
//
//
//
//
//
if bucket D [hch (ent )] is not full then store ent in that bucket;
else
// D [hch (ent )] is full, call that bucket Bi
if bl [i ] < ch then
create a new bucket, Bi+2ch1 ;
D [i + 2ch1 ] = &(Bi+2ch1 );
// store pointer to new bucket in directory
//rehash the elements of Bi to Bi and Bi+2ch1
put each element, e , of Bi in D [hch (e)];
bl [i ] and bl [i + 2ch1 ] are set to bl [i ] + 1;
insert(ent ); return;
else
//bl [i ] = ch
if ch = b then halt;
else
// split directory
ch := ch + 1;
(i )(2ch1 i < 2ch ) D [i ] := D [i 2ch1 ];
(i )(2ch1 i < 2ch ) bl [i ] := bl [i 2ch1 ];
insert(ent ); return;
89
90
CHAPTER 9
edie rick
bob
alice
abe
judy
debbie
al
bob
alice betty
carol
jane
edie edith
debbie
jane
tom
mike pete
judy karen
joe
mike
pete phil
nan
sol
rick
rob
vince
tom
vera
vince zelda
sol
FIGURE 9.8
endproc
9.15
9.16
Draw the B+ tree that results from inserting alice, betty, carol, debbie, edith,
and zelda into the index of Figure 9.22 on page 358.
Solution:
See Figure 9.8.
9.17
A particular table in a relational database contains 100,000 rows, each of which requires
200 bytes of memory. A SELECT statement returns all rows in the table that satisfy
an equality search on an attribute. Estimate the time in milliseconds to complete
the query when each of the following indices on that attribute is used. Make realistic
estimates for page size, disk access time, and so forth.
a. No index (heap le)
Exercises
Solution:
Assume a page size of 4K bytes and a page access time of 20ms. The data le
occupies 5000 pages and all pages will have to be scanned. Hence, the query will
require 100 sec.
b. A static hash index (with no overow pages)
Solution:
20ms.
c. A clustered, unintegrated B+ tree index
Solution:
If we assume that each entry in the index occupies 100 bytes then an index page
can hold 40 entries. Since the data le occupies 5000 pages, the leaf level of the
tree must contain at least 5000/40, or 125 pages. Then the number of levels in the
tree (assuming page 75% occupancy in the index) is log30125 + 1 = 3. Assume
that the index is clustered, not integrated with the data le and that all matching
entries are in a single page, 4 I/O operations and 80ms are required to retrieve all
matching records.
9.18
Estimate the time in milliseconds to insert a new row into the table of Exercise 9.17
when each of the following indices is used:
a. No index (le sorted on the search key)
Solution:
Use a binary search to locate the page in which the new row is to be inserted and
assume that there is room in the target page. The data le occupies 5000 pages and
the search will require retrieving log25000 = 13. An additional I/O operation
is required to write the page back to mass store, hence 14 I/O operations taking
280ms are required.
b. A static hash index (with no overow pages)
Solution:
Assuming once again that the target bucket has room for the new row, 2 I/O
operations and 40ms are required.
c. A clustered, unintegrated B+ tree index (with no node splitting required)
Solution:
As in Exercise 9.17, 4 I/O operations are required to retrieve the page into which
the new row must be inserted. Two additional I/O operations are required to update
the leaf page of the index and the data page. Hence, the time to do the insertion is
120ms.
9.19
Estimate the time in milliseconds to update the search-key value of a row in the table
of Exercise 9.17 when each of the following is used:
a. No index (le sorted on the search key)
Solution:
As in Exercise 9.18 it takes 280ms to locate the row to be updated and another 20ms
to delete the row from the page. Another 280+20ms is required to locate and update
the new page in which the row is to reside. Hence a total of 600ms is required.
91
92
CHAPTER 9
b. A static hash index (where the updated row goes in a dierent bucket than the
original rows page, but no overow pages are required)
Solution:
40ms to update the old bucket and 40ms to update the new bucket.
c. A clustered, unintegrated B+ tree index (where the updated row goes on a dierent
page than the original rows pages, but no node splitting is required)
Solution:
Using the answer to Exercise 9.18 it takes 120ms to delete the row and update the
index and another 120ms to insert the updated row and update the index. Hence,
240ms are required.
9.20
Estimate the amount of space required to store the B+ tree of Exercise 9.17 and
compare that with the space required to store the table.
Solution:
The minimum number of pages in the tree (assuming all pages are full) is 41 (for the
rst two levels) plus 125 leaf pages = 166 pages. The data le occupies 5000 pages.
9.21
Explain what index types are supported by your local DBMS. Give the commands
used to create each type.
Solution:
Sybase supports B trees with the command:
Design the indices for the tables in the Student Registration System. Give the rationale
for all design decisions (including those not to use indices in certain cases where they
might be expected).
Solution:
This is a hands-on problem. Its solution is not meant to be included in the manual.
9.23
Exercises
Solution:
The page in which a row is placed depends on the value of the search key in the
row. If that page is full, the le has to be reorganized. Using a ll factor of less than
1 increases the probability that there will be room in the page for the row.
b. ISAM indices
Solution:
By using a ll factor of less than 1, the probability that overow pages will be
needed is reduced.
c. B+ tree indices
Solution:
By using a ll factor of less than 1, the probability that page splits will be needed
is reduced.
d. Hash indices
Solution:
By using a ll factor of less than 1, the probability that a bucket will overow is
reduced.
9.24
Design an algorithm for maintaining join indices incrementallythat is, so that the
addition of new tuples to the relations involved in the join do not require the entire
join to be recomputed from scratch.
Solution:
Consider a join index for an equi-join of tables A and B. Implement the index as an
integrated B+ tree. The search key is the index into A of a particular row. Each leaf
entry contains a pair < p, q > where p is a search key value and q is the index of a
row in B that satises the equi-join condition with respect to the row with index p in
A. When a row with index a is added to A the index of each row, with index b, in B
that joins with it is computed and the entry < a, b > is added to the tree. Computing
the equi-join involves scanning the leaf level once. Since all entries corresponding to a
particular row of A are in sequence at that level, only a single scan of A is required.
The number of accesses to B depends on the number of rows in the equi-join.
Adding a row to B also requires that we rst nd the rids of all the matching
tuples in A and then insert the appropriate pairs of rids in the join index.
If a row is deleted from A, we can nd all the index entries for that row in the B+
tree and delete them. Deleting the rows of B requires more work because we cannot
easily nd the corresponding index entries in the join index, since the search key of
the B+ tree has the rid of A, not of B, as the rst component. Thus, we might have to
scan all the leaves of that tree in order to nd the index entries to delete.
9.25
Propose an improvement that permits bitmap indices to maintain only n 1 bit vectors
in order to represent attributes that can take n dierent values. Discuss the change
that is needed to perform selection, especially in case of a multiattribute selection that
requires a Boolean operation on multiple bit vectors.
Solution:
Consider an attribute, A, that can take n values. We can use n 1 vectors for the rst
n 1 values of A. For the n-th value, we can always reconstruct the corresponding
93
94
CHAPTER 9
bit vector using the following Boolean expression: vector1 . . . vectorn1 . This
vector has 1 exactly for the rows where A has the value that is dierent from any of
the rst n 1 values.
10
The Basics of Query Processing
EXERCISES
10.1
Consider the use of unclustered B+ trees for external sorting. Let R denote the number
of data records per disk block, and let F be the number of blocks in the data le.
Estimate the cost of such a sorting procedure as a function of R and F . Compare this
cost to merge-based external sorting. Consider the cases of R = 1, 10, and 100.
Solution:
The cost of using unclustered B+ trees is 2R F because we have to scan the leaves
of the B + tree and for each entry there to access the corresponding data le block
twice (to read and then to write). When R increases, the cost increases.
Suppose we have M pages of main memory in which to do the sorting and that we
use k-way merge. The cost of merge-based external sorting is 2F logk F /M . This
cost does not depend on R.
When R = 1, B+ tree sorting is slightly better. For R = 10, merge-sort is likely to
be better even for large les. For instance, if M = 10, 000 and R = 100, 000, 000 (i.e.,
400G), then we can do 999-way merge and
log999(100000000/10000) < 2 < R = 10
10.2
Estimate the cost of the sort-based projection assuming that, during the initial scan
(where tuple components are deleted), the size of the original relation shrinks by the
factor < 1.
Solution:
The technique rst scans the original relation and removes the tuple components that
are to be projected out. The cost of this operation is of the order of (1 + )F , where
F is the number of blocks in the relation. (F to read and F to write.)
Then the result is sorted at the cost of
2F log(M 1)F /M
where M is the number of main memory pages available for sorting and F is the
number of blocks in the le.
Finally, we scan the result again, at the cost of 2F , and since identical tuples
are right next to each other (because the relation is sorted), we can easily delete the
duplicates during this last scan.
96
CHAPTER 10
10.3
Consider hash-based evaluation of the projection operator. Assume that all buckets
are about the same size but do not t in main memory. Let N be the size of the hash
table measured in memory pages, F be the size of the original relation measured in
pages, and < 1 be the reduction factor due to projection. Estimate the number of
page transfers to and from the disk needed to compute the projection.
Solution:
In the rst phase, the original relation is scanned. During the scan, we chop o the
tuple components that are to be projected out, and the rest of the tuple is hashed
on the remaining attributes. Whenever a page of the hash table becomes full, it is
ushed to the corresponding bucket on disk. The cost of this operation is of the order
of (1 + )F , where F is the number of blocks in the relation.
Clearly, duplicate tuples are always hashed into the same bucket, so we can
eliminate duplicates in each bucket separately. This elimination is done in the second
phase of the algorithm. Because the individual buckets do not t in the main memory,
they must be sorted using external sorting. There are N buckets and for each bucket,
the cost is: 2 log(M 1)B/M , where B is the average size of each bucket measured
in memory pages, and M is the number of main memory pages available to sort the
buckets. Since all buckets are approximately the same in size, B = F /N . The total
cost of this step is: 2N log(M 1)F /(MN )).
10.4
Give an example of an instance of the Transcript relation (Figure 3.5) and a hash
function on the attribute sequence StudId, Grade that sends two identical tuples
in StudId,Semester (Transcript) into dierent hash buckets. (This shows that such a
hash-based access path cannot be used to compute the projection.)
Solution:
Consider the following two tuples: 111111111, EE101, F1997, A and
111111111,MAT123,F1997,B. Since these tuples have identical projections on
the attributes StudId,Semester, they become one in the projected relation
StudId,Semester (Transcript).
Consider now the following hash function on StudId,Grade:
f (t) = (t.StudId + (t.Grade A)) mod 1000
The rst tuple will then be sent to the bucket 111111111 mod 1000 = 111 and the
second to 111111112 mod 1000 = 112.
10.5
Clearly, the theoretical minimum for the selectivity of an access path is the number
of pages that hold the output of the relational operator involved. What is the best
theoretical upper bound on the selectivity of an access path when selection or projection
operators are involved?
Solution:
The theoretical upper bound on the selectivity of an access path when selection or
projection operators are involved is the number of pages in the original relation.
10.6
Based on the discussion in Section 10.4.2, give a precise denition of when an access
path covers the use of projection, union, and set-dierence operators.
Solution:
Exercises
1.
2.
10.7
Which of these access paths has the best selectivity, and which has the worst? Compare
the selectivity of the worst access path (among the above three) to the selectivity of
the le scan.
Solution:
With the unclustered hash index on StudId, we will nd exactly the bucket that
contains all the transcript records for student with the Id 666666666. Since the index
is unclustered, this access method will fetch (in the worst case) as many pages as the
number of transcript records for that student. In our sample relation in Figure 3.5,
this would be 3 pages. In a typical university, an undergraduate student would have to
earn 120150 credits. With 3 credits per course it would make 40-50 transcript records
and, thus, the selectivity would be this many pages of data.
With the unclustered hash index on Semester, we jump to the bucket for the
transcript records in the F1995 semester and then we need to fetch all these records
from the disk to check the other conditions. In a university with enrollment 20,000,
selectivity of this access path can be as high as that. In our sample database, however,
there are only two transcript records for Fall 1995.
With the unclustered hash index on Grade, we get into the bucket of the transcript
records where the student received the grade A. If only 10% of the students get an A,
the bucket would hold 2,000 records per semester. In 20 years (2 semesters a year),
theuniversity might accumulate as many as 80,000 transcript records in that bucket.
In our sample database, we have 5 transcript records where the student got an A.
97
98
CHAPTER 10
Thus, in a typical university, the third access path has the worst selectivity and
the rst has the best. In the sample database of Figure 3.5, the second method has
the best selectivity and the third the worst.
10.8
Nested loops
Block-nested loops
Index-nested loops with a hash index on B in s (consider both clustered and
unclustered index)
where r occupies 2000 pages, 20 tuples per page; s occupies 5000 pages, 5 tuples per
page; and the amount of main memory available for a block-nested loops join is 402
pages. Assume that at most 5 tuples of s match each tuple in r.
Solution:
1.
Nested loops: scan r and for each of its 40,000 tuples scan s once. The result is
2, 000 + 40, 000 5, 000 = 200, 002, 000 pages
2.
Block-nested loops: Scan s once per each 400-page block of r, i.e., 5 times. The
result therefore is:
2, 000 + 5, 000
3.
2, 000
= 27, 000 pages
402 2
10.9
In sort-based union and dierence algorithms, the nal scanwhere the actual union
or dierence is computedcan be performed at no cost in I/O because this step can
be combined with the last merge step during sorting of the relations involved. Work
out the details of this algorithm.
10.10
In the sort-merge join of r s, the scan in the algorithm of Figure 10.7 can be
performed at no cost in I/O because it can be combined with the nal merging step of
sorting r and s. Work out the details of such an algorithm.
Solution:
In the last merging stage of the sorting algorithm we will have n runs, r1 , . . . , rn ,
of relation r sorted on the attribute A and m runs, s1, . . . , sm , of relation s sorted
on the attribute B. The merging stage of the join is combined with that last stage of
sorting as follows. We assume that there is a function getSmallest(X,[p1 , . . . , pk ])
Exercises
which takes an attribute, X, and a list of union-compatible relations sorted on that
attribute, and returns a tuple with the smallest value of X that exists in any of the
p1, . . . , pk . The tuple is then deleted from the corresponding relation so that the next
call to getSmallest() would return the tuple with the next smallest value, etc. The
algorithm now becomes just a slight modication of the merge step for sort-merge join
presented in Figure 10.7:
Input:
Result := { }
// initialize Result
tr := getSmallest(A,[r1, . . . , rn ]) // get rst tuple
ts := getSmallest(B,[s1, . . . , sm ])
while !eof(r) && !eof(s) do {
while !eof(r) && tr.A < ts.B do
tr := getSmallest(A,[r1, . . . , rn ]) // get next tuple
while !eof(s) && tr.A > ts.B do
ts := getSmallest(B,[s1, . . . , sm ])
if tr.A = ts.B = c then {
// for some const c
Result := (A=c(r1 . . . rn ) B=c(s1 . . . sm )) Result;
tr := the next tuple t r where t[A]>c
}
}
return Result;
10.11 Estimate the number of page transfers needed to compute r A=B s using a sort-merge
join, assuming the following:
The size of r is 1000 pages, 10 tuples per page; the size of s is 500 pages, 20 tuples
per page.
The size of the main memory buer for this join computation is 10 pages.
The Cartesian product of matching tuples in r and s (see Figure 10.7) is computed
using a block-nested loops join.
r.A has 100 distinct values and s.B has 50 distinct values. These values are spread
around the les more or less evenly, so the size of A=c (r), where c r.A, does
not vary much with c.
Solution:
1.
2.
99
100
CHAPTER 10
3.
4.
10.12 The methods for computing joins discussed in Section 10.5 all deal with equi-joins.
Discuss their applicability to the problem of computing inequality joins, such as
r A<B s.
Solution:
Suppose we use sort-merge join to compute inequality joins, such as r A<B s.
Input:
Output: r A<B s
Result := { }
tr := getFirst(r)
ts := getFirst(s)
// initialize Result
// get rst tuple
}
}
return Result;
Exercises
As to other methods, hash join is not useful for equality conditions. Nested loops
and block-nested loops work with little change. Index-nested loops join also works is
minor changes, if the index on B in s is B+ tree. If the B+ tree is unclustered, then
index nested loops join is going to be expensive, but the algorithm still works.
If the index is a hash index, then Index-nested loops join is not useful for computing
inequality joins.
10.13 Consider a relation schema, R(A, B), with the following characteristics:
Estimate the number of page transfers needed to evaluate each of the following queries
for each of the proposed methods:
Solution:
The relation has 100,000 pages.
1.
A<3000
Sequential scan
Since the index is clustered, the relation is sorted, so to scan the tuples with
A < 3000 we need to fetch
100, 000 3, 000/1, 000, 000 = 300 pages
Index on A
Use the index to nd the last page page where A < 3000 and then scan the
relevant pages. The cost is 4 to search the index plus the cost of the scan,
which is the same as above:
4 + 300 = 304 pages
2.
A>3000
A<3200 B=5
Index on A
Use the index to nd the rst page where A > 3000 then scan the pages until
you nd the rst page where A 3200:
4 + 100, 000 200/1, 000, 000 = 24 pages
Index on B
On the average there are 1,000,000/100,000 = 10 tuples per any given value
of B. Use the hash index on B to nd the tuples where B=5. Then we need
one page transfer for each tuple in the buer. In total about 1.2 + 10 = 11.2
page transfers.
101
102
CHAPTER 10
3.
A =22
B =66
Sequential scan
Need to scan the entire relation: 100,000 page transfers.
Index on A
Not helpful. For every value in the range of 1 to 1,000,000, except 22,
use the index to get the page that holds that tuple: 4(depth of B+ tree)
999, 999(tuples) = 3, 999, 996 page transfers.
Index on B
This index is also not of much use. For each value in the range of 1 to
100,000, except 66, nd the bucket that contains pointers to the actual
tuples: 1.2 99, 999. For each such tuple (there are about 999,990 of them)
fetch the corresponding page. The total cost is thus
1.2 99, 999 + 999, 990 = 1119989 pages
10.14 Design an algorithm for incremental maintenance of a join index for a multiway star
join.
Solution:
With a star join of the form r cond1 r1 cond2 r2 . . . condn rn we maintain n binary
join indices, for r cond1 r1 , r cond2 r2, etc. The maintenance algorithm for these was
described in Exercise 9.24.
10.15 Design a join algorithm that uses a join index. Dene the notion of a clustered join
index (there are three possibilities in the case of a binary join!) and consider the eect
of clustering on the join algorithm.
Solution:
The actual join algorithm was described in Section 9.7.2. Here we only need to consider
clustering and its eect on the algorithm.
We can possibly say that a join index, J, for r s is clustered in either of the
three cases:
1.
2.
3.
When J is sorted on the r-part, i.e., the r-side rids in J are organized in the same
order as their corresponding tuples in r
When J is sorted on the s-part
When J is sorted simultaneously on both parts, i.e., the r-side rids in J are
organized in the order of their corresponding tuples in r and the s-side rids are
organized in the order of the corresponding tuples in s.
Note that we can always achieve options (1) and (2) by simply sorting J on its r-side or
s-side. In fact, it would be silly not to do so. For this reason, only the third possibility
can be reasonably considered to deserve the name clustered join index.
In the rst two cases, the join algorithm was described in Section 9.7.2. In the
third case, the algorithm is reminiscent of the merge stage of the sort-merge join. We
simply scan the join index and retrieve the pages of r and s that correspond to the
matching tuples. Once the pages are in, we output the requisite joined tuples. Since
the tuples in the index are organized in the same order as the tuples of both r and s,
the join is computed in less than one scan of these relations. It takes less than one
scan because only the pages that contain the tuples to be joined need to be retrieved
(we know which pages these are because the join index gives us the exact rids).
11
An Overview of Query
Optimization
EXERCISES
11.1
11.2
Write down the sequence of steps needed to transform A ((R B=C S) D =E T) into
A ((E (T) E =D ACD (S)) C =B R). List the attributes that each of the schemas R,
S, and T must have and the attributes that each (or some) of these schemas must not
have in order for the above transformation to be correct.
Solution:
1.
2.
3.
4.
5.
104
CHAPTER 11
6.
7.
11.3
Pushing projection to the second operand in the innermost join: A (AC ((E (T)
E =D ACD (S))) C =B R).
Reverse of pushing a projection: A ((E (T) E =D ACD (S)) C =B R)
Under what conditions can the expression A ((R cond1 S) cond2 T) be transformed
into A (B (R cond1 C (S)) cond2 D (T)) using the heuristic rules given in Section 11.2?
Solution:
11.4
Consider the join Professor Id=ProfId Teaching used in the running example
of Section 11.3. Let us change the statistics slightly and assume that the number of
distinct values for Teaching.ProfId is 10,000 (which translates into lower weight for
this attribute).
a. What is the cardinality of the Professor relation?
Solution:
We cannot tell exactly, because some professors might not be teaching, but since
ProfId in Teaching must be a foreign key into Professor, the latter must have
at least 10,000 tuples. We can assume that this is the correct size of this relation,
because it is unlikely that many professors did not teach ever (which is the only way
how there might not be a corresponding tuple in Teaching).
b. Let there be an unclustered hash index on ProfId and assume that, as before, 5
Professor tuples t in one page, 10 Teaching tuples t in one page, and the
cardinality of Teaching is 10,000. Estimate the cost of computing the above join
using index-nested loops and block-nested loops with a 51-page buer.
Solution:
Since in the previous problem we concluded that Professor has 10,000 tuples
and there are 5 tuple/page, this relation will have 2,000 pages. For Teaching, the
statistics do not change: 10,000 tuples, 1,000 pages.
For index-nested loops join, we scan Professor and use the index on ProfId to
fetch the matching tuples in Teaching. Since there are 10,000 tuples in Teaching
and 10,000 values of ProfId, there is 1 matching tuple per value. So, the unclustered
nature of the index on ProfId does not matter. There is going to be 1.2*10,000
page transfers in order to nd all matching tuples in Teaching plus the cost of the
scan of Professor is 2,000. The total cost is 14,000 page transfers.
For the block-nested loops join, we scan the smaller relation, Teaching, in
the outer loop. Since we have 51 buer pages, we use 49 for Teaching, one for
Professor, and one for the output buer. Thus, we will scan Professor once for
every 49-page block of Teaching: 1000/49 = 21 scans. So, the cost is 21*2,000 +
1,000 = 43,000 page transfers.
11.5
Exercises
FROM
WHERE
Employee E
Assume that
Find the best query execution plan for each of the following cases:
a. The only index is on Title, and it is a clustered 2-level B+ tree.
Solution:
Because the only index is a clustered index on Title, we can use it to nd the
1000*0.1=100 pages that contain employees who are programmers. Let us assume
that the index is integrated. Then we spend one I/O to get the top page of the B+
tree and then it takes 1 disk access per page to get the right pages. We select on
E.Dept = Production on the y, which cuts the size down to 50 pages (since 5%
of the employees are production programmers). This can t in main memory. We
can now project on Ename and sort in main memory to eliminate duplicates. Thus,
the cost is 1 + 100 = 101 page transfers.
b. The only index is on the attribute sequence Dept, Title, Ename; it is clustered
and has two levels.
Solution:
Because we have a clustered index on Dept, Title, Ename, we can use an indexonly strategy. We get the top-level page of the index and from there access the
index pages that contain the relevant tuples. There are 50 data pages that contain
the relevant tuples, since 5% of the employees are production programmers. So, the
cost of the access is at most 1 + 50 = 51 page I/Os. If the index is integrated, then
this is the number of I/Os that is going to happen. However, if the index is not
integrated, then the the index pages that contain the relevant tuples are likely to
be smaller, and there will be less I/O. Note that since Ename is part of the key, all
the production programmers will be sorted on Ename, and we will not need to do it
even in main memory.
c. The only index is on Dept, Ename, Title; it is a clustered 3-level B+ tree.
Solution:
We cannot use the index to search on Title, but we can use it to search on
Dept. This will yield 1000/10=100 pages. We can also use an index-only strategy
here. Search the index to nd all the tuples for the production department and
do projection and selection on the y. No sorting is needed, because within each
department the tuples are already sorted on Ename. The cost is 2 (for the upper
two levels of the index) + the number of index pages that refer to the tuples in the
production department. There are 100 pages of the corresponding data tuples, but
the index is expected to be much smaller.
105
106
CHAPTER 11
d. There is an unclustered hash index on Dept and a 2-level clustered tree index on
Ename.
Solution:
Searching the hash index on Dept will give us the bucket of pointers to the
production employees (about 10% of the total number of tuples, 10,000). But each
such pointer will result in a page fetch, since the index in unclustered. So, the cost
is 1.2 I/Os to nd the bucket plus 1,000 to nd the relevant tuples.
The above is actually slightly worse than the direct scan, which takes only 1,000
pages. The selection and projection is done on the y and the result (50 pages) is
placed in the buer, where it is projected and then sorted to eliminate duplicates.
Using a clustered index on Ename would require us to take a scan anyway. But
since the tuples in the result are already sorted on Ename, we do not need to do any
in-memory sorting, so this method is slightly preferable to the other two.
11.6
Employee(SSN, Name,Dept)
Project(SSN,PID,Name,Budget)
The SSN attribute in Project is the Id of the employee working on the project,
and PID is the Id of the project. There can be several employees per project, but the
functional dependency PID Name,Budget holds (so the relation is not normalized).
Consider the query
SELECT
FROM
WHERE
Employee relation
On Name: Unclustered, hash
On SSN: Clustered, 3-level B+ tree
Project relation
On SSN: Unclustered, hash
On Budget: Clustered, 2-level B+ tree
Exercises
Order byBudget
Budget, EmpName, ProjName
Rename Employee.Name to EmpName
and Project.Name to ProjName
SSN = SSN
Budget > 99
Employee
Project
FIGURE 11.1
Solution:
See Figure 11.1.
b. Find the best execution plan and the second-best plan. What is the cost of each?
Explain how you arrived at your costs.
Solution:
There are three reasonable plans:
Select Employee on Name. Since there are about 10000/1000=10 tuples in the
result, it would take 1.2*10=12 page transfers. The result contains 10 tuples, 1
page.
Using index-nested loops, join the 10 Employee tuples with Project.
There are two projects per employee on the average, so it would require to
bring in 1.2 * 10 * 2 = 24 pages, since we will be using an unclustered hash
index on SSN in Project. The result will contain 20 tuples (twice as wide as
the tuples in the original relations), 1 page. We do not need to write anything
out on disk, since after extracting the joined tuples we will be discarding the
pages of Project that we bring in for the join.
Select the result on Budget on the y, yielding 0.01 * 20 = 1 tuple (at no
cost), then project. The total cost is 12 + 24 = 36 page transfers.
107
108
CHAPTER 11
11.7
resulting number of tuples is about 200, twice the original size. This is about
10 pages.
Apply selection and projection on the y, yielding one page (at no cost).
Total: over 607 I/Os.
Select Employee on Name, and Project on Budget. From before, we know
that it takes 12 + 7 I/Os and yields 10 and 200 tuples (1 + 5 pages), respectively.
Use sort-merge-join, then project. We can sort everything in memory (we
have 10 pages in the buer) and then merge, select and project in one pass
(also in memory). Thus, the cost is 19 I/Os.
The best plan is therefore plan #3. The second best is plan #1.
Consider the following schema, where the keys are underlined (dierent keys are
underlined dierently):
SELECT
FROM
WHERE
C.CrsName, P.Name
Professor P, Teaching T, Course C
Professor relation
On Department: Clustered, 2-level B+ tree
On Id: Unclustered, hash
Course relation
On CrsCode: Sorted (no index)
On CrsName: Hash, unclustered
Teaching relation
On ProfId: Clustered, 2-level B+ -tree
On Semester, CrsCode: Unclustered, 2-level B+ tree
a. First, show the unoptimized relational algebra expression that corresponds to the
above SQL query. Then draw the corresponding fully pushed query tree.
Exercises
Name, CrsName
CrsCode = CrsCode
CrsCode, Name
CrsCode, Name
Course
Id = ProfId
Id, Name
ProfId, CrsCode
Professor
Teaching
FIGURE 11.2
Solution:
The unoptimized algebraic expression is:
109
110
CHAPTER 11
So, instead, we will use index-nested loops to join the 2 selected pages of
Professor with Teaching. Since the index on ProfId in Teaching is clustered,
we can fetch all tuples in Teaching that match any given tuple in Professor in
two page transfers (20000 teaching tuples per 1000 professors yields 20 tuples per
professor, i.e., 2 pages). For each such fetch, we need to search the B+ tree index on
ProfId. We can keep the top level of that index in main memory, so there is only
one index page fetch per search. Thus, the join takes 1 (to fetch the top level of
index) + 20 (professors) * 3 (index search plus fetching the matching tuples) = 61.
The intermediate join has 20 * 20 = 400 tuples, each tuple twice the size of the
tuples in either Professor or Teaching. So, only 5 tuples t in a page for a total
of 80 pages. However, we can select the result of the join on Semester on the y,
reducing the size to just 2 tuples, 1 page. We could also apply projection on Name
and CrsCode, but this will not give us any more gains. Since we are talking about
just a 1-page result, there is no need to write it on disk we can keep it in main
memory.
Now, since Course is sorted on CrsCode, we can use binary search on Course
(for each of the two tuples in the join result) to nd the matches. This would take
us log2 400 = 9 page transfers per search, 18 page transfers in total.
Thus, the total cost is 4+61+18=83 page transfers.
Note that joining Teaching with Course rst would require us to scan
Course at least once (since there is no index on CrsCode in either relation). So the
cost would be at least 400 pages by far higher than the 83 pages that we came
up with.
11.8
Consider the following relations that represent part of a real estate database:
Agent(Id, AgentName)
House(Address, OwnerId, AgentId)
Amenity(Address, Feature)
The Agent relation keeps information on real estate agents, the House relation has
information on who is selling the house and the agent involved, and the Amenity
relation provides information on the features of each house. Each relation has its keys
underlined. Consider the following query:
SELECT
FROM
WHERE
H.OwnerId, A.AgentName
House H, Agent A, Amenity Y
Assume that the buer space available for this query has 5 pages and that the following
statistics and indices are available:
Amenity
10,000 records on 1000 houses, 5 records per page
Clustered 2-level B+ tree index on Address
Unclustered hash index on Feature, 50 features
Agent
Exercises
OwnerId, AgentName
AgentId = Id
OwnerId, AgentId
Agent
Address = Address
Address
AgentId = '007'
Feature = '5BR'
House
Amenity
FIGURE 11.3
Answer the following questions (and explain how you arrived at your solutions).
a. Draw a fully pushed query tree corresponding to the above query.
Solution:
See Figure 11.3.
b. Find the best query plan to evaluate the above query and estimate its cost.
Solution:
We could join House with Agent or Amenity, but in any case it is clear that we
should rst select House on AgentId, because of the large reduction in size: There
are 200 agents, 1000 houses, so agent 007 must be handling about 5 houses. At 4
records per page, this would occupy 2 pages.
Because the index on AgentId is unclustered, it would take 1.2 I/Os to nd
the bucket and 5 I/Os to fetch the relevant pages: 6.2 I/Os in total.
111
112
CHAPTER 11
Next we can join with Agent, which would take 1.2 page I/Os to search the
index, since Agent has an unclustered index on Id. In addition, 1 I/O is required
to fetch the data page. The result will still have 5 tuples, but the tuples will be
about 50% larger (Agent has 10 tuples/page, while House only 4). However, we
can project out Id and AgentId, which will bring the tuple size to about the size
of the tuples in House. So, the join will still occupy a little over a page. We keep
the result of the join in the main memory buer.
Finally, we join the result with Amenity. Since the statistics indicate that
there are about 10 amenities per house, it does not make much sense to select
Amenity on Feature: the size will go down by the factor of 10 at a very high price
(unclustered hash or scan of 2,000 blocks of the Amenity relation), and we will
loose the index on Address the attribute used in the join.
So, the best way to do the join is to use index-nested loops join using the
clustered index on the attribute Address of Amenity. It would take 2 I/Os to
search the B+ tree index for each of the 5 tuples in the result of the previous join
(i.e., 2*5; if we cache the top level of the B+ tree then this search would take
2+4=6 I/Os). The number of tuples retrieved would be 50 (10 features per house
* 5 houses), which occupies 10 pages. Therefore, the total needed to retrieve the
matching tuples in Amenity is 16 I/Os.
Note that we still have enough room in the buer. The expected size of the
join is 50 tuples (5*10), which is too large for our 5 page buer. However, we can
also select on Feature=5BR on the y, reducing the size to about 5 tuples, each
about twice the size of the tuples in House. We can also project )on the y) on
OwnerId and AgentName further reducing the size. Thus, we will need 2 pages in
the buer for the result of the join of House and Agent, one page for the input
buer that is needed for reading the matching tuples of Amenity, and two to store
the nal result. This ts in the available 5 buer pages.
In total, thus, the query will take 6.2 + 1.2 + 1 + 16 = 24.4 I/Os.
c. Find the next-best plan and estimate its cost.
Solution:
Similar, except that what is left of House is rst joined with Amenity. The number
of I/Os is going to be the same, so this is also a best plan.
The next plan after that would be to do something silly, like joining House
and Agent using nested loops. Since Agent occupies 20 blocks, this can be done
in 20 I/Os. Then we could proceed to join with Amenity.
11.9
None of the query execution plans in Figure 11.3 for queries (11.2)(11.5) does
duplicate elimination. To account for this, let us add one more relational operator, ,
which denotes the operation of duplicate elimination. Modify the plans in Figure 11.3
by adding in appropriate places so as to minimize the cost of the computation.
Estimate the cost of each new plan.
Solution:
Duplicates arise only as a result of the projection operator. Since all plans in Figure 11.3
have projection only as the last step, there is not much room for duplicate elimination:
should be the last operator applied after Name . Duplicate elimination can done at
no extra cost (in terms of I/O), because the in-memory buer is large enough to hold
the result of the join (which we can sort on Name before passing it to the projection
operator and then to the duplicate elimination operator).
Exercises
Duplicate elimination does not save anything here either.
11.10 Build a database for the scenario in Exercise 11.5 using the DBMS of your choice. Use
the EXPLAIN PLAN statement (or an equivalent provided by your DBMS) to compare
the best plan that you found manually with the plan actually generated by the DBMS.
Solution:
This is a hands-on exercise where the students are invited to experiment with the
EXPLAIN PLAN statement or the visual tuning tools provides by some DBMS vendors.
11.11 Follow Exercise 11.10, but use the scenario in Exercise 11.6.
Solution:
This is a hands-on exercise like 11.10 above.
11.12 Follow Exercise 11.10, but use the scenario in Exercise 11.7.
Solution:
This is a hands-on exercise like 11.10 above.
11.13 Consider the query execution plans in Figure 11.3. Show how each of these plans can
be enhanced by pushing the projection operator past the join without altering the
strategies used for computing the join.
Solution:
In plans (a) and (c), projection cannot be pushed to Teaching, because this would
obliterate the index on ProfId and prevent the use of the indexed join strategies.
In case (a), we must preserve Id, DeptId, and Name for the subsequent operations.
Since these are all the attributes in Professor, there is nothing to push. In case (c),
we can push Id,Name .
In case (b), we can push Id,Name to Professor and ProfId to Teaching.
In case (d), we cannot push anything to Professor for the same reason as in
case (a), but we can push ProfId to Teaching.
11.14 Using the result of Exercise 11.13, show which of the enhanced plans can be further
enhanced by adding the duplicate elimination operator introduced in Exercise 11.9.
Solution:
Since projections applied to Professor must always preserve the attribute Id for the
subsequent join and since this attribute forms a key, no duplicates are possible and,
thus, would be useless after such a projection (and might actually incur costs only
to nd out that the operation has no eect).
In case of Teaching, as discussed in the solution to exercise 11.13, projection
(ProfId ) can be pushed only in cases (b) and (d), because in other cases we use the
index on the attribute ProfId. Since in a given semester a professor teaches on the
average of two courses, duplicate elimination would reduce the size of the intermediate
relation by the factor of two, maybe three. Let us examine whether the cost of such
duplicate elimination is justied by the savings in computing the subsequent join.
In case (d), since sort-merge-join is used, the operands must be sorted anyway, so
duplicate elimination can be done at no cost and would reduce the overall cost of the
join.
In case (b), the result of selection on Teaching has 250 pages. Since in this
example we use a 51-page buer, 5 runs can be produced at the cost of 500 I/Os
113
114
CHAPTER 11
(read then write), and then they are merged and duplicates eliminated at the cost of
another 250 I/Os (to just read the runs into the main memory). Thus, using duplicate
elimination does not make sense in this case, because the total estimated cost of plan
(b) in Figure 11.3 was just 512 pages.
12
Database Tuning
EXERCISES
12.1
Choose an index for each of the following SELECT statements. Specify whether your
choice is clustered or unclustered and whether it is a hash index or a B+ tree.
a.
SELECT
FROM
WHERE
S.Name
Student S
S.Id = 111111111
Solution:
Since Id is a primary key, an index on Id will automatically be created by the
DBMS. Any index, clustered or unclustered, hash or B+ tree, will support this
query. Hence, no additional index is necessary.
b.
SELECT
FROM
WHERE
S.Name
Student S
S.Status = Freshman
Solution:
Roughly 25% of the rows in the Student table satisfy the condition and the DBMS
will not automatically create an index on Status since it is not unique. A clustered
index is needed in this case because of the number of rows that have to be retrieved.
The index can be a hash or B+ tree since all that is required is a grouping of all
rows with a particular value of Status. The main index (on Id) will be unclustered
in this case.
c.
SELECT
FROM
WHERE
T.StudId
Transcript T
Solution:
An index on CrsCode (rather than Grade) is needed since CrsCode is more selective.
All rows satisfying the condition on CrsCode can be retrieved through the index
and scanned, and only those satisfying the condition on Grade will be returned.
The index should be clustered because the number of rows that will be selected
is expected to be high. If the primary key (which involves the attributes StudId,
Semester and CrsCode) is specied to be a clustered B+ tree with CrsCode as
116
CHAPTER 12
Database Tuning
the rst search key attribute then it can support the query (note that a clustered
hash index could not be used in this way).
d.
SELECT
FROM
WHERE
P.Name
Professor P
Solution:
The DBMS will not automatically create an index on Salary since it is not unique.
A clustered index is needed in this case because of the number of rows that have to
be retrieved. The index must be a B+ tree since since a range of salaries is requested.
Hence the primary index (on Id) will be unclustered.
e.
SELECT
FROM
WHERE
T.ProfId
Teaching T
Solution:
An index on CrsCode (rather than Semester) is needed since CrsCode is more
selective. All rows satisfying the condition on CrsCode can be retrieved through
the index and scanned, and only those satisfying the condition on Semester will be
returned. The index should Be clustered B+ tree because the number of rows that
will be selected is expected to be high and due to the fact that a range of search
key values has eectively been specied (note: CS is a prex of the required course
codes, so the range is CS000 through CS999). If the primary key (which involves
the attributes Semester and CrsCode) is specied to be a clustered B+ tree and
CrsCode is the rst search key attribute in that tree then this index can support
the query.
f.
SELECT
FROM
WHERE
C.CrsName
Course C, Teaching T
Solution:
The key of Course is CrsCode and there will be an index on this. This is sucient
to support an index nested loop join with Teaching in the outer loop. Each row
of Teaching can be scanned (no index on Teaching used) and if it satises the
condition on Semester, the unique row of Course that it joins with can be found
using this index. Any type of index will work (clustered, unclustered, B+ tree, hash).
An improvement on this is to have a clustered index (B+ tree or hash) on Semester
of Teaching since then a complete scan of Teaching can be avoided. By declaring
the key of Teaching to be (Semester, CrsCode) the main index can be used for
this purpose.
12.2
Suppose both queries (e) and (f) from the previous exercise need to be supported.
What indices should be chosen for Teaching and Course?
Solution:
A clustered index on Semester supports (f). It can also be used to support (e)
(although it does not do the job as well as a clustered index on CrsCode). Use this
solution if (f) is the more important query. If (e) is more important, use a clustered
index on CrsCode of Teaching to evaluate (e); the DBMS can use an index-nested
Exercises
loop join (using the index on CrsCode in Course and scanning Teaching in the
outer loop) to do (f). Recall that CrsCode is the primary key in Course so Course
will have an index on CrsCode to support such a join.
12.3
The table Faculty has 60,000 rows, each row occupies 100 bytes, and the database
page size is 4k bytes. Assuming pages in the index and data les are 100% occupied,
estimate the number of page transfers required for the following SELECT statement in
each of the cases listed below.
SELECT
FROM
WHERE
F.DeptId
Faculty F
F.Id = 111111111
The table Faculty has 60,000 rows, each row occupies 100 bytes, and the database
page size is 4k bytes. The table contains an attribute City, indicating the city in which
a professor lives, there are 50 cities with names city10 . . . city50, and professors are
randomly distributed over the cities. Assuming that pages in the index and data les
117
118
CHAPTER 12
Database Tuning
are 100% occupied, estimate the number of page transfers required for the following
SELECT statement in each of the cases listed below.
SELECT
FROM
WHERE
F.Id
Faculty F
Choose indices for the following SELECT statement. Specify whether your choices are
clustered or unclustered, hash index or B+ tree.
SELECT
FROM
WHERE
GROUP BY
C.CrsName, COUNT(*)
Course C, Transcript T
Exercises
HAVING COUNT(*) 100
Solution:
Suppose the primary key of Transcript is declared to be (Semester, CrsCode,
StudId) with a corresponding B+ tree. Then the DBMS can identify all rows of
Transcript satisfying the condition on Semester. These rows will already be
grouped on CrsCode and hence the index supports the GROUP BY clause of the query.
Each group can be scanned to determine which satises the HAVING clause and then
any index on the CrsCode attribute of Course can be used to locate the unique row of
Course containing the CrsName attribute for the group. Note that since the search
key of the index on Transcript contains both Semester and CrsCode, it supports
an index-only search. Hence, the rows of Transcript do not have to be accessed and
as a result the index does not have to be clustered.
12.6
T.CrsCode, T.Grade
Transcript T, Student S
119
120
CHAPTER 12
Database Tuning
c. In addition to the index added in (b), assume that an unclustered index on Student
on Name has been added and repeat the experiment.
Solution:
Now the selection on Student is replaced by an index search using the new index,
causing a further reduction in the response time.
12.7
Consider the table Authors with attributes Name, Publ, Title, and YearPub.
Assume that Name is the primary key (authors names are unique) and hence one
would expect that the DBMS would automatically create a clustered index on that
attribute. Consider the statement
SELECT
FROM
WHERE
GROUP BY
A.Publ, COUNT(*)
Authors A
. . . range predicate on YearPub . . .
A.Publ
a. Assume that the statement is generally executed with a very narrow range specied
in the WHERE clause (the publication year of only a few books will fall within the
range). What indices would you create for the table and what query plan would you
hope the query optimizer would use (include any changes you might make to the
index on Name).
Solution:
You might choose an unclustered B+ tree index on YearPub in which case the
query plan would search the index using the small end of the range and then scan
forward along the leaf level to locate the (few) rows satisfying the WHERE clause.
Unfortunately, these rows would not be grouped together in the table, so they would
have to be randomly retrieved. The plan would then sort them by Publ and count
the size of each group. Alternatively, you might change the index on Name from
clustered to unclustered and create a clustered B+ tree index YearPub. In this case
the qualifying rows would be contiguous in the table.
b. Repeat (a) assuming that a very broad range is generally specied.
Solution:
You might change the index on Name from clustered to unclustered and create a
clustered B+ tree index Publ. This would be useful if it is expected that a signicant
fraction of the rows in the table t within the range and hence most of the pages
will be read. In that case the query plan will scan the entire table (instead of
trying to retrieve qualifying rows through an index on YearPub). Groups are stored
contiguously in the table and the scan can simply count the qualifying rows one
group at a time as the scan progresses without having to do an expensive sort.
12.8
Give the trigger that maintains the consistency of the database when a GPA column is
added to the table Student, as described in Section 12.2.2.
Solution:
12.9
Exercises
expensive. Therefore you should only use DISTINCT when duplicates are possible
in the result set. Using the schema of Section 4.8, check the following queries to see if
duplicates are possible. Explain your answer in each case.
a.
SELECT
FROM
WHERE
S.Name
Student S
S.Id LIKE 1
Solution:
Duplicates are possible since students with dierent Ids might have the same name
b.
SELECT
FROM
WHERE
S.Id
Student S, Faculty F
S.Address = F.Address
Solution:
Since Address is not a key of Faculty, there may be several rows of Faculty
that match a row of Student. Hence duplicates are possible.
c.
SELECT
C.CrsCode, COUNT(*)
FROM
Transcript T
GROUP BY T.CrsCode
Solution:
No duplicate possible since all rows of Transcript with a particular value of
CrsCode are grouped into a unique row in the result set.
d.
Solution:
Since a professor can teach only one class at a particular time there can be no
duplicates
e.
SELECT
S.Name, F.Name, T.Semester, T.Year
FROM
Faculty, Class C, Transcript T, Student S
WHERE F.Id = C.InstructorId AND S.Id = T.StudId AND
C.CrsCode = T.CrsCode AND
C.SectionNo = T.SectNo AND
C.Year = T.Year AND C.Semester = T.Semester
Solution:
Duplicates are possible since a student may be taught by a professor in two dierent
courses in the same semester.
12.10 A particular query can have several formulations, and a query optimizer may produce
dierent query plans with dierent costs for each.
a. Assume that the Computer Science Department teaches only three 100-level courses:
CS110, CS113, and CS114. Write an SQL statement whose result set contains the
course codes of all courses that have these as prerequisites in three ways: using OR,
UNION, and a nested subquery involving LIKE.
121
122
CHAPTER 12
Database Tuning
Solution:
SELECT
FROM
WHERE
SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE
SELECT
FROM
WHERE
R.CrsCode
Requires R
R.PrereqCrsCode = CS110 OR
R.PrereqCrsCode = CS113
R.PrereqCrsCode = CS114
R.CrsCode
Requires R
R.PrereqCrsCode = CS110
R.CrsCode
Requires R
R.PrereqCrsCode = CS113
R.CrsCode
Requires R
R.PrereqCrsCode = CS114
R.CrsCode
Requires R
b. Write an SQL statement whose result set contains the names of all computer science
courses that are prerequisites to other courses in three ways: using a join, a nested
subquery involving EXISTS, and a nested subquery involving IN.
Solution:
SELECT
FROM
WHERE
C.CrsName
Course C, Requires R
C.CrsCode = R.PrereqCrsCode
SELECT
C.CrsName
FROM
Course C
WHERE EXISTS (
SELECT *
FROM
Requires R
Exercises
WHERE
SELECT
C.CrsName
FROM
Course C
WHERE C.CrsCode IN (
SELECT
FROM
C.CrsCode = R.PrereqCrsCode)
R.PrereqCrsCode
Requires R)
12.11 On page 440 we discussed the choice of indexes to optimize the execution of the
nested query (12.1) and pointed out that a clustered index on Teaching with search
key CrsCode would not be considered. It might, however, be used in optimizing the
execution of the equivalent, non-nested query
SELECT
FROM
WHERE
C.CrsName, P.Name
Professor P, Teaching T, Course C
123
PART FOUR
13
Relational Calculus,
Visual Query Languages,
and Deductive Databases
EXERCISES
13.1
13.2
Express in words the meaning of each of the following expressions (where Took(s, c)
means student s took course c). For example, the meaning of one of these expressions
is Every student has taken at least one course.
a. S Student (C Course Took(S, C))
Solution:
There is a student who has taken all courses.
b. S Student (C Course Took(S, C))
Solution:
Every student has taken at least one course.
c. C Course (S Student Took(S, C))
Solution:
There is a course that was taken by all students.
d. C Course (S Student Took(S, C))
128
CHAPTER 13
Solution:
Every course was taken by at least one student.
13.3
Write the query that nds all students who took some course from Professor Joe
Public; use TRC and DRC.
Solution:
TRC:
{ SN | Id,Addr,Status,CrsCode,Sem,Gr,PId,Dept (
Student(Id,SN,Addr,Status) AND
Transcript(Id,CrsCode,Sem,Gr) AND
Teaching(PId,CrsCode,Sem) AND
Professor(PId,Joe Public,Dept) ) }
13.4
Write a query that nds all students who took MAT123 from Professor Ann White
but did not take MAT321 from her.
Solution:
We will use DRC. The TRC solution is similar.
{ SN | Id,Addr,Status,Sem,Gr,PId,Dept (
Student(Id,SN,Addr,Status) AND
Transcript(Id,MAT123,Sem,Gr) AND
Teaching(PId,MAT123,Sem) AND
Professor(PId,Ann White,Dept) AND
NOT Sem1,Gr1 (
Transcript(Id,MAT321,Sem1,Gr1) AND
Teaching(PId,MAT321,Sem1) )
)}
13.5
Prove that any query in tuple relational calculus has an equivalent query in domain
relational calculus, and vice versa.
Solution:
Here we only show the transformation from TRC to DRC and back. A rigorous proofs
of the fact that these are equivalence transformations are long and tedious because of
the need to test all cases during the structural induction on the form of the queries.
Exercises
TRC to DRC: This transformation is simple: Each tuple variable of degree n is
replaced with n new domain variables, one for each attribute of the relation over which
T ranges. Terms like T .attr are replaced with the domain variable that corresponds to
the attr s component of T . For instance, assuming that T is a tuple variable of degree
3, S of degree 2, A is the rst attribute of P and B is the second attribute of R,
T S (. . . P(T ) . . . T .A = S .B . . . R(S ))
becomes
T1T2 T3S1S2(. . . P(T1, T2, T3) . . . T1 = S2 . . . R(S1, S2 ))
DRC to TRC: This direction is much more complex. To simplify the language, we
adopt the positional notation for accessing the components of tuple variables. For
instance, if T ranges over R with the attributes A, B, C, then instead of T .A, T .B, and
T .C we write T .1, T .2, and T .3.
To see the diculty in translating DRC to TRC, consider the following DRC
query:
{X |Y Z (P(X , Y , Z ))}
We cannot simply replace X , Y , Z with a single tuple variable T because X is free,
Y is quantied with , and Z with and, thus, the proper quantication of T is not
obvious. After moments reection one might come up with the following solution:
{R|T S (P (T ) AND Q (S )
129
130
CHAPTER 13
R.1 = T .1 AND . . . AND R.n = T .n AND R.m = S .m AND . . . AND R.r = S .r)}
Cases 2 and 3: The query is formed using OR or NOT. These cases are similar to
the above.
Case 4: The query is {X1, . . . , Xm |Xm+1(P(X1, . . . , Xm , Xm+1))},
where P(X1, . . . , Xm , Xm+1) has X1 , . . . , Xm , Xm+1 as free variables. Since this formula
has length N , the inductive hypothesis says that it translates into TRC: {T |P(T )}.
The original query can then be translated as
Prove that relational algebra queries are domain-independent. Hint: Use induction on
the structure of the relational algebra expression.
Solution:
The proof is by induction on the structure of algebraic expressions.
Base case: The expression is P, where P is a relation name. Clearly, the contents
of the relation assigned to P in the database is xed and does not depend on the
domain.
Inductive hypothesis: Assume that all expressions of length less than or equal to
N are domain independent. We will prove that then any expression of length M + 1 is
domain independent.
We need to consider the various cases that correspond to each of the relational
operators. In all cases, the reasoning is similar. For instance, let the query be E1 E2 ,
where E1 and E2 are relational expressions of smaller length. By inductive hypothesis,
they are domain independent. Clearly, then, their union is domain independent as well.
13.7
Consider the relation schema corresponding to the IsA hierarchy in Figure 4.6.
Assume that this schema has one relation per entity. (Consult Section 4.4 to refresh
your memory about translation of IsA hierarchies into the relational model.) Write
the following queries both in tuple and domain relational calculus:
a. Find the names of all sophomores in the computer science major.
Solution:
{Name | DOBSSNMajor
(Person(SSN,Name,DOB) AND Sophomore(SSN,Major)) }
Exercises
b. Find the names of all students in the computer science major.
Solution:
Write a domain relational calculus query that is equivalent to the following algebraic
expressions:
a. R S
Solution:
{A | B S (R(A, B )) }
131
132
CHAPTER 13
13.9
Express each of the following queries in tuple relational calculus, domain relational
calculus, and QBE using the schema of Figure 3.4, page 38.
a. Find all courses that are taught by professors who belong to either of the departments
EE and MGT. (Assume that certain courses that are listed in one department can be
taught by professors from other departments.)
Solution:
TRC:
{C.CrsCode, C.CrsName |
Course(C) AND T Teaching P Professor
(P.Id = T.ProfId AND T.CrsCode = C.CrsCode
AND (P.DeptId = EE OR P.DeptId = MGT )) }
DRC:
{CrsCode, CrsName |
DeptId1DeptId2DescrProfIdSemesterName
(Course(DeptId1,CrsCode,CrsName,Descr) AND
Teaching(ProfId,CrsCode,Semester) AND
Professor(ProfId,Name,DeptId2) AND
(DeptId2 = EE OR DeptId2 = MGT )) }
b. List the names of all students who took courses in spring 1997 and fall 1998.
Solution:
TRC:
DRC:
{Name | IdAddressStatusCrsCode1CrsCode2Grade1Grade2
(Student(Id,Name,Address,Status) AND
Transcript(Id,CrsCode1,S1997,Grade1) AND
Transcript(Id,CrsCode2,F1998,Grade2) ) }
c. List the names of all students who took courses from at least two professors in
dierent departments.
Solution:
Exercises
133
TRC:
DRC:
{Name | IdAddressStatus
CrsCode1Semester1Grade1
CrsCode2Semester2Grade2
ProfId1ProfId2
PName1DeptId1PName2DeptId2
(Student(Id,Name,Address,Status) AND
Transcript(Id,CrsCode1,Semester1,Grade1) AND
Transcript(Id,CrsCode2,Semester2,Grade2) AND
Teaching(ProfId1,CrsCode1,Semester1) AND
Teaching(ProfId2,CrsCode2,Semester2) AND
Professor(ProfId1,PName1,DeptId1) AND
Professor(ProfId2,PName2,DeptId2) AND
(DeptId1
= DeptId2 ) }
d. Find all courses in department MGT that were taken by all students.
Solution:
TRC:
DRC:
Find every department that has a professor who has taught all courses ever oered
by that department.
134
CHAPTER 13
Solution:
TRC:
DRC:
TRC:
Exercises
135
DRC:
TRC:
DRC:
Consider the relation schema of Exercise 5.24. Write the following queries using TRC
and DRC.
a. Find all customers who are interested in every house listed with Agent 007.
Solution:
TRC:
DRC:
136
CHAPTER 13
Only the customers who are interested in every house listed with Agent 007
are considered; and
The number of customers interested in feature is greater than 2. (If this
number is 2 or less, the corresponding tuple feature, customer is not added
to the result.)
This part of the query cannot be conveniently expressed by TRC or DRC because
they lack the counting operator. However it is possible nevertheless (and is not
hard).
Solution:
Let us denote the result of the above query as ClientOf007 and use it as a view.
TRC:
{P.Feature,C.CustName |
Preference(P) AND ClientOf007(C) AND P.CustId=C.CustId AND
P2 Preference C2 ClientOf007
P3 Preference C3 ClientOf007
(P.Feature = P2.Feature AND P.Feature = P3.Feature AND
P2.CustId = C2.CustId AND P3.CustId = C3.CustId
P.CustId
= P2.CustId AND P.CustId
= P3.CustId
AND P2.CustId
= P2.CustId) }
DRC:
{Feature,CustName |
Preference(CustId,Feature) AND ClientOf007(CustId,CustName)
AND CId2 ClientOf007.CustId CId3 ClientOf007.CustId
(Preference(CId2,Feature) AND Preference(CId3,Feature)
AND CustId
= CId2 AND CustId
= CId3 AND CId2
= CId3)
13.14 Write SQL query (5.10), page 150, and query (5.11), page 151, using TRC and DRC.
Solution:
Find all courses taught in fall 1995 together with the professors who taught those
courses.
TRC:
DRC:
Exercises
AND Teaching(PId,CrsCode,F1995) ) }
13.15 Investigate the logical relationship between the SQL operator EXISTS and the TRC
quantier . For concreteness, express query (5.26), page 160, using TRC.
Solution:
The SQL EXISTS operator is more limited: it tests whether a set is empty. The
quantier in TRC is more versatile, especially when the query contains universal
quantiers and the NOT operator in addition to . For instance, the query (5.26),
page 160, can be expressed much more naturally in TRC using than in SQL using
EXISTS:
Supplier(Name,Part)
Project(Name,Part)
A tuple, n,p, in the rst relation means that supplier n has part p. A tuple n,p in
the second relation means that the project named n uses part p. Write the following
query in tuple and domain relational calculi: Find the names of suppliers who have a
part, that is used by every project.
Solution:
Show that the iterative process of computing the transitive closure of Prereq
terminates after a nite number of steps. Show that this process can compute the
transitive closure in polynomial time.
Solution:
A recursive denition of IndirectPrereqView can be viewed as an operator, T (X ),
which computes a relational algebra expression that involves X and some other,
constant relations. For any X , T (X ) is a relation whose tuples consist of constants
that appear in X and in the other constant relations that are used by T .
137
138
CHAPTER 13
The computation starts by computing T (), then T (T ()), etc. First, it is easy
to see from the form of the denition of IndirectPrereqView that T is a monotone
operator , i.e., if X Y then T (X ) T (Y ). Since T (), it is easy to see that
T () T (T ()) . . .
Let us denote the nth application of T by T n (). Due to an earlier observation, the
tuples in T n () must be composed only of the constants that appear in the constant
relations that are part of T . Thus, the relation T n () cannot grow indenitely and for
some m we must have T m+1() = T m (), and the computation can stop there.
To see how this computation can be done in polynomial time, observe that
T (X1 X2) = T (X1) T (X2). Thus, the above computation can be modied as follows.
Let us extend the denition of T n with T 1 () = T () and T 0 () = .
0 =
S0 =
1 = T (0 ) S0
S 1 = 1 S 0
2 = T (1 ) S1
S 2 = 2 S 1
Exercises
139
Then we can compute all ights with just one stop Flight1 and then subtract
DirectFlight and Flight1 from IndirectFlight.
One might be tempted to rst create a recursive view IndirectFlight2(From,TO,NumOfStops)
and then select the ights with NumOfStops > 1.
CREATE RECURSIVE VIEW IndirectFlight2(From,To,Stops) AS
SELECT D.StartCity, D.DestinationCity, 0
FROM DirectFlight D
UNION
SELECT D.StartCity, I.To, I.Stops+1
FROM DirectFlight D, IndirectFlight2 I
WHERE D.DestinationCity = I.From
However, this recursive denition has a problem: because we keep incrementing the
number of stops with each iteration, the evaluation process will not terminate. (Check
that the termination condition will never be true!)
13.20 Use the recursion facility of SQL:1999 to express a so-called same generation query:
Given a Parent relation, nd all pairs of people who have the same ancestor and are
removed from her by equal number of generations. (For example, a child is removed
from her parent by one generation and from grandparent by two.)
Solution:
The rst two (non-recursive) queries create the initial pairs of the form p, p (certainly,
two occurrences of the same person is removed from a common ancestor by the same
number of generations). The last, recursive, subquery computes the rest.
140
CHAPTER 13
13.21
Consider the following bill of materials problem: the database has a relation
Subpart(Part, Subpart, Quantity), which tells which direct subparts are needed
for each part and in what quantity. For instance, Subpart(mounting_assembly,
screw, 4) means that the mounting assembly includes four screws. For simplicity, let
us assume that parts that do not have subparts (the atomic parts) are represented as
having NULL as the only subpart (for instance, Subpart(screw, NULL,0)). Write a
recursive query to produce a list of all parts and for each part the number of primitive
subparts it has.
Solution:
The rst, non-recursive subquery starts the computation by producing the list of
primitive parts and assigning 1 as the number of primitive components of each such
part (the check IS NULL is intended to ensure that the part is primitive). The recursive
subquery then computes the total number of primitive components for parts that are
composed of several subparts.
13.22 Consider the following relational schema:
DirectFlight(From,To,Distance)
Write a recursive SQL query that returns tuples of the form From,To,Distance,
which represent direct or indirect ights (i.e., ights composed of one or more segments)
and the aggregate distance over all segments for each such ight. (If there are several
ways to reach B from A and the total distance is dierent in each case, then the output
would have several tuples such as A,B,1000, A,B,1500, etc.) The exact query is:
Find all tuples of the above form provided that the distance is less than 10,000 .
Solution:
Exercises
FROM DirectFlight D, IndirectFlight I
WHERE D.To = I.From AND D.Distance+I.Distance < 10000) )
SELECT * FROM IndirectFlight
The iterative evaluation procedure for this query terminates, because the values
for the Distance attribute cannot grow indenitely due to the upper bound on
distances. Note that there is another way to write this query, which might or might
not terminate depending on whether the query optimizer is smart enough:
WITH RECURSIVE IndirectFlight1(From,To,Distance) AS
( (SELECT * FROM DirectFlight)
UNION
(SELECT From, To, D.Distance+I.Distance
FROM DirectFlight D, IndirectFlight1 I
WHERE D.To = I.From) )
SELECT *
FROM IndirectFlight1 I
WHERE I.Distance < 10000
The dierence is that now the selection is done in the query rather than in the
recursive denition. By itself, the recursively dened relation IndirectFlight1 is
innite and the iterative procedure will never end. However, the optimizer might
notice the selection condition in the outer query, I.Distance < 10000, and push it
to the recursive part during the evaluation, thereby essentially reducing the problem
to the rst solution above. Algorithms for doing this kind of optimization for recursive
queries are well known, but vendors might choose to not implement them.
13.23 Using Datalog, write the query that nds all students who took some course from
Professor Joe Public.
Solution:
Answer(?SN) :- Student(?Id,?SN,?Addr,?Status),
Transcript(?Id,?Crs,?Semester,?Grade),
Teaching(?PId,?Crs,?Semester),
Professor(?PId,Joe Public,?Dept).
13.24 Use Datalog to nd all students who took all classes ever oered by Professor Joe
Public.
Solution:
Answer(?SN) :- Student(?Id,?SN,?Addr,?Status),
not StudWhoDidNotTakeSomeClass(?Id).
StudWhoDidNotTakeSomeClass(?Id) :-
141
142
CHAPTER 13
Student(?Id,?SN,?Addr,?Status),
Teaching(?PId,?Crs,?Sem),
Professor(?PId,Joe Public,?Dept),
not TookClass(?Id,?Crs,?Sem).
TookClass(?Id,?Crs,?Sem) :- Transcript(?Id,?Crs,?Sem,?Gr).
13.25 Show how to express the relational operators of projection, selection, Cartesian
product, natural join, union, and set-dierence using Datalog.
Solution:
Projection on the rst and third arguments of relation Rel with four attributes:
Projection(?A,?C) :- Rel(?A,?B,?C,?D).
Selection on the second attribute of the form ?B=1 and ?B > 12:
Selection1(?A,1,?C,?D) :- Rel(?A,1,?C,?D).
Selection2(?A,?B,?C,?D) :- Rel(?A,?B,?C,?D), ?B > 12.
Cartesian product and natural join (for join, we assume that the second attribute of
Rel1 has the same name as the rst attribute of Rel2:
Flight(?F,?T) :- DirectFlight(?F,?T).
Flight(?F,?T) :- DirectFlight(?F,?I), Flight(?I,?T).
Then we dene the required query:
FlightsWithTwoOrMoreStops(?F,?T) :DirectFlight(?F,?S1),
DirectFlight(?S1,?S2),
Exercises
Flight(?S2,?T).
13.27 Using Datalog, express the bill of materials query described in Exercise 13.21.
Solution:
Here the student needs to explore the arithmetic operators of Datalog. We use the
usual operator is, which evaluates arithmetic expressions.
PartListCount(?P,?Qty) :-
?Qty = sum(?Q,?P,PrimitiveSubpartCounts(?P,?Sub,?Q)).
13.28 Express the query of Exercise 13.22 in Datalog.
Solution:
Here the student needs to explore the arithmetic operators of Datalog. We use the
usual operator is, which evaluates arithmetic expressions.
First, we dene the transitive closure of DirectFlight:
Flight(?F,?T,?Dist) :- DirectFlight(?F,?T,?Dist).
Flight(?F,?T,?D) :DirectFlight(?F,?I,?D1), Flight(?I,?T,?D2),
?D is ?D1+?D2.
The required query is now
143
14
Object Databases
EXERCISES
14.1
...
SemestersOffered CHAR(6) MULTISET,
...
)
or
class Course {
...
attribute Set<String> SemestersOffered;
...
}
b. It would be convenient to express a relationship (in the ODMG style) between two
objects.
Solution:
The relationship Requires in Figure 4.33, page 114:
class Course {
...
relationship Set<Course> Requires
...
146
CHAPTER 14
Object Databases
}
c. It would be convenient to use inheritance.
Solution:
The class Class inherits from Course:
CREATE TYPE Class UNDER Course AS (
...
)
or
class Class extends Course {
...
}
14.2
Specify an appropriate set of classes for the Student Registration System. Do this
using rst UDTs of SQL:2003 and then the ODL language of ODMG.
Solution:
We modify the tables dened in Section 4.8 in two ways: Using references to types
instead of foreign key constraints and by making use of set-valued attributes.
CREATE TYPE Course AS (
CrsCode CHAR(6),
DeptId CHAR(4) NOT NULL,
CrsName CHAR(20) NOT NULL,
creditHours INTEGER NOT NULL,
SemestersOffered CHAR(6) MULTISET,
Requires REF(Course) MULTISET,
PRIMARY KEY (CrsCode)
)
CREATE TYPE Class UNDER Course AS (
CrsCode CHAR(6),
SectionNo INTEGER,
Semester CHAR(6),
Year INTEGER,
ClassTime CHAR(6),
Enrollment INTEGER,
MaxEnrollment INTEGER,
Textbook CHAR(50),
TaughtIn REF(Room),
Instructor REF(Faculty),
PRIMARY KEY (CrsCode, SectionNo, Semester, Year)
Exercises
)
CREATE TYPE Room AS (
ClassroomId INTEGER,
Seats INTEGER,
PRIMARY KEY(ClassroomId)
)
CREATE TYPE Student AS (
Id CHAR(9),
Address CHAR(50),
Name CHAR(20),
Password CHAR(10),
Transcript REF(TranscriptRecord) MULTISET,
PRIMARY KEY(Id)
)
CREATE TYPE TranscriptRecord AS (
Grade CHAR(1),
Class REF(Class)
)
CREATE TYPE Faculty AS (
Id CHAR(9),
Name CHAR(20),
Address CHAR(50),
DeptId CHAR(4),
Password CHAR(10),
PRIMARY KEY(Id)
)
The ODMG version of the Student Registration Schema is as follows:
class Course
(extent CourseExt
keys
CrsCode)
{
attribute String CrsCode;
attribute String DeptId;
attribute String CrsName;
attribute Integer creditHours;
attribute Set<String> SemestersOffered;
relationship Set<Course> Requires;
147
148
CHAPTER 14
Object Databases
{
attribute String CrsCode;
attribute Integer SectionNo;
attribute String Semester;
attribute Integer Year;
attribute String ClassTime;
attribute Integer Enrollment;
attribute Integer MaxEnrollment;
attribute String Textbook;
relationship Room
TaughtIn;
relationship Faculty Instructor;
}
class Room
(extent RoomExt
keys
ClassroomId)
{
attribute Integer ClassroomId;
attribute Integer Seats;
}
class Student
(extent StudentExt
keys
Id)
{
attribute String Id;
attribute String Address;
attribute String Name;
attribute String Password;
relationship Set<TranscriptRecord> Transcript;
}
class TranscriptRecord
(extent TranscriptRecordExt)
{
attribute
relationship
String
Class
Grade;
Class;
}
class Faculty
(extent ProfessorExt
keys
Id)
Exercises
{
attribute
attribute
attribute
attribute
attribute
String
String
String
String
String
Id;
Name;
Address;
DeptId;
Password;
}
14.3
Explain the dierence between the object id in an object database and the primary
key of a relation in a relational database.
Solution:
In an object database, each object has a unique and immutable identity, called its
object id (oid). It which is assigned by the system when the object is created and does
not change during the life of that object.
A primary key might change (a person might change her social security number),
and it is typically assigned by the programmer, not DBMS.
14.4
Explain the dierent senses in which the objects in an object database can be considered
equal.
Solution:
1.
2.
14.5
Two objects can have the same oid, in which case they are the same object.
They can have dierent oids, but both have the same attributes and relationships
and the values of these attributes/relationships can be the same for both objects.
In this case, we can have two further possibilities when we look deeper at what
it means for the results returned by two relationships to be equal. Since these
results are objects, they can be equal in the sense (1) or (2), which gives rise to
two dierent notions.
A relational database might have a table called Accounts with tuples for each
account and might support stored procedures deposit() and withdraw(). An
object database might have a class (a UDT) called Accounts with an object for
each account and methods deposit() and withdraw(). Explain the advantages and
disadvantages of each approach.
Solution:
In an object-oriented database, methods would be stored together with the object.
Although in both cases the method is likely to be executed on the server, an object
DBMS might execute the method on the client side, if it supports the feature of code
shipment between machines.
Also, in an ODMS we can use path expressions, such as P.deposit(), to invoke
methods, which is slightly more convenient.
14.6
149
150
CHAPTER 14
Object Databases
Solution:
In the rst example, replace Person with Employee. In the second, add the attribute
Floor of type INTEGER to the type of Address. In the last example, add the attribute
Manager of type Person to the top-level type.
14.7
Give an example of an object that belongs to the domain of the type [Name:
STRING, Children: {Person}, Cars: {[Make: STRING, Model: STRING,
Year: STRING]}]. Consider a supertype [Name: STRING, Cars: {[Make: STRING,
Model: STRING]}] of that type. Show how one can obtain an object of the second
type from the object of the rst type, which you constructed earlier.
Solution:
An object of the rst type can be
Consider the following type, which describes projects: [Name: STRING, Members: {Person}, Address: [Building: INTEGER, Room: INTEGER]]. Use
SQL:1999/2003 to specify the UDT corresponding to this type.
Solution:
Let us assume that the type is called ProjectType.
CREATE TYPE ProjectType AS (
Name CHAR(20),
Members REF(Person) MULTISET,
Address ROW(Building INTEGER, Room INTEGER) );
14.9
Use the UDT constructed in Exercise 14.8 to answer the following query: List the
names of all projects that have more than ve members.
Solution:
Assume that we have a table, Project, of type ProjectType.
SELECT P.Name
FROM
Project P
WHERE count(P->Id) > 5
14.10 Suppose that the Accounts class in the object database of the previous example has
child classes SavingsAccounts and CheckingAccounts and that CheckingAccounts has a child class EconomyCheckingAccounts. Explain how the semantics
of inheritance aects the retrieval of objects in each class. (For example, what classes
Exercises
151
need to be accessed to retrieve all checking account objects that satisfy a particular
predicate?)
Solution:
Dierent accounts can be in dierent classes. A query that potentially might need to
access every account would have to access the classes Accounts, SavingsAccounts,
CheckingAccounts, EconomyCheckingAccounts.
14.11 Use SQL:2003 (with the MULTISET construct, if necessary) to complete the schema
partially dened in Section 14.4. Include UDTs for the following tables: Person,
Student, Course, Professor, Teaching, and Transcript. Your solution
should follow the object-oriented design methodology. Repeating the statements from
Chapters 3 and 4, which use SQL-92, is not acceptable.
Solution:
The objective of this exercise is to have the students use an object-oriented design and
not merely repeat the SQL-92 statements from Chapters 3 and 4.
CREATE TYPE PersonType AS (
Name CHAR(20),
Address ROW(Number INTEGER, Street CHAR(20), ZIP CHAR(5)) );
-----
Id INTEGER,
Status CHAR(2),
Transcript TranscriptRecordType MULTISET );
CREATE TABLE Student OF StudentType;
152
CHAPTER 14
Object Databases
CrsCode CHAR(6),
DeptId CHAR(4),
CrsName CHAR(20),
Description CHAR(50) );
Note: we dene an extra oid attribute , course_oid, using REF IS
because Teaching and TranscriptRecordType references course
objects. Without the explicit oid attribute we will not be able to populate
Teaching and Student (which uses TranscriptRecordType)
with tuples that have correct references to Course objects
CREATE TABLE Course OF CourseType
------
REF IS course_oid;
SELECT S.Name
FROM Student S
WHERE 5 < (SELECT count(S1.Transcript->Course)
FROM
Student S1
WHERE S1.Transcript->Course.DeptId = MAT
AND S1.Id = S.Id)
Exercises
Solution:
c. Write a method that, for each student, computes the average grade. This method
requires the value() method that you constructed for the previous problem.
Solution:
14.13 Use SQL:2003 and its MULTISET construct to represent a bank database with UDTs
for accounts, customers, and transactions.
Solution:
153
154
CHAPTER 14
Object Databases
SELECT
FROM
WHERE
A.Number
Accounts A
A.Owners.Address.Zip = 12345
b. Find the set of all customers satisfying the property that for each the total value of
his or her accounts is at least |S1,000,000.
Solution:
SELECT
FROM
WHERE
C.Name
Customers C
sum(C.Accounts->Balance) > 1000000
14.15 Explain the dierence between a set object and a set of objects.
Solution:
A set-object is a single object, which represents a set. The elements of that set are
other objects. A set of objects is just what it says: a set of distinct objects it is not
an object in its own right.
14.16 a. Explain the dierence between ODMG attributes and relationships.
Solution:
ODMG distinguishes between attributes and relationships. An attribute refers
to some value (not an object) that is stored within the object being specied. A
relationship refers to some other object stored in the database and species how that
Exercises
object is related to the object being specied. For instance, the Person denition
includes two relationships. The Spouse relationship refers to another Personobject, (stored separately) that correspond to its spouse. The Child relationship
refers to the set of Person objects (also stored separately) that correspond to its
children.
b. Explain the dierence between ODMG relationships and E-R relationships.
Solution:
The ODMG term relationship clashes with the use of this term in the E-R model.
ODMG relationships and attributes are quite similar, except that relationships point
to objects instead of values. In contrast, E-R relationships are similar to objects.
They have their own structure, which is represented using attributes and roles. In
fact, the notion of a role in E-R corresponds to the ODMG notion of relationship,
because roles are essentially attributes that point to entities (instead of primitive
types like integers and strings).
14.17 Explain the concept of type consistency of a path expression.
Solution:
Type consistency means that the type of any prex in a path expression must be
consistent with the use of the next attribute. For example, earlier we used the path
expression
S.Address.StName
to nd street names where people named Smith live. Type consistency here means
that the type of the variable S is consistent with the use of the attribute Address and
the type of the prex S.Address is consistent with the use of the attribute StName.
Type consistency of this expression follows from the following facts:
1.
2.
3.
4.
the
the
the
the
14.18 Add the appropriate inverse to the Spouse relationship in the Person denition given
in Section 14.5.1 on page 546.
Solution:
155
156
CHAPTER 14
Object Databases
relationship Person Spouse
inverse Person::Spouse;
relationship Set <Person> Child;
class Person {
attribute Integer Id;
attribute String Name;
relationship Person Spouse;
...
}
b. Give an OQL query that returns the name of a particular persons spouse.
Solution:
SELECT P.Spouse.Name
FROM PersonExt P
WHERE P.Id = 123456789
class Account {
attribute Integer AcctId;
relationship Set<Person> Owner;
relationship Set<TransactionActivity> Transactions
inverse TransactionActivity::ActivityAccount;
}
class TransactionActivity {
Exercises
attribute enum TransType {deposit,withdraw} Type;
attribute Float
Amount;
attribute Date
ActivityDate;
relationship Account ActivityAccount
inverse Account::Transactions;
}
Note that Transactions and ActivityAccount are declared to be inverses
of each other. This means that if ta is a transaction activity and ac is an account
such that ta ac.Transactions then ac = ta.ActivityAccount , and vice versa,
if ac = ta.ActivityAccount then ta ac.Transactions .
b. Give an example of a database instance satisfying that description.
Solution:
An account:
(#123,
[12345,
{#p4345, #p0987},
{#t435, #t8132}
])
...
Some transactions:
SELECT A.AcctId
FROM AccountExt A, A.Transactions T
WHERE T.Type = withdraw AND T.Amount > 10000
14.21 Give an OQL query that returns the names of all spouses of all grandchildren of the
person with SSN 123-45-6789 in the Person denition given in Section 14.5.1 on
page 546.
157
158
CHAPTER 14
Object Databases
Solution:
SELECT
FROM
WHERE
distinct S.Child.Child.Spouse.Name
PersonExt S
S.SSN = 123-45-6789
14.22 Consider the class Person with an additional attribute, age. Write an OQL query
that, for each age, produces a count of people of this age. Use two methods: with
the GROUP BY clause and without it (using a nested query in SELECT). Describe a
plausible query evaluation strategy in each case and explain which query will run
faster.
Solution:
In the rst case, the DBMS is unlikely to gure out that it needs to count the Ids,
so it would probably sort (or hash) the extent of the class Person on age and then
for each value of this attribute it would execute the subquery. This would result in
another access to the extent of the class Person.
In the second case, the ODBMS will know that it has to sort the class Person
on age and then it will compute the result in one scan of the extent of that class.
14.23 Write an OQL query that, for each major, computes the number of students who have
that major. Use the Student class dened in (14.14) on page 552.
Solution:
Exercises
We could also write this query using nested queries as follows:
SELECT DISTINCT Major: M,
count: count(SELECT S2.Id
FROM StudentExt S2
WHERE M IN S2.Major)
FROM StudentExt S, S.Major M
14.24 E-R diagrams can be used for designing class denitions for object databases. Design
ODMG class denitions for the E-R diagrams in Figure 4.1, Figure 4.6, and Figure 4.36.
Solution:
{
attribute Integer SSN;
attribute String Name;
attribute Date
DOB;
}
class Employee extends Person
(extent EmployeeExt)
{
attribute String Department;
attribute Float Salary;
}
class Student extends Person
(extent StudentExt)
{
attribute Date
attribute Float
StartDate;
GPA;
159
160
CHAPTER 14
Object Databases
}
class Secretary extends Employee
(extent SecretaryExt)
{
}
class Technician extends Employee
(extent TechnicianExt)
{
attribute String
Specialization;
}
class Freshman extends Student
(extent FreshmanExt)
{
}
class Sophomore extends Student
(extent SophomoreExt)
{
attribute String
Major;
}
class Junior extends Student
(extent JuniorExt)
{
attribute String
Major;
}
class Senior extends Student
(extent SeniorExt)
{
attribute
String
Major;
relationship Employee Advisor;
}
// For Figure 4.36
class Semester : Object
(extent SemesterExt)
{
attribute
Integer
Enrollment;
relationship Set<Date> Holidays;
}
class Course : Object
(extent CourseExt)
{
... ... ...
}
struct TranscriptRecord
Exercises
Grade;
String
Semester Semester;
Course
Course;
}
class Student extends Person
(extent StudentExt)
{
attribute
Set<TranscriptRecord>
Enrolled;
}
14.25 Consider the following ODL denitions:
class Person : Object
( extent PersonExt ) : PERSISTENT;
{
attribute String Name;
{
attribute Integer Id;
attribute Set<TranscriptRecord> Transcript;
String CrsCode;
float Grade;
String Semester;
}
Write the following query with and without the GROUP BY clause: List all students
with their corresponding average grade.
Solution:
161
162
CHAPTER 14
Object Databases
The second query just complicates things all for the sake of being able to use GROUP
BY.
15
XML and Web Data
EXERCISES
15.1
Use XML to represent the contents of the Student relation in Figure 3.2, page 36.
Specify a DTD appropriate for this document. Do not use the representation proposed
by the SQL/XML specication discussed in Section 15.4.4.
Solution:
Two representations are reasonable. One uses elements only:
<!DOCTYPE Students [
<!ELEMENT Students (Student*)>
<!ELEMENT Student (Id,Name,Address,Status)>
164
CHAPTER 15
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
Id (#PCDATA)>
Name (#PCDATA)>
Address (#PCDATA)>
Status (#PCDATA)>
]>
For the second representation, the DTD can be:
<!DOCTYPE Students [
<!ELEMENT Students (Student*)>
<!ELEMENT Student EMPTY>
<!ATTLIST Student
Id ID #REQUIRED
Name CDATA #REQUIRED
Address CDATA #IMPLIED
Status CDATA #IMPLIED>
]>
15.2
Specify a DTD appropriate for a document that contains data from both the Course
table in Figure 4.34, page 116, and the Requires table in Figure 4.35, page 117. Try
to reect as many constraints as the DTDs allow. Give an example of a document that
conforms to your DTD.
Solution:
One good example of such a document is shown below. Note that it does not try
to mimic the relational representation, but instead courses are modeled using the
object-oriented approach.
<Courses>
<Course CsrCode="CS315" DeptId="CS"
CrsName="Transaction Processing" CreditHours="3">
<Prerequisite CrsCode="CS305" EnforcedSince="2000/08/01"/>
<Prerequisite CrsCode="CS219" EnforcedSince="2001/01/01"/>
</Course>
<Course>
.....
</Course>
.....
</Courses>
An appropriate DTD would be:
<!DOCTYPE Courses [
<!ELEMENT Courses (Course*)>
<!ELEMENT Course (Prerequisite*)>
Exercises
<!ATTLIST Course
CrsCode ID #REQUIRED
DeptId CDATA #IMPLIED
CrsName CDATA #REQUIRED
CreditHours CDATA #REQUIRED >
<!ATTLIST Prerequisite
CrsCode IDREF #REQUIRED
EnforcedSince CDATA #REQUIRED>
]>
15.3
Restructure the document in Figure 15.4, page 591, so as to completely replace the
elements Name, Status, CrsCode, Semester, and CrsName with attributes in the
appropriate tags. Provide a DTD suitable for this document. Specify all applicable ID
and IDREF constraints.
Solution:
The Student elements would now look as follows:
<!DOCTYPE Report [
<!ELEMENT Report (Students,Classes,Courses)>
<!ELEMENT Students (Student*)>
<!ELEMENT Classes (Class*)>
<!ELEMENT Courses (Course*)>
<!ELEMENT Student (CrsTaken*)>
<!ELEMENT CrsTaken EMPTY>
<!ELEMENT Class (ClassRoster)>
<!ELEMENT Course EMPTY>
<!ELEMENT ClassRoster EMPTY>
165
166
CHAPTER 15
<simpleType name="ListsOfStrings">
<list itemType="string" />
</simpleType>
<simpleType name="ListsOfLength7">
<restriction base="my:ListsOfStrings">
<length value="7"/>
</restriction>
</simpleType>
b. A type whose domain consists of lists of strings, where each string is of length seven
Solution:
Assume that the target namespace is denoted by the prex my.
<simpleType name="StringsOfLength7">
<restriction base="string">
<length value="7"/>
</restriction>
</simpleType>
<simpleType name="ListsOfStringsOfLength7">
<list itemType="my:StringsOfLength7" />
</simpleType>
c. A type whose domain is a set of lists of strings, where each string has between seven
and ten characters and each list has between seven and ten elements
Exercises
Solution:
Assume that the target namespace is denoted by the prex my.
<simpleType name="StringsOfLength7-10">
<restriction base="string">
<minLength value="7"/>
<maxLength value="10"/>
</restriction>
</simpleType>
<simpleType name="ListsOfStringsOfLength7-10">
<list itemType="my:StringsOfLength7-10" />
</simpleType>
<simpleType name="ListsOfLength7-10 of StringsOfLength7-10">
<restriction base="my:ListsOfStringsOfLength7-10">
<minLength value="7"/>
<maxLength value="10"/>
</restriction>
</simpleType>
d. A type appropriate for the letter grades that students receive on completion of a
courseA, A, B+, B, B, C+, C, C, D, and F. Express this type in two dierent
ways: as an enumeration and using the pattern tag of XML Schema.
Solution:
<simpleType name="gradesAsEnum">
<restriction base="string">
<enumeration value="A"/>
<enumeration value="A-"/>
<enumeration value="B+"/>
..........
</restriction>
</simpleType>
<simpleType name="gradesAsPattern">
<restriction base="string">
<pattern value="(A-?|[BC][+-]?|[DF])"/>
</restriction>
</simpleType>
In the gradesAsPattern representation, (...) represent complex alternatives of
patterns separated by | (W3C has adopted the syntax of regular expressions used in
167
168
CHAPTER 15
Perl), [...] represent simple alternatives (of characters), and ? represents zero or
one occurrence, as usual in regular expressions.
15.5
Use the key statement of XML Schema to dene the following key constraints for the
document in Figure 15.4 on page 591:
a. The key for the collection of all Student elements
Solution:
Assume that any student in the document of Figure 15.4 is uniquely identied by the
last name and the status. Dene this key constraint.
Solution:
Exercises
169
Use the keyref statement of XML Schema to dene the following referential integrity
for the document in Figure 15.4:
a. Every course code in a CourseTaken element must refer to a valid course.
Solution:
170
CHAPTER 15
Rearrange the structure of the Class element in Figure 15.4 so that it becomes
possible to dene the following referential integrity: every student Id mentioned in a
Class element references a student from the same document.
Solution:
One way to rearrange this element is to represent each student Id in the class using an
attribute:
<Class>
<CrsCode>CS308</CrsCode><Semester>F1997</Semester>
<Taker Id="s666666666"/> <Taker Id="s987654321"/>
</Class>
If we were to use DTDs, then we could declare:
Exercises
where adm is the namespace prex used for the document in Figure 15.4 and
StudentKey is the primary key dened for Student elements:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tnc="http://xyz.edu/transcripts-N-courses"
targetNameSpace="http://xyz.edu/transcripts-N-courses">
<element name="TranscriptsAndCourses">
<complexType>
<sequence>
<element name="Transcripts">
<complexType>
<sequence>
<element name="Transcript"
type="tnc:transcriptType"
minOccurs="0"
maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Classes" type="tnc:classListType">
<complexType>
<sequence>
<element name="Class" type="tnc:classType"
minOccurs="0"
maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
171
172
CHAPTER 15
</element>
<!-- Now define transcriptType and classType -->
<complexType name="transcriptType">
<sequence>
<element name="Student">
<complexType>
<attribute name="StudId" type="integer"/>
<attribute name="Name" type="string"/>
</complexType>
</element>
<element name="CrsTaken"
minOccurs="0" maxOccurs="unbounded">
<attribute name="CrsCode" type="string"/>
<attribute name="Semester" type="string"/>
<attribute name="Grade" type="string"/>
</element>
</sequence>
</complexType>
<complexType name="classType">
<sequence>
<element name="CrsName" type="string"/>
<element name="Instructor" type="string"/>
</sequence>
<attribute name="CrsCode" type="string"/>
<attribute name="Semester" type="string"/>
</complexType>
</schema>
15.11 Use XML Schema to represent the fragment of the relational schema in Figure 3.6,
page 43. Include all key and foreign-key constraints.
Solution:
We use a combination of named and anonymous types to dene the schema.
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:stureg="http://somewhere.edu/stureg-fragment"
targetNameSpace="http://somewhere.edu/stureg-fragment">
<element name="Everything">
<complexType>
<all>
<element name="Students" type="stureg:stuList"/>
<element name="Professors" type="stureg:profList"/>
<element name="Courses" type="stureg:crsList"/>
<element name="Transcripts" type="stureg:trnList"/>
Exercises
173
174
CHAPTER 15
<sequence>
<element name="Student"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="Id" type="integer"/>
<attribute name="Name" type="string"/>
<attribute name="Address" type="string"/>
<attribute name="Status" type="string"/>
</complexType>
</element>
</sequence>
</complexType>
<complexType name="profList">
<sequence>
<element name="Professor"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="Id" type="integer"/>
<attribute name="Name" type="string"/>
<attribute name="DeptId" type="DeptIdType"/>
</complexType>
</element>
</sequence>
</complexType>
<complexType name="crsList">
<sequence>
<element name="Course"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="CrsCode" type="CrsCodeType"/>
<attribute name="DeptId" type="DeptIdType"/>
<attribute name="CrsName" type="string"/>
<attribute name="Descr" type="string"/>
</complexType>
</element>
</sequence>
</complexType>
<complexType name="trnList">
<sequence>
<element name="Transcript"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="StudId" type="integer"/>
<attribute name="CrsCode" type="CrsCodeType"/>
<attribute name="Semester" type="SemesterType"/>
Exercises
175
176
CHAPTER 15
15.12 Write an XML Schema specication for a simple document that lists stockbrokers with
the accounts that they handle and lists client accounts separately. The information
about each broker includes the broker Id, name, and a list of accounts. The information
about each account includes the account Id, the owners name, and the account
positions (i.e., stocks held in that account). To simplify matters, it suces to list the
stock symbol and quantity for each account position. Use ID, IDREF, and IDREFS to
specify referential integrity.
Solution:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:brk="http://somebrokerage.com/documents"
targetNameSpace="http://somebrokerage.com/documents">
<element name="Brokerage">
<complexType>
<sequence>
<element name="Broker" type="brk:brokerType"
minOccurs="0" maxOccurs="unbounded"/>
<element name="Account" type="brk:accountType"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="brokerType">
<attribute name="Id" type="ID" />
<attribute name="Name" type="string" />
<attribute name="Accounts" type="IDREFS" />
</complexType>
<complexType name="accountType">
<element name="Positions" />
<complexType>
<sequence>
<element name="Position"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="stockSym" type="string" />
<attribute name="qty" type="integer" />
</complexType>
</element>
</sequence>
</complexType>
</element>
<attribute name="Id" type="ID" />
<attribute name="Owner" type="string" />
</complexType>
Exercises
</schema>
15.13 Write a sample XML document, which contains
Write a DTD for this document and an XML schema. Express all key and referential
constraints. Choose your representation in such a way as to maximize the number of
possible key and referential constraints representable using DTDs.
Solution:
Note that to satisfy the requirement that the key and referential constraints must be
representable using DTDs, we must use attributes for part and supplier Ids.
<MyDocument>
<Part Name="screw" Id="1234" />
<Part Name="paint" Id="4321" />
<Part Name="hanger" Id="9876" />
<Part Name="brush" Id="30900" />
<Supplier Name="Acme" Id="AC452" />
<Supplier Name="ACE" Id="5432345" />
<Project Name="Remodeling">
<Uses Part="1234" Supplier="5432345" Qty="2" />
<Uses Part="9876" Supplier="5432345" Qty="5" />
<Uses Part="1234" Supplier="AC452" Qty="3" />
</Project>
<Project Name="Painting">
<Uses Part="4321" Supplier="AC452" Qty="1" />
<Uses Part="30900" Supplier="AC452" Qty="3" />
</Project>
</MyDocument>
<!DOCTYPE MyDocument [
<!ELEMENT MyDocument (Part*,Supplier*,Project*)>
<!ELEMENT Part EMPTY>
<!ELEMENT Supplier EMPTY>
<!ELEMENT Project (Uses*)>
<!ELEMENT Uses EMPTY>
<!ATTLIST Part
Name CDATA #REQUIRED
Id ID #REQUIRED>
<!ATTLIST Supplier
Name CDATA #REQUIRED
177
178
CHAPTER 15
Id ID #REQUIRED>
<!ATTLIST Project Name CDATA #REQUIRED>
<!ATTLIST Uses
Part IDREF #REQUIRED
Supplier IDREF #REQUIRED
Qty CDATA #REQUIRED>
]>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:doc="http://mydocuments.com/"
targetNameSpace="http://mydocuments.com/">
<element name="MyDocument">
<complexType>
<sequence>
<element name="Part"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="Name" type="string"/>
<attribute name="Id" type="string"/>
</complexType>
</element>
<element name="Supplier"
minOccurs="0" maxOccurs="unbounded">
<complexType>
<attribute name="Name" type="string"/>
<attribute name="Id" type="string"/>
</complexType>
</element>
<element name="Project" type="doc:projType"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<key name="PartKey">
<selector xpath="Part" />
<field xpath="@Id" />
</key>
<key name="SupplierKey">
<selector xpath="Supplier" />
<field xpath="@Id" />
</key>
<keyref name="RefToPart" refer="doc:PartKey">
<selector xpath="Project"/>
<field xpath="@Part"/>
</keyref>
Exercises
179
//Student[../CrsTaken/@CrsCode="MAT123"
and ends-with(@StudId,"987")]
b. Find all Student elements whose rst names are Joe and who took fewer than three
courses.
Solution:
180
CHAPTER 15
Solution:
//CrsName[../Instructor="Mary Doe"
and ../@Semester="F1995"]/text()
Note that we use text() at the end to get the text (course names themselves) as
opposed to CrsCode element nodes.
b. Find the set of all document nodes that correspond to the course names taught in
fall 1996 or all instructors who taught MAT123.
Solution:
//CrsName[../@Semester="F1996"]
| //Instructor[../@CrsCode="MAT123"]
c. Find the set of all course codes taught by John Smyth in spring 1997.
Solution:
Exercises
15.17 Use XSLT to transform the document in Figure 15.21 into a well-formed XML
document that contains the list of courses that were taught by Ann White in fall
1997. Do not include the Instructor child element in the output, and discard the
Semester attribute.
Solution:
181
182
CHAPTER 15
Solution:
Exercises
its attributes and the entire structure beneath are preserved and the element itself is
repeated twice.
Solution:
183
184
CHAPTER 15
<Student>
<tuple>
<Id value="s111111111"/> <Name value="John Doe"/>
<Address value="123 Main St."/> <Status value="U1"/>
</tuple>
Exercises
.
.
.
</Student>
Formulate the following queries using XQuery:
a. Produce the list of all students who live on Main Street.
Solution:
<StudentList>
{
FOR $s IN doc("http://xyz.edu/student.xml")//tuple
WHERE contains($s/Address/@value,"Main St")
RETURN $s
}
</StudentList>
b. Find every course whose CrsCode value does not match the Id of the department
that oers the course. (For instance, the course IS315 in information systems might
be oered by the Computer Science (CS) Department.)
Solution:
<CrsList>
{
FOR $c IN doc("http://xyz.edu/course.xml")//tuple
WHERE NOT contains($c/CrsCode/@value,$c/DeptId/@value)
RETURN $c
}
</CrsList>
c. Create a list of all records from the Teaching relation that correspond to courses
taught in the fall semester.
Solution:
<Teaching>
{
FOR $t IN doc("http://xyz.edu/teaching.xml")//tuple
WHERE starts-with($t/Semester/@value,"F")
RETURN $t
}
185
186
CHAPTER 15
</Teaching>
15.24 Using the document structure described in Exercise 15.23, formulate the following
queries in XQuery:
a. Create the list of all professors who ever taught MAT123. The information must
include all attributes available from the Professor relation.
Solution:
<ProfList>
{
FOR $p IN doc("http://xyz.edu/professor.xml")//tuple
WHERE doc("http://xyz.edu/teaching.xml")
//tuple[ProfId/@value=$p/Id/@value
and CrsCode/@value="MAT123"]
RETURN $p
}
</ProfList>
b. Create the list of all students (include student Id and name) who took a course from
John Smyth and received an A.
Solution:
<StudList>
{
LET $pset := doc("http://xyz.edu/professor.xml")
//tuple[Name/@value="John Smyth"]
LET $tset := doc("http://xyz.edu/teaching.xml")
//tuple[ProfId/@value=$pset/Id/@value]
FOR $s IN doc("http://xyz.edu/student.xml")//tuple,
$p IN $pset,
$t IN $tset
WHERE doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value
and CrsCode/@value=$t/CrsCode/@value
and Semester/@value=$t/Semester/@value
and Grade/@value="A"]
RETURN
<tuple>
$s/Id,
$s/Name
</tuple>
}
Exercises
187
</StudList>
Note that the problem statement requires that we return only the name and the Id,
so we cannot just return $s.
c. Create the list of all courses in which Joe Public received an A.
Solution:
<CrsList>
{
LET $sset := doc("http://xyz.edu/student.xml")
//tuple[Name/@value="Joe Public"]
FOR $c IN doc("http://xyz.edu/course.xml")//tuple,
$s IN $sset
WHERE doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value
and Grade/@value="A"]
RETURN $c
}
</CrsList>
15.25 Use the document structure described in Exercise 15.23 to formulate the following
queries in XQuery:
a. Create an XML document that lists all students by name and, for each student, lists
the students transcript. The transcript records must include course code, semester,
and grade.
Solution:
<StudList>
{
FOR $s IN doc("http://xyz.edu/student.xml")//tuple
LET $tr := doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value]
RETURN
<Student>
$s/Name
{
FOR $t IN $tr
RETURN
<Transcript>
$t//CrsCode,
$t//Semester,
188
CHAPTER 15
$t//Grade
</Transcript>
}
</Student>
}
</StudList>
b. Create a document that for each professor lists the name and classes taught. (A
class is identied by a course code and semester.)
Solution:
<ProfList>
{
FOR $p IN doc("http://xyz.edu/professor.xml")//tuple
LET $tch := doc("http://xyz.edu/teaching.xml")
//tuple[ProfId/@value=$p/Id/@value]
RETURN
<Professor>
$p/Name
{
FOR $t IN $tch
RETURN
<Class>
$t//CrsCode,
$t//Semester
</Class>
}
</Professor>
}
</ProfList>
c. Create a document that lists every course and the professors who taught it. The
course information must include the course name, and the professor information
should include the professors name.
Solution:
<Courses>
{
FOR $c IN doc("http://xyz.edu/courses.xml")//tuple
LET $tch := doc("http://xyz.edu/teaching.xml")
//tuple[CrsCode/@value=$c/CrsCode/@value]
LET $profs := {
Exercises
FOR
189
$t IN $tch,
$p IN doc("http://xyz.edu/professor.xml")
//tuple[Id/@value=$t/ProfId/@value]
}
RETURN
<Course>
$c/CrsName
{
FOR $p IN distinct-values($profs)
RETURN <Prof> $p/Name </Prof>
}
</Course>
}
</Courses>
15.26 Use the document structure described in Exercise 15.23 to formulate the following
queries in XQuery:
a. List all students who took more than three courses.
Solution:
<StudList>
{
FOR $s IN doc("http://xyz.edu/student.xml")//tuple
LET $tr := doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value]
WHERE count($tr) > 3
RETURN $s
}
</StudList>
b. List all students who received an A for more than three courses.
Solution:
<StudList>
{
FOR $s IN doc("http://xyz.edu/student.xml")//tuple
LET $tr := doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value
and Grade/@value="A"]
WHERE count($tr) > 3
RETURN $s
}
190
CHAPTER 15
</StudList>
c. List all professors who gave an A to more than three students.
Solution:
<ProfList>
{
FOR $p IN doc("http://xyz.edu/professor.xml")//tuple
LET $studs := {
FOR
$s IN doc("http://xyz.edu/student.xml")//tuple,
$tch IN doc("http://xyz.edu/teaching.xml")
//tuple[ProfId/@value=$p/Id/@value]
WHERE
doc("http://xyz.edu/transcript.xml")
//tuple[StudId/@value=$s/Id/@value
and Grade/@value="A"
and CrsCode/@value=$tch/CrsCode/@value
and Semester/@value=$tch/Semester/@value]
}
WHERE count(distinct-values($studs)) > 3
RETURN $p
}
</ProfList>
d. List all classes (identied by a course code and semester) where average grade is
higher than B.
Solution:
As in Section 15.4.3, assume that there is a function, numericGrade(), which
returns the numeric value of a grade. For instance, in the U.S. numericGrade("A")
= 4, numericGrade("A-") = 3.66, etc.
Exercises
191
distinct-values(classesFromTranscripts($transcripts//tuple))
LET $grades :=
$transcripts//tuple[CrsCode/@value=$c/CrsCode/@value
and Semester/@value=$c/Semester/@value]
/Grade/@value
WHERE avg(numericGrade($grades)) > numericGrade("B")
RETURN $c
}
</EasyClasses>
e. List all professors whose given grade (the average among all grades the professor
ever assigned to any student) is B or higher. For this problem, you must write an
XQuery function that computes numeric values of letter grades.
Solution:
Assume that http://xyz.edu/auxtypes.xsd contains a denition of the type
Grade (a suitable restriction of xs:string).
DECLARE NAMESPACE xs = "http://www.w3.org/2001/XMLSchema"
DECLARE NAMESPACE xyzaux = "http://xyz.edu/auxtypes.xsd"
DECLARE FUNCTION numericGrade($g AS xyzaux:Grade) AS xs:float
{
IF $g = "A"
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
ELSE IF $g =
THEN RETURN 4
"A-" THEN RETURN 3.66
"B+" THEN RETURN 3.33
"B" THEN RETURN 3
"B-" THEN RETURN 2.66
"C+" THEN RETURN 2.33
"C" THEN RETURN 2
"C-" THEN RETURN 1.66
"D" THEN RETURN 1
"F" THEN RETURN 0
}
<EasyProfessors>
{
FOR $p IN doc("http://xyz.edu/professor.xml")//tuple
LET $transcripts := {
FOR $tch IN doc("http://xyz.edu/teaching.xml")
//tuple[ProfId/@value=$p/Id/@value],
$tr IN doc("http://xyz.edu/transcript.xml")
//tuple[CrsCode/@value=$tch/CrsCode/@value
and Semester/@value=$tch/Semester/@value]
RETURN $tr
192
CHAPTER 15
}
WHERE
}
</EasyProfessors>
f. List all classes (identied by a course code and semester) where the average grade
is less than the professors given grade.
Solution:
First, create a document similar to the one computed in the previous sub-exercise,
where the structure is
<GivenGrades>
<tuple><ProfId value="..."/><Grade value="..."/></tuple>
... ... ...
</GivenGrades>
The Grade element gives the value of the given grade of the professor identied by the
ProfId element. Assume that this document is in file:///tmp/givengrades.xml
<ClassList>
{
FOR $tch IN doc("http://xyz.edu/teaching.xml")//tuple,
$gr IN doc("file:///tmp/givengrades.xml)
//tuple[ProfId/@value=$tch/ProfId/@value]
/Grade/@value
LET $grades :=
doc("http://xyz.edu/transcript.xml")
//tuple[CrsCode/@value=$tch/CrsCode/@value
and Semester/@value=$tch/Semester/@value]
/Grade/@value
WHERE avg(numericGrade($grades)) < $gr
RETURN
<tuple>
{ $tch/CrsCode, $tch/Semester }
</tuple>
}
</ClassList>
15.27 Use the document structure described in Exercise 15.23 to formulate the following
queries in XQuery:
a. List all classes (identied by a course code and semester) where every student
received a B or higher.
Exercises
193
Solution:
194
CHAPTER 15
distinct-values(classesFromTranscripts($transcripts//tuple)),
$s IN doc("http://xyz.edu/student.xml")
//tuple[Id/@value=$sid/StudId/@value]
LET $grades :=
$transcripts//tuple[StudId/@value=$sid/StudId/@value]/Grade/@value
WHERE EVERY $g IN $grades
SATISFIES numericGrade($g) >= numericGrade("B")
RETURN $s
}
</GoodStudents>
15.28 Write an XQuery function that traverses a document and computes the maximum
branching factor of the document tree, that is, the maximal number of children (text
or element nodes) of any element in the document.
Solution:
{
LET $children := $n/node()
LET $maxChildBranching :=
max{FOR $m IN $children RETURN maxBranching($m)}
LET $childCount := count($children)
RETURN
IF $childCount >= $maxChildBranching
THEN $childCount
ELSE $maxChildBranching
}
Recall that the XPath function node() matches every e- or t-node.
15.29 Write an XQuery function that traverses a document and strips element tags. For
instance,
Exercises
working group, we use a roundabout method, by counting the number of e- and
t-children of a node. For text nodes and empty element nodes this count equals 0.
DECLARE NAMESPACE xs = "http://www.w3.org/2001/XMLSchema"
DECLARE FUNCTION isTextNode($n AS node()) AS xs:boolean
{
LET $children := ./node()
IF count($children) = 0
THEN -- it is a text node or an empty element
RETURN
{
-- if descendant-or-self node set has a text node,
-- then the current node must be a text node;
-- else, it is an empty element
IF count(.//text()) > 0 THEN true ELSE
false
{
ELSE -- it is a nonempty element
RETURN false
}
DECLARE FUNCTION stripTags($n AS node()) AS xs:integer
{
LET $children := ./node()
RETURN
IF isTextNode(.)
THEN data(.)
ELSE stripTags($children)
}
15.30 Consider a document that contains a list of professors (name, Id, department Id) and
a separate list of classes taught (professor Id, course code, semester). Use aggregate
functions of XQuery to produce the following documents:
a. A list of professors (name, Id) with the number of dierent courses taught by each
professor.
Solution:
Assume that the document consists of two elements, Professors and Classes,
which in turn are lists of elements Professor and Class, respectively.
<ProfCourseCounts>
{
FOR $p IN
doc("http://xyz.edu/profs-courses.xml")//Professor
LET $courses :=
-- All courses ever taught by $p
distinct-values(
195
196
CHAPTER 15
doc("http://xyz.edu/profs-courses.xml")
//Class[ProfId=$p/Id]/CrsCode
)
RETURN $p <CourseCount>count($courses)</CourseCount>
}
</ProfCourseCounts>
b. A list of departments (department Id) along with the number of dierent courses
ever taught by the professors in them. (The same course taught in dierent semesters
or a course taught in the same semester by dierent professors is counted as one.)
Solution:
<DeptCourseCounts>
{
LET $depts :=
distinct-values(doc("http://xyz.edu/profs-courses.xml")//DeptId)
FOR $d IN $depts
LET $courses := {
-- All courses ever taught by $d
FOR $p IN doc("http://xyz.edu/profs-courses.xml")
//Professor[DeptId=$d/DeptId],
$c IN doc("http://xyz.edu/profs-courses.xml")
//Class[ProfId=$p/Id]/CrsCode
RETURN $c
}
RETURN <DeptCrsCount>$d count(distinct-values($courses))</DeptCrsCount>
}
</DeptCourseCounts>
15.31 Consider the following query:
FOR
WHERE
AND
RETURN
$c IN doc("http://xyz.edu/classes.xml")//Class,
$t IN doc("http://xyz.edu/transcripts.xml")
//CrsTaken
$c/@CrsCode = $t/@CrsCode
$c/@Semester = $t/@Semester
$c/CrsName
Explain why every course name is likely to be output more than once. Can the use
of the distinct-values() function (without any additional features) solve this
problem with duplicates? Explain your answer.
Solution:
If a class has more than one student, there will be multiple transcript records for such
Exercises
a class in the document transcripts.xml. Therefore, the RETURN clause will be
executed for every such record separately.
The distinct-values() function applies to collections produced by XQuery
expressions and eliminates duplicates from them. In our case, the only such collections
occur in the FOR clause. Using
distinct-values(doc("http://xyz.edu/transcripts.xml")//CrsTaken)
as the range for $t will not completely eliminate duplicate transcript records
corresponding to the same class: if the same class has students who received dierent
grades, then these CrsTaken elements will be still considered to be distinct.
So, by itself, distinct-values() cannot always help solve the problem of
duplicates.
15.32 Consider the relational schema in Figure 3.6, page 43. Assume that the contents of
these relations are stored in a single XML column of a relation using the following XML
format: the name of the relation is the top-level element, each tuple is represented as
a tuple element, and each relation attribute is represented as an empty element that
has a value attribute. For instance, the Student relation would be represented as
follows:
<Student>
<tuple>
<Id value="s111111111"/> <Name value="John Doe"/>
<Address value="123 Main St."/> <Status value="U1"/>
</tuple>
.
.
.
</Student>
Formulate the following queries in SQL/XML:
a. Create the list of all professors who ever taught MAT123. The information must
include all attributes available from the Professor relation.
Solution:
b. Create the list of all courses in which Joe Public received an A.
Solution:
c. Create the list of all students (include student Id and name) who took a course from
John Smyth and received an A.
Solution:
15.33 Consider an SQL/XML database schema that consists of two relations:
197
198
CHAPTER 15
Id, an integer
Name, a string
Parts, an XML type that represents the parts supplied by the supplier. Each
Project Parts, an XML type. This attribute represents the list of parts used
by the project and supplied by a supplier. Each part has an Id, a Name, and
a SupplierId.
Use SQL/XML to dene the database schema. Make sure that a CHECK constraint
validates the XML documents inserted into the database using an appropriate XML
Schema document. Then answer the following queries:
a. Find all projects that are supplied by Acme Inc. and that have Joe Public as a
member.
Solution:
b. Find all projects that are not supplied by Acme Inc., that is, none of the parts used
by the project come from Acme Inc.
Solution:
c. Find all project members who participate in every project.
Solution:
d. Find the projects with the highest number of members.
Solution:
15.34 Consider the database depicted in Figure 3.5 on page 39. Use SQL/XML to answer
the following queries:
a. Based on the relation Transcript, output the same information in a dierent
format. The output should have two attributes: StudId and Courses, where
Courses should be of type XML; it is similar to the type used throughout this
chapter for the CourseTaken element (e.g., as in Figure 15.4).
Solution:
b. Repeat the previous query, but use Teaching instead. The output should have
the attributes ProfId and Courses. The latter should be of type XML; it should
describe the courses taught by the corresponding professor with the given ProfId.
Solution:
Exercises
c. Produce a list of professors (Id and Name) along with the list of courses that that
professor teaches in spring 2004. The list of courses should have the XML type and
should include CrsCode and DeptId as attributes of an element and CrsName as a
text node.
Solution:
d. For each course, produce a list of the professors who have ever taught it. The
professor list should have the type XML. Choose your own schema.
Solution:
e. Repeat the previous query, but output only the courses that have been taught by
the largest number of professors.
Solution:
199
16
Distributed Databases
EXERCISES
16.1
16.2
16.3
Explain whether or not the following statement is true: the join of two tables obtained
by a (vertical or horizontal) partitioning of a table, T, can never contain more tuples
than are contained in T.
Solution:
By join in this exercise, we mean natural join.
1.
2.
For a vertically partitioned table, the natural join of all the partitions is exactly
the original table. The natural join of some subset of the partitions yields some
subset of the table.
For a horizonally partitioned table, the natural join of all or any subset of the
partitions is an empty table, because each row of the original table is in exactly
one partition.
16.4
16.5
Give an example of a program at site A that requires the join of two tables, one at
site B and one at site C. State the assumptions needed to justify the result that, as far
202
CHAPTER 16
Distributed Databases
as communication costs are concerned, the best implementation is to ship the table at
site B to site C, do the join there, and then send the result to site A.
Solution:
The three alternatives are
1.
2.
3.
Send the table at site B to site C, do the join there, and then send the result to
site A. The total communication cost is the number of bytes in the table in site
B, plus the number of bytes in the join. (bytesB + bytesJ )
Send the table at site C to site B, do the join there, and then send the result to
site A. The total communication cost is the number of bytes in the table in site
C, plus the number of bytes in the join. (bytesC + bytesJ )
Send both tables to site A and do the join there. The total communication cost is
the number of bytes in the table in site B, plus the number of bytes in the table
in site C. (bytesB + bytesC )
16.6
Exercises
At site C performs a SELECT statement that returns the SSnum and Age of all
employees for which Title = Manager. Call this table T1
Send T1 to site B. Since there are 50 managers, the total amount of information
that must be sent is 50 (9 + 6) or 750 bytes
At site B, perform the join of T1 with Emp21, select those names whose salary is
greater than their age, and send the result to site A.
Since we do not know how many managers have a salary greater than their age we
cannot estimate the communication costs for the third step.
16.8
Design a multidatabase query plan and a set of SQL statements that implement the
query of the previous exercise.
Solution:
1.
At site A, perform a SELECT statement on the database at site C that returns the
SSnum and Age of all employees for which Title = Manager. Since there are
2.
3.
16.9
Show that step 2 of the method used in Section 16.3.1 to perform a join using a semijoin does in fact generate the semi-join. For simplicity, assume that the join we are
attempting is a natural join. That is, prove that
attributes(T1) (T1 T2) = attributes(T1)(T1 attributes(join-condition) (T2))
Solution:
This a special case of the rule for pushing projections through joins in Section 11.2.
For every tuple t T1 T2 there is a tuple t T1 attributes(join-condition) (T2 )
that agrees with t on all attributes of T1 and vice versa. Thus the above two expressions
are equivalent.
203
204
CHAPTER 16
Distributed Databases
16.10 Show that step 3 of the method used in Section 16.3.1 to perform a join using a
semi-join does in fact generate the join. For simplicity, assume that the join we are
attempting is a natural join. In other words, show that
(T1 < T2 ) T2 = (T1 T2)
Solution:
Clearly, attributes(T1) (T1 T2) T1 , so attributes(T1) (T1 T2) T2 (T1 T2).
On the other hand, if a tuple, t, is in T1 T2, it must be a join of a
tuple t1 T1 and t2 T2 . But then t1 attributes(T1) (T1 T2 ) and so t must
be in attributes(T1) (T1 T2) T2. Hence, the opposite inclusion also holds:
attributes(T1) (T1 T2) T2 (T1 T2).
16.11 Design a query plan for the join example in Section 16.3.1, assuming the same table
sizes as in that section, but with the following dierences:
a. An application at site B requested the join.
Solution:
1. If we send Student to site C, compute the join there, and then send the result
to site B, we have to send 540,000 bytes (= 15,000 12 + 20,000 18).
2. If we send Transcript to site B and compute the join there, we have to send
300,000 bytes (= 20,000 15).
Thus alternative 2 is better. If we also allow the alternative of the semi-join described
in that section, except that after the join is performed in step 3 of that protocol, the
result is sent to site B, we have to send 465,000 bytes (= 45,000 + 60,000 + 360,000),
as described in that section Thus alternative 2 is still the best.
b. An application at site C requested the join.
Solution:
1. If we send Student to site C and compute the join there, we have to send
180,000 bytes (= 15,000 12).
2. If we send Transcript to site B, compute the join there, and then send the
result to site C, we have to send 660,000 bytes (= 20,000 15 + 20,000 18).
Thus alternative 1 is better. If we also allow the alternative of the semi-join described
in that section, except that after the join is performed in step 3 of that protocol, the
result is just kept at site C, we have to send just 105,000 bytes (= 45,000 + 60,000).
The semi-join is thus the best alternative.
16.12 Show that the method of designing horizontal partitions described in Section 16.2.1
works as advertised.
Solution:
Since in that method every tuple of T satises some Ci , it follows that T Ti . Since,
clearly, every Ti is a subset of T, the other inclusion holds as well.
Exercises
16.13 Show that the semi-join operation is not commutative, that is, T1 semi-joined with T2
is not the same as T2 semi-joined with T1 .
Solution:
The semi-join is the projection over the columns of T1 of the join of T1 and T2 :
attributes(T1) (T1 join-condition T2 )
The projection over the attributes of T1 is dierent than the projection over the
attributes of T2 , unless T1 and T2 happen to share exactly the same attribute set.
16.14 Use the example schema (16.4) on page 703 to design a query for nding the names
of all managers (employees with Title = manager) whose salary is more than
|S20,000, but assume that there are three warehouses. Also assume that the total
number of employees is 100,000, that 5000 of them make over |S20,000, that the total
number of managers is 50, and that 90% of the managers make more than |S20,000.
Solution:
The best alternative is the one given in the text.
1. At each of the warehouse sites, select all tuples for which the title is manager.
2. Send these tables to the headquarters site. Do the join there, Then send the sames
to site A
The number of bytes to be sent is exactly the same as in the text 1,300 bytes from
all the warehouse sites to the headquarters, plus 675 bytes from the headquarters site
to site A, for a total of 1,975 bytes.
16.15 In Section 16.3.2 we pointed out that in a multidatabase system, data could not
be communicated directly from one database site to another and that even indirect
communication was dicult since a query site cannot send data to a database site.
However, the following end run might be considered for computing a semi-join. The
query site, A, executes a SELECT statement at the rst database site, B, which returns
the projection, P, used in computing the semi-join. Site A then uses the projection
to dynamically construct another SELECT statement whose result set is the semi-join.
Using Example 16.3.1 on page 699 of Section 16.3.1 involving tables Student and
Transcript, give the two queries. Under what circumstances could such an approach
be considered?
Solution:
The projection of Transcript is returned by query
SELECT
FROM
T.StudId
Transcript, T
If the result of this query is the set of values a, b, . . . z, then the following query
submitted by A to C returns the semi-join
SELECT
S.Id, S.Major
205
206
CHAPTER 16
Distributed Databases
FROM
WHERE
Student, S
Such an approach would only make sense if the rst query produced a very small result
set.
17
OLAP and Data Mining
EXERCISES
17.1
17.2
Explain why, in an E-R model of a star schema, the fact table is a relationship and
the dimension tables are entities.
Solution:
In the ER-Model, a relationship connects several entities together to reect how they
are related, which is just the purpose of the fact table.
17.3
Design another fact table and related dimension tables that a supermarket might want
to use for an OLAP application.
Solution:
The fact table called Returned_Items with attributes Product_Id, Time_Id,
Market_Id, and Returned_Amt. Dimension tables Product, Time, and Market
as in the text
17.4
Explain why it is not appropriate to model the database for the Student Registration
System as a star schema.
Solution:
It would not eciently support the OLTP transactions that are the main purpose of
the system.
17.5
Design SQL queries for the supermarket example that will return the information
needed to make a table similar to that of Figure 17.10, except that markets are
aggregated by state, and time is aggregated by months.
a. Use CUBE or ROLLUP operators.
Solution:
208
CHAPTER 17
SELECT
FROM
WHERE
GROUP BY
SELECT
FROM
WHERE
GROUP BY
SELECT
FROM
WHERE
GROUP BY
SELECT
FROM
WHERE
GROUP BY
SELECT
FROM
M.Region, SUM(S.Sales_Amt)
Sales S, Market M
S.Market_Id = M.Market_Id
M.Region
T.month, SUM(S.Sales_Amt)
Sales S, Time T
S.Time_Id = T.Time_Id
T.month
SUM(S.Sales_Amt)
Sales S
SUM(Sales_Amt)
Region
17.6
Months
January June December Total
East
West
11633
16310
11641 8647
16314 16318
31921
48942
Total
27943
27955 24965
80863
a. Design a query for the supermarket example that will return the total sales (over
time) for each supermarket.
Exercises
Solution:
SELECT
S.Market_Id, SUM(S.Sales_Amt)
FROM
Sales S
GROUP BY S.Market_Id
Sales
Market_Id
17.7
M1
M2
M3
21012
10908
48942
Suppose that an application has four dimension tables, each of which contains 100
rows.
a. Determine the maximum number of rows in the fact table.
Solution:
The maximum number or rows in the fact table is 100 100 100 100 = 108
b. Suppose that a one-dimension table has an attribute that can take on 10 values.
Determine the size in bytes of a bit index on that attribute.
Solution:
The size of a bit index on that attribute is 100 10/8 = 125 bytes.
c. Determine the maximum number of tuples in a join index for a join between one of
the dimension tables and the fact table.
Solution:
The maximum size of a join index for a join between one of the dimension tables
and the fact table is 108 rows. The reason for this is the following. Let DimensionXYZ(DimensionId,...) be a dimension table and Fact(DimXYZ,...) be
a fact table. Then DimXYZ (which represents the values of dimension XYZ) must
be a foreign key referencing DimensionId, which is a key in DimensionXYZ.
Therefore, the number of tuples in the join (and therefore in the join index) is the
same as in the fact table.
d. Suppose that we use the CUBE operator on this fact table to perform aggregations
on all four dimensions. Determine the number of rows in the resulting table.
Solution:
The number of rows in the resulting table is
(100 100 100 100) + (4 100 100 100) + (4 3 100 100/2)+
209
210
CHAPTER 17
e. Suppose that we use the ROLLUP operator on this fact table. Determine the number
of rows in the resulting table.
Solution:
The number of rows in the resulting table is
(100 100 100 100) + (100 100 100) + (100 100) + 100 + 1 =
108 + 106 + 104 + 102 + 1
17.8
Design a query evaluation algorithm for the ROLLUP operator. The objective of such
an algorithm should be that the results of the previously computed aggregations are
reused in subsequent aggregations and not recomputed from scratch.
Solution:
The idea is based on the fact that to compute
SELECT
FROM
WHERE
GROUP BY
A1, . . . , An aggr(B)
SomeRelation
...
A1 , . . . , An
where aggr is min, max, sum, or count, we can rst compute the relation
SomeRelation1,...,n,n+1
SELECT
FROM
WHERE
GROUP BY
...
A1, . . . , An, An+1
...
A1 , . . . , An
In this way, we are reusing the previous aggregation of aggr(B) over the attributes of
SomeRelation minus A1, . . . , An, An+1 , B and now only need to aggregate over the
attribute An+1 .
If aggr is avg, we need to compute sum and count using the above optimization
and then divide one by the other.
We can thus start computing
Exercises
with an aggregation that groups by all the attributes, A1, . . . , An, then all but the last
attribute, etc. Thus, the following aggregates will be computed, where each successive
aggregation utilizes the result of the previous one:
GROUP BY A1, . . . , An
GROUP BY A1, . . . , An1
The rst GROUP BY above computes the relation SomeRelation1,...,n , the second
GROUP BY uses SomeRelation1,...,n to aggregate the values of B over the attribute
An , which produces the relation SomeRelation1,...,n1 . The third GROUP BY takes
SomeRelation1,...,n1 and aggregates the values of B over the attribute An1 , etc.
17.9
Design a query evaluation algorithm for the CUBE operator that uses the results of
the previously computed aggregations to compute new aggregations. (Hint: Organize
the GROUP BY clauses used in the computation of a data cube into a lattice, that
is, a partial order with the least upper bound and the greatest lower bound for each
pair of elements. Here is an example of such a partial order: GROUP BY A > GROUP
BY A,B > GROUP BY A,B,C and GROUP BY B > GROUP BY B,C > GROUP BY A,B,C.
Describe how aggregates computed for the lower parts of the lattice can be used in the
computation of the upper parts.)
Solution:
The idea is basically the same as in the case of ROLLUP: to compute
SELECT
FROM
WHERE
GROUP BY
A1, . . . , An aggr(B)
SomeRelation
...
A1 , . . . , An
where aggr is min, max, sum, or count, we can rst compute the relation
SomeRelation1,...,n,n+1
SELECT
FROM
WHERE
GROUP BY
...
A1, . . . , An, An+1
211
212
CHAPTER 17
FROM
SomeRelation1,...,n,n+1
WHERE
...
GROUP BY A1 , . . . , An
In this way, we are reusing the previous aggregation of aggr(B) over the attributes
of SomeRelation minus A1, . . . , An , An+1, B and now only need to aggregate the
values of B in SomeRelation1,...,n,n+1 over the attribute An+1.
If aggr is avg, we need to compute sum and count using the above optimization
and then divide one by the other.
We can thus start computing
GROUP BY CUBE (A1, . . . , An )
with an aggregation that groups by all the attributes, A1 , . . . , An, then move to
the next layer in the lattice, where we aggregate over all attributes but one etc. For
instance, the second level of aggregation will perform the following groupings:
GROUP BY A2 , A3 , . . . , An
GROUP BY A1 , A3 , . . . , An
Thus, the second layer will have N aggregations. The rst GROUP BY above aggregates
B in SomeRelation1,...,n over the attribute A1 , the second aggregates it over A2, etc.
The third layer will have N (N 1)/2! aggregations, etc. The important point is that at
layer 2 aggregation is done over the relation computed at layer 1, SomeRelation1,...,n ,
and at layer 3 we aggregate over the appropriate relations computed at layer 2. We
continue in this way until we reach the top layer where we have N aggregations, each
over a single attribute.
17.10 Suppose that the fact table of Figure 17.1 has been cubed and the result has been
stored as a view, Sales_v1. Design queries against Sales_v1 that will return the
tables of Figure 17.10 and Figure 17.7.
Solution:
SELECT
FROM
WHERE
S.Time_Id = NULL
17.11 We are interested in building an OLAP application with which we can analyze the
grading at our university, where grades are represented as integers from 0 to 4 (4
representing an A). We want to ask questions about average grades for dierent
courses, professors, and departments during dierent semesters and years. Design a
star schema for this application.
Exercises
Solution:
The center of the star (the relationship in the E-R diagram) is Grades. The dimension
tables might be Course, Professor, Department, and Time.
17.12 Discuss the dierence in storage requirements for a data cube implemented as a
multidimensional array and a fact table.
Solution:
If the data cube has m dimensions, and the attribute in the j th dimension can take on nj
values (e.g., there are 100 markets in the market dimension), the complete description
of that cube in a multidimensional array or a fact table would take n1 n2 . . . nm
storage locations. However, if some items are missing (e.g. Market 10 had no sales of
Product 4 in the time period T4), that row can be omitted from the fact table, but
the space has already been allocated in the array. Thus in practice the fact table is
considerably smaller than the multidimensional array.
17.13 Give examples, dierent from those in the text, of syntactic and semantic transformations that might have to be made while loading data into a data warehouse.
Solution:
Syntactic transformation: one database schema use one string to representing a
students name, while another might use two strings (last name and rst name).
Semantic transformation: One schema might store grades as A, B, C, D, and F,
while another might allow + and - grades, such as B+ or C-.
17.14 Perform the a priori algorithm on the table of Figure 17.15 to determine all reasonable
two-item associations.
Solution:
All reasonable two-item associations are:
Purchase diapers Purchase beer
Purchase beer Purchase diapers
17.15 Show that, when evaluating possible associations, the condence is always larger than
the support.
Solution:
Let the association be
ab
Let n be the total number of transactions, na be the number that contain a, and nab
the number that contains both a and b. Then the support is nab /n and the condence
is nab /na . Since n is larger than na , the condence is larger than the support
17.16 Apply the gain ratio measure to the table in Figure 17.16 to design a decision
tree.
Solution:
The nal tree is the same.
213
214
CHAPTER 17
17.17 Apply the Gini measure to the table in Figure 17.16 to design a decision tree.
Solution:
The nal tree is the same.
17.18 Show how the K-mean clustering algorithm would have worked on the table of
Figure 17.28 if the original choice for cluster centers had been 17 and 18.
Solution:
The nal cluster would have been the same.
17.19 a. Give an example of a set of three items on a straight line for which the
K-mean algorithm, with k = 2, would give a dierent answer for the two clusters
depending on the choice of items for the initial clusters.
Solution:
One choice for the three items is at distances of 1, 1.9, and 3. If the initial choice of
items is 1 and 1.9, the nal clusters are
1
1.9,3
b. Which of these nal clusters would have been obtained by the hierarchical algorithm?
Solution:
1,1.9
17.20 Suppose the table of Figure 17.16 is stored in a relational database. Use SQL to
compute the probabilities needed to compute the information gain when using the
PrevDefault attribute as the topmost attribute of a decision tree based on that
table.
Solution:
SELECT
C.PreviousDefault,
COUNT(DefYes.Count)/COUNT(*),
COUNT(DefNo.Count)/COUNT(*)
FROM
Customer C,
(SELECT DISTINCT C1.PrevDefault COUNT(*) AS Count
FROM Customer C1
WHERE C1.Default = yes
GROUP BY C1.PrevDefault) AS DefYes
(SELECT DISTINCT C1.PrevDefault COUNT(*) AS Count
FROM Customer C1
Exercises
WHERE C1.Default = no
GROUP BY C1.PrevDefault) AS DefNo
C.PrevDefault = DefYes.PrevDefault
C.PrevDefault = DefNo.PrevDefault
GROUP BY C.PrevDefault
WHERE
AND
215
PART FIVE
Transaction Processing
18
ACID Properties of Transactions
EXERCISES
18.1
Some distributed transaction processing systems replicate data at two or more sites
separated geographically. A common technique for organizing replicated systems is
one in which transactions that update a data item must change all replicas. Hence,
one possible integrity constraint in a replicated system is that the values of all of the
replicas of an item be the same. Explain how transactions running on such a system
might violate
a. Atomicity
Solution:
A transaction updates one replica and then crashes before it can update the others,
but the update to the rst replica is not rolled back.
b. Consistency
Solution:
A transaction updates one replica, but not others, thus violating the integrity
constraint that all replicas have the same value.
c. Isolation
Solution:
One transaction updates two dierent items, each of which is replicated; Another
transaction sees one replica before change, the other after.
d. Durability
Solution:
Two replicas are updated; one site crashes and forgets the update
18.2
220
CHAPTER 18
Solution:
Read/write transactions execute slower since they have to update replicas at several
sites.
c. What is the impact of replication on the communication system?
Solution:
Replication implies an increase in communication to update replicas, but a decrease
in communication to read the value of a replica.
18.3
Give three examples of transactions, other than an ATM bank withdrawal, in which a
real-world event occurs if and only if the transaction commits.
Solution:
1.
2.
3.
18.4
Students register for courses online and a conrmation of the registration is printed
if and only if the registration transaction commits
A pay per view movie is ordered for a TV, and can be seen if and only if the
ordering transaction commits.
Software is ordered over the Internet and is downloaded if and only if the
purchasing transaction commits.
The schema of the Student Registration System includes the number of current
registrants and a list of registered students in each course.
a. What integrity constraint relates this data?
Solution:
The number of students on the list equals the number of current registrants
b. How might a registration transaction that is not atomic violate this constraint?
Solution:
It might increment the number but crash before it appends to the list, and the
increment might not be rolled back
c. Suppose the system also implements a transaction that displays the current
information about a course. Give a nonisolated schedule in which the transaction
displays inconsistent information.
Solution:
The display transaction takes a snapshot between the increment and the append
18.5
Give three examples of applications in which certain transactions need not be totally
isolated and, as a result, might return data which, although not the result of a
serializable schedule, is adequate for the needs of the applications.
Solution:
1.
2.
Exercises
3.
18.6
When ordering tickets to a concert, the system might use two transactions: one
to display the available seats, and a second, executed later, to purchase tickets
and reserve seats. Each transaction is atomic, but the eect of executing both
transactions is not atomic a seat might be reserved by a concurrent transaction
between the execution of the display and purchase transactions.
Describe a situation in which the execution of a program under your local operating
system is not
a. Atomic
Solution:
The system crashes while executing a program. The partial changes made by the
program are not rolled back.
b. Isolated
Solution:
Two concurrently executing programs write to the same le
c. Durable
Solution:
The disk crashes in a thunderstorm and all the data stored on it is lost. The last
disk backup was made three days ago.
18.7
At exactly noon on a particular day, 100 people at 100 dierent ATM terminals
attempt to withdraw cash from their bank accounts at the same bank. Suppose their
transactions are run sequentially and each transaction takes .25 seconds of compute
and I/O time. Estimate how long it takes to execute all 100 transactions and what the
average response time is for all 100 customers.
Solution:
The average response time
= (0.25 + 0.50 + 0.75 + . . . + 25.00)/100
= 12.825 (seconds)
18.8
You have the choice of running a single transaction to transfer |S300 from one of
your savings accounts to another savings account at the same bank or of running two
transactions, one to withdraw |S300 from one account and a second to deposit |S300 in
the other. In the rst choice the transfer is made atomically; in the second it is not.
Describe a scenario in which after the transfer, the sum of the balances in the two
accounts is dierent (from what it was when both transactions started) at the instant
the transfer is complete. Hint: Other transactions might be executing at the same time
that you are doing the funds transfer.
Solution:
In the second choice an interest crediting transaction might execute between the two
transactions, and no interest would be credited on the money being transferred. In the
rst choice, the interest crediting transaction would execute either before or after the
transfer, and interest would be credited on the money transferred.
18.9
221
222
CHAPTER 18
transfers money from a bank account at site A to a bank account at site B executes
a subtransaction at A that does the withdrawal and then a subtransaction at B that
does the deposit.
Such a distributed transaction must satisfy the ACID properties in a global
sense: it must be globally atomic, isolated, consistent, and durable. The issue is,
if the subtransactions at each site individually satisfy the ACID properties, does
the distributed transaction necessarily satisfy the ACID properties? Certainly if the
subtransactions at each site are individually durable, the distributed transaction is
durable. Give examples of situations in which
a. The subtransactions at each site are atomic, but the distributed transaction is not
atomic.
Solution:
The subtransaction at one site commits and the subtransaction at a dierent site
aborts. In the above example, the subtransaction to withdraw money commits and
the one to deposit the money abortsthe money is lost
b. The subtransactions at each site are consistent, but the distributed transaction is
not consistent.
Solution:
A distributed database might have a global integrity constraint that two items
in databases at dierent sites have the same value (replicated data). The
subtransactions of a distributed transaction might maintain all the local integrity
constraints at their local database, but not maintain the global constraint that the
values of these two items are equal.
c. The subtransactions at each site are isolated, but the distributed transaction is not
isolated.
Solution:
The DBMS at one site serializes its subtransactions in one order, and the DBMS
at a dierents sites serializes its subtransactions in a dierent order. (The two
sites cannot agree on a serialization order.) In the above example of transferring
funds betweens accounts, a concurrent transaction to add the monthly interest
to all accounts might have a subtransaction that serializes after the withdraw
subtransaction at its site and a subtransaction that serializes before the deposit
subtransaction at its sitethose funds do not get interest that month.
18.10 Isolation is a sucient but not necessary condition to achieve correctness. Consider an
application that reserves seats for a concert. Each reservation transaction (1) reads the
list of seats that have not yet been reserved, (2) presents them to a potential customer
who selects one of them, and (3) marks the selected seat as reserved. An integrity
constraint asserts that the same seat cannot be reserved by two dierent customers.
Describe a situation in which two such transactions that reserve two dierent seats
execute in a nonisolated fashion but are nevertheless correct.
Solution:
Assume there are two unreserved seats, Sa and Sb . T1 reads that both are unreserved
and while its customer is thinking which seat she wants to reserve, T2 also reads that
Sa and Sb are unreserved. Then the customer at T1 reserves Sa and the customer at T2
reserves Sb . That is a perfectly correct execution, but T1 and T2 cannot be serialized
Exercises
in either order because neither saw the results of the others execution. (The system
must guarantee that if both customers had tried to reserve the same seat, only one
would have been allowed to.)
18.11 Assume a schedule consists of consistent transactions that execute in an isolated
fashion except for one transaction that performs a single update that is lost (as in
Figure 18.2). Show that the nal state of the database satises all integrity constraints
but nevertheless is incorrect.
Solution:
The values in the nal database are the same as if T had never executed. Since all the
other transactions are consistent and executed in an isolated fashion, the nal database
satises all integrity constraints. The database might nevertheless be incorrect because
the update that was lost might be important for the application, for example as in the
schedule of Figure 18.2.
223
19
Models of Transactions
EXERCISES
19.1
The banking system described at the end of Section 22.1 can be structured either
as a single transaction (with a savepoint after posting interest to each group of 1000
accounts) or as a chained transaction (with a commit point after posting interest to
each group of 1000 accounts). Explain the dierences in semantics between the two
implementations. State when the printing of account statements takes place in each.
Solution:
For the savepoint implementation, printing takes place at the end. Either all account
statements are printed or none. For the chain implementation, printing can take place
at the end of each subtransaction in the chain.
If system crashes during execution: in the savepoint implementation, all work will
be lost. In the chain implementation some account statements will have been printed
and the system can continue where it left o.
19.2
Explain the dierence between each of the transaction models with respect to abort,
commit, rollback, and isolation in the following cases:
a. A sequence of savepoints in a transaction and a chained transaction
Solution:
The transaction is durable at end of the chain and not at a savepoint.
b. A sequence of savepoints in a transaction and a nested transaction consisting of
subtransactions that are executed serially
Solution:
Rolling back to a savepoint is similar to a subtransaction of a nested transaction
aborting, but if the savepoint transaction aborts, the entire transaction aborts.
c. A sequence of savepoints in a transaction and a sequence of transactions linked by
a recoverable queue
Solution:
The sequence of recoverable queue transactions is not atomic, the savepoint
transaction is atomic.
226
CHAPTER 19
Models of Transactions
Exercises
19.4
Redesign the registration transaction in the Student Registration System design (given
in Section C.7) as a multilevel transaction.
Solution:
The levels could be the original transaction; each of the checks and the nal registration;
the SQL statements within the checks and the registration; and the page reads and
writes.
19.5
Show how the withdraw transaction in an ATM system can be structured as a nested
transaction with concurrent subtransactions.
Solution:
Checking the pin number and the actual withdraw are subtransactions.
19.6
a. Explain the dierence in semantics between the two versions of chaining discussed
in the text.
Solution:
In the rst version, locks are given up after each chained transaction commits. In the
second version, locks are retained. Therefore, in the rst version each subtransaction
in the chain might see a dierent database state than the one that existed when the
subtransaction before it committed.
b. Which of these versions is implemented within SQL?
Solution:
The rst version.
c. Give an example of an application where the second version would be preferable.
Solution:
The rst transaction checks whether there are seats on the plane, and the second
reserves a seat. If the rst version were used, someone might reserve the seat between
the two transactions.
19.7
Explain how the Student Registration System could interface with a student billing
system using a recoverable queue.
Solution:
At the end of the registration transaction the billing transaction would be queued.
19.8
Give three examples of applications in which isolation is not required and a recoverable
queue could be used.
Solution:
Registration and Billing; Telephone Ordering and Shipping the Order; Airplane Seat
Assignment and Meal Ordering
19.9
227
228
CHAPTER 19
Models of Transactions
not reect the state of the database. If the program rst committed and then printed
and the system crashed between the committing and printing, the printing would not
occur.
19.10 Consider the real-world transaction for dispensing cash discussed in Section 19.3.5.
For each of the critical timesbefore, during, and after the execution of the realworld transactionin which the system might crash, describe how the system (after
it recovers from the crash) determines whether or not the cash has been dispensed.
Discuss some ways in which the cash-dispensing mechanism itself might fail in such a
way that the system cannot tell whether or not the cash has been dispensed.
Solution:
If the system crashes before the real-world transaction (on the queue) commits, and
before the cash is dispensed, the transaction is restarted and checks the value stored
in the database and in the physical counter. They will be the same and hence the
transaction is reexecuted and the cash dispensed.
If the system crashes before the real-world transaction (on the queue) commits,
and after the cash is dispensed, the transaction is restarted and checks the value stored
in the database and in the physical counter. They will be dierent, so the transaction
does not dispense the cash.
The protocol will not work correctly if the counter stops working or works
incorrectly.
19.11 Explain in what ways the execution of each individual SQL statement in a transaction
is like a nested subtransaction.
Solution:
It is atomic, it inherits locks from its parent, it can independently abort.
19.12 Show how the credit card validation transaction described in the rst paragraph of
Chapter 1 can be structured as a distributed transaction.
Solution:
The credit card company and bank databases could be at dierent sites. The validation
transaction initiates subtransactions at each site
.
19.13 The Student Registration System is to be integrated with an existing student billing
system (which also bills for meal plans, dorm rooms, etc.). The databases for the two
systems reside on dierent servers, so the integrated system is distributed. Using your
imagination,
a. Give examples of two global integrity constraints that might exist for the global
database of this system.
Solution:
IC1: The amount the student is billed for corresponds to the courses she is registered
for.
IC2: A student is allowed in the dorm only if she is registered for at least 1 course.
b. Give examples of two transactions that access both databases.
Exercises
Solution:
The dorm registration transaction (to check constraint); the billing transaction.
19.14 Describe the process that the admissions oce of your university uses to admit new
students as a workow. Decompose the process into tasks, and describe the task
interaction using a diagram similar to Figure 19.9.
Solution:
Advertise university; send applications; receive applications; evaluate applications,
read SAT; send admission letters; receive acceptance letters; enter accepted students
into student database.
19.15 Consider a transaction that transfers funds between two bank accounts. It can be
structured into two subtransactions: one to debit the rst account and the second to
credit the second account. Describe how this can be done in (a) the hierarchical model
and (b) the peer model.
Solution:
In the hierarchical model the parent initiates a debit subtransaction and if it completes
successfully, initiates a credit subtransaction. When the latter completes the parent
calls commit. In the peer model the two subtransactions can be initiated concurrently.
If they both complete successfully, the parent commits causing the subtransactions to
commit. If not, the parent aborts, causing the subtransactions to abort. Alternatively,
the debit subtransaction could communicate directly to the credit subtransaction,
informing it of whether or not it was successful, and the credit transaction could could
make the deposit appropriately.
19.16 Explain how nested transactions can be implemented using savepoints and procedure
calls. Assume that the children of each subtransaction do not run concurrently.
Solution:
A savepoint is declared immediately prior to calling a procedure, and its Id is passed
as an argument to the procedure. To abort, the procedure rolls back to that savepoint.
The only rollbacks to that savepoint are in the procedures body. After executing the
rollback the procedure returns, with a failure indication as an out argument.
19.17 In an application that uses declarative demarcation, one procedure that is called
frequently from other procedures is a logging procedure that writes appropriate
information about the state of the system into a log. What transaction attribute
should be used for that procedure?
Solution:
RequiresNew because we want the transaction corresponding to that procedure to
commit even if the transaction that called it should abort.
Another possible answer is NotSupported , because we want the logging to take
place even if the logging procedure is called from a transaction that aborts. However,
in this case the logging would not itself be transactional, which might be desirable for
many applications.
229
20
Implementing Isolation
EXERCISES
20.1
20.2
232
CHAPTER 20
Implementing Isolation
Solution:
For the graph of Figure 20.5 (a)
T1 T2 T3 T4 T5 T6 T7
T1 T2 T3 T5 T4 T6 T7
T1 T2 T3 T5 T6 T4 T7
T1 T2 T3 T5 T6 T7 T4
T1 T3 T2 T4 T5 T6 T7
T1 T3 T2 T5 T4 T6 T7
T1 T3 T2 T5 T6 T4 T7
T1 T3 T2 T5 T6 T7 T4
T1 T3 T5 T2 T4 T6 T7
T1 T3 T5 T2 T6 T4 T7
T1 T3 T5 T2 T6 T7 T4
20.3
Use a serialization graph to demonstrate that the schedule shown in Figure 20.4 is not
conict serializable.
Solution:
The serialization graph has an edge
T1 T2
because T1 wrote x before T2 wrote it, and it has an edge
T2 T1
because T2 read y before T1 wrote it. Thus the graph has a cycle and the schedule is
not conict serializable.
20.4
Suppose that we declare all of the database integrity constraints in the database
schema so that the DBMS will not allow any transaction to commit if its updates
violate any of the integrity constraints. Then, even if we do not use any concurrency
control, the database always remains consistent. Explain why we must nevertheless
use a concurrency control.
Solution:
We must use a concurrency control to maintain correctness. For example, the following
schedule of two transactions preserves database consistency (the database satises its
Exercises
integrity constraints), but nevertheless the nal database does not reect the eect of
both transactions. Two interleaved deposit transactions of |S10:
r1(bal ) r2 (bal ) w1(bal = bal + |S 10) w2(bal = bal + |S 10)
The integrity constraint is bal 0. When the schedule completes, the integrity
constraint is satised, but the database is incorrect T1s update has been lost.
Alternatively, a schedule in which read/write transactions are serializable (and
hence all integrity constraints would be preserved), but read-only transactions are not
serializable with respect to read/write transactions would be allowed.
20.5
20.6
20.7
20.8
Prove that with a two-phase locking protocol, one possible equivalent serial order is
the order in which the transactions perform their rst unlock operation.
Solution:
If two transactions do not acquire conicting locks in some schedule S , all their
operations commute and they can be ordered arbitrarily in an equivalent serial schedule.
233
234
CHAPTER 20
Implementing Isolation
Suppose they acquire conicting locks on item x, and T1 accesses x before T2. Then
T1s rst unlock in S must precede T2s lock on x, which must precede T2 s rst
unlock. If they also acquire conicting locks on y, their accesses to y must be in the
same order as their accesses to x since otherwise we could conclude that T2 s rst
unlock in S must precede T1s rst unlock a contradiction. Hence, all conicting
operations in T1 and T2 are ordered in the same way as their rst unlock events and
the two transactions can be serialized in that order.
20.9
Exercises
wrote x and 2 requested to write x). But, 2 has read what 1 wrote and hence must be
aborted when 1 is aborted. Transaction 3 has read what 2 wrote and hence must be
aborted when 2 is aborted.
20.14 Give an example of a schedule produced by a nonstrict two-phase locking concurrency
control that is serializable but not in commit order.
Solution:
lock1(x) w1(x) unlock1 (x) lock2(x) r2(x) unlock2 (x) commit2 commit1
Transaction T2 is after transaction T1.
20.15 Suppose that the account object, the conict table for which is described in
Figure 20.18, has an additional operation, balance, which returns the balance in
the account. Design a new conict table for this object, including the new operation.
Solution:
Granted Mode
Requested Mode
deposit()
withdrawOK()
withdrawNO()
balance
X
X
X
X
235
236
CHAPTER 20
Implementing Isolation
e. Does the database state seen by T3 satisfy all integrity constraints? Explain.
Solution:
Yes. T3 is a read-only transaction. The read/write transactions, T1 and T2, are
serializable and hence the nal database satises all integrity constraints.
20.17
a. Assume that, in addition to the operations read(x) and write(x), a database has
the operation copy(x,y), which (atomically) copies the value stored in record x into
record y. Design a conict table for these operations for use in an immediate-update
pessimistic concurrency control.
Solution:
The conict table is for operations on a single item. Therefore we must distinguish
the cases where the copy is from the item described by the table and when the copy
is to that item. Copy-from is like a read, and copy-to is like a write.
Granted Mode
Requested Mode
read
write
copy-from
copy-to
X
X
X
X
b. Assume that, in addition to the operations read(x) and write(x), a database has
the operation increment(x,C), which (atomically) increments the value stored in
record x by the constant C , which might be positive or negative. Design a conict
table for these operations for use in an immediate-update pessimistic concurrency
control.
Solution:
Granted Mode
Requested Mode
read
write
increment
20.18 Suppose that we have a queue object that implements an FCFS (rst-come-rstserved) discipline, with operations enqueue and dequeue. enqueue always succeeds
and dequeue returns NO if the queue is empty and OK otherwise. Design a conict
table for this object using partial operations and backward commutativity.
Solution:
Granted Mode
Requested Mode
enqueue()
Exercises
dequeueOK()
dequeueNO()
20.19 A pair of (partial) database operations, p and q, are said to forward-commute if, in
every database state in which both p and q are dened, the sequences p, q and q, p
are both dened and, in both sequences, p and q return the same values and the nal
database state is the same.
a. Describe how forward commutativity can be used in the design of a deferred-update
pessimistic concurrency control.
Solution:
Does-not-forward-commute-with is used as the conict relation in the conict table.
Because the transactions might commit in any order, each transactions operations
must be dened in the database state that exists when that transaction starts.
Then, again since the transactions might commit in any order, the operations must
commute.
b. Give a conict table for such a control for the account object described in
Figure 20.18.
Solution:
Granted Mode
Requested Mode
deposit()
withdrawOK()
withdrawNO()
Note that the only conict that occurs is for two withdrawOK() transactions
because there might not be enough money in the account to satisfy both withdrawals.
20.20 In Section 20.7 we described a policy for a concurrency control that dealt with
both forward and compensating operations and guaranteed that schedules were
reducible. Generalize this policy to include undo operations using the conditions
on the commutativity described in that section, and show that your generalization
preserves reducibility.
Solution:
to be added
20.21 Design a deferred-update pessimistic concurrency control.
Solution:
Granted Mode
Requested Mode
read write
read
write
237
238
CHAPTER 20
Implementing Isolation
Even though writes are deferred, a transaction must obtain a write lock when a write
statement is executed (before the new value is written to the intentions list). There is
no conict between two write operations because the new values will be stored in the
database at commit time in commit order.
20.22 Consider an implementation of chained transactions in which a subtransaction, when
it commits, only releases locks on items that will not be accessed by subsequent
subtransactions. Explain how this can aect the ACID properties of the overall
chained transaction.
Solution:
The transaction might not be isolated. Some other transaction might see the
intermediate results of the transaction.
20.23 Give the conict table for each level of the multilevel control described in Section 20.8.5.
Each table indicates the conicts for the operations used to implement the transaction
Move(sec1, sec2). Assume that the operation TestInc is viewed as two partial
operations TestIncOK and TestIncNO.
Solution:
At L0 :
Granted Mode
Requested Mode
read
write
read write
X
X
At L1 :
Granted Mode
Requested Mode
Upd Sel
Upd
Sel
X
At L2 :
Granted Mode
Requested Mode
TestIncOK
TestIncNO
Dec
X
X
Exercises
A pessimistic control would force T2 to wait when it requested to read x but would
then allow the operation after T1 commits and subsequently allow T2 to commit. An
optimistic control would abort T2 when it requested to commit.
20.25 Give an example of a schedule in which a pessimistic concurrency control makes a
transaction wait but then allows it to commit, while an optimistic concurrency control
allows the transaction to commit without waiting.
Solution:
The following schedule would be accepted by an optimistic control.
r1(x) r2 (y) w1(z ) w2 (z ) commit1 commit2
A pessimistic control would force the operation w2 (z ) to wait.
20.26 Give an example of a schedule that is acceptable (without any delays caused by locks)
by an immediate-update pessimistic strict two-phase locking concurrency control, while
an optimistic concurrency control restarts one of the transactions.
Solution:
r1(x) r2 (x) r2(x) w2(y) commit2 r1 (y) w1(z )request to commit1
20.27 Can a deadlock occur in the timestamp-ordered control described in the text?
Solution:
Yes. Assume the read and write timestamps of x and y are initially smaller than
TS (T1) and that TS (T1) < TS (T2). The schedule is:
w1(y), w2(x), r2(y), w1(x)
r2(y) waits since y is dirty, and w1(x) waits since it cannot overwrite x until T2
completes
20.28 Give an example of a schedule produced by a timestamp-ordered concurrency control
in which the serialization order is not the commit order.
Solution:
Assume TS (T1) < TS (T2)
r1(x) w2 (x) commit2 w1 (y) commit1
Transaction T1 is before the transaction T2 but commits later.
20.29 Give an example of a schedule that is strict and serializable but not in commit order
and that could have been produced by either a timestamp-ordered concurrency control
or a two-phase locking concurrency control.
Solution:
r1(x) r1 (y) w2(x) commit2 commit1
239
240
CHAPTER 20
Implementing Isolation
Exercises
c. Explain why deadlock does not occur in a kill-wait control.
Solution:
Since a transaction waits only for an older transaction, there can be no cycle of
waits.
20.33 The wait-die concurrency control is another control that combines the concepts of the
immediate-update concurrency control and the timestamp-ordered control.
If transaction T1 makes a request that conicts with an operation of active
transaction T2
if TS (T1) < TS (T2), then make T1 wait until T2 terminates, else abort T1 .
where abort T1 is referred to as a die because T1 kills itself.
a. Show that the wait-die control serializes in commit order and prevents deadlocks.
Solution:
It serializes in commit order because it uses the same locking protocol as the
immediate-update pessimistic control. It prevents deadlocks because a transaction
can only wait for a younger transaction.
b. Compare the fairness of the execution of the kill-wait and wait-die controls.
Solution:
In the Kill-Wait system the oldest transaction has absolute priority and never waits,
but it might kill a transaction that has already been granted permission to perform
an operation. In the Wait-Die system, only the requesting transaction is aected by
the request.
20.34 Give a complete description of an algorithm for a parallel validation, optimistic
concurrency control.
Solution:
When T1 requests to commit:
1.
2.
3.
If the read phase of T1 overlaps the validation/write phase of T2, then if the set
of items read by T1 is not disjoint from the set written by T2, abort T1.
If T1 enters its validation/write phase after T2 and the execution of those phases
overlap, then if the set of items written by T1 is not disjoint from the set written
by T2, abort T1 .
If T1 has not been aborted because of (1) or (2), commit T1 .
20.35 Describe a serial validation, optimistic concurrency control that uses backward
validation and in addition uses timestamps to distinguish the order in which conicting
read and write operations occur. The validation condition in that case need not be
conservative, and only conicts that would violate commit order cause aborts.
Solution:
The concurrency control assigns a unique timestamp to each transaction, T , when it
enters its validation phase. Then when T writes an item during its write phase, the
value of T s timestamp is stored with that item in the database and also stored with
the information the concurrency control maintains about T s write set to perform
validation of other transactions. The validation is then performed as follows: When a
transaction, T1, reads an item during its read phase, the control remembers the value
241
242
CHAPTER 20
Implementing Isolation
of that items timestamp. Later when T1 enters its validation phase, the control checks
if any other transaction, T2, that committed during T1s read phase wrote any item
that T1 read. If so, it checks the timestamp of that item to see if T2 changed the value
of that item after T1 read it. If the items timestamp had been changed, T1 is aborted.
If there is no such item for which the timestamp has been changed, T1 is allowed to
commit.
20.36 Subroutines can be used in a at transaction in an attempt to mimic the behavior
of a subtransaction in the nested model: whenever a subtransaction aborts, the
corresponding subroutine manually undoes any database updates it has performed
and returns with status indicating failure. Explain why the nested model allows higher
transaction throughput.
Solution:
When a subtransaction aborts, any new locks it has obtained are released. New locks
obtained by the subroutine, however, are not released.
20.37 Suppose that transactions T1 and T2 can be decomposed into the subtransactions
T1 : T1,1, T1,2
and
T2 : T2,1, T2,2
such that the database items accessed by T1,1 and T2,1 are disjoint from the items
accessed by T1,2 and T2,2. Instead of guaranteeing that all schedules involving T1 and
T2 are serializable, suppose that a concurrency control guarantees that T1,1 is always
executed serializably with T2,1 and that T1,2 is always executed serializably with T2,2.
a. Will T1 always be serializable with T2? Explain.
Solution:
No. T1,1 might be serialized before T2,1, and T1,2 might be serialized after T2,2.
Thus there is no equivalent serial order.
b. What minimal additional condition on the subtransactions guarantees that the eect
of executing T1 concurrently with T2 is the same as a serial schedule?
Solution:
One condition is that T1,1 commutes with T2,1 and thus they can be serialized in
either order.
c. Assuming that the condition of (b) holds, what advantage does the new concurrency
control have over a concurrency control that guarantees serializability?
Solution:
Locks acquired by T1,1 or T2,1 are released early.
20.38 Suppose that transactions T1 and T2 can be decomposed into the subtransactions
T1 : T1,1, T1,2
and
T2 : T2,1, T2,2
Exercises
such that each subtransaction individually maintains the consistency constraints of
the database. Instead of guaranteeing that all schedules involving T1 and T2 are
serializable, suppose that a concurrency control guarantees that all subtransactions
are always executed serializably.
a. Will T1 always be serializable with T2? Explain.
Solution:
No. See previous exercise.
b. Will integrity constraints be maintained by all possible schedules?
Solution:
Yes, because each subtransaction starts with a consistent database and hence
produces a consistent database.
c. What possible problems might arise if the concurrency control schedules transactions
in this way?
Solution:
The database states produced might not correspond to any serial schedule and hence
might not be a correct model of the state of the world. However, this situation might
be acceptable to some applications. A similar problem might arise with respect to
the information returned to the transaction.
20.39 A blind write occurs when a transaction writes a database item it has not read. For
example, in the Student Registration System a transaction might compute a students
GPA by reading her course grades, computing the average, and then (blindly) writing
the result in the appropriate database item without rst reading that item. Some
applications have the property that no transactions perform blind writes. Show that
for such applications
a. View equivalence is equivalent to conict equivalence.
Solution:
Assume we are given a schedule, S , without blind writes that is view equivalent to
another schedule S . We rst prove by induction that the order of the writes of each
item, x, in S is the same as in S . (This statement is not true in general for view
equivalent schedules that have blind writes as shown by the example in Figure 20.4.)
Consider the sequence of transactions in S that wrote x, Tw(x)1 , Tw(x)2 , . . . , Tw(x)n ,
ordered by their occurrence in S .
To start the induction, we show that the rst write of x in S was made by
Tw(x)1 (the rst transaction that wrote x in S ). No later transaction in S , Tw(x)i ,
could have made the rst write of x because, since there are no blind writes, Tw(x)i
would have had to read some version of x and since S is view equivalent to S , that
version would have had to be the one written by Tw(x)i 1 , but Tw(x)(i 1) has not
yet written x (no transaction has).
Now assume the statement is true for the the rst j 1 transactions in the
above list. We show that the next write of x in S was made by Tw(x)j (the next
transaction that wrote x in S ). It cannot have been made by any earlier transaction
in the list because of the induction hypothesis. It cannot have been made by any
later transaction using the same reasoning as in the previous paragraph.
243
244
CHAPTER 20
Implementing Isolation
Thus the order of the writes of each item in S is the same as the order of the
writes of that item in S , and hence the writes are in the same conict order in both
schedules. Also, because S is view equivalent to S , the reads in S read the same
values as the reads in S , and hence the reads are in the same conict order with
the writes in both schedules. Thus S is conict equivalent to S .
b. The timestamp-ordered concurrency control described in Section 20.9.1 never uses
the Thomas Write Rule.
Solution:
As described in the text, the Thomas Write Rule is used when a transaction, T ,
requests to write an item x and rt(x) < TS (T ) < wt(x). But since every transaction
that writes x must have previously read it, the last transaction T that wrote
x and set its write timestamp to wt(x) = TS (T ) must have previously read x,
and hence xs read timestamp must be at least TS (T ) (if T has committed,
another transaction with a later timestamp might also have read it). Thus for such
applications, it is always true that rt(x) wt(x) and hence the condition for the
Thomas Write Rule is never satised.
c. In the timestamp-ordered concurrency control described in the text, for each item,
x, that a transaction, T , writes, when T commits, rt(x) = wt(x) = TS (T ).
Solution:
When a transaction, T , requests to write an item, x, since T has already read
x, the read timestamp, rt(x) must satisfy rt(x) TS (T ). If rt(x) > TS (T ), then
(as described in the text) T is too old to write x and so will be aborted by the
concurrency control. Hence if T is allowed to write x, it must be true that at
that instant rt(x) = wt(x) = TS (T ). If later, but before T commits, some younger
transaction requests to read x, that transaction will be made to wait until T commits
(and hence cannot write either). Therefore when T commits, it will still be true
that rt(x) = wt(x) = TS (T )
20.40 State which of the following operations has a compensating operation.
a. Give all employees a 10% raise.
Solution:
Yes. Give all employees a 9.09.09% decrease.
b. Give all employees making less than |S10,000 a 10% raise.
Solution:
No. There is no way for a compensating operation to know whether or not
to give a decrease to an employee whose salary after the original operation is, for
example, |S10,100.
c. Set the value of a particular item to 12.
Solution:
No. There is no way for a compensating operation to know what the value of
the item was before the original operation
Exercises
d. Insert a new tuple with key 1111 into the database, and set the value of one of its
attributes to 12.
Solution:
Yes. Delete the tuple with key 1111.
e. Set the value of a particular item to the square of its original value.
Solution:
No. There is no way for a compensating operation to know what the sign of the
original value was.
20.41 Assume an operation p has a compensating operation p 1 . Show that if operation q
commutes with p, then p 1 commutes with q.
Solution:
Since q commutes with p, the schedule p, q, p 1 is equivalent to the schedule
q, p, p 1. But starting in all initial states, this schedule leaves the database in the
same nal state as the schedule p, p 1 , q, which implies that p 1 commutes with q.
20.42 Give an example of a schedule of consistent transactions that is not serializable
but maintains the correctness of any integrity constraint that might conceivably be
associated with the database.
Solution:
The schedule contains a lost update. For example,
r1 (x) r2(x) w2(x) w1 (x)
T2s update is lost, and the result of the schedule is the same as if only T1 had
executed and T2 had never executed. Since T1 is assumed to be consistent, the
schedule maintains all possible integrity constraints. However, the nal database might
be incorrect. since T2s update has been lost
245
21
Isolation in Relational Databases
EXERCISES
21.1
Suppose that the transaction processing system of your university contains a table in
which there is one tuple for each currently registered student.
a. Estimate how much disk storage is required to store this table.
Solution:
10,000 tuples * 100 bytes/tuple = 1 megabyte.
b. Give examples of transactions that must lock this entire table if a table-locking
concurrency control is used.
Solution:
Find all students who are registered in at least one course.
21.2
Consider an INSERT statement that inserts tuples into table T and that contains a
nested SELECT statement having predicate P in its WHERE clause. Assume P is a
simple predicate that does not contain a nested SELECT statement and consists of a
conjunction of clauses of the form (Attribute op constant). Describe the predicate
associated with T .
Solution:
Let P is the predicate associated with T. A subset of the attributes named in P
are associated with attributes of T through the SELECT list of the nested SELECT
statement and the attribute list of the INSERT statement. Let A be such an attribute
and let A be the corresponding attribute of T. Then the conjunct of P describing A is
a conjunct of P with A substituted for A. Furthermore, if the schema for T species a
default value, def , for an attribute, B, not contained in the attribute list of the INSERT
statement, P contains a conjunct (B = def ).
21.3
Choose a set of transactions for an application of your choice (other than a banking or
student registration system). For each isolation level weaker than SERIALIZABLE, give
an example of a schedule that produces an erroneous situation.
Solution:
Airline reservation system:
248
CHAPTER 21
REPEATABLE READ
(write request item : passenger requests exotic meal for seat 5B)1
(read request item; write order item : order meal from caterer )2 commit2
abort1
Meal has been ordered that passenger does not want.
21.4
21.5
21.6
21.7
Consider a schema with two tables, Table1 and Table2, each having three attributes,
Exercises
21.8
Explain the dierence between a nonrepeatable read and a phantom. Specically, give
an example of a schedule of the SQL statements of two transactions that illustrate the
two cases. Specify an isolation level in each case.
Solution:
To illustrate a non-repeatable read consider two transactions running at READ COMMITTED:
SELECT1 (get sum of balances in all of Marys accts);
UPDATE2(post interest in all accts);
Commit2 ;
SELECT1 (get sum of balances in all of Marys accts);
In the rst case, the second SELECT returns the same set of tuples as the rst, but their
values have changed. In the second case, the second SELECT returns a new tuple that
satises the read predicate, but the values of the old tuples that satisfy that predicate
have not changed.
21.9
For each of the locking implementations of the isolation levels, state whether IS locks
are required and when they can be released.
249
250
CHAPTER 21
Solution:
READ UNCOMMITTED: No read locks are required and hence no intention to read locks.
READ COMMITTED: Read locks are short term, and for each short term read lock on
a tuple, a short term IS lock must be obtained on the table.
REPEATABLE READ: Read locks are long term, and for each long term read lock on a
21.10 The following procedure has been proposed for obtaining read locks.
Whenever an SQL statement reads a set of rows that satises some predicate in a
table, the system rst gets an IS lock on the table containing the rows and then
gets an S lock on each of the rows.
Explain why this procedure allows phantoms.
Solution:
An INSERT or DELETE statement gets a non-conicting IX lock on the table and X
locks on the tuples inserted or modied. The inserted tuples might satisfy the predicate
but since they are new there would be no lock conict to delay the insert. Similarly
the initial value of a modied tuple might not satisfy the predicate and so it would be
possible to obtain an X
21.11 Give an example of a schedule produced by a read-only multiversion concurrency
control in which the read/write transactions serialize in commit order while the
read-only transactions serialize in a dierent order.
Solution:
r1(x) w2(y) w3(z ) commit3 commit2 r1 (y) commit1
T1 does not see the new value of y written by T2 and hence serializes before T2 even
though it commits later.
21.12 Give an example of a schedule of read/write requests that is accepted by a multiversion
concurrency control in which transaction T1 starts after transaction T2 commits, yet
T1 precedes T2 in the serial order. Such a schedule can have the following nonintuitive
behavior (even though it is serializable): you deposit money in your bank account; your
transaction commits; later you start a new transaction that reads the amount in your
account and nds that the amount you deposited is not there. (Hint: The schedule is
allowed to contain additional transactions.)
Solution:
r3(x) w2(x) commit2 r1(y) w3(y) commit3 r1(x) commit1
Exercises
The order is T1 T3 T2 . Therefore the last read of T1 on x does not see the write of x
made by T2. Thus T1 is before T2 even though it started after T2 committed. (Note
that the serialization order is the reverse of the commit order.)
21.13 Show that the schedule shown in Figure 21.13 for incorrect execution at SNAPSHOT isolation can also occur when executing at OPTIMISTIC READ COMMITTED
(Section 21.2.1) and will also be incorrect.
Solution:
The schedule can occur at OPTIMISTIC READ COMMITTED because when each
transaction performs its write, the item it wrote was not changed since it was read.
21.14 Give examples of schedules that would be accepted at
a. SNAPSHOT isolation but not REPEATABLE READ
Solution:
r1 (x) r2(x) w2(x) commit2 w1(y) commit1
At REPEATABLE READ, T2 would not be allowed to write x since T1 would have a
read lock on it.
b. SERIALIZABLE but not SNAPSHOT isolation
Solution:
r1 (x) r2(y) w1 (x) commit1 r1 (x) w2(x) commit2
This schedule is serializable, but would not be allowed at SNAPSHOT isolation. At
SNAPSHOT, T2 s read of x would give the value before T1 wrote it, and then T2
would not be allowed to commit because T1 was the rst committer that wrote x.
21.15 A particular read-only transaction reads data that was entered into the database
during the previous month and uses that data to prepare a report. What is the
weakest isolation level at which this transaction can execute? Explain.
Solution:
READ UNCOMMITTED, because no other transaction can change the data.
21.16 Explain why a read-only transaction consisting of a single SELECT statement that uses
an INSENSITIVE cursor can always execute correctly at READ COMMITTED.
Solution:
It sees a snapshot of committed data.
21.17 Suppose that a locking implementation of REPEATABLE READ requires that a
transaction obtain an X lock on a table when a write is requested. When a read
is requested, a transaction is required to obtain an IS lock on the table and an S lock
on the tuples returned. Show that phantoms cannot occur.
Solution:
The INSERT statement requires an X lock on the table, and it cannot obtain that lock
if the select statement has an IS lock on the table.
251
252
CHAPTER 21
21.18 Consider an isolation level implemented using long-duration granular locks on rows
and tables. Under what conditions can phantoms occur?
Solution:
If a SELECT statement, using a read predicate, gets long term read locks on the
rows that satisfy that predicate, those locks do not prevent another transaction from
inserting a row that also satises that predicate.
21.19 Show that the algorithm for concurrently accessing a B+ tree in which operations that
only search the tree use lock coupling and operations that modify the tree handle
write latches in a two-phase fashion guarantees that all operations are carried out
successfully.
Solution:
Since modifying operations use two-phase locking and searches do not modify the
tree, modifying operations are serializable. Furthermore, since searches are read-only,
they too are serializable. Hence, our only concern is that a modifying operation does
not serialize with a search. Consider the interaction of a search, S , and a modifying
operation, M . If M starts at the root rst, S will have to wait until M completes before
it starts, so they are serializable. Suppose S starts at the root rst. Lock coupling
guarantees that S never releases a latch until it acquires the next latch on the search
path. Hence, it always holds a latch and as a result, is always at least one step ahead
of M . As a result, in any read/write conict between operations of S and M , S always
precedes M and hence S and M are serializable.
21.20 In Section 21.3.1 we described an algorithm that allowed the concurrent execution of
a search for a unique key and an insert of a row in a B+ tree. Extend the algorithm to
range searches and deletes.
Solution:
The search algorithm uses sibling pointers when it reaches the leaf level to nd keys
in the range. Read latches are obtained on additional leaf nodes. The delete algorithm
upgrades the update latch on the leaf page, p1 containing the rst key to be deleted
to a write latch and deletes the key(s). If the sibling node, p2, might also contain keys
in the range, the operation backs up to the parent of both, p, and then acquires an
update latch on p2. If p2 contains keys in the range the update latch is upgraded to
a write latch and the keys are deleted. The update latch on p is upgraded to a write
latch if p1 and p2 are merged.
21.21 a. Give an example of a schedule of two transactions in which a two-phase locking
concurrency control causes one of the transactions to wait but a SNAPSHOT isolation
control aborts one of the transactions.
Solution:
The schedule as handled by a two-phase locking concurrency control:
r1 (x) w1(x) r2(y) Request[w2 (x)] commit1 Execute[w2 (x)] commit2
The schedule as handled by a SNAPSHOT isolation control:
r1 (x) w1(x) r2(y) w2(x) commit1 Request[commit2 ] abort2
Exercises
b. Give an example of a schedule of two transactions in which a two-phase locking
concurrency control aborts one of the transactions (because of a deadlock) but a
SNAPSHOT isolation control allows both transactions to commit.
Solution:
r1 (x) r2(y) w1 (y) w2 (x)
21.22 Explain why SNAPSHOT-isolated schedules do not exhibit dirty reads, dirty writes,
lost updates, nonrepeatable reads, and phantoms.
Solution:
Dirty Reads: Reads only committed values.
Dirty Writes: First committer wins.
Lost Updates: First committer wins
Non-Repeatable Reads: Always returns items from same snapshot
Phantoms: Always returns items from same snapshot (but note discussion in text).
21.23 Consider an application consisting of transactions that are assigned dierent isolation
levels. Prove that if transaction T is executed at SERIALIZABLE and transaction T is
any other transaction, then T either sees all changes made by T or it sees none.
Solution:
Transactions executing at all the isolation levels use long term write locks, and
transactions executing at SERIALIZABLE use long term read and write locks. Hence all
operations of T are serializable with respect to the write operations of T . Hence T
either sees all the changes made by T or none.
21.24 All of the transactions in some particular application write all of the data items they
read. Show that if that application executes under SNAPSHOT isolation, all schedules
of committed transactions will be serializable.
Solution:
If two concurrently executing transactions both commit at SNAPSHOT isolation, they
have written disjoint sets of data items. If they have written all the items they have
read, then they have also read disjoint sets of data items. Therefore all their operations
have been on disjoint sets of data items, and hence all operations commute thus
they are serializable.
21.25 Consider the schedule of two bank withdrawal transactions shown in Figure 21.13 for
which SNAPSHOT isolation leads to an inconsistent database. Suppose that the bank
encodes, as an integrity constraint in the database schema, the business rule The sum
of the balances in all accounts owned by the same depositor must be nonnegative.
Then that particular schedule cannot occur.
Although the integrity constraint is now maintained, the specication of a
particular transaction might assert that when the transaction commits, the database
state satises a stronger condition. Give an example of a stronger condition that a
253
254
CHAPTER 21
which returns the number of employees whose age is 27, and the other contains the
single UPDATE statement
Exercises
UPDATE Employee
SET Salary = Salary * 1.1
WHERE Department = Adm
which gives all employees in the administration a 10% raise. Assume that there
are indices on both Department and Age and that the tuples corresponding to the
department Adm are stored in more than one page as are those corresponding to age 27.
Show how a deadlock might occur at isolation levels other than READ UNCOMMITTED.
Solution:
At isolation levels SERIALIZABLE, REPEATABLE READ, and READ COMMITTED, the
rst transaction, T1, gets an IS lock on the table and an S lock on the rst page
containing tuples corresponding to Age 27. Meanwhile, the second transaction, T2 ,
gets an IX lock on the table and an X lock on the rst page containing tuples
corresponding to the Administration Department. If the Department has 27 year old
employees, it might be the case that later T1 requests an S lock on a page T2 has
locked, and T2 requests an X lock on a page T1 has locked. A deadlock occurs.
At isolation level READ UNCOMMITTED transactions do not get read locks.
However, since each statement must execute in an isolated fashion, internal locks
on the same items must be acquired by T1 and held until the statement has been
completely processed. Therefore the deadlock could still occur.
21.29 Give an example of a schedule executing at SNAPSHOT isolation in which two
transactions each introduce a phantom that is not seen by the other transaction,
resulting in incorrect behavior. Assume that the data items referred to in the description
of SNAPSHOT isolation are rows.
Solution:
p is a predicate over a particular table and x and y are rows satisfying p.
SELECT1 (p) SELECT2 (p) INSERT1(x) INSERT2 (y)
An integrity constraint states that there can be no more than n items in the database
satisfying predicate p. Each transaction reads the tuples satisfying p nds there are
exactly (n 1) and therefore inserts a new tuple satisfying p. At the end there are
(n + 1) tuples satisfying p
21.30 In an Internet election system, each voter is sent a PIN in the mail. When that voter
wants to vote at the election web site, she enters her PIN and her vote, and then a
voting transaction is executed.
In the voting transaction, rst the PIN is checked to verify that it is valid and
has not been used already, and then the vote tally for the appropriate candidate
is incremented. Two tables are used: One contains the valid PINs together with an
indication of whether or not each PIN has been used and the other contains the names
of the candidates and the vote tally for each. Discuss the issues involved in selecting
an appropriate isolation level for the voting transaction. Discuss the issues involved
in selecting appropriate isolation levels if a new (read-only) transaction is introduced
that outputs the entire vote tally table.
255
256
CHAPTER 21
Solution:
The only SQL statements in the voting transaction will be an UPDATE statement that
checks and updates the PIN table and an UPDATE statement that updates the tally
table. Since write locks are always long duration at all isolation levels, schedules of
voting transactions will be serializable at any isolation level.
A transaction that outputs the tally table should run at READ COMMITTED and
use an insensitive cursor in order to output a consistent snapshot of the table.
21.31 An airlines database has two tables: Flights, with attributes flt_num, plane_id,
num_reserv; and Planes, with attributes plane_id, and num_seats.
The attributes have the obvious semantics. A reservation transaction contains the
following steps:
A.
B.
C.
D.
Assume that each individual SQL statement is executed in isolation, that the
DBMS uses intention locking and sets locks on tables and rows, and that host variable
f contains the number of the ight to be booked. The transaction should not overbook
the ight.
a. Assuming that the transaction is run at READ COMMITTED, what locks are held at
points A, B, and D ?
Solution:
A - No locks
B - No locks
D - X lock on row in Flights, IX lock on Flights
b. The database can be left in an incorrect state if concurrently executing reservation
transactions that are running at READ COMMITTED are interleaved in such a way
that one transaction is completely executed at point B in the execution of another.
Describe the problem.
Exercises
Solution:
Lost update causing database to incorrectly record number of passengers and ight
might be overbooked
c. In an attempt to avoid the problem described in (b), the SET clause of the UPDATE
statement is changed to F.num_reserv = F.num_reserv + 1. Can reservation
transactions now be run correctly at READ COMMITTED? Explain.
Solution:
Flight can still be overbooked, but database correctly records number of passengers
d. Assuming that the transaction is run at REPEATABLE READ and that the tables are
accessed through indices, what table locks are held at points A, B, and D ?
Solution:
A - S lock on row in Flights, IS lock on Flights
B -The locks named in (A), plus S lock on row in Planes, IS lock on Planes
D - X lock on row in Flights, IX lock on Flights, S lock on row in Planes, IS
lock on Planes
e. What problem does the interleaving of (b) cause at REPEATABLE READ? Explain.
Solution:
Deadlock
f. Does the interleaving of (b) cause an incorrect state if the transaction (either version)
is run using SNAPSHOT isolation? Explain.
Solution:
No. If two transactions attempt to update the same tuple, one will be aborted.
g. To keep track of each passenger, a new table, Passenger, is introduced that has
a row describing each passenger on each ight with attributes name, flt_num,
seat_id. SQL statements are appended to the end of the transaction (1) to read the
seat_ids assigned to each passenger on the ight specied in f and (2) to insert a
row for the new passenger that assigns an empty seat to that passenger. What is the
weakest ANSI isolation level at which the transaction can be run without producing
an incorrect state (i.e., two passengers in the same seat)? Explain.
Solution:
REPEATABLE READ. No phantom is possible since only one transaction can add a
tuple to Passenger for a particular ight at a time (since a transaction must hold
an X lock on the row in Flights before inserting a row in Passenger).
21.32 Two transactions run concurrently, and each might either commit or abort. The
transactions are chosen from the following:
T1 : r1(x) w1(y)
T2 : w2(x)
T3 : r3(y) w3 (x)
T4 : r4(x) w4(x) w4 (y)
257
258
CHAPTER 21
In each of the following cases, state (yes or no) whether the resulting schedule is always
serializable and recoverable. If the answer is no, give an example of a schedule that is
either not serializable or not recoverable.
a. T1 and T2 both running at READ UNCOMMITTED
Solution:
No.
w2(x) r1(x) w1(y) c1 a2
Not recoverable
b. T2 and T2 both running at READ UNCOMMITTED
Solution:
Yes
c. T1 and T2 both running at READ COMMITTED
Solution:
Yes
d. T1 and T3 both running at READ COMMITTED
Solution:
No
r1(x) r3(y) w1(y) w3(x)
Not serializable
e. T1 and T3 both running at SNAPSHOT isolation
Solution:
No
r1(x) r3(y) w1(y) w3(x)
Not serializable
f. T1 and T4 both running at SNAPSHOT isolation
Solution:
Yes
21.33 Give an example of a schedule that could be produced at SNAPSHOT isolation in
which there are two transactions that execute concurrently but do not have the same
snapshot number and do not see the same snapshot of the database. (Hint: the schedule
can contain more than two transactions.)
Exercises
Solution:
T1 : r (x)
T2 :
w (x) commit
r (y)
T3 :
T2 and T3 are concurrent but T3 has a snapshot number one greater than that of T2
because T1 committed before T3 started. Thus T3 reads the same value of y that T2
does but reads a dierent value of x
21.34 A database has two tables:
Student(Id, Name, . . .)Id and Name are both unique
Registered(Id, CrsCode, Credit, . . .)contains one row for each course each
student is taking this semester
A transaction type, T , has two SQL statements, S 1 followed by S 2 (with local
computations between them):
S1
SELECT
INTO
FROM
WHERE
GROUP BY
SUM(R.Credits), S.Id
:sum, :id
Student S, Registered R
S.Name = Joe AND S.Id = R.Id
S.Name, S.Id
S2
UPDATE
SET
WHERE
Registered
Credits = Credits + 1
Id = :id AND CrsCode = :crs
S 1 returns the total number of credits for which Joe is registered, together with his
Id. T maintains the integrity constraint no student shall register for more than 20
credits. If Joe has less than 20 credits, T executes S 2 to increment the number of
credits for which Joe has registered in a particular course. Suppose Joe executes two
instances of T concurrently at the following isolation levels. In each case say whether
or not the named violation of the constraint can occur and, if the answer is yes, explain
how (e.g., what locks are or are not held).
a. READ COMMITTED
lost update
Solution:
no
violation of the integrity constraint
Solution:
yes - T1 and T2 both get short term shared locks on all Joes rows in
Registered and calculate the total number of credits. Assume it is 19. Then
259
260
CHAPTER 21
T1 gets a long term exclusive lock on one of Joes rows, increments it and
commits. T2 then does the same on another (possibly the same) row.
deadlock
Solution:
no
b. REPEATABLE READ
lost update
Solution:
no
violation of the integrity constraint
Solution:
no
deadlock
Solution:
yes - Both T1 and T2 get long term shared locks on all Joes rows in
Registered. Then T1 requests an exclusive lock on one of those rows and
T2 requests an exclusive lock on another (possibly the same).
c. SNAPSHOT
lost update
Solution:
no
violation of the integrity constraint
Solution:
yes - T1 and T2 both execute from the same version. Then they execute S 2
using dierent values of :crs.
deadlock
Solution:
no
22
Atomicity and Durability
EXERCISES
22.1
Describe the contents of each of the following log records and how that record is used
(if at all) in rollback and in recovery from crashes and media failure.
a. Abort record
Solution:
When a transaction aborts, it rolls back its updates and then writes an abort record
to the log. The abort record contains the transactions Id. Using these records, the
identity of the transactions active at the time of the crash can be determined by the
recovery procedure.
b. Begin record
Solution:
To avoid a complete backward scan, when T is initiated a begin record containing its
transaction Id is appended to the log. When a transaction is aborted the backward
scan for update records can be stopped when T s begin record is encountered.
c. Begin dump record
Solution:
Before starting a dump, a begin dump record is appended to the log. This is used
to locate the checkpoint record at which roll forward during pass 2 of the media
recovery procedure must start.
d. Checkpoint record
Solution:
The system periodically writes a checkpoint record to the log listing the identities of
currently active transactions. The recovery process must (at least) scan backward
to the most recent checkpoint record inorder to determine which transactions were
active at the time of the crash.
e. Commit record
Solution:
When a transaction commits, it writes a commit record to the log. The commit record
262
CHAPTER 22
contains the transactions Id. Using these records, the identity of the transactions
active at the time of the crash can be determined by the recovery procedure. A
transaction is committed when this record is stored on the (non-volatile) log.
f. Compensation log record
Solution:
Compensation log record s are used to record the reversal of updates during abort
processing.
g. Completion record
Solution:
Indicates, in a deferred update system, that updates in the intentions list of a
transaction have been transferred to the database. On recovery, the log update
records of incomplete transactions must be used to install their updates in the
database.
h. Redo record
Solution:
With physical logging the redo record contains the new value of the data item that
was updated (the after image). It is used to update the database after a crash.
i. Savepoint record
Solution:
Each time a transaction declares a savepoint, a savepoint record , containing the
transactions identity and the identity of the savepoint, is written to the log. To roll
back to a specic savepoint, the log is scanned backwards to the specied savepoints
record.
j. Undo record
Solution:
With physical logging the undo record contains the old value of the data item that
was updated (before image). If the transaction aborts, the undo record is used to
restore the item to its original value.
22.2
Suppose that the concurrency control uses table locks and that a transaction performs
an operation that updates the value of one attribute of one tuple in one table. Does
the update record have to contain images of the entire table or just the one tuple?
Solution:
Only images of the tuple have to be stored.
22.3
Suppose that a dirty page in the cache has been written by two active transactions
and that one of the transactions commits. Describe how the caching procedure works
in this case.
Solution:
The write-ahead feature implies that the page cannot be written until the update
records of both transactions have been stored in the log. Assuming update records
contain after-images, the page does not have to be written out except as dictated by
the rule relating to the use of checkpoint records.
Exercises
22.4
Suppose that the database system crashes between the time a transaction commits
(by appending a commit record to the log) and the time it releases its locks. Describe
how the system recovers from this situation.
Solution:
No special procedures are required. Since the lock table is volatile, the system has no
way of remembering the status of locks and so locks are automatically released.
22.5
Explain why the log buer need not be ushed when an abort record is appended to it.
Solution:
If the system crashes before the log buer is ushed, that transaction will be aborted
anyway because there is no commit or abort record for it in the (non-volatile) log.
22.6
Explain why the LSN need not be included in pages stored in the database when
physical logging is used together with a cache and log buer.
Solution:
With physical logging the LSN is used to enforce the write-ahead policy and is not
used during recovery. Its value lies in determining the relationship between updates to
a page after it has been brought into the cache and the corresponding update records
in the log. Update records corresponding to updates to the page during its previous
stay in the cache must have been moved to mass storage before the page was swapped
out. Hence, it is sucient to associate the LSN with each page in the cache, but it
need not be stored in the page. When a page is brought into the cache it is clean and
the associated LSN can be initialized to a null value.
22.7
Suppose that each database page contained the LSN of the commit record of the last
transaction that has committed and written a database item in the page, and suppose
that the system uses the policy that it does not ush the page from the cache until the
LSN of the oldest record in the log buer is greater than the LSN of the page. Will
the write-ahead policy be enforced?
Solution:
No. An item updated by an uncommitted transaction might be contained in the page.
22.8
263
264
CHAPTER 22
Solution:
Because the transactions can be serialized in commit order
22.9
In the sharp checkpoint recovery procedure, explain whether or not the system needs
to obtain locks when it is using the after-images in the log to update the database.
Solution:
No. No transactions are executed while recovery is taking place.
22.10 Consider the following two-pass strategy for crash recovery using a sharp checkpoint
and physical logging: (1) The rst pass is a backward pass in which active transactions
are rolled back. Active transactions are identied as described in Section 22.2. The
pass extends at least as far as the begin record of the oldest active transaction or the
most recent checkpoint record, whichever is earlier in the log. As update records for
these transactions are encountered in the scan, their before-images are applied to the
database. (2) The second pass is a forward pass from the most recent checkpoint record
to roll forward, using after-images, all changes made by transactions that completed
since the checkpoint record was written (compensation log records are processed in
the same way as ordinary update records so that aborted transactions are handled
properly). Does the procedure work?
Solution:
Yes. The rst pass rolls back all the updates made by transactions that were active
when the crash occurred. The second pass does (or redoes) all the changes made by
transactions that committed since the last checkpoint. (those changes might or might
not have yet been stored in the database.)
22.11 In order for logical logging to work, a logical database operation must have a logical
inverse operation. Give an example of a database operation that has no inverse. Suggest
a procedure involving logical logging that can handle this case.
Solution:
An operation that changes an item x to f (x), where f () is some function with no
known inverse (such as x := 5) is an example. A combination of physical and logical
logging can be used. Physical logging is used for operations that have no inverse, logical
logging is used for others. Each update record identies the type of undo/redo that it
uses.
22.12 Consider using the crash recovery procedure described in Section 22.2 (intended for
physical logging) when logical logging is used. Explain how the procedure has to be
modied to handle crashes that occur during recovery. Assume that the eect of each
update is conned to a single page.
Solution:
Problem: The crash might occur during Phase 3. It makes a dierence during the
second execution of recovery (after the crash during Phase 3) whether the application
of an inverse operation to an updated item has already been performed. Hence, LSNs
must be used to determine whether an operation should be applied to a page.
22.13 Explain why, in a deferred-update system, the write-ahead feature that is a part of
immediate-update systems is not used when a database item is updated.
Exercises
Solution:
Neither the write-ahead feature nor a before-image in the update record is required in
this case since the database item is not updated until after the transaction commits.
22.14 Explain why in a deferred-update system, the system does not rst copy the intentions
list into the database and then append the commit record to the log.
Solution:
Because the intentions list and the update records do not contain before-images. If
the system should crash after the database has been updated, but before the commit
record has been appended to the log, there would be no way to roll the transaction
back.
22.15 Assume that the system supports SNAPSHOT isolation. Describe how a sharp
(nonfuzzy) dump could be taken without shutting down the system.
Solution:
Run a transaction that reads each item in the database, and a snapshot will be
returned. That snapshot could be used as the dump.
22.16 a. Explain how the log is implemented in your local DBMS.
b. Estimate the time in milliseconds to commit a transaction in your local DBMS.
Solution:
This is a hands-on problem for which the solution is not meant to be given in this
manual.
22.17 The LSN stored in a page of the database refers to an update record in the log describing
the most recent update to the page. Suppose that a transaction has performed the last
update to a page and later aborts. Since its update to the page is reversed, the LSN in
the page no longer refers to the appropriate update record. Why is this not a problem
in the description of logging in the text?
Solution:
When the update is reversed, a compensation log record is written to the log and its
LSN is recorded in the page. Since the UNDO can be viewed as simply another write,
the pages LSN is correct.
22.18 An airlines reservation system has demanding performance and availability standards.
Do the following play a role in enhancing performance? Do they enhance availability?
Explain your answers.
a. Page cache
Solution:
Enhances performance (page I/O avoided), not availability
b. Log buer
Solution:
Enhances performance (page I/O avoided), not availability
c. Checkpoint record
Solution:
Enhances availability (recovery is speeded up), not performance
265
266
CHAPTER 22
d. Physiological logging
Solution:
Enhances performance slightly (log records are smaller), but executing logical
operations during recovery might increase recovery time
e. Mirrored disk
Solution:
Enhances availability
PART SIX
23
Architecture of Transaction
Processing Systems
EXERCISES
23.1
Explain the advantages to a bank in providing access to its accounts database only
through stored procedures such as deposit() and withdraw().
Solution:
1.
2.
3.
4.
23.2
Correctness: The procedures are provided by the bank, which can do extensive
quality assurance on them. Since the procedures are maintained by the bank site,
they are protected from being altered by intruders. Users can be prohibited from
submitting individual SQL statements, which might corrupt the database.
Eciency: The SQL statements in the procedures can be prepared in advance and
therefore can be executed more eciently.
Authorization: The service provider can more easily authorize users to perform
particular application-specic functions.
Throughput: By oering a more abstract service, the amount of information that
must be communicated through the network is reduced. Only the input and output
arguments of the procedure must be communicated.
270
CHAPTER 23
23.4
Explain why the cancel button on an ATM does not work after the submit button has
been pressed.
Solution:
The transaction at the bank has started its execution and might have already
committed. One might say that the ATM is in a prepared state.
23.5
2.
23.6
Separating the stored procedures from the application program has the advantages
of decreasing communication costs and providing a more abstract interface to the
application program.
Executing the stored procedures on a separate transaction server instead of in
the DBMS has the advantage of reducing the load on the DBMS, thus providing
increased scalability for large numbers of transactions.
23.7
23.8
Explain whether the system you are using for your project can be characterized as
TP-Lite or TP-Heavy.
Solution:
This is a hands-on problem. Its solution is not meant to be included in the manual.
23.9
Exercises
Solution:
1.
2.
3.
4.
5.
The DBMSs are supplied by dierent vendors and speak dierent dialects of SQL.
Some of the DBMSs do not perform the two-phase commit protocol.
The systems use dierent low-level communication protocols.
At a higher level, some systems use RPCs and others use peer-to-peer communication.
Some of the legacy systems are so old that the source code for the transactions
has disappeared from the face of the earth.
23.14 Suppose that a transaction uses TRPC to update some data from a database at a
remote site and that the call successfully returns. Before the transaction completes,
the remote site crashes. Describe informally what should happen when the transaction
requests to commit.
Solution:
The TRPC has notied the coordinator that the remote site is a participant in the
transaction. Therefore when the coordinator performs the two-phase commit protocol,
it will attempt to contact that site. When it nds that the site has crashed, it will
abort the transaction.
23.15 Explain the dierence between the tx_commit() command used in the X/Open API
and the COMMIT statement in embedded SQL.
271
272
CHAPTER 23
Solution:
The COMMIT statement in embedded SQL is a local command requesting that the
current DBMS be committed. The tx_commit() statement is a request to commit a
global transaction, that is to perform a two-phase commit of all the DBMSs that the
global transaction has accessed.
23.16 Propose an implementation of distributed savepoints using the tx and xa interfaces to
the transaction manager. Assume that each subtransaction (including the transaction
as a whole) can declare a savepoint, and that when it does so, it forces its children to
create corresponding savepoints. When a (sub)transaction rolls back to a savepoint,
its children are rolled back to their corresponding savepoints.
Solution:
The transaction manager, TM , keeps track of the hierarchical relationships among
the subtransactions of a distributed transaction. When a subtransaction, T , creates
a child, T , at a resource manager, RM , RM provides TM with a savepoint and
a rollback callback (part of xa). To create a savepoint, T calls the create_savepoint
procedure (part of tx) of TM . TM in turn calls RM at its savepoint callback requesting
RM to create a corresponding savepoint for T . The relationship between a particular
savepoint in T and the corresponding savepoint in T is maintained at TM . A rollback
call from T to TM to a particular savepoint of T causes TM to call RM at its rollback
callback causing it to rollback to the corresponding savepoint.
23.17 Give three advantages of using an application server architecture in a client server
system.
Solution:
Cheaper, more maintainable; more secure; better authorization; more reliable.
23.18 Give an example of an event, dierent from that given in the text, in which the callback
function should not be part of the transaction.
Solution:
An Internet site saves the information about a user even if the users transaction
should abort.
23.19 Explain how peer-to-peer communication can be used to implement remote procedure
calling.
Solution:
Send a message requesting some services and wait for the return message that the task
was completed.
23.20 Explain some of the authentication issues involved in using your credit card to order
merchandise over the Internet.
Solution:
1.
2.
How does the merchant know you are the owner of the card.
How do you know that the merchant is who it claims to be and is not three
students in a dorm room whose main goal is to steal your card number.
Exercises
23.21 Implement a Web tic-tac-toe game in which the display is prepared by a presentation
server on your browser and the logic of the game is implemented within a servlet on
the server.
Solution:
This is a hands-on problem for which the solution is not meant to be given in this
manual.
23.22 Print out the le of cookies for your local Web browser.
Solution:
This is a hands-on problem for which the solution is not meant to be given in this
manual.
23.23 Consider a three-tiered system interfacing to a centralized DBMS, in which n1
presentation servers are connected to n2 application servers, which in turn are
connected to n3 transaction servers. Assume that for each transaction, the application
server, on average, invokes k procedures, each of which is executed on an arbitrary
transaction server, and that each procedure, on average, executes s SQL statements
that must be processed by the DBMS. If, on average, a presentation server handles r
requests per second (each request produces exactly one transaction at the application
server), how many SQL statements per second are processed by the DBMS and how
many messages ow over each communication line?
Solution:
r n1 k s statements per second are processed by the DBMS.
Each line connecting a presentation server to an application server handles 2 r
messages per second
Each application server is connected to n1/n2 presentation servers and therefore
invokes r k (n1/n2) procedures per sec. Assuming that an application server is
connected to all transaction servers and that it randomly chooses a transaction server
to execute a procedure, the number of invocations over a particular line connecting the
application server to a transaction server is r k (n1/n2)/n3, and hence the number
of messages over the line is twice that number.
The number of statements transmitted over a line connecting a transaction server to
the DBMS is r n1 k s/n3, and hence the number of messages over the line is twice
that number.
23.24 In Section 23.10 we discuss an optimistic concurrency control that uses a dierent
validation check than the concurrency control described in Section 20.9.2. Give an
example of a schedule that would be allowed by the validation check of Section 23.10
but not by the validation check of Section 20.9.2.
Solution:
273
24
Implementing Distributed
Transactions
EXERCISES
24.1
24.2
Explain why a cohort does not have to force an abort record to the log during the
two-phase commit protocol.
Solution:
The cohort might decide independently to abort (no prepare record is forced in this
case) but crashes before the abort record it writes is durable. The coordinator will
decide abort whether or not an vote message is received. The standard recovery
276
CHAPTER 24
procedures at the cohort site will cause the subtransaction to be aborted independent
of whether an abort record is found.
The other case which causes an abort record to be written is if the cohort votes
ready but receives an abort message from the coordinator. If the cohort crashes
before the abort record is durable, the presumed abort property will result in the
subtransaction being aborted anyway.
24.3
Explain how the fuzzy dump recovery procedure must be expanded to deal with
prepared records in the log (assuming the site engages in the two-phase commit
protocol).
Solution:
A transaction in the prepared state must be treated as a committed transaction on
recovery from media failure. However, items it has updated must be kept locked
until the uncertain period completes in case the transaction has been aborted by the
coordinator.
24.4
Describe the two-phase commit protocol when one or more of the database managers
is using an optimistic concurrency control.
Solution:
The validation procedure is performed after the prepare message has been received. If
validation is successful the cohort votes ready, otherwise it votes aborting.
24.5
Describe the presumed abort feature in the case in which multiple domains are involved.
Solution:
If an intermediate transaction manager crashes and is restarted and if there is a
prepared record but no commit record for some transaction in its log, the transaction
record for that transaction is restored to volatile memory. Then any transaction
manager can use the usual presumed abort feature. If any cohort in a prepared state
asks its transaction manager the state of the transaction, and that transaction manager
does not nd a transaction record for that transaction in its volatile memory, it can
presume that transaction has aborted.
24.6
Describe how the two-phase commit protocol can be expanded to deal with a cohort
site that uses a timestamp-ordered concurrency control.
Solution:
The protocol described in the text assumes that cohorts use strict two-phase locking
controls and hence that transactions are serialized in commit order. In order to achieve
serialization in commit order, when a site using a time-stamp ordered concurrency
control receives a prepare message for a transaction T , it checks to see whether T
has a time-stamp later than all committed or prepared subtransactions. If so, it votes
ready; if not it votes aborting.
24.7
Give schedules of distributed transactions executing at two dierent sites such that
the commit order is dierent at each site but the global schedule is serializable.
Solution:
Site 1: w1 (x) Commit1 w2(y) Commit2
Site 2: w1 (a)
w2(b) Commit2
Commit1
Exercises
24.8
24.9
Design a logging, timeout, and restart procedure for the linear commit protocol. Do not
assume the existence of a separate coordinator module. Assume that all communication
between cohorts is carried on along the chain.
277
278
CHAPTER 24
Solution:
The rightmost cohort keeps a log in which it stores commit records for distributed
transactions. Each cohort forces a prepare record prior to sending a ready message to
its neighbor. On recovery, a cohort in its uncertain period sends a query message down
the chain. The coordinator uses the presumed abort property in replying to the query.
24.10 Consider a distributed transaction processing system that uses a serial validation
optimistic concurrency control at each site and a two-phase commit protocol. Show
that deadlocks are possible under these conditions.
Solution:
A subtransaction enters its validation phase when the prepare message arrives. It
votes ready or abort based on the result of the validation test, but cannot start the
write phase until the commit or abort message arrives in Phase 2 of the commit
protocol, preventing other (sub)transactions at that site from entering validation. If
T1 and T2 both have cohorts at sites A and B and both start the commit protocol at
approximately the same time, it is possible that T1A enters validation at site A and
T2B enter validation at site B. A deadlock results if
1.
2.
T1 cannot complete, since T1A cannot exit from its validation phase until T1B
completes validation and T1B is waiting for T2B .
T2 cannot complete, since T2B cannot exit from its validation phase until T2A
completes validation and T2A is waiting for T1A.
24.11 Prove that if all sites use optimistic concurrency controls and if a two-phase commit
protocol is used, distributed transactions are globally serializable.
Solution:
The argument is the same as that used to demonstrate global serializability when each
site uses a strict two-phase locking protocol.
24.12 If a cohort in a distributed transaction has performed only read operations, the
two-phase commit protocol can be simplied. When the cohort receives the prepare
message, it gives up its locks and terminates its participation in the protocol. Explain
why this simplication of the protocol works correctly.
Solution:
The cohort terminates it participation in the protocol before it is determined whether
the distributed transaction, T , commits or aborts. Ordinarily, if T aborts, the cohort
must rollback the subtransaction, but with a read-only transaction, there are no
changes to roll back. It makes no dierence whether the cohort releases locks when
the prepare message is received or when the protocol terminates (as would be the case
without the simplication) because any other transaction that writes an item that the
cohort had read would have to follow T in the global ordering in either case.
24.13 Consider the following atomic commit protocol that attempts to eliminate the blocking
that occurs in the two-phase commit protocol. The coordinator sends a prepare message
to each cohort containing the addresses of all cohorts. Each cohort sends its vote directly
to all other cohorts. When a cohort receives the votes of all other cohorts it decides to
commit or abort in the usual way.
Exercises
a. Assuming no failures, compare the number of messages sent in this protocol and
in the two-phase commit protocol. Assuming that all messages take the same xed
amount of time to deliver, which protocol would you expect to run faster? Explain.
Solution:
Assume there are n cohorts. The two-phase commit protocol uses 4n messages and
takes 4 message times. The new protocol uses n prepare messages plus n(n 1)
vote messages for a total of n 2 messages and takes 2 message times
b. Does the protocol exhibit blocking when failures occur?
Solution:
Yes. If a cohort that has voted ready does not get a vote from another cohort, it is
blocked.
24.14 The kill-wait concurrency control of Exercise 20.32 is based on locking. When it is
used in a distributed system, it is referred to as the wound-wait protocol. We assume
that a distributed transaction uses RPC to communicate among cohorts so that when
the two-phase commit protocol starts, all cohorts have completed. The kill primitive
is replaced by a wound primitive.
If the cohort of transaction, T1, at some site makes a request that conicts with
an operation of the cohort of an active transaction, T2, at that site, then
if TS (T1) < TS (T2) then wound T2 else make T1 wait
where wound T2 means that T2 is aborted (as in the kill-wait protocol), unless T2 has
entered the two-phase commit protocol, in which case T1 waits until T2 completes the
protocol.
Explain why this protocol prevents a global deadlock among transactions.
Solution:
Since RPC is the mode of communication, when T2 has entered the two-phase commit
protocol, it has completed at all sites and therefore is not waiting for resources held
by T1 (or any other transaction) at any site. Because the protocol is based on locking,
once a transaction enters the two-phase commit protocol it will complete (assuming
no failures). Therefore a deadlock is not possible among active transactions.
24.15 Suppose the nested transaction model were extended so that subtransactions were
distributed over dierent sites in a network. At what point in the execution of a
distributed nested transaction would a cohort enter the prepared state? Explain your
reasoning.
Solution:
If the conditional commit implies that a vote message is sent to the parent then the
child must enter the prepared state at that time. Alternatively, if the coordinator polls
all subtransactions that have conditionally committed when the nested transaction as
a whole completes then the child need not enter the prepared state until it is polled.
24.16 In the text we state that if, in a distributed database system, each site uses a strict
two-phase locking concurrency control (all locks are held until commit time) and the
system uses a two-phase commit protocol, transactions will be globally serializable.
279
280
CHAPTER 24
Does the result also hold if the concurrency controls are not strictread locks are
released earlybut are two phase?
Solution:
No. T1A might release a read lock on x at site A and T2A might subsequently write x.
A similar situation might exist at site B on item y except that the subtransaction of
T1 and T2 might be ordered in the opposite way. Hence the serialization graph for T1
and T2 has a cycle, but do not delay each other. Both might complete, although their
subtransactions are ordered dierently at the two sites.
24.17 Explain how to implement synchronous-update replication using triggers.
Solution:
When an update is made to a replica, a trigger is activated. The trigger can be
considered immediately or at commit time. Trigger execution updates all other replicas
and completes before the transaction commits.
24.18 Design a quorum consensus protocol in which, instead of a timestamp eld, each item
has a version number eld, which is updated whenever the item is written.
Solution:
The protocol is the same as the one in the text except that each item has a version
number eld, which is incremented every time the item is written. In any read quorum,
the replica with the highest version number is the current value of the item.
24.19 Consider a quorum consensus protocol in which an item is stored as ve replicas and
the size of each read and write quorum is three. Give a schedule that satises the
following conditions:
Three dierent transactions write to the item. Then two of the replica sites fail,
leaving only three copiesall of which contain dierent values.
Explain why the protocol continues to execute correctly.
Solution:
T1 : w1 w2 w3
T2 :
T3 :
Site failures
w3 w4 w5
w1 w3 w5
s1 fails s3 fails
The three remaining copies are at s2 (which was last updated by T1), s4 (which was
last updated by T2 ), and s5 (which was last updated by T3). Each of the copies has a
dierent value, but the value at s5 was updated last and is the correct value.
24.20 Consider a quorum consensus protocol in which an item is stored as n replicas, and
the size of read and write quorums are p and q respectively.
a. What is the maximum number of replica sites that can fail and still have the protocol
work correctly?
Solution:
The minimum of n p and n q.
b. What is the minimum value that p and q can have such that p = q?
Exercises
Solution:
The smallest integer greater than n/2
c. Select p and q so that the maximum number of replica sites can fail and still have
the protocol work correctly. For this selection, how many sites can fail?
Solution:
p and q are both selected to be the smallest integer greater than n/2. The number
of sites that can fail is then the largest integer less than n/2. For example, if n = 7,
p and q are both equal to 4, and 3 sites can fail.
24.21 The quorum consensus replication algorithm requires that the timestamps of
transactions be consistent with their commit order. In the absence of synchronized
clocks, this requirement is not easily met. Propose an alternate algorithm for
tagging replicas that can be used with quorum consensus. (Hint: Instead of assigning
timestamps to transactions, assign version numbers to individual replicas.)
Solution:
When an item is created, each replica is assigned the version number 0. When a
transaction writes a new value to an item the new value of the version number of all
the replicas in the set is one more than the maximum of all the old version numbers
in the set. Since read and write quorums intersect all write quorums, the replica with
the maximum version number in any read quorum must have the most recent value of
the item.
24.22 Describe an application of replicated data items in which serializability is not needed.
Solution:
Theater Ticketing System in which each site is allocated some seats, and databases
are periodically compared and synchronized.
24.23 In what way is the checkbook you keep at home for your checking account like an
asynchronous-update replication system?
Solution:
It is asynchronous because the changes you make in the check register at home are not
synchronized with the (corresponding) changes the bank makes to your account item
in its database. Hopefully, you synchronize the numbers once a month when you get
the bank statement.
24.24 Give an example of a nonserializable schedule produced by a primary copy
asynchronous-update replication system.
Solution:
T1: w (xp ) w (yp ) commit
r (xp )
T2 :
Tru :
r (ys )
w (ys )
24.25 The following variant of the primary copy asynchronous-update replication protocol
has been proposed for totally replicated systems.
a. A transaction executing at site A updates only the replicas at A before it commits
(it needed to access no other site since replication is total).
b. After the transaction commits, a second transaction is initiated to update the
primary copies of all items updated at A.
281
282
CHAPTER 24
c. After the transaction in step (b) has completed, each primary site propagates the
update made at that site to all secondaries (including the one at site A). Updates
made to a primary copy by several transactions are propagated to secondaries in
the order in which the primary was updated.
Explain why, in step (b), all primaries must be updated in a single transaction and, in
step (c), the update is propagated to site A.
Solution:
The goal of the protocol is mutual consistency of all replicas. All primaries must be
updated in a single transaction to make sure that updates to all primaries made by
concurrent transactions occur in the same order. The update must be propagated back
to A to make sure that secondary copies at A are updated in the same order as the
primaries. For example, suppose sites A and B both have secondary replicas of x and
transactions T1 and T2 executing at A and B respectively update x concurrently. If
in step (c) the primary site did not send updates to all sites, then when the protocol
completed the value of x at A would be the value written by T2 and at B the value
written by T1.
24.26 Explain how triggers can be used to implement primary copy replication.
Solution:
A transaction, T1, updates only the primary copy of the replicated data. When T1
commits, a trigger initiates the update of the other replica
24.27 Describe a design of a logging protocol for distributed savepoints.
Solution:
The transaction manager, TM , provides a tx_create_savepoint procedure that
can be called from the application. TM knows the identity of all resource managers
participating in the transaction. It calls those resource managers using the xa interface
to request that they each create a local savepoint for the transaction and return
the savepoints identity. TM then writes a savepoint record to its log and returns a
distributed savepoint identier to the application. TM also provides a tx_rollback
procedure that can be called by the application. The application provides a distributed
savepoint identier as an argument to the procedure. The procedure locates the
savepoint record in its log and calls each resource manager, requesting it to rollback
to the appropriate local
24.28 In the presumed commit version of the two-phase commit protocol described in the
text, when the protocol starts, the coordinator forces a start record to its log. Describe
a situation in which, if the coordinator had not forced this record to its log, the
protocol would not work correctly.
Solution:
The coordinator crashes during the rst phase of the protocol after sending prepare
messages to a number of the cohorts. Some of the cohorts that received the message
have voted ready and some have voted aborting. After the coordinator crashes and
is restarted, it nds no information about the transaction in its log. Later one of the
cohorts that had voted ready (and had timed out while waiting for a response) asks
the coordinator the state of the transaction. Since the coordinator nds no transaction
Exercises
record for the transaction in its volatile memory, it reports, incorrectly, that the
transaction has committed (some cohorts have already aborted).
283
284
CHAPTER 24
25
Web Services
EXERCISES
25.1
25.2
Give the declaration of a Java method that corresponds to the RPC SOAP messages
in Figures 25.3 and 25.4.
Solution:
286
CHAPTER 25
Web Services
25.3
Give an example of a Java object that can be serialized into the parts of the messages
in the WSDLgetFareOperation.
Solution:
25.4
<message name=receiveResMsg>
<part name=custId type=xs.string/>
<part name=result type=xs:string/>
<part name=cost type=xs:float/>
</message>
Exercises
287
<message name=receiveResMsg>
<part name=custId element=gs:custIdElem/>
<part name=result element=gs:resultElem/>
<part name=cost element=gs:costElem/>
</message>
<binding name=hotelCallbackDocLitBinding type =gs:hotelCallbackPT>
<soap:binding style=document
transport=http://schemas.xmlsoap.org/soap/http/>
<operation name=gs:receiveResOp>
<input>
<soap:body
use=literal
namespace=http://www.goslow.com/wsdl/trips/>
</input>
</operation>
</binding>
25.5
288
CHAPTER 25
Web Services
Solution:
For simplicity we will assume that SlowHawks only client is GoSlow. Hence, when an
asynchronous request for fare information arrives, SlowHawk knows to whom to reply.
It does this using a callback port type provided by GoSlow. Let GoSlows port type be
named asynchRespPT and its (one-way) operation asynchRespOp.
SlowHawks asynchronous service will be hosted at a new port type:
<portType name=asynchGetFarePT>
<operation name=asynchGetFareOp>
<input message=gs:itineraryMsg/>
</operation>
</portType>
A new partner link type to connect the two processes through the new port types is
required.
<plnk:partnerLinkType name=asynchGetFarePLT>
<plnk:role name=airline>
<plnk:portType name=gs:asynchGetFarePT/>
</plnk:role>
<plnk:role name=travelAg>
<plnk:portType name=gs:asynchRespPT/>
</plnk:role>
</plnk:partnerLinkType>
SlowHawk must then declare (in its BPEL program) a partner link which asserts its
role in the message exchange.
<partnerLinks>
<partnerLink name=asynchGetFarePL
partnerLinkType=gs:asynchGetFarePLT
myRole=airline partnerRole=travelAg/>
</partnerLinks>
Finally, the receive/reply pattern of communication used for synchronous requests in
SlowHawk is replaced by a receive to accept the request from GoSlow and an invoke
to return the reply.
<receive partnerLink=asynchGetFarePL
portType=asynchGetFarePT
operation=asynchGetFareOp variable=asynchGetFareVar/>
<! body of handler for asynchronous request goes here >
<invoke partnerLink=asynchGetFarePL
portType=asynchRespPT
Exercises
289
operation=asynchRespOp inputVariable=asynchRespVar/>
25.6
SlowHawk provides versions of the getFarePT (Section 25.5) that use the HTTP GET
and POST protocols. Show a possible binding for each of these protocols. (You might
have to look on the Web to get additional information not in the text.)
Solution:
290
CHAPTER 25
Web Services
<output>
<mime:content type = "text/xml"/>
</output>
</operation>
</binding>
25.7
25.8
Design the outline of a BPEL process that the vendor can use to describe the following
business process:
When you buy something over the Internet, you send the vendor the name, catalog
number, and price of the item you want. Then the vendor sends you a form to ll
out, containing the name, catalog number, and price of the item and requesting
your name, address, credit card number, and expiration date. You return the form
to the vendor. The vendor then sends your name, credit card number, and the
Exercises
291
price of the item to the credit card company to be approved. After it is approved,
the vendor sends you a conrmation message. Then it sends a message to the
warehouse to ship the item.
Solution:
Outline of vendor BPEL process
292
CHAPTER 25
Web Services
following WSDL declarations. The le containing the declarations assigns the prex n
to its target namespace.
<portType name=negPT>
<operation name=bidOp>
<input message=n:bidMsg/>
</operation>
<operation name=acceptOp>
<input message=n:acceptMsg/>
</operation>
</portType>
<portType name=custToNegPT>
<operation name=custBidOp>
<input message=n:custBidMsg/>
</operation>
<operation name=custAcceptOp>
<input message=n:custAcceptMsg/>
</operation>
</portType>
<plnk:partnerLinkType name=negPLT>
<plnk:role name=initiator>
<plnk:portType name=n:negPT/>
</plnk:role>
<plnk:role name=negPartner>
<plnk:portType name=n:negPT/>
</plnk:role>
</plnk:partnerLinkType>
<plnk:partnerLinkType name=custToNegPLT>
<plnk:role name=negotiator>
<plnk:portType name=n:custToNegPT/>
</plnk:role>
<plnk:role name=customer>
<plnk:portType name=c:customerPT/>
</plnk:role>
</plnk:partnerLinkType>
The prex ng in the processes refers to the WSDL document containing these
declarations. We assume that each customer supports a port type, customerPT, with
operation getNextBidOp that a negotiator can invoke to relay the latest bid received
from its negotiating partner. The negotiating process at site 1, the initiator, declares
the following partnerLinks:
Exercises
<partnerLinks>
<partnerLink name=site2 partnerLinkType=ng:negPLT
myRole=initiator
partnerRole=negPartner/>
<partnerLink name=custToNeg
partherLinkType=ng:custToNegPLT
myRole=negotiator
partnerRole=customer/>
</partnerLinks>
The following sequence activity is contained in the body of the process at site 1.
<sequence>
<invoke partnerLink=site2 portType=ng:negPT
operation=bidOp inputVariable=bid1Var/>
<assign>
<copy>
<from expression = notAccept/>
<to variable = status/>
</copy>
</assign>
<while condition = status = notAccept>
<pick>
<onMessage partnerLink=site2
portType=ng:negPT
operation=acceptOp variable=. . .>
<assign>
<copy>
<from expression = accept/>
<to variable = status/>
</copy>
</assign>
</onMessage>
<onMessage partnerLink=site2
portType=ng:negPT
operation = bidOp variable = bid2Var>
<sequence>
<invoke partnerLink=custToNeg
portType=c:customerPT
operation=getNextBidOp
inputVariable=bid2Var/>
<pick>
<onMessage partnerLink=custToNeg
portType=ng:custToNegPT
293
294
CHAPTER 25
Web Services
operation=custAcceptOp
variable=. . .>
<assign>
<copy>
<from expression=accept/>
<to variable=status/>
</copy>
</assign>
</onMessage>
<onMessage partnerLink=custToNeg
portType=ng:custToNegPT
operation=custBidOp
variable=bid1Var>
<invoke partnerLink=site2
portType=ng:negPT
operation=bidOp
inputVariable=bid1Var/>
</onMessage>
</pick>
</sequence>
</onMessage>
</pick>
</while>
<!-- handle the completion of the negotiation -->
</sequence>
The outline of the BPEL process at site 2 is similar except that the roles are
reversed and site 2 does not send the initial bid message. The following partner links
are declared (once again the prex ng refers to the WSDL document)
<partnerLinks>
<partnerLink name=site1 partnerLinkType=ng:negPLT
myRole=negPartner
partnerRole=initiator/>
<partnerLink name=custToNeg
partherLinkType=ng:custToNegPLT
myRole=negotiator
partnerRole=customer/>
</partnerLinks>
and the following sequence activity is contained in the body of the process.
<sequence>
Exercises
295
<assign>
<copy>
<from expression = notAccept/>
<to variable = status/>
</copy>
</assign>
<while condition = status = notAccept>
<pick>
<onMessage partnerLink = site1
portType=ng:negPT
operation = acceptOp variable=. . .>
<assign>
<copy>
<from expression = accept/>
<to variable = status/>
</copy>
</assign>
</onMessage>
<onMessage partnerLink=site1
portType=ng:negPT
operation=bidOp variable=bid1Var>
<sequence>
<invoke partnerLink = custToNeg
portType=c:customerPT
operation=getNextBidOp
inputVariable=bid1Var/>
<pick>
<onMessage partnerLink=custToNeg
portType=ng:custToNegPT
operation =custAcceptOp
variable=. . .>
<assign>
<copy>
<from expression = accept/>
<to variable = status/>
</copy>
</assign>
</onMessage>
<onMessage partnerLink=custToNeg
portType=ng:custToNegPT
operation=custBidOp
variable=bid2Var>
<invoke partnerLink = site1
portType=ng:negPT
operation=bidOp
296
CHAPTER 25
Web Services
inputVariable=bid2Var/>
</onMessage>
</pick>
</sequence>
</onMessage>
</pick>
</while>
<!-- handle the completion of the negotiation -->
</sequence>
<portType name=custCallbackPT>
<operation name=custCallbackOp>
<input message=itineraryRespMsg/>
</operation>
</portType>
The partner link type for connecting the customer to Goslow must be changed to
indicate that both ends of the connection support port types:
<plnk:partnerLinkType name=custGoSlowLT>
<plnk:role name=travelService>
<plnk:portType name=gs:tripPT/>
</plnk:role>
<plnk:role name=cust>
<plnk:portType name=gs:custCallbackPT/>
</plnk:role>
</plnk:partnerLinkType>
Exercises
The partner link declaration in the customer must now declare a value for myRole
(myRole=cust (previously it only needed to declare that the value for
partnerRole was travelService). The partner link declaration in GoSlow becomes:
<assign>
<copy>
<from variable=itineraryVar part=custERef/>
<to partnerLink=customer/>
</copy>
</assign>
Finally, the statement in GoSlow that returns a response to the customer becomes an
invoke (instead of a reply):
297
298
CHAPTER 25
Web Services
Exercises
for the sequence are dierent than those used for correlating the messages between
the customer and S 1. When S 1 invokes S 2 for the rst time it must initialize the
correlation set at its end, so that when (the newly created instance at) S 2 responds
the response will be directed to the correct instance of S 1.
299
26
Security and Electronic Commerce
EXERCISES
26.1
Discuss some security issues involved in executing transactions over the Internet.
Solution:
An intruder can eavesdrop on messages, change messages, pretend to be someone else,
send false messages.
An intruder can read/change the database information on a server, read/change
the information on your browser, deny service by sending a large number of messages
to a server.
26.2
Anyone who uses a computer keyboard should be able to easily solve the following
simple substitution cipher:
Rsvj ;ryyrt od vjsmhrf yp yjr pmr pm oyd tohjy pm yjr lrunpstf/
Solution:
Each letter is changed to the one on its right on the keyboard.
26.3
Explain why, in general, short encryption keys are less secure than long keys.
Solution:
Because it is easier for an imposter to guess a short key or to try all keys of a particular
(short) length.
26.4
Why is it necessary, in the Kerberos protocol, to include S in the message sent from
KS to C (i.e., message M 2)? Describe an attack that an intruder can use if S is not
included.
Solution:
I can intercept the rst message (C ,S ) sent in the clear from C to KS and substitute
the message (C ,I ). Thereafter C will think I is S and send private information to I .
26.5
Explain how timestamps are used to defend against a replay attack in a security
protocol.
Solution:
The participant will not accept two dierent messages with the same timestamp.
302
CHAPTER 26
26.6
Explain how nonces are used to increase security when encrypting messages that are
short or that include elds for which the plaintext might be known to an intruder.
Solution:
It is more dicult for the intruder to guess part of the plaintext, which would make it
easier to determine the key.
26.7
26.8
26.9
Suppose that you use the SSL protocol and connect to a merchant site, M . The site
sends you M s certicate. When the SSL protocol completes, how can you be sure that
the new session key can be known only to M (perhaps an intruder has sent you a copy
of M s certicate)? Can you be sure that you are connected to M ?
Solution:
Only M can decrypt the session key you have created since you encrypted it with M s
Exercises
public key obtained from a valid certicate. Although you cannot be sure that you
are connected to M , you can nd out by sending a message encrypted with the new
session key. If the reply to that message implies that the site decrypted your message,
the site must be M .
26.10 Using your local Internet Browser
a. Describe how you can tell when you are connected to a site that is using the SSL
protocol.
Solution:
The address has the prex https and there is a closed lock in the corner of the
screen.
b. Suppose you are connected to a site that is using the SSL protocol. Describe how
you can determine the name of the CA that supplied the certicate used by that
site.
Solution:
Click on the closed lock.
c. Determine how many bits are in the keys that your browser uses for SSL encryption.
Solution:
Most browsers use 128 bits. That can also be determined by rst clicking on the
closed lock.
26.11 Suppose that you obtained a certicate of your own. Explain how you could use that
certicate to deal with situations in which an intruder might steal your credit card
number.
Solution:
You use a protocol that requires that a merchant accept a credit card only if it comes
with a certicate and that the credit card number is always encrypted with your
private key. Thus only you, and not an intruder, could have sent it. Eectively, you
are signing your credit card number.
26.12 Suppose that an intruder puts a virus on your computer that alters your browser.
Describe two dierent ways that the intruder could then impersonate some server
site S that you might attempt to communicate witheven though you use the SSL
protocoland obtain your credit card number.
Solution:
Substitute the intruders public key for one of the CAs and then forge and use a
certicate for S signed with the intruders public key. Alternately, cause the browser
to encrypt all messages with the intruders public key.
26.13 A merchant using the SSL protocol (without SET) might implement a credit card
transaction as follows: The customer purchases an item, and the merchant asks him to
send his credit card information encrypted using the session key established with the
SSL protocol. When the merchant receives that information, she initiates a separate
transaction with the credit card company to have the purchase approved. When that
transaction commits, the merchant commits the transaction with the customer.
Explain the similarities and dierences of this protocol to SET.
303
304
CHAPTER 26
Solution:
Similarities:
1.
2.
3.
Dierences:
1.
2.
3.
4.
In SET, the merchant does not get to see the credit card number.
In SET, each customer has its own certicate; but In SSL, customer does not have
its own certicate;
The automic commit protocol used by SET is the linear commit protocol; while
SSL does not use an atomic commit protocol.
In the SSL only protocol, the merchant could cheat and specify an arbitrary
charge in its transaction with the credit card company.
26.14 Assume that the merchant in the SET protocol is dishonest. Explain why he cannot
cheat the customer.
Solution:
The merchant cannot cheat the customer because the dual signature prevents him from
changing the details of the order, determining the credit card number, or submitting
the same charges to the credit card company again.
26.15 Explain why a trusted third party is used in the certied delivery protocol.
Solution:
To recover from a situation in which the merchant does not send the key, sends the
wrong key, or sends the wrong goods.
26.16 Describe a restart procedure that the merchants computer can use to deal with crashes
during the SET protocol.
Solution:
When the merchant restarts, if there is no prepare or commit record in its log, it
aborts; if there is a prepare record, but no commit record, it asks the gateway what
happened. If there is a commit record, but no complete record, it sends a commit
record to the customer.
26.17 Explain why MD2 in the SET protocol (Section 26.11) must be a part of the dual
signature.
Solution:
When G wants to verify that m1 was prepared by C it does not have m2. Hence,
without MD2 it cannot compute f (MD1 MD2 ).
26.18 Explain why a forger could not simply submit an arbitrary random number as a token
in the electronic cash protocol.
Solution:
When the bank decrypted it, it would not satisfy r ()
26.19 Assume that, in the anonymous electronic cash protocol, the bank is honest but the
customer and the merchant might not be.
Exercises
a. After receiving the tokens from the bank, the customer later claims that she never
received them. Explain what the bank should then do and why it is correct.
Solution:
The bank sends the customer another copy of the tokens. Even if the customer
is dishonest and did receive the tokens the rst time, she cannot spend the same
tokens twice.
b. After receiving the message containing the purchase order and the tokens from the
customer, the merchant claims never to have received the message. Explain what
the customer should do and why.
Solution:
Resend the message. The merchant cannot deposit the tokens twice.
26.20 Describe the methods used in each of the following protocols to prevent a replay attack:
a. Kerberos authentication
Solution:
Authenticator
b. SET
Solution:
Transaction number
c. Electronic cash
Solution:
The bank keeps a list of tokens that have been deposited and will not deposit the
same token twice.
305
A
An Overview of Transaction
Processing
EXERCISES
A.1
A.2
A-2
APPENDIX A
A deadlock might result since T1 and T2 obtain their locks in the opposite order.
A.3
A.4
A.5
Suppose that the transaction processing system of your university contains a table in
which there is one tuple for each current student.
a. Estimate how much disk storage is required to store this table.
Solution:
10,000 tuples * 100 bytes/tuple = 1 megabyte.
b. Give examples of transactions in the student registration system that have to lock
this entire table if a table locking concurrency control is used.
Solution:
Find all students who are registered in at least one course.
A.6
Give an example of a schedule at the READ COMMITTED isolation level in which a lost
update occurs.
Solution:
Suppose a transaction reads the value of an item, x, and, on the basis of the value
read, writes a new value back to x. A deposit transaction in a banking system has
this pattern of behavior, where x is the balance of the account being credited. At the
READ COMMITTED isolation level, such a deposit transaction releases its read lock
before acquiring a write lock, two deposit transactions on the same account can be
interleaved as illustrated in the following schedule
r1 (x) r2(x) w2(x) commit2 w1(x) commit1
The eect of T2 is lost since the value written by T1 is based on the original value of
x, rather than the value written by T2 . Thus if T1 was attempting to deposit |S5 and
T2 was attempting to deposit |S10, the nal database has only been incremented by
|S5, so T2 s update has been lost.
A.7
Exercises
Solution:
A-3
A-4
APPENDIX A
Solution:
r1(x) w1 (x) r2 (y) w2 (x) commit2
A two-phase locking concurrency control would make T2 wait, but a SNAPSHOT
isolation control would abort T1, since T2 was the rst committer.
b. aborts one of the transactions (because of a deadlock), but a control implementing
SNAPSHOT isolation allows both transactions to commit.
Solution:
r1(x) r2 (y) w2(x) w1 (y)
A.11
A particular read-only transaction reads data entered into the database during the
previous month and uses it to prepare a report. What is the weakest isolation level at
which this transaction can execute? Explain.
Solution:
READ UNCOMMITTED, because no other transaction can change the data.
A.12
A.13
Explain how the commit of a transaction is implemented within the logging system.
Solution:
When a transaction commits, it writes a commit record to the log. If it aborts, it
rolls back its updates and then writes an abort record to the log. Using these records,
the backward scan can record the identity of transactions that completed prior to the
crash, and ignore their update records as they are subsequently encountered. If, during
the backward scan, the rst record relating to T is an update record, T was active
when the crash occurred and must be aborted.
A transaction is committed if and only if its commit record has been successfully
appended to the log.
A.14
A.15
Explain why a cohort in the two-phase commit protocol cannot release locks acquired
by the subtransaction until its uncertain period terminates.
Exercises
Solution:
The cohort must block because the coordinator might decide (or might have already
decided) commit or abort, and the cohort must make the same decision.
A.16
Two distributed transactions execute at the same two sites. Each site uses a strict
two-phase locking concurrency control, and the entire system uses a two-phase commit
protocol. Give a schedule for the execution of these transactions in which the commit
order is dierent at each site but the global schedule is serializable.
Solution:
Site 1: w1(x)
Site 2: w1(a)
Commit1
w2(y)
Commit2
w2(b)
Commit2
Commit1
It takes a long time for the commit message for transaction 1 to get to Site 2
A.17
Give an example of an incorrect schedule that might be produced by an asynchronousupdate replication system.
Solution:
Transaction T1 updates replicas x1 and x2 and replicas y1 and y2 asynchronously. T2
reads x1 after the T1s update is made and y1 before T1s update is made, thus seeing
an inconsistent database.
A.18
A-5
B
Requirements and Specifications
EXERCISES
B.1
2.
3.
4.
Objectives:
a. Use buttons to input numbers and designate operator to perform
calculations on those numbers
b. Display the outcome of the calculations on a screen
Information to be contained in the system:
a. Buttons representing the 10 numerals. a decimal point, the operators, +,
-, ,, /, sqrt, and =.
b. Screen that can display 10 numerals, a decimal point between any two
numerals, and a minus sign
c. On/o button
d. Internal storage to store intermediate results.
Interactions with the System
a. When the system is turned on, the display shows 0.
b. The user can input up to ten numerals with or without a decimal point,
and the resulting number is shown on the screen
c. The user can input an operator
If the operator is sqrt, the result is displayed on the screen
d. The user can input up to ten numerals with or without a decimal point
the the resulting number is shown on the screen
The remainder is similar and is omitted.
Error Situations
a. More than 10 numerals are entered by the user. The system . . .
b. The result of an operation is more than 10 numerals. The system . . .
c. The result of an operation is too small (less than can be expressed with 10
zeros followed by a decimal). The system . . .
d. The user attempts to take the sqrt of a negative number. The system . . .
The remainder is similar and is omitted.
B-2
APPENDIX B
B.2
B.3
Suppose that the database in the Student Registration System satises all the integrity
constraints given in Section IV of the Requirements Document Outline (Section B.2).
Is the database necessarily correct? Explain.
Solution:
No. For example a student might have registered for a course and the database was
not updated at all! The database is consistent because it was consistent before the
registration transaction started, and it was not changed at all during the registration
transaction but the database is not correct, because it does not contain any
information about the student that just registered.
B.4
The Requirements Document for the Student Registration System does not address
security issues. Prepare a section on security issues, which might be included in a more
realistic Requirements Document.
Solution:
Some security issues assuming passwords are used:
1.
2.
3.
4.
5.
6.
B.5
In the resolution of issue 2 in Section B.3, the statement was made that new
prerequisites do not apply to courses oered in the next semester. How can a
Registration Transaction know whether or not a prerequisite is new?
Solution:
A date-added attribute can be added to each prerequisite.
Exercises
B.6
B.7
B.8
B.9
Specify a use case for the Student Registration System that assigns a room to a course
to be taught next semester.
Solution:
Assign Room.
Purpose. Assign a room for a course to be taught next semester.
Actor . A professor (actually the Director of the program oering the course).
Input. A course number and a room number.
Result. The room is assigned to the course, and an appropriate message is displayed.
If a room had previously been assigned to the course, the new assignment shall replace
the old assignment.
Exception. If the capacity of the room is less than the maximum allowed enrollment
for the course, the room assignment shall not take place and an appropriate message
shall be displayed.
B-3
C
Design, Coding, and Testing
EXERCISES
C.1
C.2
Explain why
a. Black box testing cannot usually test all aspects of the specications.
Solution:
There are too many things to test. For example, if the specication involves integers,
we cannot test for all possible integers
b. Glass box testing cannot usually test all execution paths through the code. (This
does not mean that glass box testing cannot visit all lines and visit all branches of
the code.)
Solution:
There are too many paths. For example, if there are N two-way branches, there are
2N paths.
C.3
Explain why concurrent systems (such as operating systems) are dicult to test.
Explain why transaction processing applications, even though they are concurrent, do
not have these same diculties.
Solution:
There are an astronomical number of interleavings, which are hard to replicate, so if
the system fails it is dicult to reproduce the situation that caused the failure.
The TP Monitor ensures that all schedules are atomic, isolated, and durable, and
hence these properties do not have to be tested.
C.4
C-2
APPENDIX C
small part of the project is to be built. The project manager can more eciently
deal with the changes the managers will suggest. It is easier to test the system in
incremental steps. Risk is minimized.
c. The implementation team
Solution:
The implementation team can more easily prepare the required documentation for
the project in incremental steps. Design, coding, and testing are all easier.
C.5
In the design of the registration transaction given in Section C.7.2, some of the required
checks are performed in the schema and some in the transaction program.
a. Which of the checks performed in the program can be performed in the schema?
Solution:
All of the checks can be performed in the schema.
1. The course is oered the following semester.
2. The student is not already registered for the course, is not currently enrolled in
the course, and has not completed the course with a grade of C or better.
3.The student is not already registered for another course scheduled at the same time.
4. The total number of credits taken by the student the following semester will not
exceed 20.
5. The student has completed all of the prerequisites for the course with a grade of
C or better (or is currently enrolled in some prerequisites).
b. For each such check, change the schema to perform that check.
C.6
C.7
Evaluate the coding style used in the program for the registration transaction in
Section C.7.3.
C.8
Prepare a test plan for the registration transaction given in Section C.7.2.
C.9