Database Systems
Database Systems
2 Chapter
Section 3
Paolo Atzeni
Stefano Ceri
Stefano Paraboschi
Riccardo Torlone
Database
Systems
4 Chapter
Published by
McGraw-Hill Publishing Company
Shoppenhangers Road, Maidenhead, Berkshire, sl6 2ql, England
Telephone: +()
Fax: +()
Web site: http://www.mcgraw-hill.co.uk
isbn
Publishing Director:
Publisher:
Page design:
Typesetting:
Production:
Cover:
Alfred Waller
David Hatter
Mike Cotterell
Mouse Nous
Steven Gardiner Ltd
Hybert Design
Section 5
To our students
6 Chapter
Contents
.
.
.
.
.
.
Introduction
Information and data
Databases and database management systems
Data models
.. Schemas and instances
.. Abstraction levels in dbmss
.. Data independence
Languages and users
.. Database languages
.. Users and designers
Advantages and disadvantages of dbmss
Bibliography
13
.
.
.
viii Contents
.
.
.
sql
Data definition in sql
.. Elementary domains
.. Schema definition
.. Table definition
.. User defined domains
.. Default domain values
.. Intra-relational constraints
.. Inter-relational constraints
.. Schema updates
.. Relational catalogues
sql queries
.. The declarative nature of sql
.. Simple queries
.. Aggregate queries
.. Group by queries
.. Set queries
.. Nested queries
Data modification in sql
.. Insertions
.. Deletions
.. Updates
Other definitions of data in sql
.. Generic integrity constraints
.. Assertions
.. Views
Contents ix
.
.
.
.
.
.. Views in queries
Access control
.. Resources and privileges
.. Commands for granting and revoking privileges
Use of sql in programming languages
.. Integration problems
.. Cursors
.. Dynamic sql
.. Procedures
Summarizing examples
Bibliography
Exercises
155
.
.
.
.
.
.
Conceptual design
Requirements collection and analysis
General criteria for data representation
Design strategies
.. Top-down strategy
.. Bottom-up strategy
.. Inside-out strategy
.. Mixed strategy
Quality of a conceptual schema
A comprehensive method for conceptual design
An example of conceptual design
case tools for database design
Bibliography
Exercises
Logical design
Performance analysis on e-r schemas
.
.
.
.
.
.
x Contents
.
.
.
.
.
.
.
.
.
.
281
Contents xi
.
.
.
Distributed architectures
.
Client-server architecture
.
Distributed databases
.. Applications of distributed databases
.. Local independence and co-operation
.. Data fragmentation and allocation
.. Transparency levels
.. Classification of transactions
.
Technology of distributed databases
.. Distributed query optimization
.. Concurrency control
.. Failures in distributed systems
xii Contents
.
.
.
.
.
.
Object databases
.
Object-Oriented databases (oodbmss)
.. Types
.. Classes
.. Methods
.. Generalization hierarchies
.. Persistence
.. Redefinition of methods
.. Refinement of properties and methods
.. The object-oriented database manifesto
.
The odmg standard for object-oriented databases
.. Object Definition Language: odl
.. Object Query Language: oql
.
Object-Relational databases (ordbmss)
.. sql- data model
.. sql- query language
.. The third generation database manifesto
.
Multimedia databases
.. Types of multimedia data
.. Queries on multimedia data
.. Document search
.. Representation of spatial data
.
Technological extensions for object-oriented databases
395
Contents xiii
.
.
Active databases
.
Trigger behaviour in a relational system
.
Definition and use of triggers in Oracle
.. Trigger syntax in Oracle
.. Behaviour of triggers in Oracle
.. Example of execution
.
Definition and use of triggers in
.. Trigger syntax in
.. Behaviour of triggers in
.. Example of execution
.
Advanced features of active rules
.
Properties of active rules
.
Applications of active databases
.. Referential integrity management
.. Business rules
.
Bibliography
.
Exercises
Data analysis
.
Data warehouse architecture
.
Schemas for data warehouses
.. Star schema
.. Star schema for a supermarket chain
.. Snowflake schema
.
Operations for data analysis
.. Query formulation interfaces
.. Drill-down and roll-up
.. Data cube
.
Development of the data warehouse
.. Bitmap and join indexes
.. View materialization
.
Data mining
.. The data mining process
.. Data mining problems
.. Data mining perspectives
.
Bibliography
.
Exercises
xiv Contents
519
Contents xv
Advanced features of db
.. Extension of sql for queries
.. Object-oriented features of db
Bibliography
Index
xvi Contents
0Preface
Databases are essential ingredients of modern computing systems. Although
database concepts, technology and architectures have been developed and
consolidated in the last decades, many aspects are subject to technological
evolution and revolution. Thus, writing a textbook on this classical and yet
continuously evolving field is a great challenge.
Key features
This book provides a new and comprehensive treatment of databases,
dealing with the complete syllabuses for both an introductory course and an
advanced course on databases. It offers a balanced view of concepts,
languages and architectures, with concrete reference to current technology
and to commercial database management systems (dbmss). It originates from
the authors long experience in teaching, both in academia and in industrial
and application settings.
The book is composed of four main parts and a fifth part containing three
appendices and a bibliography. Parts I and II are designed to expose students
to the principles of data management and for teaching them how to master
two main skills: how to query a database (and write software that involves
database access) and how to design its schema structure. These are the
fundamental aspects of designing and manipulating a database that are
required in order to make effective use of database technology.
Parts III and IV are dedicated to advanced concepts, required for mastering
database technology. Part III describes database management system
architectures, using a modern approach based upon the identification of the
important concepts, algorithms, components and standards. Part IV is
devoted to the current trends, focusing on object-oriented databases, active
databases, data warehouses and the interaction between databases and the
World Wide Web.
Appendices cover three popular database systems: Microsoft Access, ibm
db2 and Oracle.
xviii Preface
Preface xix
xx Preface
Preface xxi
Appendices
Three appendices conclude the volume, with descriptions of three popular
dbmss:
Appendix A deals with Microsoft Access, which is currently the most
widespread database management system on pc-based platforms. Access has
a simple yet very powerful interface, not only for programming in sql and
qbe, but also for adding forms, reports and macros in order to develop simple
applications.
Appendix B describes the db2 Universal Database, the latest member of
one of the major families of dbmss produced by ibm. The emphasis of the
presentation is on its interactive tools and its advanced functionality.
Appendix C covers Oracle, a major product in the market of dbmss and
specifically focuses on the procedural extension of sql (called pl/sql), which
is supported by Oracle for writing applications.
Use as a textbook
The book can be covered in a total of approximately lecture hours
(plus hours dedicated to exercises and practical experiences).
Our experience is that Parts I and II can be covered as a complete course in
about taught hours. Such a course requires a significant amount of
additional practical activity, normally consisting of several exercises from
each chapter and a project involving the design, population and use of a
small database. The appendixes provide useful support for the practical
activities.
Parts III and IV can be covered in a second course, or else they can be
integrated in part within an extended first course; in advanced, projectcentred courses, the study of current technology can be accompanied by a
project dedicated to the development of technological components. Part IV,
on current trends, provides material for significant project work, for
example, related to object technology, or to data analysis, or to Web
technology. The advanced course can be associated with further readings or
with a research-oriented seminar series.
An international textbook
Making the book reflect the international nature of the subject has been a
challenge and an opportunity. This book has Italian authors, who have also
xxii Preface
given regular courses in the United States, Canada and Switzerland, was
edited in the United Kingdom and is directed to the worldwide market. We
have purposely used a neutral version of the English language, thus avoiding
country-specific jargon whenever possible. In the examples, our attitude has
been to choose attribute names and values that would be immediately
understandable to everybody. In a few cases, however, we have purposely
followed the rules of different international contexts, without selecting one
in particular. The use of car registration numbers from France, or of tax codes
from Italy, will make the reader aware of the fact that data can have different
syntax and semantics in different contexts and so some comprehension and
adaptation effort may be needed when dealing with data in a truly
worldwide approach. It should also be noted that when dealing with money
values, we have omitted the reference to a specific currency: for example, we
say that a salary is thousand, without saying whether it is dollars (and
which dollars: us, Canadian, Australian, Hong Kong, ), or Euros, or Pounds
Sterling.
Additional support
Supplementary material, including overhead slides, solutions to exercises
and additional exercises can be found at the book Web site:
http://www.mcgraw-hill.co.uk/atzeni/
The Authors
Paolo Atzeni and Riccardo Torlone are professors at Universit di Roma Tre.
Stefano Ceri and Stefano Paraboschi are professors at Politecnico di Milano.
They all teach courses on information systems and database technology and
are active members of the research community. Paolo Atzeni and Stefano Ceri
have many years of experience in teaching database courses, both in
European and in North American universities. They have also presented
many courses for professional database programmers and designers. All the
authors are active researchers, operating on a wide range of areas, including
distributed databases, deductive databases, active databases, databases and
the Web, data warehouses, database design and so on. They are actively
participating in the organization of the main International Conferences and
professional Societies dealing with database technology; in particular, Paolo
Atzeni is the chairman of the edbt Foundation and Stefano Ceri is a member
of the edbt Foundation, vldb Endowment and acm Sigmod Advisory
Committee. Their appointments include being co-chairs of vldb in
Rome.
Preface xxiii
Acknowledgements
The organization and the contents of this book have benefited from our
experiences in teaching the subject in various contexts. All the students
attending those courses, dispersed over many schools and countries
(University of Toronto, Stanford University, Universit dellAquila,
Universit di Roma La Sapienza, Universit di Roma Tre, Politecnico di
Milano, Universit di Modena, Universit della Svizzera Italiana) deserve our
deepest thanks. Many of these students have field-tested rough drafts and
incomplete notes, and have contributed to their development, improvement
and correction. Similarly, we would like to thank people from companies and
government agencies who attended our courses for professionals and helped
us in learning the practical aspects that we have tried to convey in our
textbook.
We would like to thank all the colleagues who have contributed, directly
or indirectly, to the development of this book, through discussions on course
organization or the actual revision of drafts and notes. They include Carlo
Batini, Maristella Agosti, Giorgio Ausiello, Elena Baralis, Giovanni Barone,
Giampio Bracchi, Luca Cabibbo, Ed Chan, Giuseppe Di Battista, Angelo
Foglietta, Piero Fraternali, Maurizio Lenzerini, Gianni Mecca, Alberto
Mendelzon, Paolo Merialdo, Barbara Pernici, Silvio Salza, Fabio Schreiber,
Giuseppe Sindoni, Elena Tabet, Letizia Tanca, Ernest Teniente, Carson Woo
and probably some others whom we might have omitted. We thank the
reviewers of the English edition for a number of very useful suggestions
concerning the organization of the book and the specific content of chapters.
We thank the very many people who have contributed to the birth of this
book inside McGraw-Hill. We are grateful to Gigi Mariani and Alberto
Kratter Thaler who have worked with us to the Italian edition of this work.
We are deeply indebted to David Hatter, who endorsed our project
enthusiastically and was able to put together an effective team, together with
Ellen Johnson and Mike Cotterell. These three people have dedicated an
enormous amount of effort to the production process. In particular, we thank
Ellen for her support in the translation, David for his careful copy-editing
and for the many terminological suggestions and Mike for his professional
and patient processing of our manuscript through its numerous revisions.
We would also like to thank our families, for the continuous support they
have given to us and for their patience during the evenings, nights and
holidays spent on this book. Specifically, Paolo Atzeni would like to thank
Gianna, his wife and his children Francesco and Laura; Stefano Ceri wishes
to thank Maria Teresa, his wife and his children Paolo and Gabriele; Stefano
Paraboschi wishes to thank Paola, his wife; Riccardo Torlone wishes to thank
Maria Paola.
xxiv Preface
1Introduction
Collection, organization and storage of data are major tasks in many human
activities and in every computer-based system. Bank balances, telephone
numbers in directories, stock quotations, availability of credit card funds,
registers of students enrolled in a university and grades in their exams are all
examples of types of data that are fundamental to the activities to which they
refer. Computer-based systems ensure that this data is permanently
maintained, quickly updated to show changes, and made accessible to the
queries of users who can be remote from one another and from the computer
where the data is kept. Imagine, for example, queries about the availability
of funds on credit cards, asked through simple devices available in millions
of businesses (whether hotels, shops or companies), which allow purchases
made anywhere in the world to be charged to the credit card owners.
This book is about the management of data through the use of
computerized information systems; it describes the concepts necessary to
represent information on a computer, the languages for update and retrieval,
and the specialized architectures for data management. In this first chapter,
the concepts of information systems and databases are introduced, so that we
can then deal with the major characteristics of systems for managing
databases.
2 Chapter 1
Introduction
computers; for example, bank records and electoral rolls have been in place
for several centuries. In general, only a subset of the information system of
an organization is computerized. The capillary spread of computing
technology into almost all human activity, characteristic of the eighties and
of the nineties, generates a continuous growth in computerization of
information systems.
In the simplest of human activities, information is recorded and exchanged
according to the natural techniques typical of the activity itself: written or
spoken language, drawings, diagrams, numbers. In some activities, an
explicit record might not even exist, the information being more or less
accurately memorized. In any case, we can say that, as activities have
gradually become systematized, appropriate forms of organization and
codification have been found.
In computer-based systems the concept of recording and codifying is
taken to the limit: the information is recorded by means of data, which needs
to be interpreted in order to provide information. As with many basic
concepts, it is difficult to give a precise definition of the concept of data and
of the differences between the concepts of data and information: roughly we
can say that data alone has no significance, but once interpreted and suitably
correlated, it provides information that allows us to improve our knowledge
of the world.
For example, the string John Smith and the number 25775, written on a
sheet of paper, are two pieces of data that are meaningless by themselves. If
the sheet is sent in response to the question Who is the head of the research
department and what is his telephone number?, then it is possible to
interpret the data and use it to enrich our knowledge with the information
that the person named John Smith is the head of the research department and
that his telephone number is 25775.
Having introduced the concept of data in this way, we can move on to that
of the database, the main topic of this text. Various definitions are possible;
the most general being: a database is a collection of data, used to represent
information of interest to an information system. In this book, we will
mainly consider a far more restricted definition, related to a specific
technology, discussed in the next section.
Let us conclude this section with an observation. In many applications,
data is intrinsically much more stable than the (manual or automatic)
procedures that operate upon it. Returning to an example mentioned earlier,
we can observe that the data relating to bank applications has a structure
that has remained virtually unchanged for decades, while the procedures
that act upon it vary continually, as every customer can readily verify.
Furthermore, when one procedure is substituted for another, the new
procedure inherits the data of the old one, with appropriate changes. This
characteristic of stability leads us to state that data constitutes a resource
for the organization that manages it, a significant heritage to exploit and
protect.
Section 1.2 3
Databases and database management systems
4 Chapter 1
Introduction
identical; and vice versa, if every piece of data is stored only once, then
the problem of inconsistency is eliminated. In order to guarantee shared
access to data by many users operating simultaneously, the dbms makes
use of a special mechanism called concurrency control.
Databases are persistent; that is, they have a lifespan that is not limited to
single executions of the programs that use them. Conversely, note that the
data managed by a program in main memory has a life that begins and
ends with the execution of the program; therefore, such data is not
persistent.
dbmss ensure reliability; that is, the capacity of the system to preserve
the content of the database (or at least to allow its reconstruction) in case
of hardware or software failure. To fulfil this requirement, dbmss provide
specific functions for backup and recovery.
dbmss ensure data privacy. Each user, who is recognized by a user name
that is specific to that users access to the dbms, is qualified to carry out
only certain operations on the data, through the mechanisms of
authorization.
dbmss are also concerned with efficiency, that is, the capacity to carry out
operations using an appropriate amount of resources (time and space) for
each user. This characteristic relies on the techniques used in the
implementation of the dbms, and on how well the product has been
designed. It should be stressed that dbmss provide a wide-ranging
combination of features that require many resources, and therefore they
often put heavy requirements on the resources provided by the operating
environment.
Finally dbmss increase effectiveness, that is, the capacity of the database
system to make the activities of its users productive, in every sense. This
is clearly a generic definition and does not correspond to a specific
function, given that a dbms provides various services and functions to
different users. The task of designing a database and the applications that
use it aims essentially to guarantee the good, overall effectiveness of the
system.
It is important to stress that the management of large and persistent
collections of data is also possible by means of instruments less sophisticated
than dbmss, beginning with the files present in all operating systems. Files
were introduced to manage data local to a specific procedure or application.
dbmss were conceived and created to extend the functions of the file system,
permitting shared access to the same data by more than one user and
application, and also guaranteeing many other services in an integrated
manner. Clearly, dbmss in their turn use files for the storage of data;
however, as we shall discuss in Part III of the book, files managed by dbmss
allow the data organization to be of a higher level of complexity.
Section 1.3 5
Data models
TEACHING
Course
Databases
Networks
Languages
Tutor
Smith
Jones
Robinson
PROSPECTUS
DegreeProgramme
Information Systems
Information Systems
Information Systems
Electrical Engineering
Electrical Engineering
Subject
Year
Databases
4
Networks
4
Languages
3
Databases
4
Networks
4
6 Chapter 1
Introduction
The data models listed above are all available in commercial dbmss; they
are called logical, to underline the fact that the structures used for these
models, although abstract, reflect a particular organization (tree, graph,
table, or object). Other data models known as conceptual models, have been
introduced to describe data in a manner independent of the logical model;
but these are not available in commercial dbmss. Their name comes from the
fact that they tend to describe concepts of the real world, rather than the data
needed for their representation. These models are used in the preliminary
phase of the database design process, to analyze the application in the best
possible way, without implementational contaminations. In Part II of this
book, dedicated to the design of databases, we examine in detail a conceptual
model, the Entity-Relationship model.
The rows of the table vary with time, and correspond to the courses
currently offered and their respective tutors. During the life of the database,
tutors and courses are added, removed or modified; similarly, the prospectus
is modified from one year to the next. The instance of a relation is made up of
a collection of rows, which vary with time; in the example we have three
pairs:
Databases Smith
Networks Jones
Languages Robinson
We could also say that the schema is the intensional component of the
database and the instance is the extensional component. These definitions
will be developed and discussed in detail in Chapter 2.
Section 1.3 7
Data models
8 Chapter 1
Introduction
Section 1.4 9
Languages and users
give an idea of only a few of their aspects in the appendices that are
devoted to specific systems.
Through friendly interfaces that allow the formulation of queries without
the use of a textual language. These too differ greatly from one system to
another and are continually evolving. Again, we touch upon a few simple
and important aspects in the appendices.
A large number of programs for data entry, management and printing have
a common structure; consequently, the existence of development languages
and friendly interfaces considerably simplifies the production of
applications, reducing development time and costs.
End
users, who use transactions, that is, programs that carry out
frequent and predefined activities, with few exceptions known and
taken into account in advance.3
Casual users, able to use the interactive languages to gain access to the
3. In database usage, the term transaction also has a more specific meaning, which
will be discussed in Part III of the book.
10 Chapter 1
Introduction
1.6 Bibliography
There are many other books and additional readings on the general aspects
of database systems. The more similar in goals to this book, with balanced
treatment of methodological and technical aspects, include ElMasri and
Section 1.6 11
Bibliography
12 Chapter 1
Introduction
Part I
IRelational databases
16 Chapter 2
The relational model
contexts other than in databases. On the other hand, the availability of a clear
and simple formalization has allowed the development of a theory to support
the model, with very interesting results.
The relational model satisfies the requirement of data independence, as
discussed in Chapter : users and programmers make reference only to
relations (logical or external level), which are then implemented by means of
appropriate physical structures; however, to gain access to the data it is not
necessary to know about the physical structures themselves. The relational
model was proposed at the end of the sixties in order to provide a higher
level of data independence than the network and hierarchical models. These
included reference to the underlying structure, by means of the use of
pointers and the physical ordering of data.
Section 2.1 17
The structure of the relational model
1
1
2
2
4
4
a
b
a
b
a
b
1
1
4
a
b
b
x
x
x
x
x
x
y
y
y
y
y
y
a
a
b
b
c
c
a
a
b
b
c
c
3
5
3
5
3
5
3
5
3
5
3
5
(b)
x
x
x
y
y
y
a
a
c
a
c
c
3
5
5
3
3
5
Liverpool
Milan
Roma
Milan
3
2
1
0
1
0
2
1
18 Chapter 2
The relational model
Section 2.1 19
The structure of the relational model
VisitingTeam
Liverpool
Milan
Roma
Milan
20 Chapter 2
The relational model
Let us introduce a useful notation that we will use frequently in the future.
If t is a tuple on X and A X , then t [ A ] (or t.A) indicates the value of t on
A. For example, if t is the first tuple of the relation in Figure ., we can say
that
t[VisitingTeam] = Liverpool
The same notation is also used for sets of attributes, in which case it
denotes a tuple:
t[VisitingTeam, VisitorGoals]
is a tuple on two attributes.1
Surname
Smith
Black
Verdi
Smith
Brown
FirstName
Mary
Anna
Paolo
Lucy
Mavis
BirthDate
25/11/1980
23/04/1981
12/02/1981
10/10/1980
01/12/1980
Title
Physics
Chemistry
Chemistry
Tutor
Grant
Beale
Clark
Section 2.1 21
The structure of the relational model
STUDENTS RegNum
276545
485745
200768
587614
937653
Surname
Smith
Black
Verdi
Smith
Brown
FirstName
Mary
Anna
Paolo
Lucy
Mavis
B
COURSES Code
01
03
04
Title
Physics
Chemistry
Chemistry
Tutor
Grant
Beale
Clark
BirthDate
25/11/1980
23/04/1981
12/02/1981
10/10/1980
01/12/1980
22 Chapter 2
The relational model
Section 2.1 23
The structure of the relational model
Surname
Smith
Black
Verdi
Smith
Brown
FirstName
Mary
Anna
Paolo
Lucy
Mavis
BirthDate
25/11/1980
23/04/1981
12/02/1981
10/10/1980
01/12/1980
WORKERS
RegNum
276545
485745
937653
24 Chapter 2
The relational model
Da Mario
Receipt No:
Date: //
covers
.
hors doeuvre .
first course
.
steak
.
Total: .
Da Mario
2
2
2
2
2
Receipt No:
Date: //
covers
.
hors doeuvre .
first course
.
bream
.
coffee
.
Da Mario
Total: .
Receipt No:
Date: //
covers
.
hors doeuvre .
first course
.
bream
.
salad
.
coffee
.
Total: .
RECEIPTS
Number Date Total
1357
5/5/92 29.00
2334
4/7/92 27.50
3007
4/8/92 29.50
DETAILS
Number Quantity Description
1357
3
covers
1357
2
hors doeuvre
1357
3
first course
1357
2
steak
2334
2
covers
2334
2
hors doeuvre
2334
2
first course
2334
2
bream
2334
2
coffee
3007
2
covers
3007
2
hors doeuvre
3007
3
first course
3007
1
bream
3007
1
salad
3007
2
coffee
Cost
3.00
5.00
9.00
12.00
2.00
2.50
6.00
15.00
2.00
3.00
6.00
8.00
7.50
3.00
2.00
Section 2.1 25
The structure of the relational model
once in each receipt (number, date and total) and the relation DETAILS
contains the various lines of each receipt (with quantity, description and
overall cost), associated with the same receipt, by means of the appropriate
receipt number.
We should point out here that for the database in Figure . to represent
the receipts correctly, two conditions must be satisfied:
it is not necessary to keep track of the order in which the lines appear on
each receipt; in fact, since no order is defined among the tuples of a
relation, the tuples in DETAILS are not in any fixed order;
no duplicate lines appear on any receipt (which could happen in the case
of different orders for the same dishes with the same quantity).
Both problems can be resolved by adding an attribute, which indicates the
position of the line of the receipt (see Figure .); in this way it is always
RECEIPTS
Number Date Total
1357
5/5/92 29.00
2334
4/7/92 27.50
3007
4/8/92 29.50
DETAILS
Number Line Quantity Description
Cost
1357
1
3
covers
3.00
1357
2
2
hors doeuvre 5.00
1357
3
3
first course
9.00
1357
4
2
steak
12.00
2334
1
2
covers
2.00
2334
2
2
hors doeuvre 2.50
2334
3
2
first course
6.00
2334
4
2
bream
15.00
2334
5
2
coffee
2.00
3007
1
2
covers
3.00
3007
2
2
hors doeuvre 6.00
3007
3
3
first course
8.00
3007
4
1
bream
7.50
3007
5
1
salad
3.00
3007
6
2
coffee
2.00
26 Chapter 2
The relational model
The value of the attribute Telephone might not be available for all the
tuples. It is worth noting that it would not be correct to use a value of the
domain to represent an absence of information, as this would generate
confusion. In this case, supposing the telephone numbers to be represented
by integers, we could, for example, use zero to indicate the absence of the
significant value. In general, however, this choice is unsatisfactory, for two
reasons.
In the first place, it requires the existence of a value of the domain never
used for significant values: in the case of telephone numbers, zero is clearly
distinguishable, but in other cases there exists no available value for this
purpose; for example, in an attribute that represents the date of birth and
that uses as a domain a type Date correctly defined, there are no unused
elements that would therefore be usable to denote absence of information.
In the second place, the use of domain values can generate confusion: the
distinction between actually significant values and fictitious ones (placeholders) is hidden, and therefore the programs that have access to the
database must take this into account, distinguishing among them (and
keeping track of which are the fictitious values in each case).
In order to represent simply, but at the same time conveniently, the nonavailability of values, the concept of relation is usually extended to include
the possibility that a tuple can assume, on each attribute, either a value of
the domain, as seen up to now, or a special value, called a null value. The null
value denotes an absence of information, and is an additional value, not in
the domain. In the tabular representations we will use for the null values the
symbol NULL, as in Figure ., which deals with the addresses of
City
GovernmentAddress
Rome
Via Quattro Novembre
Florence NULL
Tivoli
NULL
Prato
NULL
Section 2.1 27
The structure of the relational model
government offices in county towns in Italy. Note that all county towns have
local government offices, but other towns do not. With reference to the table
in the figure, we can note how in effect the three null values that appear in it
are assigned for different reasons, as follows.
Florence is a county town and as such must certainly have a local
government office. At the moment we do not know its address. The null
value is used because the real value cannot be recorded in the database:
for this reason we say that it is an unknown value.
Tivoli is not a county town and therefore has no local government office.
Thus the attribute GovernmentAddress can have no value for this tuple.
Here the null value denotes the inapplicability of the attribute, or in other
words, the non-existence of the value: the value is non-existent.
The county of Prato has been established very recently and we do not
know if the local government office has yet been opened, nor do we know
its address (whether already operational or planned). In effect, we do not
know if the value exists and, if it exists, we do not know what it is. In
fact, we find ourselves in a situation that corresponds to the logical
disjunction (the or) of the two preceding: the value is either nonexistent or unknown. This type of null value is usually called noinformation, because it tells us absolutely nothing: the value might or
might not exist, and if it exists we dont know what it is.
In relational database systems no hypothesis is made about the meaning of
null values; therefore, in practice, we find ourselves in the third situation,
that of the no-information value.
For a further reflection on null values, consider now the database in
Figure ., which is defined on the same database schema as Figure .. The
null value on the date of birth in the first tuple of the relation STUDENTS is
more or less admissible, in that one can accept that the information is not
essential in this context. However, a null value for the registration number
or the course code creates serious problems, since these values, as we have
discussed with reference to Figure ., are used to establish correspondences
between tuples of different relations. At this point, the presence of null
values in the relation EXAMS actually makes the information unusable: for
example, the second tuple, with just the grade and two null values, provides
no useful information. Thus, the presence of null values in a relation can in
some cases generate doubts as to the actual significance and identity of the
tuples: the last two tuples of the relation COURSES can be different or can
actually coincide! Hence the necessity for keeping a suitable control over the
use of null values in our relations is evident: only certain relation instances
should be admitted. In general, when a relation is defined, it is possible to
specify that null values are admissible only for some attributes and not for
others. At the end of the next section we will present a criterion for the
selection of attributes from which null values must be excluded.
28 Chapter 2
The relational model
STUDENTS
RegNum Surname FirstName BirthDate
276545 Smith
Mary
NULL
NULL
Black
Anna
23/04/1972
NULL
Verdi
Paolo
12/02/1972
EXAMS
Student Grade Course
276545
C
01
NULL
B
NULL
200768
A
NULL
COURSES
Code Title
Tutor
01 Physics
Grant
03 Chemistry NULL
NULL Chemistry Clark
Surname
Verdi
Smith
Brown
FirstName BirthDate
Paolo
12/02/1981
Lucy
10/10/1980
Mavis
01/12/1980
COURSES
Code Title
01 Physics
03 Chemistry
04 Chemistry
Tutor
Grant
Beale
Clark
Section 2.2 29
Integrity constraints
As
30 Chapter 2
The relational model
it is possible to define the constraint that imposes the condition that the net
amount is equal to the difference between the total amount and the
deductions, in the following manner:
Net = Amount Deductions
2.2.2 Keys
In this section we will discuss key constraints, which are undoubtedly the
most important of the relational model; we could even go so far as to say that
without them the model itself would have no sense. Let us begin with an
example. In the relation in Figure ., the values of the various tuples on
the attribute RegNum are all different from each other: the value of the
RegNum unambiguously identifies the students; the very idea of the
registration number itself was introduced many years ago, well before the
introduction of databases, precisely in order to have a simple and effective
tool for referring to the students in an unambiguous way. Similarly, in the
relation there are no pairs of tuples with the same values on any of the three
attributes Surname, FirstName and BirthDate: these pieces of information also
identify each person unambiguously.2 Other sets of attributes also identify
unambiguously the tuples of the relation in Figure .: for example, the pair
RegNum and DegreeProg, given that RegNum is sufficient on its own.
Section 2.2 31
Integrity constraints
RegNum
284328
296328
587614
934856
965536
Surname
Smith
Smith
Smith
Black
Black
32 Chapter 2
The relational model
general? Certainly not, given that there could easily be students with the
same surname enrolled on the same degree programme.
RegNum
296328
587614
934856
965536
Surname
Smith
Smith
Black
Black
are associated the constraints that impose as keys the two sets of attributes
discussed above:
RegNum
Surname, FirstName, BirthDate
Section 2.2 33
Integrity constraints
34 Chapter 2
The relational model
RegNum
643976
587614
934856
735591
Surname
Smith
Smith
Black
Black
Section 2.2 35
Integrity constraints
OFFENCES
Code
143256
987554
987557
630876
539856
Date
Officer
25/10/92
567
26/10/92
456
26/10/92
456
15/10/92
456
12/10/92
567
OFFICERS RegNum
567
456
638
CARS Registration
6544 XY
7122 HT
5694 FR
6544 XY
Department
75
75
75
47
Department
75
75
75
47
47
Surname
Brun
Larue
Larue
Registration
5694 FR
5694 FR
6544 XY
6544 XY
6544 XY
FirstName
Jean
Henri
Jacques
Owner
Cordon Edouard
Cordon Edouard
Latour Hortense
Mimault Bernard
Address
Rue du Pont
Rue du Pont
Avenue Foch
Avenue FDR
36 Chapter 2
The relational model
secondly, that CARS contains no tuple with the value 75 for Department and
6544 XY for Registration (note that there is a tuple with the value 75 for
Department and another with the value 6544 XY for Registration, but this is
not sufficient, because there is a need for a tuple with both values: only in
this way can the two values make reference to a tuple of the relation CARS).
OFFENCES
Code Date
Officer Department Registration
987554 26/10/92
456
75
6544 XY
630876 15/10/92
456
47
6544 XY
OFFICERS RegNum Surname FirstName
567
Brun
Mavis
638
Larue
Jacques
CARS
Registration
7122 HT
5694 FR
6544 XY
Department
75
93
47
Owner
Cordon Edouard
Latour Hortense
Mimault Bernard
Address
Rue du Pont
Avenue Foch
Avenue FDR
4. Let us suppose for simplicitys sake that there are always only two vehicles.
Section 2.2 37
Integrity constraints
because in the relation CARS there is no vehicle with the registration 9775 GF
and department 93.
6974
93
5694 FR
93
9775 GF
Address
Rue du Pont
Avenue Foch
Rue de la Gare
Avenue FDR
6974
93
5694 FR
93
9775 GF
CARS
Registration
7122 HT
5694 FR
6544 XY
Department
75
93
75
Owner
Cordon Edouard
Latour Hortense
Mimault Bernard
Address
Rue du Pont
Avenue Foch
Avenue FDR
38 Chapter 2
The relational model
2.3 Conclusions
In this chapter we have defined the structures and constraints of the
relational model. First we discussed the concept of relation, with some
variations with respect to the concepts of set theory. Then, we showed how
relations can be used to organize more complex collections of data using the
data itself to create references between different components (without the
use of explicit pointers). Then, after introducing the necessity for using null
values to denote the absence of information, we discussed the concept of
integrity constraints through three fundamental classes: tuple constraints,
keys and referential constraints.
In the next two chapters, we complete the presentation of the relational
model from two points of view:
in Chapter , we illustrate the foundations of query languages, that is, the
languages used to access information in databases;
in Chapter , we show how all the concepts, those relative to the
structures and to the constraints, as discussed in this chapter, and those
relative to query languages (Chapter ) are implemented in commercial
dbmss, using sql.
2.4 Bibliography
It is worth consulting the original article by Codd [] that contains the
original proposal for the relational model: its motivations and the general
presentation of the model are still valid. For this work, Codd received the
acm Turing Award, the most important recognition in the computing field;
the discussion he developed in such an occasion is also very interesting [].
Tsichritzis and Lochovsky [] offer general and comparative discussions
on data models.
More formal and detailed treatments on the relational model and its
associated theory (which is not developed much in this book) are offered by
Maier [], Ullman [], Paredaens et al. [], Atzeni and De Antonellis [],
Abiteboul, Hull and Vianu []. Interesting discussions on null values, with
the various approaches, were developed by Codd [] and Zaniolo [].
Section 2.5 39
Exercises
2.5 Exercises
Exercise 2.1 Describe in words, the information organized in the database
in Figure ..
PATIENT
Code
A102
B372
B543
B444
S555
Surname
Harris
Rossini
Johnson
Johnson
Rose
DOCTOR
Number
203
574
461
530
405
501
FirstName
Lucy
Peter
Nadia
Luigi
Jean
Surname
Black
Bisi
Boyne
Clark
Mizzi
Mount
ADMISSION
Patient Admitted
A102 2/05/94
A102 2/12/94
S555 5/10/94
B444 1/12/94
S555 5/10/94
FirstName Ward
Peter
A
Mavis
B
Steve
B
Nicola
C
Nicola
A
Mavis
A
Discharged Ward
9/05/94
A
2/01/95
A
3/12/94
B
1/01/95
B
1/11/94
A
WARD
Code
Name
Consultant
A
Surgical
203
B
Paediatric
574
C
Medical
530
40 Chapter 2
The relational model
first name and date of birth), and subsidiaries (each with code, branch and
director, who is an employee). Each employee works for a subsidiary.
Indicate the keys and the referential constraints of the schema. Show an
instance of the database and check that it satisfies the constraints.
Exercise 2.6 A family tree represents the structure of a family. Show how
the information of a family tree can be represented by means of a relational
database, possibly starting with a simplified structure, in which only the
male line or only the female line is represented (that is, only the offspring of
the male or the female members of the family are represented).
Exercise 2.7 For each of the Exercises .., evaluate the needs for null
values, with the related benefits and difficulties.
Exercise 2.8 Define a database schema that organizes the information
necessary to generate the radio programmes page of a daily newspaper, with
stations, times and programme titles; besides the name, include the
transmission frequency and the location of the radio station.
3Relational algebra
and calculus
We have seen in the previous chapters that information of interest to data
management applications can be represented by means of relations. The
languages for specifying operations for querying and updating the data itself
constitute, in their turn, an essential component of each data model. An
update can be seen as a function that, given a database, produces another
database (without changing the schema). A query, on the other hand, can
also be considered as a function that, given a database, produces a relation.
So, in order either to interrogate or to update the database, we need to
develop the ability to express functions on the database. It is important to
learn the foundations of query and update languages first, and then apply
those foundations when studying the languages that are actually supported
by commercial dbmss.
We will look first at relational algebra. This is a procedural language (that
is, one in which the data retrieval functions are specified by describing the
procedure that must be followed in order to obtain the result). We will
illustrate the various operators of the algebra, the way operators can be
combined to form expressions, and the means by which expressions can be
transformed to improve efficiency. We will also describe the influence that
null values have on the relational algebra, and then how a query language
can be used to define virtual relations (also known as views), which are not
stored in the database.
Then, we will give a concise presentation of relational calculus, a
declarative language, in which the data retrieval functions describe the
properties of the result, rather than the procedure used to obtain it. This
language is based on first order predicate calculus and we will present two
versions, the first directly derived from predicate calculus and the second
that attempts to overcome some of the limitations of the first.
42 Chapter 3
Relational algebra and calculus
Section 3.1 43
Relational algebra
MANAGERS
Number
9297
7432
9824
GRADUATES MANAGERS
Number Surname Age
7274
Robinson 37
7432
OMalley 39
9824
Darkes
38
9297
OMalley 56
GRADUATES MANAGERS
Number Surname Age
7432
OMalley 39
9824
Darkes
38
Surname Age
OMalley 56
OMalley 39
Darkes
38
GRADUATES MANAGERS
Number Surname Age
7274
Robinson 37
3.1.2 Renaming
The limitations we have had to impose on the standard set operators,
although justified, seem particularly restrictive. For instance, consider the
two relations in Figure .. It would be meaningful to execute a sort of union
on them in order to obtain all the parentchild pairs held in the database,
but that is not possible, because the attribute that we have instinctively
called Parent, is in fact called Father in one relation and Mother in the other.
PATERNITY
Father
Adam
Adam
Abraham
Abraham
Child
Cain
Abel
Isaac
Ishmael
MATERNITY
Mother Child
Eve
Cain
Eve
Seth
Sarah
Isaac
Hagar Ishmael
PATERNITY MATERNITY ??
44 Chapter 3
Relational algebra and calculus
QParentFather(PATERNITY)
Child
Cain
Abel
Isaac
Jacob
Parent
Adam
Adam
Abraham
Isaac
Child
Cain
Abel
Isaac
Jacob
Child
Cain
Abel
Isaac
Ishmael
Cain
Seth
Isaac
Ishmael
Section 3.1 45
Relational algebra
unaltered and are associated with new attributes. In practice, in the two lists
A1A2Ak and B1B2Bk we indicate only those attributes that are renamed
(that is, those for which Ai Bi). This is the reason why in Figure . we have
written
QParentFather(PATERNITY)
and not
QParent,ChildFather,Child(PATERNITY)
Figure . shows another example of union preceded by renaming. In this
case, in each relation there are two attributes that are renamed and therefore
the ordering of the pairs (Branch, Salary and so on) is significant.
EMPLOYEES
Surname Branch Salary
Patterson Rome
45
Trumble London
53
STAFF
Surname Factory Wages
Cooke
Chicago
33
Bush
Monza
32
Location Pay
Rome
45
London 53
Chicago 33
Monza
32
3.1.3 Selection
We now turn our attention to the specific operators of relational algebra that
allow the manipulation of relations. There are three operators, selection,
projection and join (the last having several variants).
Before going into detail, note that selection and projection carry out
functions that could be defined as complementary (or orthogonal). They are
both unary (that is, they have one relation as argument) and produce as result
a portion of that relation. More precisely, a selection produces a subset of
tuples on all the attributes, while a projection gives a result to which all the
tuples contribute, but on a subset of attributes. As illustrated in Figure .,
we can say that selection generates horizontal decompositions and
projection generates vertical decompositions.
Figure . and Figure . show two examples of selection, which illustrate
the fundamental characteristics of the operator, denoted by the symbol ,
with the appropriate selection condition indicated as subscript. The result
contains the tuples of the operand that satisfy the condition. As shown in the
examples, the selection conditions can allow both for comparisons between
46 Chapter 3
Relational algebra and calculus
selection
projection
Age<30Salary>4000(EMPLOYEES)
Surname FirstName Age Salary
Smith
Mary
25
2000
Verdi
Nico
36
4500
Section 3.1 47
Relational algebra
CITIZENS
Surname
Smith
Black
Verdi
Smith
FirstName
Mary
Lucy
Nico
Mark
PlaceOfBirth
Rome
Rome
Florence
Naples
Residence
Milan
Rome
Florence
Florence
PlaceOfBirth=Residence(CITIZENS)
Surname FirstName PlaceOfBirth Residence
Black
Lucy
Rome
Rome
Verdi
Nico
Florence
Florence
3.1.4 Projection
The definition of the projection operator is also simple: given a relation r(X)
and a subset Y of X, the projection of r on Y (indicated by Y(r)) is the set of
tuples on Y obtained from the tuples of r considering only the values on Y:
Y ( r ) = { t [ Y ] t r }
Figure . shows a first example of projection, which clearly illustrates the
concept mentioned above. The projection allows the vertical decomposition
of relations: the result of the projection contains in this case as many tuples
as its operand, defined however only on some of the attributes.
Surname,FirstName(EMPLOYEES)
EMPLOYEESMAKESPACE
Surname
Smith
Black
Verdi
Smith
FirstName
Mary
Lucy
Mary
Mark
Department
Sales
Sales
Personnel
Personnel
Head
De Rossi
De Rossi
Fox
Fox
Surname
Smith
Black
Verdi
Smith
FirstName
Mary
Lucy
Mary
Mark
48 Chapter 3
Relational algebra and calculus
FirstName
Mary
Lucy
Mary
Mark
Department
Sales
Sales
Personnel
Personnel
Head
De Rossi
De Rossi
Fox
Fox
Department,Head(EMPLOYEES)
Department Head
Sales
De Rossi
Personnel
Fox
we can say that for all the relations, the projection on RegNum and that on
Surname, FirstName and BirthDate have the same number of tuples as the
operand. Conversely, a projection on Surname and DegreeProg can have fewer
Section 3.1 49
Relational algebra
3.1.5 Join
Let us now examine the join operator, which is the most important one in
relational algebra. The join allows us to establish connections among data
contained in different relations, comparing the values contained in them and
thus using the fundamental characteristics of the model, that of being valuebased. There are two main versions of the operator, which are, however,
obtainable one from the other. The first is useful for an introduction and the
second is perhaps more relevant from a practical point of view.
Natural join The natural join, denoted by the symbol J, is an operator
that correlates data in different relations, on the basis of equal values of
attributes with the same name. (The join is defined here with two operands,
but can be generalized.) Figure . shows an example. The result of the join
is a relation on the union of the sets of attributes of the operands: in the
figure, the result is defined on Employee, Department, Head, that is, on the
union of Employee, Department and Department, Head. The tuples in the join
are obtained by combining the tuples of the operands with equal values on
the common attributes, in the example the attribute Department: for
instance, the first tuple of the join is derived from the combination of the first
tuple of the relation r1 and the second tuple of r2: in fact they both have sales
as the value for Department.
r1 Employee Department
Smith
Black
Bianchi
r2 Department Head
sales
production
production
production
sales
Mori
Brown
sales
production
production
Brown
Mori
Mori
50 Chapter 3
Relational algebra and calculus
Code
143256
987554
987557
630876
539856
CARS Registration
6544 XY
7122 HT
5694 FR
6544 XY
Date
Officer
25/10/92
567
26/10/92
456
26/10/92
456
15/10/92
456
12/10/92
567
Department
75
75
75
47
Department
75
75
75
47
47
Registration
5694 FR
5694 FR
6544 XY
6544 XY
6544 XY
Owner
Cordon Edouard
Cordon Edouard
Latour Hortense
Mimault Bernard
Address
Rue du Pont
Rue du Pont
Avenue Foch
Avenue FDR
OFFENCES J CARS
Code
Date
143256
987554
987557
630876
539856
25/10/92
26/10/92
26/10/92
15/10/92
12/10/92
Officer Department
567
75
456
75
456
75
456
47
567
47
Regist
-ration
5694 FR
5694 FR
6544 XY
6544 XY
6544 XY
Owner
Address
Latour Hortense
Latour Hortense
Cordon Edouard
Mimault Bernard
Mimault Bernard
Avenue Foch
Avenue Foch
Rue du Pont
Avenue FDR
Avenue FDR
Figure 3.12 The relations OFFENCES and CARS (from Figure .) and their
join.
Figure . shows another example of join, using the same relations as we
have already used (Figure .) to demonstrate a union preceded by
renamings. Here, the data of the two relations is combined according to the
Section 3.1 51
Relational algebra
value of the child, returning the parents for each person for whom both are
indicated in the database.
PATERNITY
Father
Adam
Adam
Abraham
Abraham
MATERNITY
Mother Child
Eve
Cain
Eve
Seth
Sarah
Isaac
Hagar Ishmael
Child
Cain
Abel
Isaac
Ishmael
PATERNITY J MATERNITY
Father
Child
Mother
Adam
Cain
Eve
Abraham Isaac
Sarah
Abraham Ishmael Hagar
r2 Department Head
sales
production
production
production
purchasing
production
production
Mori
Mori
Mori
Brown
52 Chapter 3
Relational algebra and calculus
There is even the possibility, as an extreme case, that none of the tuples of
the operands can be combined, and this gives rise to an empty result (see the
example in Figure .).
r1 Employee Department
Smith
Black
White
r2 Department Head
sales
production
production
marketing
purchasing
Mori
Brown
r1 Employee Project
Smith
Black
White
r2
A
A
A
Project
A
A
Head
Mori
Brown
A
A
A
A
A
A
Mori
Mori
Mori
Brown
Brown
Brown
Section 3.1 53
Relational algebra
sales
production
production
r2 Department Head
production
purchasing
Mori
Brown
sales
production
production
NULL
Mori
Mori
production
production
purchasing
Mori
Mori
Brown
sales
production
production
purchasing
NULL
Mori
Mori
Brown
54 Chapter 3
Relational algebra and calculus
to outer join, for which some of the properties discussed here do not hold.)
First let us observe that it is commutative, that is, r1Jr2 is always equal to
r2Jr1, and associative, r1J(r2Jr3) is equal to (r1Jr2)Jr3. Thus, we can
write, where necessary, join sequences without brackets:
r1 J r2 J J rn
or
J i =1 r i
Note also that we have stated no specific hypothesis about the sets of
attributes X1 and X2 on which the operands are defined. Therefore, the two
sets could even be equal or be disjoint. Let us examine these extreme cases;
the general definition given above is still meaningful, but certain points
should be noted. If X1 = X2, then the join coincides with the intersection
r1 ( X1 ) J r2 ( X1 ) = r1 ( X1 ) r2 ( X1 )
since, by definition, the result is a relation on the union of the two sets of
attributes, and must contain the tuples t such that t [ X 1 ] r 1 and
t [ X 2 ] r 2. If X1 = X2, the union of X1 and X2 is also equal to X1, and thus t
is defined on X1: the definition thus requires that t r1 and t r2, and
therefore coincides with the definition of intersection.
The case where the two sets of attributes are disjoint requires even more
attention. The result is always defined on the union X1X2, and each tuple is
always derived from two tuples, one for each of the operands. However, since
such tuples have no attributes in common, there is no requirement to be
satisfied in order for them to participate in the join. The condition that the
tuples must have the same values on the common attributes is always
verified. So the result of this join contains the tuples obtained by combining
the tuples of the operands in all possible ways. In this case, we often say that
the join becomes a cartesian product. This could be described as an operator
defined (using the same definition given above for natural join) on relations
that have no attributes in common. The use of the term is slightly misleading,
as it is not really the same as a cartesian product between sets. The cartesian
product of two sets is a set of pairs (with the first element from the first set
and the second from the second). In the case here we have tuples, each
obtained by juxtaposing a tuple of the first relation and a tuple of the second.
Figure . shows an example of the cartesian product, demonstrating how
the result contains a number of tuples equal to the product of the
cardinalities of the operands.
Theta-join and equi-join If we examine Figure ., it is obvious that a
cartesian product is, in general, of very little use, because it combines tuples
in a way that is not necessarily significant. In fact, however, the cartesian
product is often followed by a selection, which preserves only the combined
tuples that satisfy the given requirements. For example, it makes sense to
define a cartesian product on the relations EMPLOYEES and PROJECTS, if it is
followed by the selection that retains only the tuples with equal values on
the attributes Project and Code (see Figure .).
Section 3.1 55
Relational algebra
EMPLOYEES
Employee Project
Smith
A
Black
A
Black
B
EMPLOYEES J PROJECTS
Employee Project Code
Smith
A
A
Black
A
A
Black
B
A
Smith
A
B
Black
A
B
Black
B
B
PROJECTS
Code Name
A
Venus
B
Mars
Name
Venus
Venus
Venus
Mars
Mars
Mars
PROJECTS
Code Name
A
Venus
B
Mars
Project=Code(EMPLOYEES J PROJECTS)
Employee Project Code Name
Smith
A
A
Venus
Black
A
A
Venus
Black
B
B
Mars
56 Chapter 3
Relational algebra and calculus
From the practical point of view, the theta-join and the equi-join are very
important. This is because most current database systems do not take
advantage of attribute names in order to combine relations, and thus use the
equi-join and theta-join rather than the natural join. We examine this
concept more thoroughly when we discuss sql queries in Chapter . In fact
sql queries mainly correspond to equi-joins, while the natural join was made
available only in the most recent versions of sql.
At the same time, we presented the natural join first because it allows the
simple discussion of important issues, which can then be extended to the
equi-join. For example, we refer to natural joins in the discussion of some
issues related to normalization in Chapter .
Note also that the natural join can be simulated using renaming, equi-join
and projection. Without going into too much detail, here is an example.
Given two relations, r1(ABC) and r2(BCD), the natural join of r1 and r2 can
be expressed by means of other operators in three steps:
renaming the attributes so as to obtain relations on disjoint schemas:
Q BC BC ( r 2 )
equi-joining such relations, with equality conditions on the renamed
attributes: r1 JB=BC=C(QBCBC(r2))
concluding with a projection that eliminates all the duplicate attributes
(one for each pair involved in the equi-join):
ABCD(r1JB=BC=C(QBCBC(r2)))
Section 3.1 57
Relational algebra
EMPLOYEES
Number
101
103
104
105
210
231
252
301
375
Name
Mary Smith
Mary Bianchi
Luigi Neri
Nico Bini
Marco Celli
Siro Bisi
Nico Bini
Steve Smith
Mary Smith
Age
34
23
38
44
49
50
44
34
50
SUPERVISION
Head Employee
210
101
210
103
210
104
231
105
301
210
301
231
375
252
Salary
40
35
61
38
60
60
70
70
65
(3.1)
Name
Luigi Neri
Marco Celli
Siro Bisi
Nico Bini
Steve Smith
Mary Smith
Age
38
49
50
44
34
50
(3.2)
58 Chapter 3
Relational algebra and calculus
Head
210
301
375
NameH
Marco Celli
Steve Smith
Mary Smith
SalaryH
60
70
65
Section 3.1 59
Relational algebra
Number Name
Salary NumberH NameH
SalaryH
104
Luigi Neri
61
210
Marco Celli
60
252
Nico Bini
70
375
Mary Smith
65
(3.5)
60 Chapter 3
Relational algebra and calculus
The distinction between the two cases is due to the fact that the attributes
of the operands are not specified in the expressions (particularly in the
natural join operations). An example of absolute equivalence is the
following:
AB(A>0(R)) A>0(AB(R))
while the following equivalence
AB(R1) J AC(R2) R ABC(R1 J R2)
holds only if in the schema R the intersection between the sets of attributes
of R1 and R2 is equal to A. In fact, if there were also other attributes, the join
would operate only on A in the first expression and on A and such other
attributes in the second, with different results in general.
The equivalence of expressions in algebra is particularly important in
query optimization, which we discuss in Chapter . In fact, sql queries
(Chapter ) are translated into relational algebra, and the cost is evaluated,
cost being defined in terms of the size of the intermediate and final result.
When there are different equivalent expressions, the one with the smallest
cost is selected. In this context, equivalence transformations are used, that is,
operations that substitute one expression for another equivalent one. In
particular, we are interested in those transformations that are able to reduce
the size of the intermediate relations or to prepare an expression for the
application of one of the above transformations. Let us illustrate a first set of
transformations.
. Atomization of selections: a conjunctive selection can be substituted by a
cascade of atomic selections:
F1 F2 ( E ) F1 ( F2 ( E ) )
where E is any expression. This transformation allows the application of
subsequent transformations that operate on selections with atomic
conditions.
. Cascading projections: a projection can be transformed into a cascade of
projections that eliminate attributes in various phases:
X ( E ) X ( XY ( E ) )
if E is defined on a set of attributes that contain Y (and X). This too is a
preliminary transformation that will be followed by others.
. Anticipation of the selection with respect to the join (often described as
pushing selections down):
F ( E1 J E2 ) E1 J F ( E2 )
if the condition F refers only to attributes in the sub-expression E2.
Section 3.1 61
Relational algebra
Y1 = ( X1 Y ) J1
Y2 = ( X2 Y ) J2
62 Chapter 3
Relational algebra and calculus
Finally, we can eliminate from the first argument of the join (with a
projection) the unnecessary attributes, using Rule :
Head(Number(Age<30(EMPLOYEES)) JNumber=EmployeeSUPERVISION)
Some other transformations can be useful, particularly other forms of
anticipation of selections and projections.
. Distribution of the selection with respect to the union:
F (E1 E2) F (E1) F (E2)
. Distribution of the selection with respect to the difference:
F (E1 E2) F (E1) F (E2)
. Distribution of the projection with respect to the union:
X(E1 E2) X(E1) X(E2)
It is worth noting that projection is not distributive with respect to
difference, as we can verify by applying the expressions:
A(R1 R2)
and
A(R1) A(R2)
Section 3.1 63
Relational algebra
the languages discussed in this chapter. The discussion is dealt with further
in Chapter , in the context of the sql language.
Let us look at the relation in Figure . and the following selection:
Age>30(PEOPLE)
PEOPLE
Name
Aldo
Andrea
Maria
Age
35
27
Salary
15
21
42
NULL
not
F
U
T
T
U
F
and
T
U
F
T
T
U
F
U
U
U
F
F
F
F
F
or
T
U
F
T
T
T
T
U
T
U
U
F
T
U
F
We should point out that the three-valued logic for algebraic operators
also presents some unsatisfactory properties. For example, let us consider the
algebraic expression
Age>30(PEOPLE) Age30(PEOPLE)
64 Chapter 3
Relational algebra and calculus
Logically, this expression should return precisely the PEOPLE relation, given
that the age value is either higher than 30 (first sub-expression) or is not
higher than 30 (second sub-expression). On the other hand, if the two subexpressions are evaluated separately, the third tuple of the example (just like
any other tuple with a null value for Age), has an unknown result for each
sub-expression and thus for the union. Only by means of a global evaluation
(definitely impractical in the case of complex expressions) can we arrive at
the conclusion that such a tuple must certainly appear in the result. The same
goes for the expression
Age>30Age30(PEOPLE)
in which the disjunction is evaluated according to the three-valued logic.
In practice the best method for overcoming the difficulties described above
is to treat the null values from a purely syntactic point of view. This
approach works in the same way for both two-valued logic and three-valued
logic. Two new forms of atomic conditions of selection are introduced to
verify whether a value is specified or null:
A is null assumes the value true on a tuple t if the value of t on A is null
and false if it is not;
A is not null assumes the value true on a tuple t if the value of t on A
comes from the domain of A and false if the value is null.
In this context, the expression
Age>30(PEOPLE)
returns the people whose age is known and over 30, whereas to obtain those
who are or could be over 30 (that is, those whose age is known and over 30
or not known), we can use the expression:
Age>30Age is null(PEOPLE)
Similarly, the expressions
Age>30(PEOPLE) Age30(PEOPLE)
Age>30Age30(PEOPLE)
do not return an entire relation, but only the tuples that have a value not null
for Age. If we want the entire relation as the result, then we have to add an
is null condition:
Age>30Age30Age is null(PEOPLE)
This approach, as we explain in Chapter , is used in the present version
of sql, which supports a three-valued logic, and is usable in earlier versions,
which adopted a two-valued logic.
Section 3.1 65
Relational algebra
3.1.9 Views
In Chapter , we saw how it can be useful to make different representations
of the same data available to users. In the relational model, this is achieved
by means of derived relations, that is, relations whose content is defined in
terms of the contents of other relations. In a relational database, there can
thus exist base relations, whose content is autonomous and actually stored in
the database, and derived relations, whose content is derived from the
content of other relations. It is possible that a derived relation is defined in
terms of other derived relations, on condition that an ordering exists among
the derived relations, so that all derived relations can be expressed in terms
of base relations.1
There are basically two types of derived relations:
materialized views: derived relations that are actually stored in the
database;
virtual relations (also called views, without further qualification):
relations defined by means of functions (expressions in the query
language), not stored in the database, but useable in the queries as if they
were.
Materialized views have the advantage of being immediately available for
queries. Frequently, however, it is a heavy task to maintain their contents
consistent with those of the relations from which they are derived, as any
change to the base relations from which they depend has to be propagated to
them. On the other hand, virtual relations must be recalculated for each
query but produce no consistency problems. Roughly, we can say that
materialized views are convenient when there are fewer updates than queries
and the calculation of the view is complex.2 It is difficult, however, to give
general techniques for maintaining consistency between base relations and
materialized views. For this reason, most commercial systems provide
mechanisms for organizing only virtual relations, which from here on, with
no risk of ambiguity, we call simply views.
Views are defined in relational systems by means of query language
expressions. Then queries on views are resolved by substituting the
definition of the view for the view itself, that is, by composing the original
query with the view query. For example, consider a database on the
relations:
R1(ABC), R2(DEF), R3(GH)
with a view defined using a cartesian product followed by a selection
1. This condition is relaxed in the recent proposals for deductive databases, which
allow the definition of recursive views. We discuss this issue briefly in
Section ..
2. We return to this subject in Chapter , in which we discuss active databases,
and in Chapter , in which we discuss data warehouses.
66 Chapter 3
Relational algebra and calculus
R = A>D(R1 J R2)
On this schema, the query
B=G(R J R3)
is executed by replacing R with its definition
B=G(A>D(R1 J R2) J R3)
The use of views can be convenient for a variety of reasons.
A user interested in only a portion of the database can avoid dealing with
the irrelevant components. For example, in a database with two relations
on the schemas
EMPLOYEES(Employee, Department)
MANAGERS(Department, Supervisor)
Section 3.2 67
Relational calculus
Employee,Supervisor(EMPLOYEES J MANAGERS)
Assume we want to insert a tuple into the view: we would like to have tuples
to insert into the base relations that allow the generation of the new tuple in
the view. But this is not possible, because the tuple in the view does not
involve the Department attribute, and so we do not have a value for it, as
needed in order to establish the correspondence between the two relations.
In general, the problem of updating views is complex, and all systems have
strong limitations regarding the updating of views.
We return to the subject of views and present further examples in
Chapter , in which we show how views are defined and used in sql.
68 Chapter 3
Relational algebra and calculus
Then, in predicate calculus both open formulas (those with free variables),
and closed formulas (those whose variables are all bound and none free), are
of interest. The second type have a truth value that, with respect to an
interpretation, is fixed, while the first have a value that depends on the
values substituted for the free variables. In relational calculus, only the open
formulas are of interest. A query is defined by means of an open calculus
formula and the result consists of tuples of values that satisfy the formula
when substituted for free variables.
strictly necessary) then x(f) and x(f) are formulas ( and are the
existential quantifier and universal quantifier, respectively).
The list of pairs A1 : x1, , Ak : xk is called the target list because it defines
the structure of the result, which is made up of the relation on A1, , Ak that
contains the tuples whose values when substituted for x1, , xk render the
formula true. The formal definition of the truth value of a formula goes
beyond the scope of this book and, at the same time, its meaning can be
explained informally. Let us briefly follow the syntactic structure of formulas
(the term value here means an element of the domain, where we assume,
for the sake of simplicity, that all attributes have the same domain):
an atomic formula R(A1 :x1, , Ap :xp) is true for values of x1, , xp that
form a tuple of R;
Section 3.2 69
Relational calculus
an atomic formula xy is true for values of x and y such that the value of x
stands in relation with the value of y; similarly for xc;
the meaning of connectives is the usual one;
for the formulas built with quantifiers:
x(f) is true if there exists at least one value for x that makes f true;
x(f) is true if f is true for all possible values for x.
Let us begin with a very simple query: find the registration numbers,
names, ages and salaries of the employees earning more than 40 thousand,
which we can formulate in algebra with a selection:
Salary>40(EMPLOYEES)
(3.6)
(3.7)
Note the presence of two conditions in the formula (connected by the logical
operator and):
the first, EMPLOYEES(Number :m, Name :n, Age :a, Salary :s), requires that the
values substituted respectively for the variables m, n, a, s constitute a
tuple of the relation EMPLOYEES;
the second requires that the value of the variable s is greater than 40.
The result is made up of the values of the four variables that originate from
the tuples of EMPLOYEES for which the value of the salary is greater than
thousand.
A slightly more complex query is: find the registration numbers, names and
ages of the employees who earn more than 40 thousand. This query requires a
subset of the attributes of EMPLOYEES and thus in algebra can be formulated
with a projection (Expression .):
Number,Name,Age(Salary>40(EMPLOYEES))
This query in calculus can be formulated in various ways. The most direct,
if not the simplest, is based on the observation that what interests us are the
values of Number, Name and Age, which form part of the tuples for which
70 Chapter 3
Relational algebra and calculus
Salary is greater than 40. That is, for which there exists a value of Salary,
greater than 40, which allows the completion of a tuple of the relation
EMPLOYEES. We can thus use an existential quantifier:
(3.8)
The use of the quantifier is not actually necessary, since by simply writing
{Number :m, Name :n, Age :a |
EMPLOYEES(Number :m, Name :n, Age :a, Salary :s) s > 40}
(3.9)
(3.10)
where the variable m, common to both atomic conditions, builds the same
correspondence between tuples specified in the join. Here, also, we can use
existential quantifiers for all the variables that do not appear in the target list.
However, as in the case above, this is not necessary, and would complicate
the formulation.
If the involvement of different tuples of the same relation is required in an
expression, then it is sufficient to include more conditions on the same
predicate in the formula, with different variables. Consider the query: find the
names and salaries of the supervisors of the employees earning more than 40
thousand, expressed in algebra by Expression ., which has a join of the
relation with itself:
NameH,SalaryH (QNumberH,NameH,SalaryH,AgeHNumber,Name,Salary,Age (EMPLOYEES)
JNumberH=Head
(SUPERVISION JEmployee=Number (Salary>40(EMPLOYEES))))
This query is formulated in calculus by requiring, for each tuple of the
result, the existence of three tuples: one relating to an employee earning
more than thousand, a second that indicates who is his supervisor, and the
last (again in the EMPLOYEES relation) that gives detailed information on the
supervisor:
Section 3.2 71
Relational calculus
(3.11)
Consider next the query: find the employees earning more than their
respective supervisors, showing registration number, name and salary of the
employees and supervisors (Expression . in algebra). This differs from the
preceding one only in the necessity of comparing values of the same
attribute originating from different tuples, which causes no particular
problems:
{Number :m, Name :n, Salary :s, NumberH :h, NameH :nh, SalaryH :sh |
EMPLOYEES(Number :m, Name :n, Age :a, Salary :s)
SUPERVISION(Employee :m, Head :h)
EMPLOYEES(Number :h, Name :nh, Age :ah, Salary :sh) s > sh}
(3.12)
The last example requires a more complex solution. We must find the
registration numbers and names of the supervisors whose employees all earn
more than 40 thousand. In algebra we used a difference (Expression .) that
generates the required set by taking into account all the supervisors except
those who have at least one employee earning less than thousand:
Number,Name(EMPLOYEES JNumber=Head
(Head(SUPERVISION)
Head(SUPERVISION JEmployee=Number (Salary40(EMPLOYEES)))))
In calculus, we must use a quantifier. By taking the same steps as for
algebra, we can use a negated existential quantifier. We use many of these,
one for each variable involved.
{Number :h, Name :n | EMPLOYEES(Number :h, Name :n, Age :a, Salary :s)
SUPERVISION(Employee :m, Head :h)
m(n(a(s(EMPLOYEES(Number :m, Name :n, Age :a, Salary :s)
SUPERVISION(Employee :m, Head :h) s 40))))}
(3.13)
As an alternative, we can use universal quantifiers:
{Number :h, Name :n | EMPLOYEES(Number :h, Name :n, Age :a, Salary :s)
SUPERVISION(Employee :m, Head :h)
m(n(a(s((EMPLOYEES(Number :m, Name :n, Age :a, Salary :s)
SUPERVISION(Employee :m, Head :h)) s > 40))))}
(3.14)
This expression selects a supervisor h if for every quadruple of values m,
n, a, s relative to the employees of h, s is greater than 40. The structure
f g corresponds to the condition If f then g (in our case, if m is an
employee having h as a supervisor, then the salary of m is greater than 40),
given that it is true in all cases apart from the one in which f is true and g is
false.
72 Chapter 3
Relational algebra and calculus
Section 3.2 73
Relational calculus
Based on the expressions seen above, we can say that relational calculus is
not domain independent. At the same time, it is easy to see that relational
algebra is domain independent, because it constructs the results from the
relations in the database, without ever referring to the domains of the
attributes. So the values of the results all come from the instance to which
the expression is applied.
If we say that two query languages are equivalent when for each expression
in one there exists an equivalent expression in the other and vice versa, we
can state that algebra and calculus are not equivalent. This is because
calculus, unlike algebra, allows expressions that are domain dependent.
However, if we limit our attention to the subset of relational calculus made
up solely of expressions that are domain independent, then we get a language
that is indeed equivalent to relational algebra. In fact:
for every expression of relational calculus that is domain independent
there exists an expression of relational algebra equivalent to it;
for every expression of relational algebra there is an expression of
relational calculus equivalent to it (and thus domain independent).
The proof of equivalence goes beyond the scope of this text, but we can
mention its basic principles. There is a correspondence between selections
and simple conditions, between projection and existential quantification,
between join and conjunction, between union and disjunction and between
difference and conjunction associated with negation. The universal
quantifiers can be ignored in that they can be changed to existential
quantifiers using de Morgans laws.
In addition to the problem of domain dependence, relational calculus has
another disadvantage, that of requiring numerous variables, often one for
each attribute of each relation involved. Then, when quantifications are
necessary the quantifiers are also multiplied. The only practical languages
based at least in part on domain calculus, known as Query-by-Example (QBE),
use a graphic interface that frees the user from the need to specify tedious
details. Appendix A, which deals with the Microsoft Access system,
presents a version of qbe.
In order to overcome the limitations of domain calculus, a variant of
relational calculus has been proposed, in which the variables denote tuples
instead of single values. In this way, the number of variables is often
significantly reduced, in that there is only a variable for each relation
involved. This tuple relational calculus would however be equivalent to
domain calculus, and thus also have the limitation of domain dependence.
Therefore, we prefer to omit the presentation of this language. Instead we
will move directly to a language that has the characteristics of tuple calculus,
and at the same time overcomes the defect of domain dependence, by using
the direct association of variables with relations of the database. The
following section deals with this language.
74 Chapter 3
Relational algebra and calculus
x(R)(f)
(3.15)
Section 3.2 75
Relational calculus
(3.16)
For queries involving more than one relation, more variables are necessary,
specifying the conditions of correlation on the attributes. The query that
requests find the registration numbers of the supervisors of the employees
earning more than 40 thousand (Expression . in algebra and Expression .
in domain calculus) can be formulated with:
{s.Head | e(EMPLOYEES), s(SUPERVISION) |
e.Number = s.Employee e.Salary > 40}
(3.17)
Note how the formula allows for the conjunction of two atomic conditions,
one that corresponds to the join condition (e.Number = s.Employee) and the
other to the usual selection condition (e.Salary > 40).
In the case of expressions that correspond to the join of a relation with
itself, there will be more variables with the same range. The query: find
names and salaries of supervisors of employees earning more than 40 thousand
(Expression . and Expression .) can be formulated using the following
expression:
{NameH, SalaryH :e.(Name, Salary) |
e(EMPLOYEES), s(SUPERVISION), e(EMPLOYEES) |
e.Number = s.Head s.Employee = e.Number
e.Salary > 40}
(3.18)
Similarly, we can find the employees who earn more than their respective
supervisors, showing registration number, name and salary of the employees and
supervisors (Expression . in algebra and Expression . in domain
calculus):
{e.(Name, Number, Salary), NameH, NumberH, SalaryH :e.(Name, Number, Salary) |
e(EMPLOYEES), s(SUPERVISION), e(EMPLOYEES) |
e.Number = s.Employee s.Head = e.Number
e.Salary > e.Salary}
(3.19)
Queries with quantifiers are much more concise and practical here than in
domain calculus. The query that requests find the registration number and
name of the supervisors whose employees all earn more that 40 thousand
(Expression . in algebra and Expression . or Expression . in domain
calculus) can be expressed with far fewer quantifiers and variables. Again,
there are various options, based on the use of the two quantifiers and of
negation. With universal quantifiers:
{e.(Number, Name) | e(EMPLOYEES), s(SUPERVISION) |
e.Number = s.Head e(EMPLOYEES)(s(SUPERVISION)
((s.Head = s.Head s.Employee = e.Number)
e.Salary > 40))}
(3.20)
76 Chapter 3
Relational algebra and calculus
(3.21)
Section 3.3 77
Datalog
3.3 Datalog
We conclude this chapter with a brief discussion of another database query
language that has generated considerable interest in the scientific community
since the mid-eighties. The basic concept on which the Datalog language is
based is that of adapting the logic programming language Prolog for use with
databases. We can illustrate neither Datalog nor Prolog in detail here, but we
can mention the most interesting aspects, particularly from the point of view
of a comparison with the other languages seen in this chapter.
In its basic form, Datalog is a simplified version of Prolog,3 a language
based on first order predicate calculus, but with a different approach from
the relational calculus discussed above. There are two types of predicate in
Datalog:
extensional predicates, which correspond to relations in the database;
intensional predicates, which essentially correspond to views (virtual
relations), specified by means of logical rules.
Datalog rules have the form:
head body
where
the head is an atomic formula of the form R(A1 : a1, , Ap : ap), similar to
those used in domain relational calculus,4 where each ai, however, can be
a constant or a variable;
the body is a list of atomic formulas, of both forms allowed in domain
calculus, that is, the form R() and the comparison between variables or
between a variable and a constant.
3. For those acquainted with Prolog, note that function symbols are not used in
Datalog.
4. For the sake of continuity with previous sections, we use a non-positional
notation for atomic formulas, while Datalog and Prolog usually have a positional
notation. The substance of the language is, however, the same.
78 Chapter 3
Relational algebra and calculus
(3.22)
Section 3.3 79
Datalog
80 Chapter 3
Relational algebra and calculus
for each employee, the supervisor, the supervisors supervisor and so on,
with no limits. For this we need two rules:
SUPERIORS(Employee :e, SuperHead :h)
SUPERVISION(Employee :e, Head :h)
SUPERIORS(Employee :e, SuperHead :h)
SUPERVISION(Employee :e, Head :h)
SUPERIORS(Employee :h, SuperHead :h)
3.4 Bibliography
Relational algebra was proposed by Codd [] as an essential component of
the model. Relational calculus and the close correspondence of the two
families of languages were also proposed by Codd []. Deeper and more
formal treatment of relational languages can be found in the books devoted
to database theory: Ullman [], Maier [], Paredaens et al. [], Atzeni and
De Antonellis [], Abiteboul, Hull and Vianu []. Datalog is discussed in
Section 3.5 81
Exercises
depth by Ceri, Gottlob and Tanca [], Ullman [], Abiteboul, Hull and
Vianu [].
3.5 Exercises
Exercise 3.1 Study the database schema containing the relations:
FILMS(FilmNumber, Title, Director, Year, ProductionCost)
ARTISTS(ActorNumber, Surname, FirstName, Sex, BirthDate, Nationality)
ROLES(FilmNumber, ActorNumber, Character)
. Produce a database on this schema for which the joins between the
various relations are all complete.
. Assuming two referential constraints between the relation ROLES and the
other two, discuss possible cases of incomplete join.
. Show a cartesian product that involves relations in this database.
. Show a database for which one (or more) of the joins is (are) empty.
Exercise 3.2 With reference to the schema in Exercise ., express the
following queries in relational algebra, in domain calculus, in tuple calculus
and in Datalog:
. the titles of the films starring Henry Fonda;
. the titles of the films in which the director is also an actor;
. the actors who have played two characters in the same film; show the
titles of the films, first name and surname of the actor and the two
characters;
. the titles of the films in which the actors are all of the same sex.
Exercise 3.3 Consider the database containing the following relations:
REPRESENTATIVE(Number, Surname, FirstName, Committee, County, Constituency)
CONSTITUENCIES(County, Number, Name)
COUNTIES(Code, Name, Region)
REGIONS(Code, Name)
COMMITTEES(Number, Name, President)
82 Chapter 3
Relational algebra and calculus
. find the name, surname and constituency of the members of the finance
committee;
. find the name, surname, county and region of election of the delegates of
the finance committee;
. find the regions in which representatives having the same surname have
been elected.
Exercise 3.4 Show how the formulation of the queries in Exercise . could
be facilitated by the definition of views.
Exercise 3.5 Consider the database schema on the relations
COURSES(Number, Faculty, CourseTitle, Tutor)
STUDENTS(Number, Surname, FirstName, Faculty)
TUTORS(Number, Surname, FirstName)
EXAMS(Student, Course, Grade, Date)
STUDYPLAN(Student, Course, Year)
Section 3.5 83
Exercises
. find the names, regions and populations for the cities that (i) have more
than thousand inhabitants and (ii) and are crossed by the Thames or
the Mersey;
. find the cities that are crossed by (at least) two rivers, giving the name of
the city and that of the longest of the rivers.
Exercise 3.7 With reference to the following database schema:
TRIBUTARIES(Tributary, River)
RIVERS(River, Length)
formulate in Datalog, the query that finds all the tributaries, direct and
indirect, of the Mississippi.
Exercise 3.8 Consider the relational schema consisting of the following
relations:
TUTORS(Number, Surname, FirstName)
COURSES(Number, CourseName, Tutor)
STUDENTS(Number, Surname, FirstName)
EXAMS(Student, Course, Date, Grade)
84 Chapter 3
Relational algebra and calculus
4SQL
sql is an acronym for Structured Query Language1. It was originally
developed for the relational dbms System R, created by the ibm Research
Laboratory at San Jose in California during the late seventies. sql has since
been adopted by many other systems; it has been standardized and has
become the reference language for relational databases.
sql is not merely a query language. It contains the dual features of a Data
Definition Language, ddl (with commands for the definition of a relational
database schema) and a Data Manipulation Language, dml (with commands
for the modification and querying of a database instance). In this chapter, we
first introduce sql as the definition language of a database schema
(Section 4.1); then we describe the specification of queries (Section 4.2) and
updates (Section 4.3). In Section 4.4 we describe some more advanced
features of data definition, which require knowledge of the query language.
In Section 4.5, we illustrate the commands for authorization control, and we
conclude the chapter with a description of the interaction between sql and
traditional programming languages (Section 4.6).
Some advanced features of dbmss, shown in Part III and Part IV, are also
supported by sql. We will defer the presentation of these aspects of the
language until they can be introduced alongside the appropriate concepts.
For this reason, we describe the sql commands for the support of
transactions and the definition of indices in Chapter 9, and the definition of
active rules in Chapter 12.
Standardization of SQL The widespread use of sql is largely due to the
vast amount of standardization work that has been devoted to it, carried out
mainly within ansi (the American National Standards Institute) and iso (the
Organization for International Standardization). Many vendors of relational
systems have been able to take part in the decision-making process, rather
1. There are two different pronunciations of this acronym; the first enunciates the
letters separately: s-q-l, whereas the second pronounces it like the word
sequel. Sequel is the name by which the language was originally known.
86 Chapter 4
SQL
Section 4.1 87
Data definition in SQL
88 Chapter 4
SQL
single character. A varying string must specify its maximum length. sql also
allows the compact forms char and varchar, for character and varying
character respectively.
Bit This domain, introduced in sql-2, is used by attributes that can assume
only the value 0 or the value 1. The domain bit is typically used to represent
attributes, known as flags, which specify whether an object has or has not a
certain property. sql also allows a domain string of bits, for which the
length is specified as a parameter. When no length is specified, the length of
the string is set equal to one. The bit strings can be used for the concise
representation of groups of properties. For bits, we can also define strings of
variable length. The syntax is:
bit [varying] [(Length)]
The domains numeric and decimal represent numbers with a decimal base.
The parameter Precision specifies the number of significant digits; using a
domain decimal (4) we can represent values between 9,999 and +9,999.
Using the parameter Scale we can specify the scale of representation, that is,
we can indicate how many digits should appear after the decimal point. If we
want to include two decimal digits, we assign the value 2 to Scale. In order
Section 4.1 89
Data definition in SQL
90 Chapter 4
SQL
time and timestamp we can specify the precision, which represents the
each schema consists of a set of domains, tables,2 indices, assertions, views and
privileges, defined by the following syntax:
create schema [SchemaName] [[authorization] Authorization]
{SchemaElementDefinition}
Section 4.1 91
Data definition in SQL
Authorization represents the name of the user who owns the schema. If the
term is omitted, it is assumed that the user who issued the command is the
owner. The name of the schema can be omitted and, in this case, the name of
the owner is adopted as the name of the schema. After the create schema
command, the user can define the schema components. It is not necessary for
all the components to be defined at the same time as the schema is created:
this can take place in several successive phases. Let us now look at tables and
domains, postponing the other elements of the schema (assertions, views and
privileges) until Section 4.4.
The above table has three attributes of character string domain; the
attribute Name constitutes the primary key of the table.
The syntax for the definition of a table is:
create table TableName
(AttributeName Domain [DefaultValue] [Constraints]
{, AttributeName Domain [DefaultValue] [Constraints]}
[OtherConstraints]
)
Each table is defined by giving its name and the definition of its attributes;
each attribute, in turn, has a name and domain and possibly a set of
constraints, which must be satisfied by the attribute values. Once all the
attributes have been defined, we can then define the constraints that involve
more than one attribute of the table. A table is initially empty and the creator
holds all the privileges regarding the table, that is, the rights to access and to
modify the data.
92 Chapter 4
SQL
high-level programming language (c, Pascal, etc.). In both cases the goal is
the definition of the legal values for data. However, there are also important
differences. On the one hand, the type constructors in sql are much more
limited than those in programming languages. On the other hand, however,
sql offers domain constraints, which have no counterpart in such languages.
In sql, new domains are specified using the predefined domains described in
Section 4.1.1 by means of the create domain command.
create domain DomainName as ElementaryDomain
[DefaultValue]
[Constraints]
Section 4.1 93
Data definition in SQL
If, during the insertion of a row, the value of the attribute is not specified,
it is assigned the value zero.
94 Chapter 4
SQL
This constraint can be defined in two ways. The first alternative can be
used only when the constraint involves a single attribute. In this case, the
specification of the attribute is followed by the keyword unique (similar to
the specification of the not null constraint):
RegistrationNumber character(6) unique
In the first case, the constraint imposes the condition that there can be no
two rows that have both the same first name and the same surname. In the
second (stricter) case, the constraint is violated if either the same first name
or the same surname appears more that once.
Primary key As we discussed in Section 2.2, it is usually important to
specify a primary key, the most important identifier for a relation.
Accordingly, sql allows a primary key constraint to be specified only once for
each table (while it is possible to use the constraints unique and not null an
arbitrary number of times). Like the unique constraint, the primary key
constraint can be directly defined on a single attribute, or it can be defined
by listing the several attributes that make up the primary key. None of the
attributes of a primary key can assume the null value; thus, the definition of
primary key implies an implicit definition not null for all the attributes of the
primary key.
For example, the following definition imposes the constraint that the pair
of attributes FirstName and Surname constitute the primary key:
FirstName character(20),
Surname character(20),
Dept
character(15),
Salary
numeric(9) default 0,
primary key (Surname,FirstName)
Section 4.1 95
Data definition in SQL
The constraint specifies that the attribute Dept can assume only one of the
values that the rows of the table DEPARTMENT possess for the attribute
DeptName.
If we then need the attributes FirstName and Surname to appear in a table
of personal records, we need to use the second alternative:
create table Employee
(
RegNo
character(6) primary key,
FirstName character(20) not null,
Surname character(20) not null,
Dept
character(15)
references Department(DeptName),
Salary
numeric(9) default 0,
City
character(15),
unique (Surname,FirstName),
96 Chapter 4
SQL
foreign key(FirstName,Surname)
references PersonalRecord(FirstName,Surname)
)
Section 4.1 97
Data definition in SQL
98 Chapter 4
SQL
By using alter domain and alter table we can add and remove constraints
and modify default values associated with domains and attributes;
furthermore, we can add and remove attributes and constraints within the
schema of a table. Note that when a new constraint is defined, it must be
satisfied by the data already present in the database. If the database contains
violations of the new constraint, the constraint definition will be rejected.
For example, the following command extends the schema of the table
DEPARTMENT with an attribute NoOfOffices that makes it possible to represent
the number of offices within the department:
alter table Department add column NoOfOffices numeric(4)
The drop command While the alter command carries out modifications
to the domains or schemas of the tables, the drop command allows the
removal of components, whether they be schemas, domains, tables, views or
assertions. Assertions are constraints that are not associated with any
particular table; these will be presented in Section 4.4. The command has the
syntax:
drop schema | domain | table | view | assertion ComponentName
[restrict | cascade]
The restrict option specifies that the command must not be carried out if
the component being deleted is not empty. Thus, a schema is not removed if
it contains tables or other elements; a domain is not removed if it appears in
a table definition; a table is not removed if it possesses rows or if it is present
in a definition of a table or view; and, finally, a view is not removed if it is
used in the definition of other views. The restrict option is the default.
With the cascade option, the component is removed together with the
components depending on it. Thus, when a non-empty schema is removed,
all the objects of which it is constructed are also eliminated. By removing a
domain that appears in the definition of an attribute, the cascade option
causes the name of the domain to be removed, but the attributes that were
defined using that domain remain associated with the same basic domain
definition. Consider, for example, the domain LongString, defined as
char(100). If LongString is eliminated (by means of the command drop domain
LongString cascade) all the attributes defined on that domain will directly
assume the domain char(100). When a table is removed with the cascade
option, all its rows are lost. If the table appeared in another definition of a
Section 4.1 99
Data definition in SQL
100 Chapter 4
SQL
form an interface for the data dictionary, which must be offered by the
systems that want to be compatible with the standard. The
INFORMATION_SCHEMA contains views such as DOMAINS, DOMAIN_CONSTRAINTS,
TABLES, VIEWS, COLUMNS, up to a total of 23 views that describe the structure
of the database.
Rather than describe the structure of these tables, we provide a simplified
example of the contents of one of these views. In Figure 4.1 we can see the
simplified contents of the COLUMNS view of the catalogue describing the
tables EMPLOYEE and DEPARTMENT.
Table_Name Column_Name Ordinal_Position Column_Default Is_Nullable
Employee
Employee
Employee
Employee
Employee
Employee
Department
Department
Department
RegNo
FirstName
Surname
Dept
Salary
City
DeptName
Address
City
1
2
3
4
5
6
1
2
3
NULL
NULL
NULL
NULL
0
NULL
NULL
NULL
NULL
N
N
N
Y
Y
Y
N
Y
Y
Figure 4.1 Part of the contents of the view COLUMNS of the data
dictionary.
In Figure 4.2 we see an example of the reflexivity of the data dictionary,
with the description in COLUMNS of the view itself.
Table_Name Column_Name
Columns
Columns
Columns
Columns
Columns
Table_Name
Column_Name
Ordinal_Position
Column_Default
Is_Nullable
NULL
NULL
NULL
NULL
N
N
N
Y
N
The three parts that make up a select instruction are often called
respectively the target list, the from clause, and the where clause. A more
detailed description of the same syntax is as follows:
select AttrExpr [[as] Alias] {, AttrExpr [[as] Alias]}
from Table [[as] Alias] {, Table [[as] Alias]}
[where Condition]
An sql query considers the rows that belong to the cartesian product of
the tables listed in the from clause, and selects those that satisfy the
conditions expressed in the where clause. The result of the execution of an
sql query is a table, with a row for every row selected by the where clause,
whose columns result from the evaluation of the expressions AttrExpr that
appear in the target list. Each column can be re-named by means of the Alias
that appears immediately after the expression. The tables can also be renamed using the Alias; the table alias is used either as a shorthand or as a
variable, in a way that we will go into later.
Consider a database containing the tables EMPLOYEE(FirstName, Surname,
Dept, Office, Salary, City) and DEPARTMENT(DeptName, Address, City).
102 Chapter 4
SQL
EMPLOYEE FirstName
Mary
Charles
Gus
Jackson
Charles
Laurence
Pauline
Alice
Surname
Brown
White
Green
Neri
Brown
Chen
Bradshaw
Jackson
Dept
Office Salary City
Administration
10
45 London
Production
20
36 Toulouse
Administration
20
40 Oxford
Distribution
16
45 Dover
Planning
14
80 London
Planning
7
73 Worthing
Administration
75
40 Brighton
Production
20
46 Toulouse
The target list can contain generic expressions on the values of the
attributes of each selected row.
Query 3: Find the monthly salary of the employees named White. The result
is shown in Figure 4.6.
MonthlySalary
3.00
DEPARTMENT DeptName
Administration
Production
Distribution
Planning
Research
Address
Bond Street
Rue Victor Hugo
Pond Road
Bond Street
Sunset Street
City
London
Toulouse
Brighton
London
San Jos
104 Chapter 4
SQL
FirstName
Mary
Charles
Gus
Jackson
Charles
Laurence
Pauline
Alice
Surname
Brown
White
Green
Neri
Brown
Chen
Bradshaw
Jackson
City
London
Toulouse
London
Brighton
London
London
London
Toulouse
The result in Figure 4.9 is obtained from the database in Figure 4.3.
FirstName Surname
Gus
Green
Query 7: Find the first names and surnames of the employees who work in
either the Administration department or the Production department.
select FirstName, Surname
from Employee
where Dept = 'Administration' or
Dept = 'Production'
By applying the query to the table in Figure 4.3, we obtain the result in
Figure 4.10.
FirstName
Mary
Charles
Gus
Pauline
Alice
Surname
Brown
White
Green
Bradshaw
Jackson
FirstName
Mary
106 Chapter 4
SQL
select *
from Employee
where Surname like '_r%n'
FirstName
Mary
Charles
Gus
Surname
Brown
Brown
Green
Dept
Office Salary City
Administration
10
45
London
Planning
14
80
London
Administration
20
40
Oxford
NULL.
Salary > 40
This predicate will be true for the rows in which the salary attribute is
greater than 40. Bearing in mind what we said in Section 3.1.8, note that
there are two different solutions for dealing with the situation in which the
Salary attribute has the null value. The first solution, which was adopted by
sql-89, uses traditional two-valued logic and simply allows the predicate to
be considered false. The second solution, on the other hand, is the one
adopted by sql-2. This uses a three-valued logic, in which a predicate
returns the unknown value when any of the terms of the predicate has a null
value. Note that the is null predicate is an exception, since it always returns
either of the values true or false, never the unknown value.
The difference between the solutions based on two- or three-valued logic
emerges only when complex expressions are evaluated. In some cases, the
behaviour of the system when null values are present can be far from
intuitive, particularly when complex predicates are constructed using
negation or nested queries (introduced in Section 4.2.6).
Algebraic interpretation of sql queries We can construct a correspondence between sql queries and the equivalent queries expressed in relational
algebra.
Given a query in sql in its simplest form:
108 Chapter 4
SQL
FirstName
Mary
Charles
Giovanni
Pietro
Surname
Brown
Leblanc
Brown
Brown
City
Verona
Paris
Verona
Milan
Query 11: Find the cities of people named Brown, with each city appearing
only once:
select distinct City
from Person
where Surname = 'Brown'
By executing the two queries shown above on the table in Figure 4.13, we
obtain the results shown in Figure 4.14.
City
Verona
Verona
Milan
City
Verona
Milan
Using this syntax, the join condition does not appear as the argument of
the where clause, but instead is moved into the from clause, associated with
the tables that are involved in the join.
The parameter JoinType specifies which type of join to use, and for this we
can substitute the terms inner, right outer, left outer, or full outer (the
term outer is optional). The inner join is the traditional theta-join of
relational algebra.
Query 12: Query 5 can be rewritten using the syntax of the inner join in the
following way:
select FirstName, Surname, D.City
from Employee inner join Department as D on Dept = DeptName
With the inner join between the two tables, the rows involved in the join
are generally a subset of the rows of each table. It can happen that some rows
are not included because there exists no corresponding row in the other table
for which the condition is satisfied. This property often conflicts with the
demands of applications that might need to retain the rows that would be
eliminated by the join. In writing the application, we might prefer to use null
values to represent the absence of information in the other table. As we have
seen in Section 3.1.5, the outer join has precisely the task of executing a join
while maintaining all the rows that appear in one or both the tables involved.
There are three different types of outer join: left, right and full. The left
join gives the same result as the inner join, but includes the rows of the
table that appears in the left of the join for which no corresponding rows
exist in the right-hand table. The right join behaves symmetrically (keeps
the rows of the right-hand table); finally, the full join gives the result of the
inner join along with the rows excluded from both tables.
Consider the tables DRIVER and AUTOMOBILE shown in Figure 4.15.
Query 13: Find the drivers with their cars, including the drivers without cars:
select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model
from Driver left join Automobile on
(Driver.DriverID = Automobile.DriverID)
The result is shown in Figure 4.16. Note the last row that shows a driver
for whom there is no automobile.
Query 14: Find all the drivers and all the cars, showing the possible
relationship between them:
110 Chapter 4
SQL
DRIVER FirstName
Mary
Charles
Marco
AUTOMOBILE CarRegNo
ABC 123
DEF 456
GHI 789
BBB 421
Surname
Brown
White
Neri
Make
BMW
BMW
Lancia
BMW
DriverID
VR 2030020Y
PZ 1012436B
AP 4544442R
Model
323
Z3
Delta
316
DriverID
VR 2030020Y
VR 2030020Y
PZ 1012436B
MI 2020030U
Firstname
Mary
Mary
Charles
Marco
Surname
Brown
Brown
White
Neri
DriverID
VR 2030020Y
VR 2030020Y
PZ 1012436B
AP 4544442R
CarRegNo
ABC 123
DEF 456
GHI 789
Make
BMW
BMW
Lancia
Model
323
Z3
Delta
NULL
NULL
NULL
The query produces the table shown in Figure 4.17. Note the last row of
the table, describing an automobile for which there is no corresponding
element in DRIVER.
Firstname
Mary
Mary
Charles
Marco
Surname
Brown
Brown
White
Neri
DriverID
VR 2030020Y
VR 2030020Y
PZ 1012436B
AP 4544442R
CarRegNo
ABC 123
DEF 456
GHI 789
Make
BMW
BMW
Lancia
Model
323
Z3
Delta
NULL
NULL
NULL
NULL
NULL
NULL
BBB 421
BMW
316
Query 15:
select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model
from Driver, Automobile
where Driver.DriverID * = Automobile.DriverID
These solutions, being outside the sql-2 standard, are not portable from
one system to another.
A further feature of sql-2 is the possibility of using the keyword natural
before the join type. This makes it possible to define the natural join of
relational algebra. Therefore, in the joining of two tables, we can use an
implicit condition of equality on all the attributes having the same name (see
Section 3.1.5). For example, Query 14 could be written as:
Query 16:
select FirstName, Surname, Driver.DriverID, CarRegNo, Make, Model
from Driver natural full join Automobile
112 Chapter 4
SQL
This query compares each row of EMPLOYEE with all the rows of EMPLOYEE
associated with the Production department. Note that in this query, each row
whose Dept attribute has the value Production is also compared with itself,
but the comparison of the row with itself will never be satisfied, in that the
predicate of inequality on the attribute FirstName can never be true.
To illustrate the execution of this query, we can imagine that when we
define the aliases, two different tables E1 and E2 are created, each containing
all the rows of EMPLOYEE. Figure 4.18 shows the idea and the fact that there is
a comparison of each row of E1 with each row of E2.
Employee
Employee E1
Employee E2
In this way, the query specifies the attributes that must be used for the
ordering. The rows are ordered according to the first attribute in the list. For
rows that have the same value for this attribute, the values of the attributes
that follow are taken into account, in sequence. The order on each attribute
can be ascending or descending, and it is specified by means of the qualifiers
asc or desc. If the qualifier is omitted, the ascending order is assumed.
Consider the database in Figure 4.15.
Query 19: Extract the content of the AUTOMOBILE table in descending order of
make and model.
select *
from Automobile
order by Make desc, Model desc
Make
Lancia
BMW
BMW
BMW
Model
Delta
Z3
323
316
DriverID
PZ 1012436B
VR 2030020Y
VR 2030020Y
MI 20220030U
114 Chapter 4
SQL
The query is first executed in the normal manner, taking into account only
the from and where clauses. The aggregate operator is then applied to the table
containing the result of the query. In the example above, firstly a table is
constructed containing all the rows of EMPLOYEE having Production as the
value of the attribute Dept. Then, the aggregate operator is applied to the
table, counting the number of rows that appear in it.
Standard sql provides five aggregate operators; they are count, sum, max,
min and avg.
The count operator uses the following syntax:
count (* | [ distinct | all] AttributeList)
The first option (*) returns the number of rows. The distinct option
returns the number of different values for the attributes in AttributeList. The
all option returns the number of rows that possess values different from the
null value for the attributes in AttributeList. If an attribute is specified
without distinct or all, all is assumed as default.
Query 21: Find the number of different values on the attribute Salary for all
the rows in EMPLOYEE:
select count(distinct Salary)
from Employee
Query 22: Find the number of rows of EMPLOYEE having a not null value on the
attribute Salary:
select count (all Salary)
from Employee
Let us turn to the other four operators sum, max, min and avg. They allow as
argument an attribute or expression, possibly preceded by one of the
keywords, distinct or all. The aggregate functions sum and avg allow as
argument only expressions that represent numeric values or intervals of
time. The functions max and min require only that an order be defined in the
expression, making them applicable also to character strings and instants of
time.
sum | max | min | avg ([distinct | all] AttributeExpression)
The operators apply to the rows that are selected by the where clause of the
query, and have the following meanings:
sum: returns the sum of the values possessed by the attribute expression;
max and min: return respectively the maximum and minimum values;
Aggregate evaluation may take place after an arbitrary query, such as the
following one, which has a join.
Query 25: Find the maximum salary among the employees who work in a
department based in London.
select max(Salary)
from Employee, Department
where Dept = DeptName and
Department.City = 'London'
Note that the following version of the same query is not correct:
Query 26:
select FirstName, Surname, max(Salary)
from Employee, Department
where Dept = DeptName and
Department.City = 'London'
On an intuitive level, this query would select the highest value of the
Salary attribute, and thus would automatically select the attributes FirstName
and Surname of the corresponding employee. However, such semantics could
not be generalized to the aggregate queries, for two reasons. In the first place,
there is no guarantee that the operator will select a single element, given that
there could be more than one row containing a particular salary. In the
second place, written like this, the query could be applied to the operators
max and min, but would have no meaning for the other aggregate operators.
Therefore, the sql syntax does not allow aggregate functions and attribute
expressions (such as, for example, attribute names) within the same target list
(except for queries with the group by clause, described in the next section).
116 Chapter 4
SQL
Suppose that the table contains the information shown in Figure 4.20.
EMPLOYEE FirstName
Mary
Charles
Gus
Jackson
Charles
Laurence
Pauline
Alice
Surname
Brown
White
Green
Neri
Brown
Chen
Bradshaw
Jackson
Dept
Office Salary City
Administration
10
45
London
Production
20
36
Toulouse
Administration
20
40
Oxford
Distribution
16
45
Dover
Planning
14
80
London
Planning
7
73
Worthing
Administration
75
40
Brighton
Production
20
46
Toulouse
Dept
Administration
Production
Administration
Distribution
Planning
Planning
Administration
Production
Salary
45
36
40
45
80
73
40
46
Figure 4.21 Projection on the attributes Dept and Salary of the EMPLOYEE
table.
Dept
Administration
Administration
Administration
Distribution
Planning
Planning
Production
Production
Salary
45
40
40
45
80
73
36
46
Salary
125
82
45
153
118 Chapter 4
SQL
Query 28:
select Office
from Employee
group by Dept
This query should return the department name, the number of employees
of each department, and the city in which the department is based. Given
that the attribute DeptName is the key of DEPARTMENT, a particular value of
City corresponds to each value of DeptName. The system could therefore
provide a correct response, but sql prohibits queries of this nature. In
theory the language could be modified in such a way as to allow the
identification of the attributes that are keys to the schemas of the tables, and
highlight the attributes that could appear in the target list. In practice it was
preferred to keep the language simple, possibly requiring that the query use
a redundant group of attributes. The query can be correctly rewritten as
follows:
Query 30:
select DeptName, count(*), D.City
from Employee E join Department D on (E.Dept = D.DeptName)
group by DeptName, D.City
Group predicates We have seen how rows can be organized into subsets
by means of the group by clause. It is sometimes possible that an application
needs to consider only the subsets that satisfy certain conditions. If the
conditions that must be satisfied by the subsets are verifiable at the single
row level, then it is sufficient to use the appropriate predicates as the
argument of the where clause. If, however, the conditions have to refer to
values that are the result of aggregations, we have to use a new construct, the
having clause.
The having clause describes the conditions that must be applied at the end
of the execution of a query that uses the group by clause. Each subset of rows
forms part of the result of the query only if the predicate argument of having
is satisfied.
Query 31: Find which departments spend more than 100 thousand on
salaries:
select Dept
from Employee
group by Dept
having sum(Salary) > 100
Applying the query to the table shown in Figure 4.20, we follow the steps
described for group by queries. Once the rows have been grouped according
to the value of the Dept attribute, the predicate argument of the having clause
is evaluated. This selects the departments for which the sum of the values of
the attribute Salary, for all the rows of the subset, is higher than 100. The
result of the query is shown in Query 4.24.
Dept
Administration
Planning
120 Chapter 4
SQL
The query first obtains the values of the attribute FirstName for the rows
of EMPLOYEE. It then obtains the values of the attribute Surname for the same
rows and finally constructs the table by uniting the two partial results. Given
that the set operators eliminate the duplicates, there will be no repeated
In this case all the duplicates are retained. The result of the query, again
based on the table in Figure 4.20, is shown in Figure 4.26.
Query 35: Find the surnames of employees that are also first names:
select FirstName as Name
from Employee
intersect
select Surname
from Employee
122 Chapter 4
SQL
select Surname
from Employee
The query selects the rows of EMPLOYEE for which the value of the attribute
Dept is equal to at least one of the values of the DeptName attribute of the
rows of DEPARTMENT with City equal to London.
This query can also be expressed using a join between the tables EMPLOYEE
and DEPARTMENT, and, in fact, the optimizers are generally able to deal with
the two different formulations of this query in the same way. The choice
between one representation and the other can be influenced by the degree of
readability of the solution. In cases as simple as these, there is no difference,
but for more complex queries, the use of several nested queries can improve
readability.
Let us consider a query that allows us to find the employees belonging to
the Planning department who have the same first name as a member of the
Production department. The query lends itself to both possible formulations:
the first is more compact and uses variables.
Query 38:
select E1.FirstName, E1.Surname
from Employee E1, Employee E2
where E1.FirstName = E2.FirstName and
E2.Dept = 'Production' and
E1.Dept = 'Planning'
The second method uses a nested query, expressing the query without
needing aliases:
Query 39:
select FirstName, Surname
from Employee
where Dept = 'Planning' and
FirstName = any (select Firstname
124 Chapter 4
SQL
from Employee
where Dept = 'Production')
The nested query selects the values of Dept for all the rows in which the
surname is Brown. The condition is therefore satisfied by the rows of
DEPARTMENT for which the value of the attribute DeptName is not among the
values produced by the nested query. This query could not be expressed by
means of a join. Note how this query could have been implemented in
relational algebra using the following expression (DeptName(DEPARTMENT)
QDeptNameDeptDept(Surname=Brown(EMPLOYEE))), and therefore could also
have been expressed using the set operator except as follows:
Query 41:
select DeptName
from Department
except
select Dept as DeptName
from Employee
where Surname = 'Brown'
sql offers two special operators to represent set membership and its
negation, in and not in. These are indeed shorthand for = any and <> all
respectively. Examples of their use are given in the queries of the next
section.
Finally we can observe how some queries that use the operators max and
min could be expressed by means of nested queries.
Query 42: Find the department of the employee earning the highest salary
(using the aggregate function max):
select Dept
from Employee
where Salary = any (select max(Salary)
from Employee)
Query 43: Find the department of the employee earning the highest salary
(using only a nested query):
select Dept
from Employee
where Salary >= all (select Salary
from Employee)
The two queries are equivalent in that the maximum value is exactly the
value that is greater than or equal to all the values of the same attribute in
the other rows of the relation. In these cases, however, it is advisable to use
the aggregate operator, as it gives a more readable result (and possibly it is
executed more efficiently). It is also interesting to note that for the first
nested query there is no difference between using the keywords any and all,
since the query always returns a single row.
Complex nested queries A simple and intuitive interpretation for
understanding nested queries lies in the assumption that the nested query is
executed before the analysis of the rows of the external query. The result of
the query can be saved in a temporary variable and the predicate of the
external query can be evaluated by using the temporary result. What is
more, this interpretation produces an efficient execution, since the nested
query is processed only once. Consider again Query 40. The system can first
carry out the nested query, which returns the value of the Dept attribute for
all the employees named Brown. Once this is done, each department is then
checked to see that the name is not included in the table produced, using the
operator <> all.
Sometimes, however, the nested query refers to the context of the query
in which it is enclosed; this generally happens by means of a variable defined
within the external query and used in the internal query. Such a mechanism
is often described as the transfer of bindings from one context to another.
This mechanism enhances the expressive power of sql. In this case, the
simple interpretation given before for nested queries is no longer valid.
Instead, it is necessary to reconsider the standard interpretation of sql
queries, which first compute the cartesian product of the tables in the from
clause and next evaluate the where clause on each row. The nested query is a
component of the where clause and it must also be evaluated separately for
every row produced by the consideration of the external query. Thus, the
new interpretation is the following: for each row of the external query, we
first evaluate the nested query, and then compute the row-level predicate of
the external query. Such a process can be arbitrarily nested for an arbitrarily
complex use of variables within the nested queries; however, the readability
and declarative nature of the language are compromised. With regard to the
visibility (or scope) of sql variables, there is a restriction on the use of a
variable: a variable can be used only within the query in which it is defined
or within a query that is recursively nested in the query where it is defined.
If a query contains nested queries at the same level (on distinct predicates),
the variables introduced in the from clause of a query cannot be used in the
context of another query. The following query, for example, is incorrect.
Query 44:
select *
from Employee
where Dept in (select DeptName
from Department D1
where DeptName = 'Production') or
126 Chapter 4
SQL
The query searches the rows of the PERSON table for which there exists a
further row in PERSON with the same FirstName and Surname, but a different
TaxCode.
In this case, we cannot execute the nested query before evaluating the
external query, given that the nested query is not properly defined until a
value has been assigned to the variable P. It is necessary, instead, to evaluate
the nested query for every single row produced within the external query.
Thus, in the example, the rows of the variable P will first be examined one
by one. For each of these rows, the nested query will be executed and it will
return or not the empty result depending upon whether or not there are
persons with the same name and surname. This query could also have been
formulated with a join of the PERSON table with itself.
Consider now a different query.
Query 46: Find all persons who do not have homonyms:
select *
from Person P
where not exists (select *
from Person P1
where P1.FirstName = P.FirstName and
P1.Surname = P.Surname and
P1.TaxCode <> P.TaxCode )
The interpretation is similar to that of the preceding query, with the single
difference that the predicate is satisfied if the nested query returns an empty
result. This query could also have been implemented by a difference that
subtracted from all the first names and surnames, those of the people sharing
a first and second name, determined using a join.
Another way to formulate the same query uses the tuple constructor,
represented by a pair of curved brackets that enclose the list of attributes.
Query 47: Find all the persons who do not have homonyms (using the tuple
constructor):
select *
from Person P
where (FirstName, Surname) not in (select FirstName, Surname
from Person P1
where P1.TaxCode <> P.TaxCode)
SONGWRITER(Name, Song).
Query 48: Find the singers who have performed only their own songs:
select Name
from Singer
where Name not in (select Name
from Singer S
where Name not in
(select Name
from Songwriter
where Songwriter.Song = S.Song))
The external query has no link with the nested queries, and therefore can
be initially suspended, waiting for the result of the first-level nested query.
Such first-level query, however, presents a binding. Therefore, the query is
executed by means of the following phases.
1. The nested query select Name from Singer S is applied to all the rows of
the table Singer.
2. For each row, the most internal query is evaluated. This returns the
names of the songwriters of the song titles appearing in the rows of S.
Thus, if the name of the singer does not appear among the names of the
songwriters (which therefore means that the singer is not the writer of
the song he sings), then the name is selected.
3. Finally, the table that contains the names of the singers who have
performed only their own songs (the more external query) is
constructed. This query returns all the rows whose Name does not
appear as result of the nested query.
The logic of this query execution is more evident when we consider that
the query can be expressed in the same way by using the except operator:
Query 49:
select Name
from Singer
except
select Name
from Singer S
128 Chapter 4
SQL
Commercial sql systems do not always carry out the nested queries
internally by scanning the external table and producing a query for every
row of the relation. Instead, they try to process as many queries as possible
in a set-oriented way, with the aim of handling a large quantity of data by
means of as few operations as possible. Several optimizations are possible,
such as retaining the results of the nested subqueries for multiple uses, and
anticipating the evaluation of the parts of query predicates that are not
nested. We will discuss optimization issues further in Section 9.6.
4.3.1 Insertions
The command for inserting a row into the database offers alternative
syntaxes:
insert into TableName [(AttributeList)] values (ListOfValues) |
SelectSQL
The first form allows the insertion of single rows into the tables. The
argument of the values clause represents explicitly the values of the
attributes of the single row. For example:
insert into Department(DeptName, City)
values('Production', 'Toulouse')
The second form allows the addition of a set of rows, which are first
retrieved from the database.
The following command inserts the results of the selection from the table
PRODUCT of all the rows having London as the value of the attribute ProdArea,
into the table LONDONPRODUCTS.
insert into LondonProducts
(select Code, Description
from Product
where ProdArea = 'London')
The two forms have different applications. The first case is typically used
in programs to fill in a table with data provided directly by the users. Each
use of the insert command is generally associated with the filling of a form,
that is, a friendly interface in which the user supplies values for various
attributes. The second case inserts data into a table based on other
information already present in the database.
If the values of some of the attributes of the table are not specified during
an insertion, the default value is assigned to them, or failing this, the null
value (Section 4.1.6). If the insertion of the null value violates a not null
constraint defined on the attribute, the insertion will be rejected. Finally,
note that the correspondence between the attributes of the table and the
values to be inserted is dictated by the order in which the terms appear in
the definition of the table. For this reason, the first element of the ValuesList
(for the first form of the statement) or the first element of the target list (for
the second form) must correspond to the first attribute that appears in
AttributeList (or in the definition of the table if AttributeList is omitted), and
so on for the other attributes.
4.3.2 Deletions
The delete command eliminates rows from the tables of the database,
following this simple syntax:
delete from TableName [where Condition]
When the condition forming the subject of the where clause is not
specified, the command removes all the rows from the table, otherwise only
the rows that satisfy the condition are deleted. In case there is a referential
integrity constraint with a cascade policy in which the table is referenced,
the cancellation of rows of the table can cause the cancellation of rows
belonging to other tables. This can generate a chain reaction if these
cancellations in their turn cause cancellation of rows in other tables.
delete from Department
where DeptName = 'Production'
The command deletes the row of DEPARTMENT having the name Production.
(Since DeptName was declared as the primary key for the table, there can be
only one row having that particular value.)
The condition has the same syntax as in the select statement, which means
that nested queries referring to other tables can appear within it. A simple
example is the command that deletes the departments without employees:
delete from Department
where DeptName not in (select Dept
from Employee)
Note the difference between the delete command and the drop command
described in Section 4.1.8. A command such as:
delete from Department
deletes all the rows of the DEPARTMENT table, possibly also deleting all the
rows of the tables that are linked by referential integrity constraints with the
table, if the cascade policy is specified in the event of deletion. The schema
of the database remains unchanged, however, and the command modifies
only the database instance. The command:
130 Chapter 4
SQL
has the same effect as the command delete, but in this case the schema of the
database is also modified, deleting the DEPARTMENT table from the schema, as
well as all the views and tables that refer to it in their definitions. However,
the command:
drop table Department restrict
4.3.3 Updates
The update command is slightly more complex.
update TableName
set Attribute = Expression | SelectSQL | null | default
{, Attribute = Expression | SelectSQL | null | default}
[where Condition]
operates on a single row, updating the salary of the employee number m2047
whereas the following example operates on a set of rows.
update Employee set Salary = Salary * 1.1
where Dept = 'Administration'
The command produces a 10% salary increase for all the employees who
work in Administration. The assignment operator has the usual property, for
which Salary on the right-hand side of the operator represents the old value,
which is evaluated for every row to which the update must be applied. The
result of the expression becomes the new value of the salary.
The set-oriented nature of sql should be taken into account when writing
update commands. Suppose we wish to modify the employees salaries,
increasing the salaries under 30 thousand by 10%, and the higher salaries by
15%. One way to update the database to reach this goal is to execute the
following command:
update Employee set Salary = Salary * 1.1
where Salary <= 30
update Employee set Salary = Salary * 1.15
where Salary > 30
The conditions that can be used are those that can appear in the where
clause of an sql query. The condition must always be verified to maintain the
correctness of the database. In this way it is possible to specify all the tuple
constraints we discussed in Section 2.2.1, and even more, because the
Condition can include references to other fields.
An effective demonstration of the power of the construct is to show how
the predefined constraints can all be described by means of the check clause.
For this, we can redefine the schema of the first version of the EMPLOYEE table
given in Section 4.1.7:
132 Chapter 4
SQL
4.4.2 Assertions
Besides the check clause, we can define a further component of the schema of
the database, assertions. Introduced in sql-2, these represent constraints
that are not associated with any row or table in particular, and belong to the
schema.
Using assertions, we can express all the constraints that we have already
specified when dealing with table definitions. But assertions also allow the
expression of constraints that would not otherwise be expressible, such as
constraints on more than one table or constraints that require that a table
have a minimum cardinality. Assertions are named, and therefore they can be
explicitly dropped by means of the drop instruction for schema updates (see
Section 4.1.8).
The syntax for the definition of assertions is as follows:
4.4.3 Views
In Chapter 3 views were introduced as virtual tables, whose contents
depend on the contents of the other tables in the database. Views are defined
in sql by associating a name and a list of attributes with the result of the
execution of a query. Other views can also appear within the query that
defines the view. However, views cannot be mutually dependent, either
immediately (defining a view in terms of itself), or transitively (defining a
view V1 using a view V2, V2 using V3 and so on until Vn is defined by means
of V1),
A view is defined using the command:
create view ViewName [(AttributeList)] as SelectSQL
[with [local | cascaded] check option]
134 Chapter 4
SQL
The sql query and the schema of the view must have the same number of
attributes. We can, for example, define a view ADMINEMPLOYEE that contains
all the employees in the Administration department with a salary higher
than 10 thousand.
create view AdminEmployee(RegNo, FirstName, Surname, Salary) as
select RegNo, FirstName, Surname, Salary
from Employee
where Dept ='Administration' and
Salary > 10
Query 50: Find the department with the highest salary expenditure.
select Dept
from SalaryBudget
where SalaryTotal = (select max(SalaryTotal)
from SalaryBudget )
This solution is not recognized by all sql systems, which may impose that
the condition in the having clause must be a simple comparison with an
attribute or a constant, and cannot use a nested query. We can give another
example of the use of views to formulate complex queries.
create view DeptOffice(DeptName,NoOfOffices) as
select Dept, count(distinct Office)
from Employee
group by Dept
136 Chapter 4
SQL
Query 53:
select avg(count(distinct Office))
from Employee
group by Dept
The query is, however, incorrect, because the sql syntax does not allow
the cascading of aggregate operators. The basic problem is that the
evaluation of the two different operators happens at different levels of
aggregation, whereas a single occurrence of the group by clause for every
query is admissible.
grants to the user Stefano the select privilege on the DEPARTMENT table. The
138 Chapter 4
SQL
with grant option clause specifies whether the privilege of propagating the
privilege to other users must also be granted. We can use the keywords all
privileges in place of the distinct privileges. These identify all the privileges
that the user can grant on a particular resource. Thus the command:
grant all privileges on Employee to Paolo, Riccardo
concedes to the users Paolo and Riccardo all the privileges on the EMPLOYEE
table that can be granted by whoever executes the command.
The revoke command does the reverse, taking away privileges that a user
had already been granted.
revoke Privileges on Resource from Users [restrict | cascade]
Among the privileges that can be revoked, apart from those that can
appear as subject of the grant command, there is also the grant option
privilege, which derives from the use of the clause with grant option.
The only user who can revoke privileges from another user is the user who
granted the privileges in the first place; a revoke can take away all or a subset
of the privileges that were originally granted. The default option restrict
specifies that the command must not be carried out in case revoking this
users privilege causes a further revoking of privileges. This can happen
when the user has received the privileges with the grant option and has
propagated the privileges to other users. In a situation of this nature, the
restrict option will cause an error to be signalled. With the cascade option,
instead, the execution of the command is imperative; all the privileges that
were propagated are revoked and all the elements of the database that were
constructed by exploiting these privileges are removed. Note that in this case
too, the cascade option can generate a chain reaction, in which for each
element removed, all the objects that have some dependent relation to it are
also removed. As in other such cases, it is necessary to be very careful that a
simple command does not produce extensive and undesirable modifications
on the database.
It is not only the revoke command that can produce chain reactions: the
grant command can also produce similar effects. It can happen that a user has
received a privilege on a table that has allowed him to create views that refer
to this table, by means of, for example, the select privilege. If the user is
granted further privileges on the table, then these privileges will be
automatically extended to cover the views (and subsequently to views
constructed on these views).
The use of special applications rather than the sql interpreter to gain
access to information is justified by a number of factors. Very often, access to
the information is not required directly by a user but by a non-interactive
(batch) application. Further, for interactive users, the access techniques are
often simple and predictable. It is therefore useful to reduce the complexity
of access to the database by constructing an application that provides a
simplified interface for carrying out the task. Finally, the presentation of the
data offered by the system might be unsuitable for the users requirements,
while a special application is free from constraints and can provide a
representation adequate for the requirements.
There are many tools that can be used for the creation of database
applications. A thriving market is that of the fourth generation languages
(4gls), highly sophisticated development tools that make it possible to
develop complete database management applications with little effort. Most
database vendors offer, together with the database manager, a set of tools for
the development of applications. Moreover there is a rich supply of products
that are not linked to any database in particular, all of which are able to
manage a dialogue with the relational system by means of standard sql.
These tools make possible the effective definition of database schemas, and
the construction of complex interfaces.
Another method for writing applications uses traditional high-level
programming languages. We will concentrate our analysis on this method,
because it is still of considerable importance, and because of the lack of
uniformity among 4gls.
140 Chapter 4
SQL
There are two possible solutions to this problem. The first consists of using
a programming language that makes more powerful data constructs available
and in particular is able to organize a set of rows type structure in a natural
way. This solution is gaining more interest due to the increasing spread of
object-oriented programming languages, characterized by powerful typedefinition mechanisms. However, most applications are written using
languages that do not possess this capability. A further difficulty with this
solution is the lack of a standard solution accepted by all systems, for
example, by object-oriented and object-relational systems (see Chapter 11).
The second strategy, more commonly used, has been standardized and
does not need a complicated extension of existing programming languages,
although it is not as high-level and friendly to the programmer as one would
like it to be. This solution is based on the use of cursors.
4.6.2 Cursors
A cursor is a mechanism that allows a program to access the rows of a table
one at a time; the cursor is defined using a query. Let us look first at the
syntax for the definition and use of cursors.
declare CursorName [scroll] cursor for SelectSQL
[for read only | update [of Attribute {, Attribute}]]
The open command applies to a cursor; when invoked, the query is carried
out and the query result can be accessed using the fetch command.
fetch [Position from] CursorName into FetchList
The fetch command takes a row from the cursor and returns its values into
the variables of the program that appear in FetchList. The FetchList must
include a variable for every element in the target list of the query, such that
every element of the FetchList is type-compatible with the domains of the
elements of the target list of the query. An important concept is that of
current row, which represents the last row used by the fetch operation. The
Position parameter is used to set the current row. It can assume the following
values:
next (the current row becomes the row that follows the current one);
first (the first row of the query result);
last (the last row);
absolute IntegerExpression (the row that appears in the i-th position of the
query result, where i is the result of the evaluation of the expression);
relative IntegerExpression (the row that appears in the i-th position,
starting from the current position).
These options can be used only on condition that the scroll option (which
guarantees the possibility of free movement within the result of the query)
was specified when the cursor was defined. If the scroll option is not
specified, the only value available for the parameter is next; in this case,
implementation is easier, as the rows of the result can be discarded
immediately after being returned. This can be very useful for reducing
response times, particularly when the query returns a large number of rows.
update TableName
set Attribute = Expression | null | default
{, Attribute = Expression | null | default}
where current of CursorName
In this way, the cursor EmpoloyeeCursor is allocated to the query that makes
it possible to obtain data relating to the employees who earn between 40
thousand and 100 thousand.
Let us now look at a simple example of a c program that uses cursors. The
sql commands are identified by the character $ in the first column, and the
variables of the program are distinguished by the fact that their names are
preceded by the : (colon) character. The variables must be of a type
compatible with the values that they will contain. A predefined variable,
sqlcode, is used. It contains zero if the execution of the last command has
been successful, and a non-zero error code otherwise. Its main use is to
detect when the rows of a cursor have all been fetched.
142 Chapter 4
SQL
The values of the attributes FirstName and Surname of the employee whose
registration number is contained in the variable EmpNumber will be copied into
the variables EmpFName and EmpSurname.
but also in the structure of the queries and in the set of tables being accessed.
The mechanisms for invoking sql commands seen in the previous section
will not work in this context, given that they require the structure of the
queries to be predefined. This family of mechanisms is collectively known as
static sql. An alternative family of commands permit the use of dynamic sql.
These commands make it possible to construct a program that executes sql
commands constructed when the program is run. These commands, however,
require special support from the system.
The greatest problem to be overcome is the transfer of parameters between
the sql command and the program, both incoming and outgoing. Given that
the sql command is arbitrary, the program has no way of recognizing at the
time of compilation, which are the required input and output parameters of
the command. This information is necessary for the program to be able to
organize the query internally.
The use of dynamic sql alters the mode of interaction with the system. In
static sql, the commands can be processed by a compiler, which analyzes the
structure of the command and constructs a translation into the internal
language of the system. In this way, commands do not need to be translated
and optimized each time, but can be executed immediately. This brings
considerable advantages in terms of performance. If, for example, a command
is carried out repeatedly, with this solution the translation is made only once,
whereas interacting with the engine each separate execution of the command
would require its own phase of translation.
In dynamic sql attempts are made to retain these advantages as much as
possible, making available two different types of interaction. The query can
be carried out immediately, meaning that the execution of the query follows
immediately after the analysis, or, alternatively, the management of the
query happens in two phases, analysis and execution.
Immediate execution command The execute immediate command
requires the execution of an sql statement, either directly stated or
contained in a program variable of type string of characters.
execute immediate SQLStatement
The immediate method can be used only for statements that do not require
parameters for either input or output. An example of the use of the command
is as follows:
execute immediate
"delete from Department where Name = 'Administration'"
144 Chapter 4
SQL
The target list contains the list of parameters in which the result of the
execution of the statement must be written. (This part is optional if the sql
command has no output parameters.) The list of parameters, on the other
hand, specifies which values must be assumed by the variable parameters on
the list. (This part can also be omitted if the sql statement has no input
parameters.)
Here is an example:
execute :SQLStatement into :city using :department
cursor allow the specification of the into and using clauses, which allow the
specification of possible input and output parameters.
One example of the use of a dynamic cursor is as follows, in which we
suppose that the query defined in the string SQLStatement allows an input
parameter:
prepare :SQLStatement from :SQLString
declare PrgCursor cursor from :SQLStatement
open PrgCursor using :PrgVariable
4.6.4 Procedures
Standard sql allows for the definition of procedures, also known as stored
procedures because they are usually stored in the database as components of
the schema. As with programming languages, procedures make it possible to
associate a name with an sql statement, with the possibility of specifying
parameters to be used for communication to the procedure. The advantages
are an increase in the clarity of the program, easier maintenance, and in many
cases a noticeable increase in efficiency. Once the procedure has been
defined, it is treated as part of the set of predefined sql commands. As a first
example, let us look at the following sql procedure, which updates the name
of the city of a department.
procedure AssignCity(:Dep char(20), :City char(20))
update Department
set City = :City
where Name = :Dep;
Standard sql-2 does not handle the writing of complex procedures, but is
limited to the definition of procedures made up of a single sql command.
Many systems remove this limitation, driven by users requirements.
The procedural extensions proposed by many systems differ widely among
themselves. There are systems that allow only sequences of commands
within a procedure, and others that allow the use of control structures,
declarations of local variables and the invocation of external programs. In
each case, the use of these functions is outside the scope of the sql-2
standard and renders the sql code thus generated non-portable. sql-3
extends this aspect of the language and provides a rich syntax for the
definition of procedures. In the meantime, if we decide to use the procedural
extensions provided by a given system, we must also rewrite them in case
there is a need to move the application to another environment.
The following example shows a (non-standard) procedure composed of a
sequence of two sql instructions. The procedure makes it possible to give to
the attribute City the value of :NewCity, for all the rows of DEPARTMENT and
EMPLOYEE in which the attribute is equal to :OldCity.
146 Chapter 4
SQL
values(ClientAccount,Withdrawal,sysdate);
end if;
end Debit;
Solution:
select Name
from Stadium
where Name not in
(select StadiumName
from Match
where (Country1 in
(select Country
from Nationality
where Continent = 'Europe' ))
or
(Country2 in
(select Country
from Nationality
where Continent = 'Europe' )))
Solutions:
i. Relational algebra:
Name(STADIUM)
QNameStadiumNameStadiumName((Country
(Continent ='Europe' (NATIONALITY)))
JCountry1=Country Country2=Country
(StadiumName,Country1,Country2(MATCH)))
ii. Relational calculus:
{s.Name | s(STADIUM)
| (m(MATCH) (n(NATIONALITY)
148 Chapter 4
SQL
(m.StadiumName = s.Name
n.Continent = 'Europe'
(m.Country1 = n.Country m.Country2 = n.Country))))}
iii. Datalog:
STADIUMWITHEUROPE(StadiumName : n)
MATCH(StadiumName : n, Date : d, Time : t,
Country1 : c1, Country2 : c2),
NATIONALITY(Country : c1, Continent : cn, Category : ct),
cn = 'Europe'
STADIUMWITHEUROPE(StadiumName : n)
MATCH(StadiumName : n, Date : d, Time : t,
Country1 : c1, Country2 : c2),
NATIONALITY(Country : c2, Continent : cn, Category : ct),
cn = 'Europe'
(c) Extract the total capacity of the stadiums in which matches are played
and that have a South American national team as the first team. (Note: to
evaluate the total capacity, summarize the capacities allocated to each
contest, even if many contests take place in the same stadium).
Solution:
select sum(Capacity)
from Stadium join Match on Name = StadiumName
where Country1 in
(select Country
from Nationality
where Continent = 'South America')
(d) Extract the city in which the Brazil team plays the most matches.
Solutions:
Here are two solutions.
i. With a specific view:
create view BrazilStad(StadiumName,NumberOfMatches) as
select StadiumName, count(*)
from Match
where Country1 = 'Brazil' or
Country2 = 'Brazil'
group by StadiumName
select City
from Stadium
where Name in
(select StadiumName
from BrazilStad
where NumberOfMatches =
(select max(NumberOfMatches)
from BrazilStad))
Solutions:
i. First solution:
select Name
from Owner join Motorcycle on Owner.Number = Motorcycle.Number
where Name not in (select Name
from Owner join Motorcycle on
Owner.Number = Motorcycle.Number
where Nationality <> 'Japanese')
group by Name
having count(distinct Make) >= 2
150 Chapter 4
SQL
Solution:
Name((OWNER J MOTORCYCLE)
JMakeMake2Name=Name2
(QName2Name(OWNER) J QMake2Make(MOTORCYCLE)))
Name(OWNER J Nationality'Japanese'MOTORCYCLE)
(c) For each owner, highlight the tax he or she must pay for all the
motorcycles owned, taking into account that if there is more than one
owner for a motorcycle, the total tax will be equally divided among the
owners.
Solution:
create view IndTax(Number, Tax) as
select Number, Tax/count(*)
from Motorcycle join Owner
on Motorcycle.Number = Owner.Number
group by Number, Tax
select Name, sum(Tax)
from Owner join IndTax
on Owner.Number = IndTax.Number
group by Name
4.8 Bibliography
sql was first proposed by Chamberlin et al. [21] and [22]. The official
description of standard sql can be obtained from the international standards
organization iso. These documents are, indeed, very expensive and not easy
to read. However, there are a great number of books on sql-2, including
those by Cannan and Otten [12], Date and Darwen [34], and Melton and
Simon [61]. Eisenberg and Melton [36] discuss the standardization process in
general and with specific reference to the database field. Melton [60] gives an
overview of the main issues in sql-3.
Most of the manuals accompanying commercial relational systems are very
detailed and can provide a useful reference point. The manual of each system
is also essential to know which sql features have been implemented in that
particular system.
4.9 Exercises
Exercise 4.1 Order the following domains according to the maximum value
that can be represented, taking integer to have 32 bits for its representation
and smallint 16 bit: numeric(12,4), decimal(10), decimal(9), integer,
smallint, decimal(6,1).
For the foreign key constraint specify a cascade policy on deletions and set
null on updates.
Exercise 4.5 Given the schema in Exercise 4.4, explain what can happen as
a result of the execution of the following update commands:
delete from Author
where Surname = 'Russell'
update Book set FirstName = 'Umberto'
where Surname = 'Eco'
insert into Author(FirstName, Surname)
values('Isaac', 'Asimov')
update Author set FirstName = 'Emile'
where Surname = 'Zola'
152 Chapter 4
SQL
4. The exclusive composers and singers. That is, composers who have never
recorded a song and singers who have never written a song.
5. The singers on the CD that contains the largest number of songs.
6. The CDs on which all the songs are by a single singer and on which at
least three recordings are from years preceding the release year of the
CD.
7. The singers who have never recorded a song as soloists.
8. The singers who have never made a CD in which they appear as the only
singers.
9. The singers who have always recorded songs as soloists.
Exercise 4.9 Give a sequence of update commands that alter the attribute
Salary in the EMPLOYEE table, increasing by 10% the salaries below 30
thousand and decreasing by 5% those above 30 thousand.
Exercise 4.10 Define on the EMPLOYEE table the constraint that the
Administration department has fewer than 100 employees, with an average
salary higher than 40 thousand.
Exercise 4.11 Define at schema level the constraint that the maximum
salary of the employees of departments based in London is less than the
salary of all the employees in the Directors department.
Exercise 4.12 Define a view that shows for each department the average
value of the salaries higher than the average.
Exercise 4.13 Using the definition of a view, allow the user Fred to access
the contents of EMPLOYEE, excluding the Salary attribute.
Exercise 4.14 Describe the effect of the following instructions: which
authorizations are present after each instruction? (Each row is preceded by
the name of the person who issues the command.)
Stefano: grant select on Table1 to Paolo, Riccardo
with grant option
Paolo:
grant select on Table1 to Piero
Riccardo: grant select on Table1 to Piero with grant option
Stefano: revoke select on Table1 from Paolo cascade
Piero:
grant select on Table1 to Paolo
Stefano: revoke select on Table1 from Riccardo cascade
154 Chapter 4
SQL
Part II
IIDatabase design
5Design techniques
and models
158 Chapter 5
Design techniques and models
Feasibility study
Design
Implementation
Validation and
testing
Operation
160 Chapter 5
Design techniques and models
Application
requirements
Database
design
Conceptual design
CONCEPTUAL SCHEMA
Logical design
LOGICAL SCHEMA
Physical design
PHYSICAL SCHEMA
Database structure and
related documentation
162 Chapter 5
Design techniques and models
Physical design. In this phase, the logical schema is completed with the
details of the physical implementation (file organization and indexes) on a
given dbms. The product of this phase is called the physical schema, and
refers to a physical data model. This model depends on the specific
database management system chosen and takes into account the criteria
for the physical organization of the data in that system.
Let us now look at how the application requirements are used in the
various phases of design. We can distinguish between data requirements,
concerning the content of the database, and operational requirements,
concerning the use of the database by users or programs. In conceptual
design, data requirements provide most of the information, whereas
operational requirements are used only to verify that the conceptual schema
is complete. (That is, it contains the information necessary to carry out all the
operations that will be needed.) In logical design, on the other hand, the
conceptual schema, given as input, summarizes the data requirements;
whereas the operational requirements, together with the predicted
application load, are used to obtain a logical schema, which allows for the
efficient execution of such operations. Finally, in the physical design, the
logical schema and the operational requirements are used to optimize the
performance of the information system. In this phase, it is necessary to take
into account the characteristics of the particular dbms used.
The result of the design process of a database is not only the physical
schema, but also the conceptual schema and the logical schema. The
conceptual schema provides a high-level representation of the database,
which can be very useful for documentation purposes. The logical schema
provides a description of the contents of the database that, leaving aside the
implementation aspects, is useful as a reference for writing queries and
updates.
In Figure ., we show the products of the various phases in the case of the
design of a relational database based on the use of the best-known
conceptual data model, the Entity-Relationship model. An EntityRelationship schema can be represented by a diagram, which shows the
database at conceptual level. This representation is then translated into a
relational schema, made up of a collection of tables. Finally, the data is
described from a physical point of view (type and size of the fields), and
auxiliary structures, such as indexes, are specified for efficient access to data.
In the next chapters we will examine in detail the various activities of
database design described in Figure . and with reference to the models
used in Figure .. Before we begin, we will present, in Section ., the
Entity-Relationship model, which is recognized as the standard conceptual
data model for database design. Conceptual design, which we will discuss in
the next chapter, is based on this model. During logical design we will use
the relational data model, which, as we have seen in earlier chapters, is the
most widely used by current dbmss.
Conceptual design
Logical design
Physical design
Figure 5.3 The products of the various phases of the design of a relational
database with the Entity-Relationship model.
164 Chapter 5
Design techniques and models
constructs are used to define schemas, which describe the organization and
dictate which the legal occurrences1 of data are, that is, the actual values that
the database could hold at different times.
In the table in Figure . all the constructs that the e-r model provides are
listed: note that, for every construct, there is a corresponding graphical
Construct
Graphical representation
Entity
Relationship
Simple attribute
Composite attribute
Cardinality of a
Cardinality of an
attribute
(m1,M1)
(m2,M2)
(m,M)
Internal identifier
External identifier
Generalization
Subset
Figure 5.4 The constructs of the e-r model and their graphical
representation.
1. The term instance is generally used rather than occurrence, but we have
preferred to use occurrence here in order to avoid confusion with the
concept of instance (set of tuples) used in the relational model.
EMPLOYEE
DEPARTMENT
CITY
SALE
Figure 5.5 Examples of entity of the e-r model.
Relationships These represent logical links between two or more entities.
RESIDENCE is an example of a relationship that can exist between the entities
CITY and EMPLOYEE, whereas EXAM is an example of a relationship that can
exist between the entities STUDENT and COURSE. An occurrence of a
relationship is an n-tuple (a pair in the most frequent case of the binary
166 Chapter 5
Design techniques and models
S1
C1
S2
S3
C2
e4
e5
S4
S5
Student
C3
e6
Course
STUDENT
EXAM
COURSE
WORKPLACE
EMPLOYEE
RESIDENCE
CITY
COLLEAGUE
EMPLOYEE
SUCCESSION
Predecessor
SOVEREIGN Successor
SUPPLIER
SUPPLY
PRODUCT
DEPARTMENT
Figure 5.9 Example of a ternary relationship in the e-r model.
168 Chapter 5
Design techniques and models
S1
P1
s1
S2
P2
P3
S3
s2
P4
s3
P5
S4
s4
Supplier
Product
D1
D2
D3
D4
Department
Date
Mark
Number
Name
STUDENT
EXAM
COURSE
EnrolmentDate
Year
WORKPLACE
Name
Surname
Salary
EMPLOYEE
BIRTHPLACE
CITY
Age
NumberOf
Inhabitants
DateOfBirth
PERSON
Surname
Age
Sex
Address
Street
HouseNumber
PostCode
related attributes, for example, all clinical exams of a patient, omitting their
detailed description.
Schemas with basic constructs The three constructs of the e-r model
seen up to now already allow us to define schemas of a certain level of
complexity. Consider for example the e-r schema shown in Figure .. It
represents some information concerning a firm with several branches.
Beginning with the entity BRANCH and continuing in an anti-clockwise
direction, we can see that each branch of the firm is situated in a city and has
an individual address (attributes City and Address). A branch is organized
into departments (COMPOSITION relationship), and each department has a
name and a telephone number (DEPARTMENT entity and related attributes).
The employees of the company belong to these departments, starting on a
particular date (MEMBERSHIP relationship and StartDate attribute) and some
employees manage such departments (MANAGEMENT relationship). For each
employee, the surname, salary and age and an identification code are shown
(EMPLOYEE entity and related attributes). The employees work on projects
beginning on a certain date (PARTICIPATION relationship and StartDate
attribute). Each project has a name, a budget and a release date ( PROJECT
entity and related attributes).
170 Chapter 5
Design techniques and models
MANAGEMENT
Code
Surname
Salary
Phone
EMPLOYEE
MEMBERSHIP
DEPARTMENT
Age
Name
StartDate
PARTICIPATION
COMPOSITION
StartDate
Name
Budget
City
PROJECT
Number
BRANCH
Address
ReleaseDate
Street
PostCode
EMPLOYEE
(1,5)
ASSIGNMENT
(0,50)
TASK
172 Chapter 5
Design techniques and models
ORDER
(0,1)
PERSON
(1,1)
TOURIST
(1,N)
SALE
RESIDENCE
RESERVATION
(1,1)
INVOICE
(0,N)
CITY
(0,N)
VOYAGE
zero in the first case, and a cardinality equal to many (N) in the second.
Figure . shows an example of an entity whose attributes have cardinality.
Due to cardinalities, we know that a person has one and only one surname,
can have a driving license (but if he or she has one, it is unique) and can have
several cars, but also none.
(0,N)
PERSON
CarRegistration
Surname
(0,1)
LicenceNumber
174 Chapter 5
Design techniques and models
Registration
AUTOMOBILE
Model
PERSON
Colour
Name
STUDENT
(1,1)
ENROLMENT
(1,N)
UNIVERSITY
Surname
City
Address
each entity must have one (internal or external) identifier, but can have
more than one. Actually, if there is more than one identifier, then the
attributes and entities involved in an identification can be optional
(minimum cardinality equal to ).
At this point we can re-examine the schema shown in Figure .,
introducing cardinality and identifiers. The resulting schema is shown in
Figure ..
(0,1)
Code
Surname
MANAGEMENT
(1,1)
(1,N)
Salary
EMPLOYEE
Age
(0,1)
MEMBERSHIP
(1,N)
(0,N)
Phone
DEPARTMENT
(1,1)
Name
StartDate
PARTICIPATION
COMPOSITION
StartDate
Name
(1,N)
Budget
(0,1)
ReleaseDate
PROJECT
(1,N)
City
BRANCH
Address
Number
Street
PostCode
176 Chapter 5
Design techniques and models
TaxCode
PERSON
Surname
Address
PROFESSIONAL
Age
MAN
WOMAN
LAWYER
ENGINEER
MaternityStatus
DOCTOR
Specialization
PEOPLE
Surname
Age
Number
Salary
WOMAN
MAN
EMPLOYEE
STUDENT
Maternity
Status
MANAGER
PROGRAMMER
Language
ANALYST
PROJECT
MANAGER
178 Chapter 5
Design techniques and models
CONSTRUCT
Minimum
Cardinality
Number
Name
Name
(0,N)
(1,1)
MEMBERSHIP
BASIC
CONSTRUCT
GENERALIZATION
CHILD
PARENT
Maximum
Cardinality
COMPOSITION
(1,1)
(1,N)
ATTRIBUTE
(0,1)
(0,N)
ENTITY
RELATIONSHIP
(0,N)
(0,N)
Maximum
Cardinality
PARTICIPATION
(1,N)
COMPOSITE
ATTRIBUTE
(2,N)
Minimum
Cardinality
Figure 5.22 Description of the e-r model using the e-r model.
We can see that the model is made up of a series of constructs of which two
are considered basic: the entity and the relationship. An entity can
participate in zero or more relationships, whereas a relationship involves two
or more entities. The participation of an entity in a relationship has a
minimum and a maximum cardinality. The other constructs of the model are
attributes and generalizations. An attribute has a name and a minimum and
maximum cardinality, and belongs to a basic concept, that is, to an entity or
a relationship. Due to the generalization, the relationship MEMBERSHIP is
inherited by the children of the entity BASICCONSTRUCT. Composite attributes
are a specialization of attributes and are made up of one or more attributes.
According to the cardinalities, a generalization has exactly one parent entity
and one (in the case of subsets) or more child entities. An entity can be parent
or child of many generalizations (and also of none). Finally, note that a basic
construct is identified unambiguously by its name (in fact, it is essential in a
schema to avoid using the same name for different entities or relationships).
Two attributes can have the same name if they belong to different
relationships or entities as described by the external identification. (See for
example the attribute Name in Figure ..) Generalizations do not usually
have a name and to identify them we assume that they are numbered.
There exist, finally, other constraints on the use of constructs that cannot
be expressed in the schema. For example, a hierarchy of generalizations
cannot contain cycles. Also, a minimum cardinality must be less than or
equal to the related maximum cardinality. The problem of documentation of
constraints that can not be expressed in the e-r model will be discussed more
fully in the next section.
We will conclude the section with a consideration of a general nature. e-r
schemas provide an abstract representation of the data involved in an
application, and therefore they can be used for purposes other than database
design. There are various examples of the possible use of conceptual schemas
apart from design.
e-r schemas can be used for documentation purposes, as non-specialists
easily understand them.
They can be used to describe the data of an information system already in
existence (for example, to integrate it with other databases). In the case of
a system made up of heterogeneous subsystems, there is the advantage
that the e-r schema can represent the various components with an
abstract language, which is therefore unifying.
In the case of modification of the requirements of an application, e-r
schemas can be used to understand which portion of the system must be
changed and what modifications are to be carried out.
180 Chapter 5
Design techniques and models
while the e-r model is sufficiently expressive to represent data, it does not
provide suitable means for the representation of complex constraints on data.
Thus, it is indispensable to provide every e-r schema with support
documentation, which can facilitate the interpretation of the schema itself
and describe properties of the data that cannot be expressed directly by the
constructs of the model. In the next sections we will therefore describe
structures and techniques for the documentation of an e-r schema. The
concepts introduced should not be taken as new representation constructs,
but simply as tools suitable for completion and enrichment of the description
of the data in an application created using a conceptual model. They should
therefore be considered as a support for the conceptual analysis and cannot
be a substitute for it.
The rules that describe integrity constraints and derivations, on the other
hand, lend themselves more to formal definitions and rather complex syntax
has been proposed for expressing them. Given, however, that there are no
standards and that every formalization runs the risk of not being sufficiently
expressive, we will continue to use definitions in natural language, taking
care, however, to structure such definitions in the appropriate manner.
In particular, integrity constraints can always be expressed in the form of
assertions, that is, statements that must always be satisfied in our database.
For reasons of clarity and ease of creation, such statements must be atomic.
That is, they cannot be decomposed into phrases that constitute assertions
in themselves. Furthermore, since they are used to document an e-r schema,
assertions should be stated in a declarative manner, which should not
suggest ways to satisfy them, because such suggestion would give insights
on the implementation, but this is not relevant to conceptual representation.
For this reason, a notation of the type if <condition> then <action> is not
suitable to express business rules, when they document an e-r schema. An
appropriate structure to state business rules in the form of assertions could
be the following:
<concept> must/must not <expression on concepts>
where the concepts cited can correspond either to concepts of the e-r schema
to which it refers, or to concepts that can be derived from them. For example,
returning to the example given for the schema in Figure ., business rules
that express integrity constraints are:
(br)
(br2) an employee must not have a salary greater than that of the manager
of the department to which he or she belongs;
(br3) a department of the Rome branch must be managed by an employee
with more than years employment with the company.
Note how concepts such as department manager and employee with
more than years employment with the company are not represented
directly on the schema, but can nonetheless be retrieved from it.
Let us now consider the business rules that describe derivations. These
rules can be expressed by specifying the operations (arithmetical or
otherwise) that allow us to obtain the derived concept. A possible structure
is thus:
<concept> is obtained by <operations on concepts>
For example, if, in our example, the entity DEPARTMENT has an attribute
NumberOfEmployees, there could be a rule of the type:
(br4) the number of employees in a department is obtained by counting the
employees who belong to it
182 Chapter 5
Design techniques and models
Entity
Description
Attributes
Identifier
EMPLOYEE
Code
Name, Budget,
ReleaseDate
Name
City
Relationship
Description
Entities involved
Attributes
MANAGEMENT
Employee (0,1)
Department (1,1)
PROJECT
DEPARTMENT
BRANCH
MEMBERSHIP
PARTICIPATION
COMPOSITION
Phone, Name
Name,
BRANCH
Employee (0,1)
Department (1,N)
StartDate
Employee (0,N)
Project (1,N)
StartDate
Department (1,1)
Branch (1,N)
(br5) The budget for a project is obtained by multiplying the sum of the
salaries of the employees who are working on it by .
Figure 5.24 Business rules of the schema in Figure ..
5.4 Bibliography
There are many texts on information system development and, more in
general, on software engineering, among which we mention Davis [],
Fairly [], Senn [] and Pressman []. The organization of the database
design process in four phases (conceptual, logical, and physical design,
preceded by requirement analysis) was proposed by Lum et al. [] as the
184 Chapter 5
Design techniques and models
5.5 Exercises
Exercise 5.1 Consider the e-r schema in Figure .: the schema represents
various properties of men and women.
BIRTHPLACE
RESIDENCE
PERSON
Surname
Firstname
Name
CITY
County
State
BIRTHPLACE
SERVICE
MAN
MILITARY
SERVICE
Age
Height
Age
Age
WOMAN
WORKER
Exercise 5.2 Add the minimum and maximum cardinalities and the
identifiers to the schema produced in Exercise .. State whether there are
integrity constraints on the schema that cannot be expressed by the EntityRelationship model.
Exercise 5.3 Represent the following, using the constructs of the EntityRelationship model.
. In a zoological garden there are animals belonging to a species and
having a certain age; each species is situated in a sector (having a name)
of the zoo.
. An automobile rental firm has a vehicle pool, in which each automobile
has a registration number and a colour and belongs to one category; for
each category, there is a rental tariff.
. A company produces cds with a code and a title; each cd has been
recorded by one or more singers, each of whom has a name and an
address and some of whom have a stage name.
Exercise 5.4 Complete the schemas produced in the exercise above with
further information based on reasonable assumptions about each one.
Exercise 5.5 Show the following concepts, using, where appropriate, the
generalization construct of the Entity-Relationship model. Indicate, in each
case, the attributes of the various entities and the type of generalization,
resolving the cases of overlapping.
. The employees of a company are divided into managers, programmers,
analysts, project leaders and secretaries. There are analysts who are also
programmers. The project leader must be a manager. Each employee has a
code, a name and a surname. Each category of employee has its own
basic salary. Each employee, apart from the managers, has fixed working
hours.
. A French airline offers flights, each of which has a number that identifies
the flight (for example ParisBoston), a date ( March ), a departure
time (:) and an arrival time (:), a departure airport and a
destination airport. There are national and international flights. The
international flights can have one or more stopovers. For completed
flights, information to be recorded is the actual time of departure and
arrival (for example with reference to the flight given above, : and
:). For future flights, the number of seats available must be known.
. An automobile company produces cars, motor cycles, lorries and
tractors. The vehicles are identified by a chassis number and have a name
(for example Punto), a cylinder capacity and a colour. The cars are sub-
186 Chapter 5
Design techniques and models
REFEREE
Day
Series
Number
Surname
City
Region
(1,N)
Month Year
REFEREEING
Number
Date
DAY
(1,1)
(1,N)
(1,1)
Result
MATCH
PLACING
(1,1)
HOME
(1,1)
(1,N)
VISITING
(1,N)
POSITION
NEUTRAL
GROUND
POSTPONED
Reason City
Date
(1,N)
TEAM
(1,N)
City
(1,N)
Trainer
Points
Name
CONTRACT
(1,1)
Day
Month
Year
SSN
FirstName
Surname
Position
PLAYER
(0,N)
PARTICIPATION
(1,N)
DateOfBirth
BirthPlace
Position
. If a team plays a match at home, then it is the visiting team in the next
match.
Produce the complete documentation for such a schema.
Exercise 5.8 Modify the Entity-Relationship schema in Figure . so as to
describe also the past contracts between players and teams with the date of
beginning and end of the contracts and the main position played by each
player in each team. It is possible that a player can have different contracts
with the same team in different periods. For current contracts we wish to
know the date of commencement.
Exercise 5.9 In each of the following cases, reference is made to two or more
entities defined in an Entity-Relationship schema and to a concept that
involves them. Specify the schemas, defining the constructs (one or more
relationships and, if necessary, further entities with the relative identifier)
necessary to represent the concept, maintaining the entities indicated.
. Entities: sport, country and surface. Concept: the fact that in one
country a sport is practised on a certain surface (for example, tennis is
played on grass in England and in Australia, on red shale in Italy and
France, on Astroturf in the usa, Italy and France; soccer on grass in
Italy, on grass and Astroturf in the usa, on grass in England).
. Entities: scientist and department. Concept: the fact that the scientist has
held seminars in the department. For every seminar, it is necessary to
show the date, time and title, with the constraint that a scientist cannot
hold more than one seminar in one day.
. Entities: professional and company. Concept: the fact that the
professional has been a consultant for the company. It is necessary to
show the number of consulting sessions held by the professional for each
company, with the total cost.
Exercise 5.10 Consider a ternary relationship involving the entities
EMPLOYEE, PROJECT and CONSULTANT. Show in which of the following cases it
188 Chapter 5
Design techniques and models
6Conceptual design
The conceptual design of a database consists of the construction of an EntityRelationship schema, providing an optimal description of the user
requirements. Even for simple applications, the schema can contain many
concepts, correlated in a quite complex way. It follows that the construction
of the final schema is, often, an incremental process. The conceptual schema
is refined and enriched during a series of transformations and possibly
corrections. In this chapter, we describe the strategies that can be followed
during the development of a conceptual schema.
Before we begin to discuss these strategies, it is worth devoting some
attention to the activity that precedes the actual design process itself: the
collection and analysis of the requirements. This phase is not entirely
separate from design, but often tends to overlap with it. The construction of
an e-r schema begins well before the collection of requirements is completed
and it can influence further collection activity.
The discussion of requirements collection and analysis is followed by a
presentation of some general criteria for transforming informal requirements
into Entity-Relationship constructs. We then move on to illustrate the most
important design strategies and to analyze the qualities that a well-designed
conceptual schema must possess. We close the chapter with a comprehensive
method for conceptual design. To give a better explanation of the various
aspects, we use a practical example, which refers to the design of an
application for a training company.
190 Chapter 6
Conceptual design
Training Company
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
192 Chapter 6
Conceptual design
Term
Description
Trainee
Synonym
Links
Participant Course,
Company
Tutor
Course
Seminar
Instructor,
Trainee
Trainee
Once the various ambiguities and inaccuracies have been identified, they
can be eliminated by substituting more appropriate terms for the incorrect
ones. Where doubt remains, the user who provided the information must be
re-interviewed, or the particular document must be consulted again.
Let us look at the main modifications to be made to our text. First, all the
instances of course participant must be replaced by trainee. Then, as we have
said, place of birth (lines & ) should be substituted by town of birth.
Moreover, we have to make explicit that address and telephone number (line
) refer to the employers of the trainees. The terms period (line ) and
assessment (line ), must be replaced by start date and end date and marks out
of ten, respectively. It must also be clear that a trainee does not attend
seminars (line ) but rather editions of courses. Also, the term place (line )
must be replaced by classroom, title (line ) by professional title, and tutor
(line ) by instructor. Finally, title of course (line ) is a homonym of
professional title of a trainee. In this case, we could replace title of course by
name of course.
At this point, we can rewrite our specifications using the suggested
modifications. It is very useful, in this phase, to break down the text into
groups of homogenous phrases, referring to the same concept. In this way,
we can begin to impose a structure on the data requirements as shown in
Figure ..
Of course, as well as the specification of the data itself, we also need to
specify the operations to be executed on this data. For this, we must use the
same terminology as that chosen for the data and we must find out how often
the various operations will need to be carried out. As we shall see, this
knowledge will be important in the logical design phase. For our application,
the operations on the data could be as follows:
operation : insert a new trainee including all his or her data (to be
carried out approximately times a day);
operation : assign a trainee to an edition of a course ( times a day);
operation : insert a new instructor, including all his or her data and the
courses he or she is qualified to teach (twice a day);
operation : assign a qualified instructor to an edition of a course (
times a day);
operation : display all the information on the past editions of a course
with title, class timetables and number of trainees ( times a day);
operation : display all the courses offered, with information on the
instructors who are qualified to teach them ( times a day);
operation : for each instructor, find the trainees for all the courses he or
she is teaching or has taught ( times a week);
operation : carry out a statistical analysis of all the trainees with all the
information about them, about the editions of courses they have attended
and the marks obtained ( times a month).
194 Chapter 6
Conceptual design
many different ways. This is because two designers can have different
perceptions of the same situation. We can however establish some general
guidelines for the e-r model. It is recommended, overall, that the conceptual
rules of the model should be followed.
If a concept has significant properties and/or describes classes of objects with
an autonomous existence, it is appropriate to represent it by an entity. For
example, referring to the specifications for the training company above, it
makes sense to represent the concept of instructor with an entity, because
it possesses various properties (surname, age, town of birth) and its
existence is independent of the other concepts.
If a concept has a simple structure, and has no relevant properties associated
with it, it is convenient to represent it by an attribute of another concept to
which it refers. For example, the concept of age can certainly be
represented as an attribute of the trainee. In general, the concept of town
could have significant properties. In our application, however, it is better
to model it as an attribute, because, apart from the name, none of its other
properties is of interest to us.
If the requirements contain a concept that provides a logical link between
two (or more) entities, this concept can be represented by a relationship. For
example, in our application, the concept of attending a course can
certainly be represented by a relationship between the entities that
represent the trainees and the editions of courses. It must be stressed that
this is valid only in the situation in which the concept does not, itself,
have the characteristics of the entity. A typical example, which we have
already mentioned in Section .., is the concept of exam relating to
students and courses. This concept can be represented by a relationship
between student and course if the aspect of interest is, say, only the
individual student marks. Suppose however, that we are also interested in
the date, the location and the examining board, and, above all, that we
wish to represent the fact that a student can take an exam more than once
for the same course. In this case the exam must be represented by an
entity linked by one-to-many relationships to the entities representing
students and courses.
If one or more concepts are particular cases of another concept, it is
convenient to represent them by means of a generalization. In our
application, it is obvious that the concepts of professional and employee
constitute particular examples of the concept of trainee and it is therefore
a good idea to define a generalization between the entities that represent
these concepts.
These criteria are valid in general, and are thus independent of the chosen
design strategy. In fact, for every strategy, it is eventually necessary to
translate a specification into an e-r construct.
196 Chapter 6
Conceptual design
Initial schema
Refinement
Intermediate schema
Refinement
Intermediate schema
Refinement
Final schema
Transformation
Initial concept
T1
From one entity to two
entities and a
relationship between
them
T2
From one entity to a
generalization
T3
From one relationship
to multiple
relationships
T4
From one relationship
to an entity with
relationships
T5
Adding attributes to an
entity
T6
Adding attributes to a
relationship
Result
198 Chapter 6
Conceptual design
Specifications
Component 1
. . . . . . . . .
Schema 1,1
. . .
Schema 1,m
Component n
Schema n,1
. . .
Schema n,p
Integration of schemas
Final schema
In this case also, the final schema is obtained by means of some elementary
transformations, here called bottom-up transformation primitives. In
Figure ., the main bottom-up transformation primitives are shown. As we
can see, these primitives introduce into the schema new concepts that were
not present previously and are capable of describing aspects of the
application that have not been taken into account before.
Transformation T: this is applied when a class of objects with properties
in common is identified in the specifications. For example, in the application
for the training company, this can happen when we identify the entity
COURSECLASS (held in a certain classroom at a certain time) from the course
specification.
Transformation T: this is applied when a logical link between two entities
is identified in the specifications. In our application, this can happen when
200 Chapter 6
Conceptual design
Transformation
Initial concept
Result
T1
Generation of
an entity
T2
Generation of a
relationship
T3
Generation of a
generalization
T4
Aggregation of
attributes on
an entity
T5
Aggregation of
attributes on a
relationship
(0,N)
Code
MANAGEMENT
(1,1)
Surname
Salary
Phone
EMPLOYEE
Age
(0,1)
MEMBERSHIP
(0,N)
StartDate
PARTICIPATION
Name
(1,N)
Budget
PROJECT
(1,N)
DEPARTMENT
(1,1)
Name
COMPOSITION
StartDate
(1,N)
City
Number
BRANCH
Address
ReleaseDate
Street
PostCode
202 Chapter 6
Conceptual design
TRAINEE
A TTENDANCE
COURSE
T EACHING
INSTRUCTOR
represent the parts of the specification of the database that are not yet
represented. It must also be stated that, in almost all practical cases, the
mixed strategy is the only one that can actually be adopted. In fact, it is often
necessary to begin the design before all the data requirements are available,
and of the data that is known, our knowledge of detail can vary.
204 Chapter 6
Conceptual design
necessary
necessary
necessary
necessary
Note that if Step and Step are omitted, and if in Step only refinements
are made, we have a pure top-down strategy. Conversely, if the basic step is
not carried out and if in the iterative step only new concepts are added, we
are using a pure bottom-up strategy. Finally, in the bottom-up
transformations, we can proceed according to the inside-out strategy.
In the method shown, we have only briefly mentioned an important
activity that accompanies every design phase: the documentation of the
schemas. As we said in Section ., this activity should also be suitably
organized. Specifically, it is very useful to construct, in parallel to the
development of a schema, a data dictionary that makes the interpretation of
the various concepts easier. Furthermore, we can use business rules to
describe the presence of redundancies or requirements of the application
that we are not able to translate into constructs of the e-r model.
Finally, a few comments on the analysis of the quality of the design are
needed. First, it must be stated that quality analysis should not be relegated
to the end of the conceptual design: indeed this is a parallel activity, which
should be carried out regularly during the development of a conceptual
schema. Furthermore, it is very delicate, since it often requires restructuring
to be carried out in order to remedy errors made in previous phases. It is
necessary to pay particular attention to concepts of the schema that have
particular properties: for example, entities without attributes, groups of
concepts that form cycles, over-complex generalization hierarchies or
particularly tortuous portions of the schema. As we mentioned in
Section ., this analysis does not automatically mean the necessity for
restructuring, but only a re-organization of the schema to make it more
readable.
206 Chapter 6
Conceptual design
EMPLOYER
StartDate
(0,N)
EndDate
(0,N)
StartDate
PAST
CURRENT
EMPLOYMENT
EMPLOYMENT
SSN
(1,1)
Surname
Sex
Code
Age
(0,N)
TRAINEE
TownOfBirth
EMPLOYEE
PROFESSIONAL
(0,1)
Position
Level
ProfessionalTitle
Expertise
TownOfBirth
INSTRUCTOR
FREELANCE
PERMANENT
Time
Room
CLASS
Date
(1,1)
NoOfPart
COMPOSTION
(1,N)
EndDate
COURSE
EDITION
Name
StartDate
(1,1)
TYPE
(0,N)
Code
COURSE
208 Chapter 6
Conceptual design
Name
Phone
CLASS
EMPLOYER
StartDate EndDate
(0,N)
(0,N)
CURRENT
PAST
EMPLOYMENT
(1,1)
StartDate
EMPLOYMENT
Surname
Sex
SSN
Marks
(0,N)
(0,N)
PAST
ATTENDANCE
(0,N)
(1,1)
COMPOSITION
PAST
TEACHING
SSN Surname
(1,N)
Phone
Age
(1,N)
(0,1)
TRAINEE
Code
(0,N)
CURRENT
ATTENDANCE
Age
TownOfBirth
COURSE
EDITION
NoOfPart
(1,1)
EMPLOYEE
PROFESSIONAL
(0,1)
Level ProfessionalTitle
Expertise
(0,1)
(0,1)
CURRENT
TEACHING
EndDate
StartDate (1,N)
TYPE
Position
(0,N)
(0,1)
INSTRUCTOR
TownOfBirth
(1,N)
QUALIFICATION
(0,N)
COURSE
Name
FREELANCE
PERMANENT
Code
Figure 6.13 The final e-r schema for the training company.
Note that we have worked in this case by decomposing and then
integrating. However, given that we are dealing with a relatively simple
schema, we could also have worked directly on the skeleton schema,
gradually refining it without integration steps.
At this point, we have to verify the properties of the schema thus obtained.
In particular, the completeness is verified by going back over all the
requirements and checking that all the data is represented and that the
210 Chapter 6
Conceptual design
the notations used, nor of the conceptual model, and each system
substantially adopts a specific version of the e-r model. There are, for
example, systems that manage generalizations of only a certain type, others
that do not manage them at all, still others that manage only binary
relationships. Furthermore, in practice, all the products use different
notations to represent the same constructs. Thus, considerable effort is often
required on the part of the designer to adapt his or her own personal
knowledge of models and techniques to the characteristics of the chosen
product.
6.8 Bibliography
Conceptual data design is dealt with in detail in the books by Batini, Ceri and
Navathe [] and by Teorey []. Batini, Ceri and Navathe also discuss the
problem of the integration of schemas. We also mention two interesting texts
reporting on the dataid project, which has developed many aspects of
database design, by Albano, De Antonellis and Di Leva [] and Ceri []. Our
description of the activities of collection and analysis of requirements is
based on the results of this project.
An in-depth review written by David Reiner on case tools for database
design is given in Chapter of Batini, Ceri and Navathe [].
6.9 Exercises
Exercise 6.1 We wish to automate the management of loans in a library. The
specification of the application, acquired through an interview with the
librarian, is shown in Figure .. Analyze the specifications, filter the
The Library
A reader who uses the library has an identity card on which is written
his or her code, name and address. The user makes requests for the loan
of books catalogued in the library. Each book has a title and a list of
authors and there can be many copies of any book. Each book in the
library is identified by a code. Following a request, the archive of
available books is first consulted (that is, those not out on loan at
present). If the book is available, we look for the book on the shelves.
Once the book is found it is given to the reader. The text is then classified
as one on loan. When the reader has finished, the book is returned, put
back on the shelves and re-classified as available. For each loan the
times and dates of taking out and returning are recorded.
Figure 6.15 The library specification for Exercise ..
ambiguities and then group them according to type. Pay particular
attention to the difference between the concept of book and copy of book.
212 Chapter 6
Conceptual design
Identify the logical links between the various groups of specifications thus
obtained.
Exercise 6.2 Represent the specifications of the previous exercise using an
e-r model schema.
Exercise 6.3 Define an Entity-Relationship schema that describes the data
of an application relating to a chain of automobile workshops. We are
interested in:
the workshops, with name (identifying), address and telephone number;
the automobiles, with registration number (identifying), and model (a
string of characters with no further structure) and owner;
the customers (automobile owners), with social security number,
surname, first name and telephone; each client can be the owner of more
than one automobile;
the maintenance work carried out in a workshop, with a number (unique
in a particular workshop), start date and end date, parts replaced (with
quantities) and number of hours labour;
the spare parts available with code, name and unit cost.
Show the cardinalities of the relationships and (at least) one identifier for
each entity.
Exercise 6.4 Define an e-r schema that describes the data of an application
relating to the electoral roll of the city of WhoKnowsWhere, with citizens
and families. The following are stored:
information on the citizens born in the area and on those resident in the
area; each citizen is identified by a social security number, and has
surname, first name, sex and date of birth; furthermore:
for
for anyone born in another area, the city and state of birth are stored.
information on resident families, each of which has one and one only head
of the family and zero or more other members; for each of them, the
relationship to the head of the family is recorded (spouse, child, parent or
other); each resident citizen belongs to one and one family only; all the
members of a family have the same residence (street, street number,
apartment number).
Try to use the inside-out strategy. At the end, verify the quality of the
schema obtained.
214 Chapter 6
Conceptual design
216 Chapter 6
Conceptual design
7Logical design
The aim of logical design is to construct a logical schema that correctly and
efficiently represents all of the information described by an EntityRelationship schema produced during the conceptual design phase. This is
not just a simple translation from one model to another for two reasons. First,
there is not a close correspondence between the models involved because not
all the constructs of the Entity-Relationship model can be translated
naturally into the relational model. For example, while an entity can easily
be represented by a relation, there are various options for the
generalizations. Secondly, the aim of conceptual design is to represent the
data accurately and naturally from a high-level, computer-independent
point of view. Logical design is instead the basis for the actual
implementation of the application, and must take into account, as far as
possible, the performance of the final product. The schema must therefore be
restructured in such a way as to make the execution of the projected
operations as efficient as possible. In sum, we must plan a task that is not only
a translation (from the conceptual model to the logical) but also a
reorganization. Since the reorganization can for the most part be dealt with
independently of the logical model, it is usually helpful to divide the logical
design into two steps, as shown in Figure 7.1.
Restructuring of the Entity-Relationship schema, which is
independent of the chosen logical model and is based on criteria for the
optimization of the schema and the simplification of the following step.
Translation into the logical model, which refers to a specific logical
model (in our case, the relational model) and can include a further
optimization, based on the features of the logical model itself.
The input for the first step is the conceptual schema produced in the
preceding phase and the estimated database load, in terms of the amount of
data and the operational requirements. The result obtained is a restructured
e-r schema, which is no longer a conceptual schema in the strict sense of the
218 Chapter 7
Logical design
Conceptual
schema
Database
load
Logical
model
Logical design
Restructuring of
the E-R schema
Translation to a
logical schema
Logical schema
Integrity
constraints
Logical
schema
Support
documentation
term, in that it constitutes a representation of the data that takes into account
implementation aspects.
This schema and the chosen logical model constitute the input of the
second step, which produces the logical schema of our database. In this step,
we can also carry out quality controls on the schema and, possibly, further
optimizations based on the characteristics of the logical model. An analysis
technique used for the relational model (called normalization) will be
presented separately in the next chapter. The final logical schema, the
integrity constraints defined on it and the relevant documentation,
constitute the final product of logical design.
In the remainder of this chapter, we will present the two steps that make
up the logical design of a database. We will first discuss the techniques that
can be used to analyze the efficiency of a database by referring to its
conceptual schema.
(0,1)
Code
Surname
MANAGEMENT
(1,1)
(1,N)
Salary
EMPLOYEE
Age
(0,1)
MEMBERSHIP
(1,N)
(0,N)
Phone
DEPARTMENT
(1,1)
Name
StartDate
PARTICIPATION
COMPOSITION
StartDate
Name
(1,N)
Budget
(0,1)
ReleaseDate
PROJECT
(1,N)
City
BRANCH
Address
Number
Street
PostCode
220 Chapter 7
Logical design
Table of operations
Operation Type Frequency
Op 1
I
50 per day
Op 2
I
100 per day
Op 3
I
10 per day
Op 4
B
2 per week
(1,N)
EMPLOYEE
Salary
Age
(0,1)
MEMBERSHIP
(1,N)
Phone
DEPARTMENT
Name
(0,N)
StartDate
PARTICIPATION
StartDate
(1,N)
Name
Budget
(0,1)
PROJECT
ReleaseDate
222 Chapter 7
Logical design
Table of accesses
Concept
Type
Accesses Type
Employee
Entity
1
R
Employment Relation
1
R
Department Entity
1
R
Participation Relation
3
R
Project
Entity
3
R
E-R
schema
Database
load
Removing
generalizations
Partitioning/merging of
entities and relations
Selection of
primary identifiers
Restructured
E-R schema
224 Chapter 7
Logical design
NetAmount
Tax
INVOICE
GrossAmount
TotalAmount
Price
PURCHASE
(1,N)
COMPOSITION
(1,N)
PRODUCT
NumberOf
Inhabitants
PERSON
(1,1)
(0,N)
STUDENT
(0,N)
(1,N)
ATTENDANCE
RESIDENCE
TEACHING
(1,N)
TOWN
(1,N)
(1,1)
COURSE
(1,1)
ASSIGNMENT
LECTURER
Let us suppose, moreover, that for this application, the load is that shown
in Figure 7.8.
Table of volumes
Concept
Type Volume
Town
E
200
Person
E
1000000
Residence
R 1000000
Table of operations
Operation Type Frequency
Op 1
I
500 per day
Op 2
I
2 per day
Figure 7.8 Tables of volumes and operations for the schema in Figure 7.7.
Let us first try to evaluate the indicators of performance in the case of
presence of redundancy (attribute NumberOfInhabitants in the TOWN entity).
Assume that the number of inhabitants of a town requires four bytes. We
can see that the redundant data requires 4 200 = 800 bytes, that is, less than
one Kbyte of additional storage. Let us now move on to estimating the cost
of the operations. As described in the access tables in Figure 7.9, operation 1
requires the following. A write access to the PERSON entity (to add a new
Table of accesses in
presence of redundancy
Operation 1
Concept Type Acc. Type
Person
E
1
W
Residence R
1
W
Town
E
1
R
Town
E
1
W
Operation 2
Concept
Town
Table of accesses in
absence of redundancy
Operation 1
Conceto Type
Person
E
Residence R
Acc.
1
1
Type
W
W
Operation 2
Concept Type Acc.
Town
E
1
Residence R
5000
Type
R
R
Figure 7.9 Tables of accesses for the schema on electoral roll data in
Figure 7.7.
person), a write access to the RESIDENCE relationship (to add a new persontown pair) and finally a read access (to find the relevant town) and another
write access to the TOWN entity (to update the number of inhabitants of that
occurrence). This is all repeated 500 times per day, for a total of 1500 write
accesses and 500 read accesses. The cost of operation 2 is almost negligible,
as it requires a single read access to the TOWN entity to be repeated twice a
day. Supposing a write access to cost twice as much as a read access, we have
a total of 3500 accesses a day when there is redundant data.
Let us now consider what happens when there is no redundant data.
226 Chapter 7
Logical design
For operation 1 we need a write access to the PERSON entity and a write
access to the RESIDENCE relationship for a total of 1000 write accesses per day.
(There is no need to access the TOWN entity since there is no derived
information). For operation 2 however, we need a read access to the TOWN
entity (to obtain the data for the town), which we can neglect, and 5000 read
accesses to the RESIDENCE relationship on average, (obtained by dividing the
number of people by the number of towns) to calculate the number of
inhabitants of this town. This gives a total of 10000 read accesses per day.
Counting twice the write accesses, we have a total of 12000 accesses per day
when there is no redundant data. Thus, approximately 8500 accesses more
per day are required where there is no redundant data in order to save a mere
Kbyte. This depends on the fact that the read accesses needed to compute the
derived data are much more than the write accesses needed to keep the
derived data up to date.
It is obvious that, in this case, it worth maintaining the redundant data.
A 02
E0
E3
R1
A 21
A 11
E1
E2
(X,Y)
R2
E4
A 11
A 02
A 01
A 21
(0,1)
(0,1)
E0
A type
(0,Y)
R1
E3
R2
E4
R 11
E3
R 12
A 11
A 01
A 21
A 01
A 02
E1
A 11 (1,1)
E1
R2
E4
A 02
E0
R G1
(X,Y)
E2
A 01
(0,1)
A 02
E3
R1
(0,1)
R G2
(1,1) A 21
E2
(X,Y)
R2
E4
228 Chapter 7
Logical design
these factors, it would seem that Option 3 would hardly ever be suitable, as
it usually requires more accesses in order to carry out operations on the data.
This restructuring, however, has the great advantage of generating entities
with fewer attributes. As we shall see, this translates into logical and then
physical structures of small dimensions for which a physical access allows
the retrieval of a greater amount of data (tuples) at once. Therefore, in some
critical cases, a more refined analysis needs to be carried out. This might take
into account other factors, such as the quantity of data that can be retrieved
by means of a single access to secondary storage. These aspects will be
discussed in more detail in Chapter 9.
The options presented are not the only ones allowed, but it is possible to
carry out restructurings that combine them. An example is given in
Figure 7.12, which consists in another possible transformation of the schema
given in Figure 7.10. In this case, based on considerations similar to those
A type
A 01
A 02
A 11
(0,1)
E0
E3
R1
(0,1)
R G2
(1,1) A
21
E2
(X,Y)
R2
E4
230 Chapter 7
Logical design
EmployeeNumber
Name
Level
EMPLOYEE
Address
Salary
DateOfBirth
Tax
EmployeeNumber
Name
Address
DateOfBirth
PERSONAL
DATA
(1,1)
Level
EMPLOYEE
DATA
(1,1)
EMPLOYMENT
DATA
Salary
Tax
AGENCY
Town
(1,N)
Telephone
Number
Name
TELEPHONE
(1,1)
HOLDER
(1,N)
AGENCY
Addresss
Town
SSN
Name
Address
AptNumber
(0,1)
PERSON
OWNER
(1,1)
APARTMENT
AptAddress
Age
SSN
AptNumber
Name
Address
(0,1)
PERSON
(0,1)
Age
AptAddress
232 Chapter 7
Logical design
can be suggested by the fact that the most frequent operations on the PERSON
entity always require data relating to the apartment that the person
possesses. Thus, we wish to avoid the accesses necessary to retrieve this data
by means of the OWNER relationship. A side-effect of this restructuring is the
possible presence of null values because, according to the cardinalities, there
are people who do not own apartments. Therefore, there are no values for
them for the attributes APTADDRESS and APTNUMBER.
Merging is generally carried out on one-to-one relationships, rarely on
one-to-many relationships and hardly ever on many-to-many relationships.
This is because merging of entities linked by a one-to-many relationship or
a many-to-many relationship generates redundancies. In particular, it is easy
to verify that redundancies can appear in non-key attributes of the entity
that participates in the original relationship with a maximum cardinality
equal to N. We will come back to illustrate this point in Chapter 8.
Other types of partitioning and merging Partitioning and merging operations can also be applied to relationships. This can be done for two
reasons. Firstly, in order to separate occurrences of a relationship that are
always accessed separately. Secondly, to merge two (or more) relationships
between the same entities into a single relationship, when their occurrences
are always accessed together. An example of partitioning of relationship is
given in Figure 7.16 in which the current players of a basketball team are
distinguished from past players.
Name
Name
PLAYER
(1,N)
COMPOSITION
Position
(1,N)
TEAM
Town
(0,1)
DateJoined
DateLeft
DateJoined
(0,1)
PRESENT
COMPOSITION
(0,N)
Name
Name
PLAYER
TEAM
Position
Town
(0,N)
DateJoined
PAST
COMPOSITION
(0,N)
DateLeft
We should mention that the decisions about partitioning and merging can
be postponed until the physical design phase. Many of todays database
management systems allow the specification of clusters of logical structures,
that is, grouping of tables, carried out at the physical level. Clusters allow
rapid access to data distributed throughout different logical structures.
234 Chapter 7
Logical design
Surname
Salary
EMPLOYEE
(0,N)
PARTICIPATION
(0,N)
PROJECT
Budget
Code
Number
StartDate
The domain of the Employee attribute is a set of employee numbers and that
of the Project attribute is a set of project codes. There are referential
constraints between these attributes and the EMPLOYEE relation and the
PROJECT relation respectively.
Renaming is essential in some cases. For example, when we have recursive
relationships such as that in Figure 7.18.
Quantity
(0,N)
COMPOSITION
(0,N)
PRODUCT
Part
Cost
Subpart
Code
Name
In this schema, both the attributes Part and Subpart have product codes as
domain. There is in fact a referential constraint between each of them and the
PRODUCT relation.
The translation of a relationship involving more than two entities is similar
to the translation of a binary relationship. For example, consider the schema
with a ternary relationship in Figure 7.19.
Quantity
SupplierID
Code
SUPPLIER
(0,N)
(1,N)
SUPPLY
SupplierName
PRODUCT
Type
(1,N)
DEPARTMENT
Name
Telephone
236 Chapter 7
Logical design
SUPPLIER(SupplierID, SupplierName)
PRODUCT(Code, Type)
DEPARTMENT(Name, Telephone)
SUPPLY(Supplier, Product, Department, Quantity)
There are referential constraints for the schema thus obtained between the
attributes Supplier, Product, and Department of the SUPPLY relation and,
respectively, the SUPPLIER relation, the PRODUCT relation, and the DEPARTMENT
relation.
In this last type of translation, we need to verify whether the identifiers of
the entities, taken together, do not constitute a key but, instead, a redundant
superkey for the relation that represents the relationship of the e-r schema.
This can happen, for example, in the case of the schema in Figure 7.19, if
there is a sole supplier who supplies a given product to a given department.
Note that the cardinality is still valid, since this supplier can supply many
products to this or other departments. In this case, the key to the SUPPLY
relation would be made up of the attributes Product and Department only,
because, given a product and a department, the supplier is unambiguously
determined.
Name
PLAYER
(1,1)
CONTRACT
(0,N)
TEAM
Position
Town
TeamColours
Salary
Note that in the CONTRACT relation, the key consists of only the identifier
of PLAYER because the cardinalities of the relationship tell us that each player
has a contract with a single team. At this point, the relations PLAYER and
CONTRACT have the same key (the surname and the date of birth of a player)
and we can therefore merge them in a single relation with no risk of
redundancy. This is because there is a one-to-one correspondence between
the respective instances. Thus, for the schema in Figure 7.20, the following
translation is preferable, in which the PLAYER relation represents both the
entity and the relationship of the original e-r schema:
Here there are referential constraints between the attribute Supplier of the
PRODUCT relation and the SUPPLIER relation, and between the attribute
Department of the PRODUCT relation and the DEPARTMENT relation.
Name
STUDENT
(1,1)
ENROLMENT
(1,N)
Surname
UNIVERSITY
Town
Address
238 Chapter 7
Logical design
Name
HEAD
(1,1)
MANAGEMENT
(1,1)
DEPARTMENT
Number
StartDate
Telephone
Branch
With the referential constraint between the attribute Department of the HEAD
relation and the DEPARTMENT relation, or:
HEAD(Number, Name, Salary)
DEPARTMENT(Name, Telephone, Branch, Head, StartDate)
for which there is the referential constraint between the attribute Head of the
DEPARTMENT relation and the HEAD relation.
Name
EMPLOYEE
(0,1)
MANAGEMENT
(1,1)
DEPARTMENT
Number
StartDate
Telephone
Branch
for which there is the referential constraint between the attribute Head of the
DEPARTMENT relation and the EMPLOYEE relation. This option is preferable to
the one in which the relationship is represented in the EMPLOYEE relation
through the name of the department managed, because, for this attribute, we
could have null values.
Finally, consider the case in which both the entities have optional
participation. For example, assume that, in the schema in Figure 7.23, there
can be departments with no head (and thus the minimum cardinality of the
DEPARTMENT entity is equal to zero). In this case, there is a further possibility
that allows for three separate relations:
EMPLOYEE(Number, Name, Salary)
DEPARTMENT(Name, Telephone, Branch)
MANAGEMENT(Head, Department, StartDate)
Note that the key of the MANAGEMENT relation could be the attribute
Department as well. Here, we have referential constraints between the
attributes Head and Department of the MANAGEMENT relation and the EMPLOYEE
and DEPARTMENT relations, respectively.
This solution has an advantage of never having null values on the
attributes that implement the relationship. On the other hand, we need an
extra relation, with a consequent increase of the complexity of the database.
Therefore, the three-relation solution is to be considered only if the number
of occurrences of the relationship is very low compared to the occurrences of
the entities involved in the relationship. In this case, there is the advantage
of avoiding the presence of many null values.
240 Chapter 7
Logical design
AR3
A51 A52
A12
A11
E1
(1,1)
R1
(0,N)
E5
(1,N)
(1,1)
(0,1)
(0,1)
R6
A21
R3
R4
R5
(0,1)
(0,N)
E6
A61
A62
A63
(1,N)
AR5
(1,1)
A22
E2
(1,N)
AR21
AR22
R2
A31
A32
E3
(0,N)
E4
A41
A42
(0,N)
Note how E2 takes the attribute A11 and (transitively) the attribute A51,
which, together with A21, identifies E1. Some referential constraints are
defined for the relations produced (for example, there is a referential
constraint between the attribute A51 of E1 and E5).
We now move on to the translation of relationships. Relationships R1 and
R6 have already been translated because of the external identification of E2
and E1 respectively. We assume we have decided to obtain a minimum
number of relations in the final schema and we will try therefore to merge
relations where possible. We obtain the following modifications to be carried
out on the initial relations:
in order to translate R3, we introduce, with appropriate renaming, the
attributes that identify E6, among those of E5, as well as the attribute AR3
of R3. Thus, we introduce A61R3, A62R3 and AR3 in E5;
Let us note that we have obtained relations (E2 and R2) with keys
composed of many attributes. In such cases one could decide to introduce
simple keys (codes) either at this stage or earlier in the restructuring step as
discussed in Section 7.2.4.
242 Chapter 7
Logical design
Type
Initial schema
E1
Binary
many-to-many
relationship
(X,N)
A E11
A E12
AR
(X,N)
E1
E3
One-to-many
relationship with
optional
participation
(X,N)
(X,N)
A E11
A E12
AR
(X,N)
A E21
A E22
E2
A E31 A E32
One-to-many
relationship with
mandatory
participation
A E21
AE22
E2
Ternary
many-to-many
relationship
Possible translation
E1
(1,1)
R
E2
A E21
AE22
E1
A E11
A E12
(0,1)
AR
(X,N)
E2
A E11
A E12
AR
(X,N)
A E21
AE22
A E11
A E12
E1
Relationship with
external identifiers
(1,1)
R
(X,N)
E2
AR
A E21
AE22
Type
Initial schema
E1
One-to-one
relationship with
mandatory
participation
for both entities
(1,1)
Possible translation
E1( A E11 , AE12, A E21 , AR)
A E11
A E12
AR
Alternatively:
E2( A E21 , AE22, A E11 , AR)
(1,1)
One-to-one
relationship with
optional
participation
for one entity
E2
A E21
AE22
E1
A E11
A E12
(1,1)
AR
(0,1)
A E21
AE22
E2
One-to-one
relationship with
optional
participation
for both entities
(0,1)
Alternatively:
A E11
A E12
* , A R* )
E1( A E11 , AE12, A E21
E2( A E21 , AE22)
AR
(0,1)
E2
A E21
AE22
Alternatively:
E1( A E11 , AE12)
E2( A E21 , AE22)
R( A E11 , A E21 , AR)
EMPLOYEE
Number
PROJECT
Surname
Salary
Code
Name
Budget
PARTICIPATION
Employee
Project
StartDate
244 Chapter 7
Logical design
DateOfBirth
Team
Salary
Position
TEAM
Name
CONTRACT
Town
TeamColours
A52
A61R3
A62R3
R1
R5
E6
E1
R4
R6
E2
A21 A11 A51 A22
R2
A21
A11
E3
A31 A32
A51
A31
A41
AR21
AR22
E4
A41 A42
Address
Name
Phone
CLASS
EMPLOYER
(0,N)
StartDate EndDate
CURRENT
PAST
EMPLOYMENT
(1,1)
(0,N)
StartDate
EMPLOYMENT
Surname
Sex
SSN
Marks
(0,N)
(0,N)
PAST
ATTENDANCE
(0,N)
(1,1)
COMPOSITION
PAST
TEACHING
SSN Surname
(1,N)
Phone
Age
(1,N)
(0,1)
TRAINEE
Code
(0,N)
CURRENT
ATTENDANCE
Age
TownOfBirth
COURSE
EDITION
NoOfPart
(1,1)
EMPLOYEE
PROFESSIONAL
(0,1)
Level ProfessionalTitle
Expertise
(0,1)
(0,1)
CURRENT
TEACHING
EndDate
StartDate (1,N)
TYPE
Position
(0,N)
(0,1)
INSTRUCTOR
TownOfBirth
(1,N)
QUALIFICATION
(0,N)
COURSE
Name
FREELANCE
PERMANENT
Code
246 Chapter 7
Logical design
operation : carry out a statistical analysis of all the trainees with all the
information on them, on the edition of courses they have attended and on
the marks obtained.
Table of operations
Operation Type
Frequency
Op 1
I
40 per day
Op 2
I
50 per day
Op 3
I
2 per day
Op 4
I
15 per day
Op 5
I
10 per day
Op 6
I
20 per day
Op 7
I
5 per day
Op 8
B
10 per month
Figure 7.31 Tables of volumes and operations for the schema in Figure 7.30.
Analysis of redundancies There is only one redundant piece of data in
the schema: the attribute NumberOfParticipants in COURSEEDITION, which can
be derived from the relationships CURRENTATTENDANCE and PASTATTENDANCE.
The storage requirement is 4 1000 = 4000 bytes, having assumed that four
bytes are necessary for every occurrence of COURSEEDITION to store the
number of participants. The operations involved with this information are 2,
5 and 8. The last of these can be left out because it deals with an infrequent
operation that is carried out in batch mode. We will therefore evaluate the
cost of operations 2 and 5 in the cases of the presence or absence of
redundant data. We can deduce from the table of volumes that each edition
of the course has, on average, eight classes and 10 participants. From this data
we can easily derive the access tables shown in Figure 7.32.
248 Chapter 7
Logical design
therefore, leave the entities EMPLOYEE and PROFESSIONAL, adding two one-toone relationships between these entities and the TRAINEE entity. In this way,
we can avoid having attributes with possible null values on the parent entity
of the generalization and we can reduce the dimension of the relations. The
result of the restructuring can be seen in the schema in Figure 7.33.
Partitioning and merging of concepts From the analysis of data and
operations, many potential restructurings of this type can be identified. The
first relates to the COURSEEDITION entity. We can see that operation 5 relates
only to the past editions and that the relationships PASTTEACHING and
PASTATTENDANCE refer only to these editions of the course. Thus, in order to
make the above operation more efficient, we could decompose the entity
horizontally to distinguish the current editions from the past ones. The
disadvantage of this choice, however, is that the relationships COMPOSITION
and TYPE would be duplicated. Furthermore, operations 7 and 8 do not make
great distinctions between current editions and past ones and would be more
expensive, because they would require visits to two distinct entities.
Therefore, we will not partition this entity.
Two other possible restructurings that we could consider are the merging
of the relationships PASTTEACHING and PRESENTTEACHING and the similar
relationships PASTATTENDANCE and PRESENTATTENDANCE. In both cases, we are
dealing with two similar concepts between which some operations make no
difference (7 and 8). The merging of these relationships would produce
another advantage: it would no longer be necessary to transfer occurrences
from one relationship to another at the end of a course edition. A negative
factor is the presence of the attribute Mark, which does not apply to the
current editions and could thus produce null values. For the rest, the table
of volumes tells us that the estimated number of occurrences of the
CURRENTATTENDANCE relationship is 500. Therefore, supposing that we need
four bytes to store the marks, the waste of storage would be only two Kbytes.
We can decide therefore to merge the two pairs of relationships as described
in Figure 7.33. We must add a constraint that is not expressible by the
schema, which requires that an instructor cannot teach more than one
edition of a course in any one period. Similarly, a participant cannot attend
more than one edition of a course at a particular time.
Finally, we need to remove the multi-valued attribute Telephone from the
INSTRUCTOR entity. To do this we must introduce a new entity TELEPHONE
linked by a one-to-many relationship with the INSTRUCTOR entity, from which
the attribute will be removed.
It is interesting to note that some decisions made in this phase reverse, in
some way, decisions made during the conceptual design phase. This is not
surprising however: the aim of conceptual design is merely to represent the
requirements in the best way possible, without considering the efficiency of
the application. In logical design we must instead try to optimize the
performance and re-examining earlier decisions is inevitable.
Name
Phone
Number
CLASS
TELEPHONE
EMPLOYER
StartDate EndDate
(0,N)
(0,N)
CURRENT
HOLDER
COMPOSITION
EMPLOYMENT
Surname
Sex
(1,1)
(1,1)
PAST
EMPLOYMENT
(1,1)
StartDate
SSN
Marks
(0,N)
Code
(1,N)
SSN
Surname
(1,N)
(0,1)
(0,N)
TRAINEE
Code
(1,N)
(1,N)
ATTENDANCE
Age
TownOfBirth
COURSE
EDITION
(1,1)
(0,1)
EMPLOYMENT
DATA
(0,1)
PROFESSIONAL
DATA
(1,1)
(1,1)
EMPLOYEE
PROFESSIONAL
(1,1)
(0,N)
EndDate
StartDate (1,N)
TYPE
Age
INSTRUCTOR
TEACHING
TownOfBirth
(1,N)
Type
QUALIFICATION
(0,N)
COURSE
(0,1)
Position
Level
ProfessionalTitle
Expertise
Name
Code
Figure 7.33 The e-r schema of Figure 7.30 after the restructuring phase.
250 Chapter 7
Logical design
TELEPHONE(Number, Instructor)
COURSE(Code, Name)
QUALIFICATION (Course, Instructor)
TRAINEE (Code, SSN, Surname, Age, TownOfBirth, Sex)
ATTENDANCE(Trainee, Edition, Marks*)
EMPLOYER(Name, Address, Telephone)
PASTEMPLOYMENT(Trainee, Employer, StartDate, EndDate)
PROFESSIONAL(Trainee, Expertise, ProfessionalTitle*)
EMPLOYEE(Trainee, Level, Position, Employer, StartDate)
7.6 Bibliography
Logical design is covered in detail in the books by Batini, Ceri and
Navathe [7], Teorey [84] and Teorey and Fry [85]. The problem of translating
an e-r schema into the relational model is discussed in the original paper by
Chen [23] and in a paper by Teorey, Yang and Fry [86], which considers a
detailed list of cases.
7.7 Exercises
Exercise 7.1 Consider the e-r schema in Exercise 6.4. Make hypotheses on
the volume of data and on the operations possible on this data and, based
on these hypotheses, carry out the necessary restructuring of the schema.
Then carry out the translation to the relational model.
Exercise 7.2 Translate the e-r schema on the personnel of a company
(shown again for convenience in Figure 7.35) into a schema of the relational
model.
252 Chapter 7
Logical design
(0,1)
Code
Surname
MANAGEMENT
(1,1)
Phone
(1,N)
Salary
(0,1)
EMPLOYEE
Age
MEMBERSHIP
(1,N)
DEPARTMENT
(0,N)
Name
(1,1)
StartDate
PARTICIPATION
COMPOSITION
StartDate
Name
(1,N)
(1,N)
Budget
PROJECT
(0,1)
City
BRANCH
Address
ReleaseDate
Number
Street
PostCode
E1
(0,N)
(0,1)
(1,1)
R4
R5
A31
(0,N)
E3
(0,N)
(0,N)
R1
R3
A21
A22
E2
(1,1)
A41
(1,1)
(0,N)
R2
(1,1)
E4
Exercise 7.6 Take the e-r schema in Figure 7.37. Restructure the schema,
deleting the generalizations, supposing the most important operations are as
follows, each carried out 10 times per day:
A 11
A 12
E1
A 13
A 21
A 31
E2
E3
A 22
A 51
A 41
E4
E5
A 42
A 52
254 Chapter 7
Logical design
Balance
TotalBalance
AccountNumber
NumberOfAccounts
CreditLimit
ClientNumber
ACCOUNT
(1,N)
ACCOUNT
HOLDER
(1,N)
Name
CLIENT
Address
(1,N)
OPERATION
COMPANY
PERSON
(1,N)
Tax
Capital
Number
TRANSACTION
Amount
TransactionNumber
Date
Type
Volumes
Concept
Type Volume
Client
E
15000
Account
E
20000
Transaction
E
600000
Person
E
14000
Company
E
1000
AccountHolder
R
30000
Operation
R
800000
Operations
Operation Type
Frequency
Op 1
I
100 per day
Op 2
I
500 per day
Op 3
I
1000 per day
Op 4
I
2000 per day
Op 5
I
1000 per day
Op 6
I
200 per day
Op 7
B
1500 per day
Op 8
B
1 per month
Op 9
I
75 per day
Op 10
I
20 per day
Figure 7.39 Volumes and operations tables for the schema in Figure 7.38.
Carry out the logical design phase on the e-r schema, taking into account
the data provided. In the restructuring phase, keep in mind the fact that
there are two redundancies on the schema: the attribute TotalBalance and
NumberOfAccounts in the entity CLIENT. These can be derived from the
relationship ACCOUNTHOLDER and from the ACCOUNT entity.
8Normalization
256 Chapter 8
Normalization
Budget
2
15
15
15
15
2
2
15
15
15
Function
technician
designer
designer
manager
consultant
consultant
manager
designer
designer
manager
Project. We can also easily verify that the relation satisfies the following
properties:
Project, but, since Project is part of the key, this is not allowed, as we saw
in Chapter 2. This problem is known as the deletion anomaly.
258 Chapter 8
Normalization
argument can be made for the relationship between the attributes Project and
Budget because the value of the budget functionally depends on the value of
the project.
We can formalize this principle as follows. Given a relation r on a schema
R(X) and two non-empty subsets Y and Z of the attributes X, we say that
there is a functional dependency on r between Y and Z, if, for each pair of
tuples t1 and t2 of r having the same values on the attributes Y, t1 and t2 also
have the same values of the Z attributes.
A functional dependency between the attributes Y and Z is generally
indicated by the notation Y Z and, as with other integrity constraints, is
associated with a schema: a valid relation on that schema has to satisfy this
functional dependency. Given a functional dependency Y Z, we will call
Y the left hand side and that Z the right hand side of the dependency.
Returning to our example, we can thus say that on the relation in Figure 8.1
there are the functional dependencies:
Employee Salary
Project Budget
That is, two tuples with the same values on each of the attributes Employee
and Project have the same value on the attribute Project, which is one of them.
This is a trivial functional dependency because it asserts an obvious
property of the relation. Functional dependencies should instead be used to
describe significant properties of the application that we are representing.
We then say that a functional dependency Y Z is non-trivial if no attribute
in Z appears among the attributes of Y.
Note that a hybrid functional dependency of the type
Project Project Budget
with the same values on K. Referring to our example, we have said that the
attributes Employee and Project form a key. We can then affirm that, for
example, the functional dependency Employee Project Function is valid. In
particular, there will be a functional dependency between the key of a
relation and all the other attributes of the schema of the relation. In our case
we have:
Employee Project Salary Budget Function
260 Chapter 8
Normalization
cannot contain two tuples with the same values of these attributes (and thus
of the Function attribute). Also, from a conceptual point of view, we can say
that it cannot generate anomalies, because each employee has a salary (and
one only) and each project has a budget (and one only), and thus for each
employee-project pair we can have unique values for all the other attributes
of the relation. In some cases, such values might not be available. In these
cases, since they are not part of the key, we would simply replace them with
null values without any problem. We can thus conclude that the
dependencies:
Employee Salary
Project Budget
Employee Salary
Brown
20
Green
35
Hoskins
55
Moore
48
Kemp
48
Employee
Brown
Green
Green
Hoskins
Hoskins
Hoskins
Moore
Moore
Kemp
Kemp
Project Budget
Mars
2
Jupiter
15
Venus
15
Project
Mars
Jupiter
Venus
Venus
Jupiter
Mars
Mars
Venus
Venus
Jupiter
Function
technician
designer
designer
manager
consultant
consultant
manager
designer
designer
manager
262 Chapter 8
Normalization
We can imagine what could happen when the relation has many attributes
and several functional dependencies are defined on it.
Employee Category Salary
Hoskins
3
30
Green
3
30
Brown
4
50
Moore
4
50
Kemp
5
72
that specify the fact that each employee works at a single branch and that
each project is developed at a single branch. Observe that each employee can
work on more than one project even if, based on functional dependencies,
they must all be projects allocated to the branch to which he or she belongs.
Employee
Brown
Green
Green
Hoskins
Hoskins
Project
Mars
Jupiter
Venus
Saturn
Venus
Branch
Chicago
Birmingham
Birmingham
Birmingham
Birmingham
Employee
Brown
Green
Hoskins
Branch
Chicago
Birmingham
Birmingham
Project
Mars
Jupiter
Saturn
Venus
Branch
Chicago
Birmingham
Birmingham
Birmingham
264 Chapter 8
Normalization
Project
Mars
Jupiter
Venus
Saturn
Venus
Saturn
Jupiter
Branch
Chicago
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Birmingham
Figure 8.6 The result of the join of the relations in Figure 8.5.
relation (Figure 8.4) as well as other tuples (the last two in the table). The
situation in the example corresponds to the general situation: given a
relation r on a set of attributes X, if X1 and X2 are two subsets of X the union
of which is equal to X itself, then the join of the two relations obtained by
projecting r on X1 and X2, respectively, is a relation that contains all the
tuples of r, plus possible others, which we can call spurious. Let us say that
the decomposition of r on X1 and X2 is lossless if the join of the projections
of r on X1 and X2 is equal to r itself (that is, not containing spurious tuples).
It is clearly desirable, or rather an indispensable requirement, that a
decomposition carried out for the purpose of normalization is lossless.
We can identify a condition that guarantees the lossless decomposition of
a relation, as follows. Let r be a relation on X and let X1 and X2 be subsets of
X such that X1 X2 = X. Furthermore, let X0 = X1 X2. If r satisfies the
functional dependency X0 X1 or the functional dependency X0 X2, then
the decomposition of r on X1 and X2 is lossless.
In other words, we can say that r has a lossless decomposition on two
relations if the set of attributes common to the two relations is a key for at
least one of the decomposed relations. In the example, we can see that the
intersection of the sets of attributes on which we have carried out the two
projections is made up of the Branch attribute, which is not the left hand side
of any functional dependency.
We can justify the condition in the following manner, with reference to a
relation r on the attributes ABC and to its projections on AB and AC. Let us
suppose that r satisfies A C. Then, A is key for the projection of r on AC
and thus in this there are not two different tuples with the same values of A.
The join constructs tuples based on the tuples in the two projections. Let us
consider a generic tuple t = (a, b, c) in the result of the join. We show that t
Employee
Brown
Green
Hoskins
Branch
Chicago
Birmingham
Birmingham
Employee
Brown
Green
Green
Hoskins
Hoskins
Project
Mars
Jupiter
Venus
Saturn
Venus
266 Chapter 8
Normalization
we wish to insert a new tuple that specifies the participation of the employee
named Armstrong, who works in Birmingham, on the Mars project. In the
original relation, that is, the one in Figure 8.4, an update of this kind would
be immediately identified as illegal, because it would cause a violation of the
Project Branch dependency. On the decomposed relations however, it is not
possible to reveal any violation of dependency. On the relation over Employee
and Project, it is actually not possible to define any functional dependency
and thus there can be no violations to show, while the tuple with the values
Armstrong and Birmingham satisfies the dependency Employee Branch. We
can therefore note how it is not possible to carry out any verification on the
dependency Project Branch, because the two attributes Project and Branch
have been separated: one into one relation and one into the other.
We can conclude that, in each decomposition, each of the functional
dependencies of the original schema should involve attributes that appear all
together in one of the decomposed schemas. In this way, it is possible to
ensure, on the decomposed schema, the satisfaction of the same constraints
as the original schema. We can say that a decomposition that satisfies this
property preserves the dependencies of the original schema.
Project
Mars
Jupiter
Mars
Saturn
Venus
Branch
Chicago
Birmingham
Birmingham
Birmingham
Birmingham
268 Chapter 8
Normalization
for the relation, while Manager Branch has a unique attribute for the right
hand side, which is part of the Project Branch key. Note that the relations
show a form of redundancy: each time a manager appears in a tuple, the
branch for which he or she works is repeated. This redundancy is tolerated
however, by the third normal form, because intuitively, a decomposition that
eliminated such redundancy and at the same time preserved all the
dependencies would not be possible.
The third normal form is less restrictive than the BoyceCodd normal form
and for this reason does not offer the same guarantees of quality for a
relation; it has the advantage however, of always being achievable.
The name of this normal form suggests the existence of other normal
forms, which we will deal with briefly. The first normal form simply
establishes a condition that is at the basis of the relational model itself: the
attributes of a relation are defined on atomic values and not on complex
values, whether sets or relations. We will see in Chapter 11 how this
constraint is relaxed in other database models. The second normal form is a
weak variation of the third, which allows functional dependencies such as
that between Category and Salary of the relation in Figure 8.3. Note that this
dependency satisfies neither of the conditions of the third normal form.
There are indeed other normal forms that refer to other integrity constraints.
None of these normal forms is used significantly in current applications since
the third normal form and the BoyceCodd normal form already provide the
right compromise between simplicity and the quality of results.
Employee
Brown
Green
Green
Hoskins
Hoskins
Project Salary
Mars
50
Jupiter
30
Venus
30
Saturn
40
Venus
40
Project
Mars
Jupiter
Mars
Saturn
Venus
Branch
Division
Chicago
1
Birmingham
1
Birmingham
1
Birmingham
2
Birmingham
2
270 Chapter 8
Normalization
Manager
Brown
Green
Hoskins
Branch
Division
Chicago
1
Birmingham
1
Birmingham
2
Project
Mars
Jupiter
Mars
Saturn
Venus
Branch
Division
Chicago
1
Birmingham
1
Birmingham
1
Birmingham
2
Birmingham
2
PRODUCT
ProductName
Address
SupplierCode
Supplier
272 Chapter 8
Normalization
This entity has SupplierCode as the identifier and Name and Address as further
attributes.
Since the concepts of product and supplier appear together in the same
entity in the original schema, it is evident that if we separate them into two
entities it is appropriate that these entities be connected. That is, there is a
relationship that links them. We can reason about the cardinalities of this
relationship as follows. Since there is a functional dependency from Code to
SupplierCode, we are sure that each product has at most one supplier. Thus,
the participation of the entity PRODUCT in the relationship must have a
maximum cardinality of one. Since there is no dependency from SupplierCode
to Code, we have an unlimited maximum cardinality (N) for the participation
of the entity SUPPLIER in the relationship. For the minimum cardinalities, we
can reason intuitively. For example, assume that for each product the
supplier must always be known, while we can also have suppliers that (at the
moment) do not supply any product. The cardinalities are those in
Figure 8.13.
Code
PRODUCT
(1,1)
SUPPLY
(0,N)
SUPPLIER
ProductName
SupplierCode
Address
Name
DEPARTMENT
(0,N)
PROFESSOR
(0,N)
THESIS
(0,1)
STUDENT
(0,N)
DEGREE
PROGRAMME
Figure 8.14 A relationship for which normalization is to be verified.
We can arrive at the following conclusions:
each student is enrolled on a degree programme;
each student writes a thesis under the supervision of a single professor
(who is not necessarily related to the degree programme);
each professor belongs to a single department and the students under his
or her supervision write their theses under the care of that department.
Let us suppose that for the purposes of the thesis, the professors
department is not relevant to the degree programme on which the student is
enrolled. We can then say that the properties of the application are
completely described by the following three functional dependencies:
STUDENT DEGREEPROGRAMME
STUDENT PROFESSOR
PROFESSOR DEPARTMENT
The (unique) key of the relation is STUDENT: given a student, the degree
programme, the professor and the department are unambiguously identified.
Consequently, the third functional dependency causes a violation of the
third normal form. The affiliation of a professor to a department is a concept
independent of the existence of students who write theses with the
professor. Reasoning as before, we can conclude that the relationship
presents undesirable aspects and that it should be decomposed, separating
the functional dependencies with different left hand sides. In this way, we
can obtain the schema in Figure 8.15, which contains two relationships, both
in third normal form (and in BoyceCodd normal form). Here also, we have
a lossless decomposition with preservation of dependencies.
274 Chapter 8
Normalization
PROFESSOR
(0,N)
THESIS
(0,1)
STUDENT
(0,N)
(1,1)
AFFILIATION
DEGREE
PROGRAMME
(0,N)
DEPARTMENT
Figure 8.15 The result of the decomposition of a relationship.
PROFESSOR
(0,N)
THESIS
(0,1)
STUDENT
(1,1)
(1,1)
AFFILIATION
REGISTRATION
(0,N)
(0,N)
DEGREE
PROGRAME
DEPARTMENT
BRANCH
(0,N)
ASSIGNMENT
(0,N)
MANAGER
(0,N)
PROJECT
Figure 8.17 A relationship that is difficult to decompose.
We can see that the relationship is not in BoyceCodd normal form and
cannot be usefully decomposed. At this stage we can identify the possibility
of introducing the concept of division by means of a new entity, as shown in
the schema in Section 8.18, which replaces the ternary relationship.
This entity separates the individual branches, as indicated by its external
identifier. Moreover, the cardinality constraints tell us that each division of
a branch has a manager and several projects associated with it. From this
conceptual schema it is possible to obtain the relational schema in
Figure 8.11.
276 Chapter 8
Normalization
MANAGER
(1,1)
MANAGEMENT
Code
BRANCH
(0,N)
COMPOSITION
(1,1)
(1,1)
DIVISION
(1,N)
PROJECT
(0,N)
ASSIGNMENT
8.7 Bibliography
The basic notions on normalization, with the definition of third normal form,
were proposed by Codd [27]. The theory of normalization can be studied in
depth in the texts on database theory, such as those by Maier [58],
Ullman [88], and Atzeni and De Antonellis [3]. They also give an in-depth
and formal study of various aspects related to functional dependencies.
8.8 Exercises
Exercise 8.1 Consider the relation in Figure 8.19 and identify the functional
dependencies of the corresponding application. Identify possible
redundancies and anomalies in the relation.
Tutor
Thomson
Thomson
Robinson
Robinson
MacKay
Department
Maths
Maths
Physics
Physics
Physics
Faculty
Engineering
Engineering
Engineering
Science
Science
HeadOfDept
Jackson
Jackson
Jackson
Johnson
Johnson
Course
Statistics
Number theory
Statistics
Statistics
Relativity
Component
Wood
Screw
Glass
Wood
Screw
Screw
Wood
Handle
Wood
Type
Quantity PriceOfC Supplier
PriceOfP
Walnut
5
10.00
Smith
400
B212
200
0.10
Brown
400
Crystal
3
5.00
Jones
400
Oak
5
15.00
Smith
300
B212
250
0.10
Brown
300
B412
150
0.30
Brown
300
Walnut
10
8.00
Quasimodo
250
H621
10
20.00
Brown
250
Walnut
4
10.00
Smith
200
278 Chapter 8
Normalization
Title
Decameron
Rubiyt
Rubiyt
Le Bourgeois Gentilhomme
Le Bourgeois Gentilhomme
Washington Square
Richard III
Author
Boccaccio
Omar Khayym
Omar Khayym
Molire
Molire
James
Shakespeare
TEAM
(1,N)
COACH
(0,1)
COMPOSITION
(0,1)
PLAYER
(1,N)
CITY
Figure 8.22 A relationship whose normalization is to be verified.
Verify whether the schema satisfies the BoyceCodd normal form and if
not, restructure it into a new schema so that it satisfies this normal form.
Exercise 8.8 Consider the relation in Figure 8.23 and the following possible
decompositions:
Department, Surname in one relation and Surname, FirstName, Address in
the other;
Department, Surname, FirstName in one relation and FirstName, Address in
the other;
Department, Surname, FirstName in one relation and Surname, FirstName,
Address in the other.
Department
Sales
Purchasing
Accounts
Personnel
Surname
Eastland
Eastland
Watson
Eastland
FirstName
Fred
Fred
Ethel
Sydney
Address
6 High Street
6 High Street
27 Acacia Avenue
27 Acacia Avenue
With reference both to the specific instance and to the possible instances
on the same schema, identify which of these decompositions are lossless.
Exercise 8.9 Reconsider the relation in Figure 8.23. Verify whether the
following decompositions preserve the dependencies:
a relation on Department, Surname and FirstName and the other on Surname
and Address;
a relation on Department, Surname and FirstName and the other on
Surname, FirstName and Address;
a relation on Department and Address and the other on Department,
Surname and FirstName.
280 Chapter 8
Normalization
Part III
IIIDatabase
technology
9Technology of a
database server
This chapter concentrates on the technological aspects of database servers,
that is, of systems dedicated to data management. While up to now we have
concentrated on the external functionality of database management systems,
we will now look at the internal mechanisms that make such functionality
possible. There are various important reasons for looking inside a dbms.
Firstly, database administrators are often required to make decisions about
configurations or execution parameters that influence the behaviour of
the system, and such decisions require an understanding of the
underlying technology.
Secondly, although the mechanisms described below are often
encapsulated within database products, many of these mechanisms can be
extracted from the dbms and made available in the form of services.
Knowledge of these services is essential in order to decide on the best
configuration of an application.
The following components are present in a data management server:
The optimizer decides the best strategies for data access, that is, the ones
that guarantee the fastest query response. This component receives a
query, from which it performs a lexical, syntactic and semantic analysis,
to identify possible errors. It then transforms the correct queries into an
internal form, similar to the relational algebra seen in Chapter 3, and
selects the best strategy for access to the data.
The access methods manager, known as the relational storage system (rss)
in a relational dbms, has the task of carrying out the physical accesses to
data, according to the strategy defined by the optimizer.
284 Chapter 9
Technology of a database server
286 Chapter 9
Technology of a database server
that the system must redo the work carried out. The correct execution of the
commit fixes the atomic (and thus indivisible) event in which the transaction
is successfully completed. Before executing the commit, any failure will
cause the elimination of all the effects of the transaction, whose original state
is recreated.
When the rollback work command is carried out, the situation is similar
to a suicide decided independently within the transaction. Conversely, the
system can decide that the transaction cannot be successfully completed and
kills the transaction. Finally, various transactions can be killed following a
failure in the system. In both situations (suicide or homicide), the
mechanisms that create the abort of a transaction use the same data
structures and sometimes the same algorithms. In general, we expect the
applications to be well written and for this reason most transactions are
successfully completed and end with a commit command. In only a few
sporadic cases due to failures or unforeseen situations do the transactions
terminate with an abort command.
Consistency Consistency demands that the carrying out of the transaction
does not violate any of the integrity constraints defined on the database.
When a transaction violates a constraint, the system intervenes to cancel the
transaction or to correct the violation of the constraint.
The verification of constraints of immediate type can be made during the
transaction execution: a constraint violation removes the effects of the
specific instruction that causes the violation of the constraint, without
necessarily causing the transactions to be aborted. By contrast, the
verification of integrity constraints of the deferred type must be carried out
at the end of the transaction, after the user has requested a commit. Note that
in this second case, if the constraint is violated, a commit instruction cannot
be successfully completed, and the effects of the transaction are cancelled in
extremis. That is, just before producing and showing the final state of the
database, given that this state would be inconsistent.
Isolation Isolation demands that the execution of a transaction is
independent of the simultaneous execution of other transactions. In
particular, it requires that the parallel execution of a set of transactions gives
the result that the same transactions would obtain by carrying them out
singly. The goal of isolation is also to make the result of each transaction
independent of all the others. It must thus prevent the execution of a
rollback of a transaction from causing the rollback of other transactions,
possibly generating a chain reaction.
Durability Durability, on the other hand, demands that the effect of a
transaction that has correctly executed a commit is not lost. In practice, a
database must guarantee that no piece of data is lost for any reason. To
understand the importance of durability, consider the use of databases that
support financial applications, such as banks and systems for stock trading.
288 Chapter 9
Technology of a database server
Database
Z,X
Scheduler
Y
Main memory
buffer
Transaction t2
bot
r1 ( x)
x=x+1
bot
r 2 ( x)
x=x+1
w 2 ( x)
commit
w1(x)
commit
In this case, the final value of x is 3, because both the transactions read 2
as the initial value of x. This anomaly is called a lost update, because the
effects of the transaction t2 (the first to write the new value for x) are lost.
Dirty read
Transaction t2
bot
r1 ( x)
x=x+1
w1(x)
bot
r2 ( x)
x=x+1
w 2 ( x)
commit
abort
The final value of x at the end of the execution is 4, but it should be 3. The
critical aspect of this execution is the reading of the transaction t2, which
sees an intermediate state generated by the transaction t1. The transaction t2,
however, should not have seen this state, because it is produced by the
transaction t1, which subsequently carries out an abort. This anomaly is
known as dirty read, as a piece of data is read that represents an intermediate
state in the processing of a transaction. Note that the only way to restore
consistency following the abort of t1 would be to impose the abort of t2 and,
therefore, of all the transactions that would have read data modified by t2.
This situation, known as the domino effect, is extremely hard to manage.
Inconsistent read Let us suppose now that the t1 transaction carries out
only read operations, but that it repeats the read of the x data in successive
instants, as described in the following execution:
Transaction t1
Transaction t2
bot
r1 ( x)
bot
r2 ( x)
x=x+1
w 2 ( x)
commit
r1 ( x)
commit
In this case, x assumes the value 2 after the first read operation and the
value 3 after the second read operation. Instead, it is convenient that a
transaction that accesses the database twice finds exactly the same value for
each piece of data read, and is not affected by the other transaction.
Ghost update Consider a database with three objects, x, y and z, which
satisfy an integrity constraint, such that x + y + z = 1000; assume that we
carry out the following transactions:
290 Chapter 9
Technology of a database server
Transaction t1
Transaction t2
bot
r1 ( x)
bot
r2 ( y)
r1 ( y)
y = y 100
r2 ( z)
z = z + 100
w 2 ( y)
w 2 ( z)
commit
r1 ( z)
s=x+y+z
commit
The transaction t2 does not alter the sum of the values and thus does not
violate the integrity constraint. However, at the end of the evaluation of t1
the variable s, which contains the sum of x, y and z, takes the value 1100. In
other words, the transaction t1 observes only some of the effects of the
transaction t2, and thus observes a state that does not satisfy the integrity
constraints. This anomaly is called a ghost update.
where r1(x) represents the reading of the object x carried out by the
transaction t1, and w2(z) the writing of the object z carried out by the
transaction t2. The operations appear in the schedule following the
chronological order in which they were carried out in the database.
The task of concurrency control is to accept some schedules and refuse
others. For example, the system must avoid the anomalies shown in the
section above. This is carried out by a scheduler, the task of which is to keep
track of all the operations performed on the database by the transactions, and
to accept or reject the operations that are requested by the transactions.
We will begin by assuming that the transactions that appear in schedules
have a result (commit or abort) known in advance. In this way, we can ignore
the transactions that produce an abort, removing all their actions from the
schedule, and concentrating only on the transactions that produce a commit.
Such a schedule is known as a commit-projection of the actual execution of
the input/output operations, since it contains only the actions of transactions
that produce a commit. This assumption simplifies the theory of concurrency
control, but is unacceptable in practice, because the scheduler must decide
whether or not to accept the actions of a transaction independently of their
final result, which cannot be known beforehand. For example, this
assumption makes it impossible to deal with dirty reads described above,
which are generated when the transaction results in an abort. Thus, we must
abandon this assumption when we move from the theory of concurrency
control to practical concurrency control methods.
We now need to determine the conditions of the schedules that guarantee
the correct execution of the corresponding transactions. For this purpose, we
define as serial a schedule in which the actions of all the transactions appear
in sequence, without being mixed up with instructions from other
transactions. The schedule S2 is a serial schedule in which the transactions
t0, t1 and t2 are executed in sequence.
S2 :
r0(x) r0(y) w0(x) r1(y) r1(x) w1(y) r2(x) r2(y) r2(z) w2(z)
292 Chapter 9
Technology of a database server
w0(z)
r1(z)
r2(x) r3(z)
w3(z) w1(x)
t0
t1
r2(x)
r1(x)
r1(z) w1(x)
r3(z) w3(z)
t2
t3
294 Chapter 9
Technology of a database server
grant locks is represented in the conflict table in Figure 9.3, in which the
rows identify the requests and the columns the current state of the resource
requested. The first value of the cell shows the result of the request and the
second value in the cell shows the state that will be assumed by the resource
after the execution of the primitive.
Request
free
r_lock
w_lock
unlock
Resource state
r_locked
w_locked
296 Chapter 9
Technology of a database server
Resource
requested
growing phase
shrinking phase
represent the same sequence of accesses that introduced a ghost update, and
show that 2pl resolves the problem. Figure 9.5 describes for each resource its
t1
t2
bot
r_lock1(x)
r1(x)
x
free
1:read
y
free
z
free
bot
w_lock2(y)
r2(y)
r_lock1(y)
2:write
1:wait
y = y 100
w_lock2(z)
r2(z)
z = z + 100
w2(y)
w2(z)
2:write
commit
1:read
unlock2(y)
r1(y)
r_lock1(z)
1:wait
1:read
unlock2(z)
r1(z)
eot
s=x+y+z
commit
unlock1(x)
unlock1(y)
unlock1(z)
free
free
free
eot
298 Chapter 9
Technology of a database server
Strict two-phase locking: the locks on a transaction can be released only after
having carried out the commit/abort operations.
With this constraint, the locks are released only at the end of the
transaction, after which each item of data has arrived at its final state. This
version of 2pl is the one used by commercial dbmss. By using strict 2pl the
anomaly of dirty reads, shown in Section 9.2.2, does not occur. The example
in Figure 9.5 uses strict 2pl, in that the release actions of the lock follow the
commit action, explicitly required by the schedule.
Concurrency control based on timestamps We will conclude the overview of concurrency control theory by introducing a method that is easy to
manage, but is less efficient than two-phase locking. This method makes use
of a timestamp, that is, of an identifier that defines a total ordering of
temporal events within a system. In centralized systems, the timestamp is
generated by reading the value of the system clock at the time at which the
event happened. The concurrency control with timestamps ( TS method) is
carried out as follows:
every transaction is assigned a timestamp that represents the time at
which the transaction begins.
a schedule is accepted only if it reflects the serial ordering of the
transactions based on the value of the timestamp of each transaction.
This method of concurrency control, perhaps the simplest of all from the
point of view of its construction, serializes transactions on the basis of the
order in which they acquire their timestamps. Each object x has two
indicators, rtm(x) and wtm(x), which are the highest timestamps of the
transactions that carried out respectively read and write operations on x. The
scheduler receives requests for access to objects of the type read(x, ts) or
write(x, ts), where ts represents the timestamp of the requesting transaction.
The scheduler accepts or rejects the requests according to the following
policies:
read(x, ts): if ts < wtm(x) then the request is rejected and the transaction
is killed, otherwise the request is accepted and rtm(x) is set equal to the
greater of rtm(x) and ts.
write(x, ts): if ts < wtm(x) or ts < rtm(x) then the request is rejected and
the transaction is killed, otherwise the request is accepted and wtm(x) is
set equal to ts.
In practice, no transaction can read or write an item of data written by a
transaction with a greater timestamp, and cannot write on an item of data
that has already been read by a transaction with a greater timestamp.
Let us look at an example. Suppose that rtm(x) is equal to 7 and wtm(x)
equals 5 (that is, the object x was read by the transaction with highest
timestamp 7 and written by the transaction with highest timestamp 5).
Below, we will describe the schedulers reply to the read and write requests
received:
Request
read(x, 6)
read(x, 8)
read(x, 9)
write(x, 8)
write(x, 11)
read(x, 10)
Response
ok
ok
ok
no
ok
no
New values
rtm(x) = 8
rtm(x) = 9
t8 killed
wtm(x) = 11
t10 killed
300 Chapter 9
Technology of a database server
Comparison of VSR, CSR, 2PL and TS Figure 9.6 illustrates the taxonomy
of the methods vsr, csr, 2pl and ts. Observe that the vsr class is the most
general: it strictly includes csr, which in its turn includes both the 2pl class
and the ts class. 2pl and ts in their turn present a non-empty intersection,
but neither of them includes the other. This last characteristic can be easily
verified, by constructing a schedule that is in ts but not in 2pl, or in 2pl but
not in ts, or finally in 2pl and in ts.
CSR
2PL
VSR
TS
r1(x) w2(x) r3(x) r1(y) w2(y) r1(v) w3(v) r4(v) w4(y) w5(y)
t2
t5
t3
t4
302 Chapter 9
Technology of a database server
Tab 1
Tab 2
Fragment 1 Fragment 2
Tuple 1
Tab n
Fragment n
Tuple 2
Field 1
Tuple n
Field 2
Field n
304 Chapter 9
Technology of a database server
Request
ISL
IXL
SL
SIXL
XL
ISL
Resource state
IXL SL
SIXL
XL
OK
OK
OK
OK
No
OK
OK
No
No
No
No
No
No
No
No
OK
No
OK
No
No
OK
No
No
No
No
systems using mechanisms that lock particular data structures, which are
known as indexes, and which will be introduced in Section 9.7. The
serializable level allows for the use of predicate locks and thus also avoids
this anomaly, while the repeatable read level does not introduce them and
thus guarantees the level of isolation that is obtained by strict 2pl. Note that
the term repeatable read is misleading, in that really the two readings of
aggregate data discussed above can, when repeated, give different values.
Finally, let us look at the cases of read committed and read uncommitted. In
both cases, the 2pl protocol is not used, and thus the serializability is not
guaranteed. In the first case, the readings of data corresponding to an
intermediate (uncommitted) state of a transaction are excluded, thus
avoiding the anomaly of dirty read described in Section 9.2.2. This effect is
obtained by a read lock request, which will, however, be immediately
released after having been obtained. In the second case, no locks at all are
used for read operations, thus even dirty reads are accepted.
The most developed systems make all four levels of isolation available to
the programmer. It is up to the application programmer to choose which level
to use. For the applications for which the accuracy of the data read is
essential (for example, financial applications), the highest level will be
chosen. Where the accuracy is not important (for example, statistical
evaluations in which approximate values are acceptable), lower levels will be
chosen.
306 Chapter 9
Technology of a database server
the length of the chain. Limiting ourselves to the case of deadlocks caused
by pairs of transactions, the probability of conflict increases in a linear
manner with the global number k of transactions present in the system.
Further, it increases quadratically with the average number m of resources to
which each transaction has access. The actual probability of the occurrence
of deadlock is slightly higher than the simple statistical analysis above would
lead us to believe, due to the dependencies that exist among data. (When a
transaction has access to a given data item, it is more likely that it accesses
other items that are semantically related.) In conclusion, we can assume that
the probability of a deadlock in transactional systems is low, but not
negligible. This consideration is confirmed by experiment.
Three techniques are commonly used to resolve the problem of deadlock:
1. timeout;
2. deadlock detection;
3. deadlock prevention.
Use of timeout Avoiding deadlocks by means of timeouts is very simple.
The transaction remains in waiting for a resource for a pre-set time. If this
time expires and the resource has not yet been granted, then the lock request
is given a negative response. In this way, a transaction in deadlock is in any
case removed from the waiting condition, and presumably aborted. Because
of its simplicity, this technique is preferred by commercial dbmss.
The choice of timeout values depends on the following trade-off. On one
hand, a timeout that is too high tends to resolve deadlocks late, after the
transactions involved in the lock have spent a long time in waiting. On the
other hand, a timeout that is too low runs the risk of defining as deadlock
situations in which a transaction is waiting for a resource without causing an
actual deadlock. This might needlessly kill a transaction and waste the work
already carried out by the transaction.
Deadlock prevention Different techniques can be used to prevent the
occurrence of a deadlock. One simple but impractical technique is based on
requesting locks on all the resources necessary to the transaction at once.
Unfortunately, this technique cannot be used because transactions do not
normally know beforehand the resources to which they require access.
Another technique for the prevention of deadlock is to cause the
transactions to acquire a timestamp. The technique consists of allowing the
transaction ti to wait for a resource acquired by tj only if there is a
determined relation of precedence between the timestamps of ti and tj (for
example, i < j). In this way, about 50% of the requests that generate a conflict
can wait in a queue, while in the remaining 50% of cases a transaction must
be killed.
There are various options for choosing the transaction to kill. Let us first
separate them into pre-emptive policies, and non-pre-emptive policies. A
308 Chapter 9
Technology of a database server
Database
create, delete
extend
use unfix
force
Buffer
Manager
open, close
X
Y
read,
read_seq
write,
write_seq
File System
flush
Y
X Z
Main
memory
buffer
Scheduler
310 Chapter 9
Technology of a database server
subject and operating systems. Listed below are the functions traditionally
offered by the file system and exploited by the dbms.
The creation (create) and removal (delete) of a file. In general, at the time
of creation an initial number (minimum) of blocks is allocated to a file,
which can be dynamically extended (extend).
The opening (open) and closing (close) of a file, necessary to load the
information that describes the file in appropriate main memory
structures. The opening of a file normally allocates a numerical identifier
(fileid) to the name of the file (filename).
The primitive read(fileid, block, buffer) for the direct access to a
block of a file, identified by the first two parameters, which is transcribed
in the page of the buffer indicated using the third parameter.
The primitive read_seq(fileid, f-block, count, f-buffer) for sequential
access to a fixed number (count) of blocks of a file, identifying the first
block of the file by means of the second parameter and the first page of
the buffer by means of the last parameter.
The dual primitives write and write_seq, characterized by exactly the
same parameters as the corresponding read primitives.
Furthermore, other primitives allow for the structuring of the secondary
memory by introducing directories, to which the files are allocated. The file
system is responsible for knowing the structure of the secondary memory in
directories and the current situation of secondary memory use. It must
identify which blocks are free and which are allocated to files, to be able to
respond to the primitives.
312 Chapter 9
Technology of a database server
the path he has taken. A similar role is given by Hansel and Gretel to the
crumbs of bread left along the way through the forest, but in the Grimms
fairy tale the crumbs were eaten by the birds, and Hansel and Gretel were
lost in the forest. This analogy shows that, in order to be able to carry out its
role effectively, the log must be sufficiently durable.
Restart
procedure
Reliability
control system
Warm
restart
Cold
restart
read,
read_seq
write,
write_seq
flush
Y
X
Z
Main
memory
buffer
control are defined as if the stable memory were immune to failure. A failure
of the stable memory would be considered catastrophic and we assume it to
be impossible, at least in this section.
The stable memory is organized in different ways, depending upon the
specific requirements of an application. In some applications, it is assumed
that a tape unit is stable. In other cases, it is assumed that a pair of devices
are stable, for example, a tape unit and a disk storing the same information.
A typical organization of a stable memory uses, in place of a single disk unit,
two disk units referred to as mirrored. The two disks contain exactly the
same information and are written with a careful writing operation, which
is held to be successful only if the information is recorded on both the disks.
In this way, the stable information is also in line (available on a direct access
device).
Checkpoint
Checkpoint
t
C
Top of
the log
Transaction record t1
314 Chapter 9
Technology of a database server
Structure of log records Listed below are the log records that are written
to describe the action of a transaction t1.
The begin, commit and abort records contain the type of record and the
identifier t of the transaction.
The update records contain the identifier t of the transaction, the
identifier O of the object on which the update takes place, and then two
values BS and AS, which describe respectively the value of the object O
before the modification (the before state) and after the modification (the
after state). In this section we will assume for the sake of simplicity that
AS and BS contain complete copies of the modified pages, but in practice,
this information is much more compact.
The insert and delete records are similar to those of update; in the insert
record there is no before state, while in the delete records there is no after
state.
From here on we will use the symbols B(T), A(T) and C(T) to denote begin,
abort and commit records and U(T, O, BS, AS), I(T, O, AS) and D(T, O, BS) to
denote update, insert and delete records.
Undo and redo The log records make it possible to undo and redo the
respective actions on the database.
The undo primitive: to undo an action on an object O it is sufficient to
copy the value BS into the object O. The insert is undone by deleting the
object O.
The redo primitive: to redo an action on an object O it is sufficient to copy
the value AS into the object O. The delete will be redone by deleting the
object O.
Given that the primitives undo and redo are defined by means of a copy
action, this counts as an essential property, known as the idempotence of undo
and redo, for which the carrying out of an arbitrary number of undos and
redos of the same action is equivalent to the carrying out of such actions only
once. In fact:
undo(undo(A)) = undo(A)
redo(redo(A)) = redo(A)
316 Chapter 9
Technology of a database server
U(T,X,BS,AS) U(T,Y,BS,AS) C
Write in log
t
Write in database
W(X)
B(T)
(a)
W(Y)
U(T,X,BS,AS) U(T,Y,BS,AS) C
t
W(Y)
B(T)
W(X)
(b)
U(T,X,BS,AS) U(T,Y,BS,AS) C
t
W(X)
W(Y)
(c)
Figure 9.13 Description of protocol for the joint writing of log and
database.
In the first schema, illustrated in Figure 9.13.a, the transaction first writes
the record B(T), then carries out its update actions by writing first the log
records and then the pages of the database, which thus changes from the
value BS to the value AS. These pages are written (either with flush
primitives or with explicit requests for force) from the buffer manager to
secondary memory before the commit, which is always implemented with
a synchronous writing (force) in the log. In this way, at the commit, all the
pages of the database modified by the transaction are already written in
the secondary memory. This schema does not require redo operations.
In the second schema, illustrated in Figure 9.13b, the writing of log
records precedes that of the actions on the database, which, however,
happen only after the decision to commit and the consequent
synchronous writing of the commit record in the log. This schema does not
require undo operations.
The third schema, more general and commonly used, is illustrated in
Figure 9.13.c. According to this schema, the writing in the database, once
protected by the appropriate writing on the log, can happen at any time
with regard to the writing of the commit record in the log. This schema
allows the buffer manager to optimize the flush operations; however, it
requires both undo and redo.
Note that all three protocols respect the two rules (wal and CommitPrecedence) and write the commit record synchronously. They differ only
regarding the time in which the pages of the database are written.
We have seen which actions must be carried out in the log in order to
support failure recovery. These actions have a cost, comparable to the cost of
updating the database. The use of the above protocols represents a sensitive
overloading of the system, but cannot be avoided because of the need for
acid properties of the transactions. Log operations can be optimized, for
example by writing several log records in the same page, or by writing them
on the page in which the commit record of the transaction will be written,
and then using only one force operation. Other optimization techniques
allow a group-commit of transactions: various commit records are placed on
the same page of the log and written with a single force, expected by all the
requesting transactions. Finally, a transaction system with a high number of
transactions per second (tps) can also resort to parallel schemas for the
writing of the log.
318 Chapter 9
Technology of a database server
Stop
Fail
Boot
End of
recovery
Recovery
Warm restart
1. The last block of the log is accessed, that is, the one that was at the top at
the time of failure, and the log is traced back until the most recent
checkpoint record.
2. Decisions are made about which transactions must be redone or undone.
Two sets are constructed, called UNDO and REDO, containing transaction identifiers. The UNDO set is initially equal to the active transactions
at the checkpoint; the REDO set is initially empty. The log is then traced
forward, adding to the UNDO set all the transactions for which there is a
begin record, and moving from the UNDO set to the REDO set all the
identifiers of the transactions for which a commit is present. At the end
of this phase, the UNDO and REDO sets contain respectively all the
identifiers of the transactions to undo or redo.
3. The log is traced back undoing all the transactions in the UNDO set, until
the first action of the oldest transaction in the two sets, UNDO and
REDO, is found. Note that this action could precede the checkpoint
record in the log.
4. Finally, in the fourth phase, the redo actions are applied, in the order in
which they are recorded in the log. In this way, the behaviour of the
original transactions is replicated exactly.
This mechanism ensures atomicity and durability of the transactions. As
far as atomicity is concerned, it guarantees that the transactions in progress
at the time of failure leave the database either in the initial state or in the final
one. Concerning durability, we know that the pages in the buffer relating to
transactions completed but not yet transcribed to the secondary memory are
actually completed by a write to the secondary memory. Note that each
uncertain transaction that is present in the last checkpoint record or started
after the last checkpoint, is either undone or redone. It is undone if its last
record written in the log is a transaction or an abort record, and redone if its
last record written in the log is a commit record.
Let us look at an example of the application of the protocol. Suppose that
in the log the following actions are recorded: B(T1), B(T2), U(T2, O1, B1, A1),
I(T1, O2, A2), B(T3), C(T1), B(T4), U(T3, O2, B3, A3), U(T4, O3, B4, A4), CK(T2,
T3, T4), C(T4), B(T5), U(T3, O3, B5, A5), U(T5, O4, B6, A6), D(T3, O5, B7),
A(T3), C(T5), I(T2, O6, A8). Following this, a failure occurs.
The protocol operates as follows.
1. The checkpoint record is accessed; UNDO = {T2, T3, T4}, REDO = {}.
2. Then the log record is traced forward, and the UNDO and REDO sets are
updated:
(a) C(T4): UNDO = {T2, T3}, REDO = {T4}
320 Chapter 9
Technology of a database server
Query plans
fix
B+
tree
mgr
use
Sort
mgr
unfix
Hash
mgr
...........
DBMS
Recovery Manager
Buffer Manager
322 Chapter 9
Technology of a database server
stack
stack
access method control information
file system control information
Furthermore, in some cases all the tuples have the same size. In this way,
the page structure is simplified, but there is a risk of wasting space on the
page. As we shall see, some access methods are characterized by tuples of
fixed size. If the tuples can be of different lengths, the page dictionary
contains an indication of the offset of each tuple relative to the beginning of
the useful part and of each value of the various fields present in the tuple
relative to the beginning of the tuple itself. Finally, in some cases it is possible
to have tuples belonging to different relations on the same page.
The primitives offered by the page manager are the following.
Insertion and update of a tuple, which does not require a reorganization of
the page if there is sufficient space to manage the extra bytes introduced.
Otherwise, the operations must be preceded by a reorganization of the
page, which has limited cost when it takes place in the main memory, but
could sometimes need access to other blocks or the allocation of new
blocks.
Deletion of a tuple, which is always possible and is often carried out
without reorganizing the information on the page (that is, without
reducing the stack relative to the useful part, but simply marking the
tuple as invalid).
Access to a particular tuple, identified by means of the value of the key or
based on its offset, present in the dictionary.
Access to a field of a particular tuple, identified according to the offset
and to the length of the field itself, after identifying the tuple by means of
its key or its offset (as described above).
Note that tree structures have a different page organization, which will be
illustrated in Section 9.5.5.
324 Chapter 9
Technology of a database server
sequence into a file, called the main file. The modifications were collected in
differential files, also ordered according to the key value, and periodically
processed by batch processes, which were responsible for merging the main
file and the differential file, obtaining a new version of the main file.
Obviously, a periodic merge is unacceptable in the present dbms technology.
Having rejected this technique, let us look instead at which options are
still possible for the management of a sequentially ordered file. The main
problem with this structure is the necessity for inserting new tuples (or
changing them, when the change brings about an increase in the space
needed), as these modifications represent a reordering of the tuples already
present. The cancellations can be created in situ, making the corresponding
positions in the memory invalid (and unused). To avoid these reorderings,
the following techniques are available.
We can leave a certain number of slots free at the time of first loading.
This will allow us to retain the sequential organization using local
reordering operations.
We can integrate the sequentially ordered files with an overflow file,
dedicated to the management of new tuples, which require extra space.
The blocks of the overflow file are linked among themselves in an
overflow chain. Each chain starts from a block of the sequentially ordered
file. Thus, the sequential searches must be intertwined with the analysis
of the overflow blocks. This technique is also used for hash-based
structures, described below.
326 Chapter 9
Technology of a database server
The primitive that allows the transformation of a key value into a block
number has the format: hash(fileid,Key):Blockid. It receives the name of
the file and the key value as parameters, and returns a block number. The
corresponding function offered by the system consists of two parts.
A first operation, known as folding, transforms the key values so that they
become positive integer values, uniformly distributed over a large range.
An example of folding consists of separating the key into various
sections, each four bytes long, and then computing the exclusive or
(xor) of the bits of all the sections. This produces four bytes, to be
interpreted as a positive binary number between zero and 2 1.
The successive hashing operation transforms the positive binary number
into a number between zero and B 1. For example, a simple hashing
function is obtained by the modulo B division. A more complex
function requires raising 2 to the power of the number obtained after the
folding. Log2B bits of this number are then taken and interpreted as an
internal positive binary number, which is used as the block number.
This technique works better if the file is made larger than necessary. More
precisely, if T represents the number of tuples expected for the file and F the
average number of tuples stored in each page, then a good choice for B is
given by T/(0.8 F), thereby using only 80% of the available space for
storing tuples. This choice is justified by statistical considerations, which are
beyond the scope of this text.
The main problem of structures managed by hashing is that of collisions,
that is, situations in which the same block number is returned by the
function, based on two different values of the key. Each page can contain a
maximum of F tuples. However, when the value of F is exceeded, it is then
necessary to resort to a different technique to allocate and retrieve the tuples
that find their blocks occupied. We will try, first, to quantify the probability
of such an event. If the tuples are uniformly distributed, the probability p(t)
that an arbitrary page receives t tuples is equal to the probability that the
hashing function produces exactly t identical values and T t different
values; p(t) is given by the following formula:
where T denotes the
t binomial coefficient
1 t
1 (T t)
T
p ( t ) = --- 1 ---
t B
B
p = 1
p(i)
i=0
appear. The table in Figure 9.17 shows the average length of the overflow
chain as a function of the ratio T/(F B) and of the average number F of
tuples per page. Obviously, the presence of overflow chains slows the search
time, as it is necessary to request an input/output operation for each block
in the chain. In particular, a search has a positive result when the requested
tuple is found, and a negative result at the end of the scan of the overflow
chain. An insertion will take place in the first slot available, sometimes at the
end of the chain.
F
----------FB
.5
.6
.7
.8
.9
1
0.500
0.750
1.167
2.000
4.495
2
0.177
0.293
0.494
0.903
2.146
3
0.087
0.158
0.286
0.554
1.377
5
0.031
0.066
0.136
0.289
0.777
10
0.005
0.015
0.042
0.110
0.345
Figure 9.17 The average length of the overflow chain following too many
collisions.
In conclusion, note that hashing is the most efficient technique for gaining
access to data based on queries with equality predicates, but is extremely
inefficient for queries with interval predicates, that is, queries that require
access to intervals of values
328 Chapter 9
Technology of a database server
trees with a limited number of levels, in which the majority of pages are
occupied by leaf nodes. Another important requirement for the successful
functioning of these data structures is that the trees be balanced; when a tree
is perfectly balanced, the lengths of the paths from the root node to the leaf
nodes are all equal. In this case, the access times to the information contained
in the tree are almost constant.
Node contents and search techniques The typical structure of each
intermediate node of a tree (including the root) is shown in Figure 9.18. Each
node contains F keys (in lexicographic order) and F + 1 pointers. Each key
Kj, 1 j F, is followed by a pointer Pj; K1 is preceded by a pointer P0. Each
pointer addresses a sub-tree:
the pointer P0 addresses the sub-tree that contains the information about
the keys with values less than K1;
the pointer PF addresses the sub-tree that contains the information about
the keys with values greater than or equal to KF;
each intermediate pointer Pj, 0 < j < F, addresses a sub-tree that contains
all the information about the keys K included in the interval Kj K < Kj+1.
P0
K1
P1
. . . . .
Kj
Pj
. . . . .
KF
PF
The search continues in this way to the leaf nodes of the tree, which can
be organized in two ways.
In the first case, the leaf node contains the entire tuple. The data structure
obtained in this case is called key-sequenced. In it, the position of a tuple is
determined by the value assumed by its key field. However, as we shall
see, it is quite simple to insert or cancel tuples in this structure. The
position is not produced by an algorithm (as in the case of the relative
sequential structure or hashing), but can vary dynamically.
In the second case, each leaf node contains pointers to the blocks of the
database that contain tuples with specified key values. The data structure
that is obtained in this case is called indirect. The tuples can be anywhere
in the file, and thus this mechanism makes it possible to access tuples
allocated by means of any other primary mechanism (for example,
entry-sequenced, hash-based, or key-sequenced).
In some cases, the index structure is not complete. That is, not all the key
values are included in the index. In this case, the index is called sparse. A
sparse index can be constructed only on a sequentially ordered structure,
using indexes to locate a key value close to the value being sought, and then
to carry out a sequential-type search.
A key-sequenced structure is generally preferred for the creation of the socalled primary index of each table, that is, the one that is usually defined on
the primary key. The leaves generally contain a number of tuples less than F
because the size of a tuple is generally larger than the sum of the dimensions
of a key and a pointer. However, in this case, the leaf nodes do not contain
pointers to data pages.
Indirect structures are preferred in order to create the secondary indexes,
which can be either unique or multiple. In the first case, only one tuple is
associated with each index key. In the second case, various tuples can
correspond to each index key; each tuple is reached by means of a different
pointer placed in suitably organized leaf nodes.
The insertion and cancellation of tuples also produce updates to the tree
structure, which must reflect the situation generated by a variation in the
values of the key-field. An insertion does not cause problems when it is
possible to insert the new key value into a leaf of the tree, whose page has a
free slot. In this case, the index remains unchanged, and the new key value
is found by simply applying the search algorithm. When the page of the leaf
has no available space, however, a split operation is necessary. The split
divides the information already present in the leaf plus the new information
into two equal parts, allocating two leaf nodes in place of one. This operation
requires a modification of the arrangement of the pointers, shown in
Figure 9.19.a. Note that a split causes an increment in the number of pointers
on the next (higher) level in the tree. In this way, it can again exceed the
capacity of a page, causing a further split. In practice, the split can continue
330 Chapter 9
Technology of a database server
to happen in this way as far back as the tree root. In extreme cases, it can
cause the addition of one level to the tree. A deletion can always be carried
out in situ. The slot previously occupied by the deleted tuple is simply
shown as empty. There are two other problems, however.
initial situation
k1
k6
k1
k2
k4
k1
k3
k6
k5
k1
k2
k3
k4
k5
k6
k1
k3
k4
k5
The modification of the value of a key field is treated as the deletion of its
initial value followed by the insertion of a new value. Hence, it is dealt with
by a sequence of a deletion and an insert, as discussed above.
The careful use of the split and merge operations makes it possible to
maintain the average occupancy of each node higher than 50%. Furthermore,
even if the tree is initially balanced, differences in the pathway lengths can
appear, making it necessary to re-balance the tree. A perfectly balanced tree
gives the highest retrieval efficiency. The re-balancing of the tree is an
operation that is typically decided by the database administrator, when the
tree efficiency becomes too low.
Difference between b and b+ trees It now only remains to clarify the
distinction between b and b+ trees. In b+ trees, the leaf nodes are linked by
a chain, which connects them in the order imposed by the key, as illustrated
in Figure 9.20. This chain allows the efficient execution even of queries with
a selection predicate that is satisfied by an interval of values. In this case, it
is sufficient to access the first value of the interval (using a normal search),
then scan sequentially the leaf nodes of the tree up to a key value greater
than the second value of the interval. In the key-sequenced case, the
response will consist of all the tuples found by this type of search, while in
the indirect case it will be necessary to access all the tuples using the pointers
thus selected. In particular, this data structure also makes possible an
ordered scan, based on the key values, of the entire file, which is quite
efficient. This versatility makes the b+ structure widely used in dbmss.
Root node
Peter
First level
Mavis
Rick
Second level
Babs
David
Mavis
Peter
Rick
Tracy
332 Chapter 9
Technology of a database server
of the two pointers is used to point directly to the block that contains the
tuple corresponding to Ki, interrupting the search. The other pointer is used
to continue the search in the sub-tree that includes the key values greater
than Ki and less than Ki + 1, as shown in Figure 9.21. The first pointer P0
highlights the sub-tree corresponding to key values less than K1, while the
last pointer PF highlights the sub-tree corresponding to key values greater
than KF. This technique saves space in the pages of the index and at the same
time allows the termination of the search when a given key value is found on
intermediate nodes, without having to go through each level.
k1
k2
k3
t(k2)
k4
k6
k 10
k5
k7
t(k6) t(k10)
k8
t(k7)
k9
t(k8) t(k9)
the data dictionary to allow semantic checks. The data dictionary also
supplies statistical information concerning the size of the tables. Once
accepted, the query is translated into an internal, algebraic form. At this
point, the actual optimization begins. It consists of the following phases.
First, an algebraic optimization is carried out. This consists of the
execution of all the algebraic transformations that are always convenient,
such as the push of selections and projections, as described in
Section 3.1.7. This logical optimization happens independently of the
systems cost model.
Following this, there is an optimization that depends on both the type of
data access methods supported by the underlying level, and the cost
model used. For this phase, although general optimization principles are
well defined, each system presents its own particular characteristics.
Finally, code is generated using the physical data access methods
provided by the dbms. Thus, an access program is obtained in object or
internal format, which uses the data structures provided by the system.
The process of optimization of a query is illustrated in Figure 9.22. Note
that, unlike all the other system modules described in this chapter, the
optimizer is a module that acts at compilation time. Often, the query is
compiled once and carried out many times (compile and store approach). In
this case, the code is produced and stored in the database, together with an
indication of the dependencies of the code on the particular versions of
tables and indexes of the database, present in the data dictionary. In this way,
if the database changes significantly for the query (for example, because an
index has been added), the compilation of the query is invalidated and
repeated. Sometimes, however, a query is compiled and carried out
immediately (compile and go approach), without being stored.
Hereafter, we will concentrate on the central phase of this process, looking
at cost-based optimization. Given that this part of the optimization depends
specifically on the storage structures and on the cost model used by the
dbms, we can give only a qualitative and approximate description. We
assume at this point that the algebraic optimization has produced an
optimized description of the query, in which all the obvious algebraic
transformations have been carried out. The result of this work represents
each sql query in a tree structure, in which the leaf nodes represent tables
and the intermediate nodes represent operations of relational algebra.
334 Chapter 9
Technology of a database server
Query
Catalogue
Algebraic
optimization
Cost-based
optimization
Query
plan
Profiles
Dependencies
2. size(T) =
i=1size(Ai(T));
n
336 Chapter 9
Technology of a database server
used by dbmss exploit these algorithms, which we will not describe further.
However, a dbms must resolve a second problem, to do with the loading of
data in the buffer. At times, it is not possible to load all the data in the buffer,
because of the excessive quantity of data and therefore the impossibility of
allocating a large enough number of buffers to the operation. In that case,
portions of the data must be separately ordered and then merged, using the
available buffer space.
Indexed access Indexes, created using tree structures, are created by the
database administrator to favour the associative access of queries that
include simple predicates (of the type Ai = V) or interval predicates (of the
type V1 Ai V2). In this case, we say that a predicate of the query is
supported by the index.
In general, if the query presents only one supported predicate, it is
convenient to use the corresponding index. When a query presents a
conjunction of supported predicates, the dbms chooses the most selective
one (that is, the predicate that is satisfied by fewest tuples) for the primary
access via index. The other predicates are evaluated in main memory, once
the pages that satisfy the first predicate are loaded in the buffer. When, on
the other hand, the query presents a disjunction of predicates, it is sufficient
that one of them be not supported to impose the use of a complete scan. If
instead all the predicates of a disjunctive expression are supported, we can
use either the corresponding indexes or a scan. If indexes are used, however,
it is necessary to be careful to eliminate the duplicates of those tuples that are
found using more than one index.
Note that the use of indexes requires multiple accesses for each retrieved
tuple. When the query is not very selective, a simple scan can be more
efficient than using an index.
Join methods The join is considered the most costly operation for a dbms,
as there is a risk of an explosion of the number of tuples of the result.
Defining the method and order of the join operations has a central role in the
global optimization of queries. For this reason it is not surprising that dbms
technology has produced various methods for join evaluation. Only recently,
with the increase in interest in aggregate operations, similar algorithms and
quantitative approaches have been dedicated to aggregate operations and
grouping. Below, we will look at three techniques for join evaluation, called
nested-loop, merge-scan and hashed.
Nested-loop In a nested-loop join, one table is defined as external and
one as internal (see Figure 9.23). A scan is opened on the external table.
For each tuple found by the scan, the value of the join attribute is
collected, and then the matching tuples of the internal tables are searched
for. The matching is most efficient if there is an index on the join attribute
of the internal table, which could be created ad-hoc. Otherwise, it is
necessary to open a scan on the internal table for every value of the join
338 Chapter 9
Technology of a database server
External table
external
scan
JA
----------------
JA
a
---------------
---------------
---------------
internal scan
or indexed
access
Right table
A
-------------------------------
left
scan
right
scan
a
b
b
c
c
e
f
h
A
a
a
b
c
e
e
g
h
---------------
join by carrying out B simple joins between the partitions with equal
partition numbers, as shown in Figure 9.25. Various versions of this
method allow the optimization of performance using a careful
construction of the hash functions and careful management of the main
memory buffers.
a
hash(a)
Left table
Right table
A
hash(a)
d
e
a
c
j
j
e
m
a
a
j
z
340 Chapter 9
Technology of a database server
(R
nested-loop
1 R internal
nested-loop
S)
nested-loop
1 R external
nested-loop
2 T internal
2 T external
strategy 1
strategy 2
(R
T)
merge-scan
merge-scan
strategy 3
(S
T)
hash-join
hash-join
strategy 4
necessary to evaluate the cost of the query. The cost is obtained from the sum
of all the accumulated costs due to all the operations that make up a plan. The
search for optimal solutions is typically done by discarding the solutions of
those sub-trees whose partial cost is higher than the cost of the global
strategy. This is done using a technique of operations research, called branch
and bound, for the exact or approximate elimination of sub-trees.
Intermediate results are often stored in the buffers and discarded
immediately after their production, exploiting the pipelining of the
operations. Pipelining is the process of running through the entire tree of
operations for each of the tuples extracted, rather than carrying out each
operation completely on all the tuples. Sometimes however, it is necessary to
rewrite the results of the intermediate operations in the secondary memory.
In this case, the cost of rewriting the intermediate results becomes part of the
cost of a strategy.
The optimizers are generally satisfied by obtaining good solutions, that
is, solutions whose cost is near that of the optimal solution. In particular,
good solutions are suggested with a compile and go approach. There is no
sense in finding the optimal solution by a method that takes a long time,
when it is possible to find a good solution in a shorter time and carry out
the strategy in a total time (inclusive of optimization time) that is lower than
that of the optimal solution.
342 Chapter 9
Technology of a database server
a command show plan, which describes the access strategy chosen by the
dbms. For this reason, the choice of indexes in physical relational database
design is often carried out empirically, with a trial-and-error approach. More
generally, the tuning activity of physical design often makes it possible to
improve the performance of the database
This command can be useful when the application context changes and a
certain index is no longer used. It is also useful when the advantage obtained
in terms of response times for certain queries does not compensate for the
extra work required by the index, in order to keep it consistent with updates
to the table.
To give an example of the use of commands seen above, we can specify an
index on the EMPLOYEE table, which allows efficient access to data of the
employee, given the surname and town:
create index TownNameIndex on Employee(Surname, Town)
9.8 Bibliography
The subjects presented in this chapter are discussed both in general texts on
databases and in more specific books. The main reference for most of the
topics of the chapter is the comprehensive book by Gray and Reuter [46]. For
344 Chapter 9
Technology of a database server
9.9 Exercises
Exercise 9.1 Indicate whether the following schedules can produce
anomalies; the symbols ci and ai indicate the result (commit or abort) of the
transaction.
1. r1(x), w1(x), r2(x), w2(y), a1, c2
2. r1(x), w1(x), r2(y), w2(y), a1, c2
3. r1(x), r2(x), r2(y), w2(y), r1(z), a1, c2
4. r1(x), r2(x), w2(x), w1(x), c1, c2
5. r1(x), r2(x), w2(x), r1(y), c1, c2
6. r1(x), w1(x), r2(x), w2(x), c1, c2
Exercise 9.2 Indicate whether the following schedules are vsr:
1. r1(x), r2(y), w1(y), r2(x), w2(x)
2. r1(x), r2(y), w1(x), w1(y), r2(x), w2(x)
3. r1(x), r1(y), r2(y), w2(z), w1(z), w3(z), w3(x)
4. r1(y), r1(y), w2(z), w1(z), w3(z), w3(x), w1(x)
Exercise 9.3 Classify the following schedules (as: Non-vsr, vsr, csr). In the
case of a schedule that is both vsr and csr, indicate all the serial schedules
equivalent to them.
1. r1(x), w1(x), r2(z), r1(y), w1(y), r2(x), w2(x), w2(z)
2. r1(x), w1(x), w3(x), r2(y), r3(y), w3(y), w1(y), r2(x)
3. r1(x), r2(x), w2(x), r3(x), r4(z), w1(x), w3(y), w3(x), w1(y), w5(x), w1(z),
w5(y), r5(z)
4. r1(x), r3(y), w1(y), w4(x), w1(t), w5(x), r2(z), r3(z), w2(z), w5(z), r4(t), r5(t)
5. r1(x), r2(x), w2(x), r3(x), r4(z), w1(x), r3(y), r3(x), w1(y), w5(x), w1(z), r5(y),
r5(z)
6. r1(x), r1(t), r3(z), r4(z), w2(z), r4(x), r3(x), w4(x), w4(y), w3(y), w1(y), w2(t)
7. r1(x), r4(x), w4(x), r1(y), r4(z), w4(z), w3(y), w3(z), w1(t), w2(z), w2(t)
Exercise 9.4 If the above schedules are presented to a scheduler that uses
two-phase locking, which transactions would be placed in waiting? (Note
that once a transaction is placed in waiting, its successive actions are not
considered.)
Exercise 9.5 Define the data structure necessary for the management of
locking, for a non-hierarchical model with read repeatability. Implement in
a programming language of your choice the functions lock_r, lock_w and
unlock. Assume that an abstract data type queue is available with the
appropriate functions for the insertion of an element into a queue and for
extracting the first element of the queue.
Exercise 9.6 With reference to the exercise above, add a timeout
mechanism. Assume that we have available functions for getting the current
system time and for extracting a specific element from a queue.
Exercise 9.7 If the schedules described in Exercise 9.3 were presented to a
timestamp-based scheduler, which transactions would be aborted?
Exercise 9.8 Consider both single-version and multi-version concurrency
control based on timestamp for an object X. Initially wtm(X) = 5, rtm(X) =
7. Indicate the actions of the scheduler in response to the following input:
r(x, 8), r(x, 17), w(x, 16), w(x, 18), w(x, 23), w(x, 29), r(x, 20), r(x, 30), r(x, 25)
Exercise 9.9 Define the data structures necessary for buffer management.
Implement in a programming language of your choice the functions fix, use
and unfix. Assume we have available the file system functions described in
Section 9.3.4.
Exercise 9.10 Describe the warm restart, indicating the progressive
building of the sets UNDO and REDO and the recovery actions, given the
following situation on the log:
dump, b(T1), b(T2), b(T3), i(T1, O1, A1), d(T2, O2, R2), b(T4), u(T4, O3, B3, A3),
u(T1, O4, B4, A4), c(T2), ck(T1, T3, T4), b(T5), b(T6), u(T5, O5, B5, A5), a(T3),
ck(T1, T4, T5, T6), b(T7), a(T4), u(T7, O6, B6, A6), u(T6, O3, B7, A7), b(T8),
a(T7), failure
346 Chapter 9
Technology of a database server
Exercise 9.11 Assume that in the above situation a device failure involves
the objects O1, O2 and O3. Describe the cold restart.
Exercise 9.12 Consider a hash structure for storing tuples whose key field
contains the following names:
Green, Lovano, Osby, Peterson, Pullen, Scofield, Allen, Haden, Harris,
McCann, Mann, Brown, Newmann, Ponty, Cobbham, Coleman, Mingus,
Lloyd, Tyner, Hutcherson, Green, Fortune, Coltrane, Shepp.
1. Suggest a hashing function with B = 8 and F = 4.
2. Supposing B = 40 and F = 1, what is the probability of conflict? And
with B = 20 and F = 2?
3. With F = 5 and B = 7, what is the approximate average length of the
overflow chain?
Exercise 9.13 Consider a b+ tree structure for storing tuples whose key
field contains the data listed in the above exercise.
1. Describe a balanced b+ tree structure with F = 2, which contains the
listed data.
2. Introduce a data item that causes the split of a node at leaf level, and
show what happens at leaf level and at the level above.
3. Introduce a data item that causes a merge of a node at leaf level, and
show what happens at leaf level and at the level above.
4. Show a sequence of insertions that causes the split of the root and the
lengthening of the tree.
5. Describe a b tree structure, with F = 3, that contains the given data.
Exercise 9.14 Consider the database made up of the following relations:
PRODUCTION(ProdNumber, PartType, Model, Quan, Machine)
ORDERDETAIL (OrderNumber, ProdNumber)
ORDER(OrderNumber, Client, Amount)
COMMISSION(OrderNumber, Seller, Amount)
card(PRODUCTION) = 200,000
card(ORDERDETAIL) = 50,000
card(ORDER) = 10,000
card(COMMISSION) = 5,000
SIZE(PRODUCTION)
SIZE(ORDERDETAIL)
SIZE(ORDER)
= 45
SIZE(COMMISSION)
= 41
= 15
= 35
size(ProdNumber) = 10
size(PartType) = 1
size(Model) = 10
size(Quan) = 10
size(Machine) = 10
size(OrderNumber) = 5
size(Client) = 30
size(Amount) = 10
size(Seller) = 20
val(ProdNumber) = 200,000
val(PartType) = 4
val(Model) = 400
val(Quan) = 100
val(Machine) = 50
val(OrderNumber) = 10,000
val(Client) = 400
val(Amount) = 5,000
val(Seller) = 25
348 Chapter 9
Technology of a database server
10
10Distributed
architectures
Distribution is an ingredient of almost any modern application of database
systems. This chapter defines the various types of distributed architectures,
and then deals with each of them separately.
The simplest and most widespread distributed architecture uses the clientserver paradigm. This architecture is based on separating the role of server
from the role of client. The server contains the core functions of the dbms
engine and is executed on a computer with suitable main and secondary
memory. The client runs on a different computer, which is dedicated to the
user interface and hosts the users productivity software (for example, e-mail
and word processing). The network connects the server and client
computers.
Distributed databases present an entirely different set of problems. A
distributed database is characterized by the presence of at least two database
servers, which can carry out local transactions independently from each
other. However, in many cases the servers must interact, thus supporting
distributed transactions. This interaction takes place on increasingly
complex levels. We will see that the greatest problem is presented by the
difficulty of guaranteeing the acid properties of distributed transactions (as
defined in Section 9.1.1), especially when the transactions carry out write
operations on two or more servers. To manage these situations, a two-phase
commit protocol is introduced; this is one of the most interesting protocols
in the data distribution context, as it takes into account the interaction of
aspects of distribution, atomicity and durability. We will also look at another
typical protocol that describes deadlock detection in a distributed context.
Another type of database architecture uses parallelism to improve
performance. The parallel databases are characterized by the use of
multiprocessor machines and multiple data management devices, with
350 Chapter 10
Distributed architectures
352 Chapter 10
Distributed architectures
Apart from the functional breakdown of processes and tasks, the use of
different computers for client and server in the database is particularly
convenient. The computer dedicated to the client must be suitable for
interaction with the user. It is often a personal computer, provided with
productivity tools (electronic mail, word processing, spreadsheets,
Internet access, and workflow management). Among these tools, often
masked by a user-friendly interface, some applications request the use of
a database. The power of the computer dedicated to the server depends
on the services that it must offer. It must have a large main memory (to
support buffer management) and a high capacity disk (for storing the
entire database).
The sql language, used in all relational databases, offers an ideal
programming paradigm for the identification of the service interface.
The sql queries are formulated by the client and sent to the server. The
results of the query are calculated by the server and returned to the
client. The query can be expressed at the client side as an invocation of a
predefined remote service, or can be sent at execution time as a string of
characters. The server processes the queries, extracting the query result
from the database content, and packaging the result of the query.
Furthermore, it carries out any database update as expressed in the query.
Thus, on the network, only the information useful to the client will
travel, and this represents a tiny fraction of the information extracted
from the secondary memory. In addition, the standardization, portability
and interoperability of the sql language allows the construction of client
applications that involve different server systems.
The client-server architecture can adapt itself both to statically-compiled
queries and to queries with dynamic sql. With a static process (compile and
Client
Client
Client
LAN
Input
queue
Output
queue
Server
process
Database server
Database
store), the queries are presented to the server once and are then recalled
many times. Using a dynamic process, (compile and go), the queries are
transmitted in the form of strings of characters that are compiled and
processed by the server. In both cases, the optimizer and the access methods
lie with the server, which thus includes all the mechanisms illustrated in the
preceding chapter.
With the static process, a server normally stores a parametric query; at
execution time, the client assigns values to some entry parameters and then
calls the execution of the query, typically using a procedure. Often, the
server that manages such requests is multi-threaded. From the point of view
of the operating system, it behaves like a single process that works
dynamically on behalf of different transactions. Each unit of execution of the
server process for a given transaction is called a thread. This organization,
shown in Figure 10.1, allows the management of servers as permanently
active processes that control an input queue for client requests and an output
queue for the query results. The use of a multi-threaded model is more
convenient than the allocation of a dedicated process to each request,
because it saves process allocation and de-allocation times. The servers can
manage the queues directly or by means of other processes, called
dispatchers, whose task is to distribute the requests to the servers and return
the responses to the clients. In some cases, the dispatchers can dynamically
define the number of active server processes as a function of the number of
requests received. In this case, we say that a server class is available. The
server class contains an arbitrary number of server processes
indistinguishable one from another. This phenomenon is similar to the
management of a supermarket, where the number of open checkouts varies
dynamically depending on the number of customers present.
The architecture illustrated up to now is called a two-tier architecture
because it encompasses a client, with functions both of user interface and of
application management, and a server dedicated to data management.
Recently an alternative architecture has become popular, called three-tier
architecture, in which a second server is present, known as the application
server, responsible for the management of the application logic common to
many clients. In this architecture, the client is named thin-client; it is
responsible only for the interface with the final user. The client sends
requests to the application server, which communicates with the data
management server using the techniques illustrated above. This architecture
is becoming widespread with the growth of the Internet, because it allows
the creation of clients using a simple browser (see Chapter 14).
354 Chapter 10
Distributed architectures
from the functional point of view, looking at how a user can specify
distributed queries. In the next section, we will concentrate on the
technological aspects and we will look at how we need to extend the server
technology so as to allow their use in a distributed database.
sectors must interact, but at this point the resulting information system is
heterogeneous. This company evolution justifies the use of open architectures,
that is, of systems that can interact regardless of their heterogeneity. They are
in contrast with the proprietary architectures, in which hardware and
software are provided by a sole supplier, and are capable of guaranteeing
compatibility only within a family of products.
Airline booking systems are often distributed over many nodes linked by
wans. Communication among different systems could be necessary, for
instance, in order to put together a journey of many stages involving more
than one airline; this requires the use of complex protocols based upon the
interaction of heterogeneous systems on wans. Another example of
widespread use, which falls into the context of wans for heterogeneous
systems, is that of inter-banking systems. These allow the execution of
financial operations that involve many banks, in which each bank is in turn
the owner of a distributed database, on a local or wide area network. The
table in Figure 10.2 shows the typical application of distributed databases
described up to now, classifying them according to the type of dbms and
network.
Type of DBMS
Network type
LAN
Homogeneous
Heterogeneous
WAN
Travel management and
financial applications
Integrated banking and
inter-banking systems
356 Chapter 10
Distributed architectures
The reconstruction of the relation based on its fragments requires an equijoin operation with equal key-values (natural join).
EMPLOYEE = EMPLOYEE1 J EMPLOYEE2
Name
Robert
Greg
Anne
Charles
Alfred
Paolo
George
DeptNum
Salary Tax
Production
3.7
1.2
Administration 3.5
1.1
Production
5.3
2.1
Marketing
3.5
1.1
Administration 3.7
1.2
Planning
8.3
3.5
Marketing
4.2
1.4
Name
DeptNum
Salary Tax
Robert
Production
3.7
1.2
Greg
Administration 3.5
1.1
Anne
Production
5.3
2.1
First horizontal fragment
EmpNum
4
5
6
7
Name
DeptNum
Salary
Charles Marketing
3.5
Alfred
Administration 3.7
Paolo
Planning
8.3
George Marketing
4.2
Second horizontal fragment
Tax
1.1
1.2
3.5
1.4
358 Chapter 10
Distributed architectures
EmpNum Name
1
Robert
2
Greg
3
Anne
4
Charles
5
Alfred
6
Paolo
7
George
First vertical fragment
EmpNum DipNum
Salary
1
Production
3.7
2
Administration 3.5
3
Production
5.3
4
Marketing
3.5
5
Administration 3.7
6
Planning
8.3
7
Marketing
4.2
Second vertical fragment
Tax
1.2
1.1
2.1
1.1
1.2
3.5
1.4
Fragmented horizontally into two fragments for the cities of London and
Manchester, the only cities in which the company operates:
SUPPLIER1 = City=London (SUPPLIER)
SUPPLIER2 = City=Manchester (SUPPLIER)
Absence of transparency: In this case, each dbms accepts its own sql
dialect, because the system is heterogeneous and the dbmss do not
support a standard of common interoperability. As in the earlier case, the
programmer must indicate explicitly the fragments and their allocation in
the system.
360 Chapter 10
Distributed architectures
Note that the last two levels characterize the queries that are actually
carried out by the servers. Queries expressed at a higher level of
transparency are transformed during the optimization phase, by introducing
into their specifications the choice of particular fragments and their
allocation to specific nodes. Such transformation is done by the distributed
query optimizer, a subsystem that is aware of data fragmentation and
allocation.
This application can be made more efficient by using parallelism: instead
of submitting the two requests in sequence, they can be processed in
parallel, thus saving on the global response time. Note, however, that in this
specific example, parallel processing requires the computation of two
queries (one of which is empty), while the sequential processing is
interrupted if the first query is successful, thereby saving some computation.
We will deal further with parallelism in Section 10.7.
A different strategy is shown in the following program, which uses the
information about the city of the supplier in order to direct the query
towards the right server. The query operates at the level of allocation
transparency. Note that this solution introduces an element of rigidity: if in
future the fragmentation should change, the code would be rewritten.
procedure Query4(:snum, :name, :city);
case :city of
"London":
select Name into :name
from Supplier1
where SNum = :snum;
"Manchester":
select Name into :name
from Supplier2
where SNum = :snum;
end procedure;
We can assume that the relation is fragmented so that all the accounts with
account numbers lower than 10000 are allocated on the first fragment and all
the accounts above this number are allocated to the second fragment. A
transfer of 100,000 from the account number 3154 to account number 14878
thus consists of two operations, one for each fragment. This is an example of
a distributed transaction written at the allocation transparency level.
begin transaction
update Account1
set Total = Total - 100000
where AccNum = 3154;
update Account2
set Total = Total + 100000
where AccNum = 14878;
commit work;
end transaction
The holders of accounts numbered 3154 and 14878 (and obviously the
bank) wish the transaction to be carried out accurately on both nodes.
Alternatively, it is acceptable that neither of the two modifications is
executed. It is an unacceptable violation of atomicity that one of the
modifications is executed while the other is not.
362 Chapter 10
Distributed architectures
364 Chapter 10
Distributed architectures
2.1
1.2
5.3 6.3
2.2
7.1
8.1
9.1 10.1
3.2 4.2
7.3
5.2 10.2
8.3
11.1
11.2
12.1
13.1
14.1
12.2
11.3
13.2
13.3
node 1
node 1
node 1
366 Chapter 10
Distributed architectures
DBMS 1
t11
DBMS 2
activation (rpc)
waiting
(lock)
t12
waiting
(lock)
t21
activation (rpc)
t22
DBMS 2
DBMS 3
t1
t1
t2
t3
t2
t3
a. initial situation
DBMS 2
E
t1
t3
t2
t2
t3
368 Chapter 10
Distributed architectures
the situation that is verified by dbms 2 after having received the wait
condition S1. Thus a new wait condition appears:
S4 : EXT t3 t2 EXT
This wait condition is sent to dbms 3, where the sub-transaction activated
by t2 is being carried out. Figure 10.8c shows the situation that is generated
in dbms 3 after receiving the wait condition S4. Finally, the deadlock
consisting of the pair t2, t3 is recognized (note that the original deadlock also
involved t1). One of the two transactions is chosen for the rollback, which
involves all of its sub-transactions on all the nodes, and the deadlock is
resolved.
370 Chapter 10
Distributed architectures
2. The rms that are ready await the decision message. As soon as the
message arrives, they write the commit or abort record on their own logs.
Then they send an acknowledgement to the tm. At this point, the
implementation of the commit or abort proceeds on each server as
described in the preceding chapter. In particular, the pages modified by
the transaction are written to the database by the buffer manager.
3. The tm collects all the ack messages from the rms involved in the second
phase. If the timeout expires without the tm having received all the
acks, the tm sets another timeout and repeats the transmission to all the
rms from which it has not received an ack. This sequence is repeated
until all the rms respond by sending their acks. When all the acks have
arrived, the tm writes the complete record on its log.
To summarize, a communication failure between tm and rm during the
first phase provokes a global abort, while a communication failure between
tm and rm during the second phase provokes a repetition of the
transmissions, until a communication between the tm and all the rms is reestablished. In this way, the final result of the transaction is communicated
to all the rms in a ready condition. The protocol is shown in Figure 10.9,
which describes the exchange of messages and writes on the logs of the tm
and one of the rms.
Prepare
Global Decision
timeout 1
Complete
timeout 2
TM
prepare
msg
ready
msg
decision
msg
Ready
ack
msg
Local Decision
RM
Window of uncertainty
372 Chapter 10
Distributed architectures
of the ready record and the writing of the commit or abort record is called the
window of uncertainty. The protocol is designed to keep this interval to a
minimum.
The entire group of actions carried out by the processes of client, server
and tm is described in Figure 10.10 for a specific rm (but obviously, there
must be at least two rms). The client sends a task to be executed by the rm
and waits for it to be completed. It then sends, either in sequence or in
parallel, other tasks to be executed by other servers. Note that in the model
in Figure 10.10 the client acts as coordinator of the distributed execution.
Alternatively, the client can direct its request to a single rm and this last can
send requests to other rm processes, carrying out the role of coordinator of
the distributed execution. In both cases, the client or the rm process
coordinator waits for all the rms to finish their tasks, and then activates the
two-phase commit protocol, interacting with the tm. At this point, all the
rms are waiting to participate in the commit, receiving the first prepare
message. The commit protocol happens very rapidly, as both the rm and the
tm simply write the records in the log and send messages, thereby
minimizing the window of uncertainty.
Client
2pc
exec
done
done
TM
Begin
374 Chapter 10
Distributed architectures
When the last record in the log is a complete, the failure of the
coordinator has no effect on the transaction.
Note that the repetition of the second phase can cause a participant to
receive the same decision about the same transaction many times. In this case,
the participant can simply ignore the decision, but must in any case respond
with an ack, to complete the recovery process.
Message loss and network partitioning Finally, let us analyze the cases
of message loss and of network partitioning.
The loss of a prepare message and the loss of the succeeding ready
messages are not distinguishable by the tm. In both cases, the timeout of
the first phase expires and a global abort decision is made.
The loss of a decision message or of the succeeding ack message are also
indistinguishable. In both cases, the timeout of the second phase expires
and the second phase is repeated.
A network partitioning does not cause further problems, in that the
transaction will be successful only if the tm and all the rms belong to the
same partition during the critical phases of the protocol
general its loss causes the repetition of the second phase. In conclusion, only
the records ready and commit, in the rm log, and global commit, in the tm
log, must be written synchronously, and must be written using the force
primitive. These records can be written using group operations, as indicated
in Section 9.4.3, in order to improve performance.
Read-only optimization A further optimization of the two-phase
commit protocol appears when a participant is found to have carried out read
operations but no write operations. In this case, the participant must not
influence the outcome of the transaction, and must remain disinterested in
the 2pc protocol. Note that the participants whose read-only role was known
beforehand could be excluded from the protocol even earlier. The
optimization of the protocol of a participant found to be read-only consists
of responding read-only to the prepare message arriving from the
coordinator. The participant at this point does not carry out any write
operation on the log and suspends the execution of the protocol after
sending the message. Having received the read-only reply, the coordinator
ignores the participant in the second phase of the protocol.
376 Chapter 10
Distributed architectures
Prepare
Global Commit
Complete
coordinator (TM)
P
GC
backup
Ready
Commit
participant (RM)
Prepare
Ready
Pre-Commit
Pre-Commit
Local Commit
If the new coordinator finds a ready in its log, it knows that none of the
other participants in the protocol can have gone beyond the pre-commit
condition, and thus can make the decision to abort. Thus the new
coordinator registers a global abort decision and communicates it to the
other participants.
If the new coordinator finds a pre-commit in its log, it knows that the
other participants are at least in the ready state, and thus can make the
decision to commit. Thus, the participant registers a global commit
decision and communicates it to the other participants.
The three-phase commit protocol has, however, serious inconveniences,
which in practice make it unusable. In the first place, it lengthens the
window of uncertainty, and thus makes blocking more probable. In the
second place, the atomicity can be lost whenever a network partitioning
occurs and two or more participants are chosen as coordinators to end the
protocol (one in each partition). To resolve the problem, we must also be sure
that the commit or abort decision is made by a sufficient number of
participants to guarantee that a different decision could not be made in
another partition. This is done using mechanisms based on the use of votes
and quorums, which go beyond the scope of this textbook.
10.5 Interoperability
Interoperability is the main problem in the development of heterogeneous
applications for distributed databases. The term denotes the capacity for
interaction, and requires the availability of functions of adaptability and
conversion, which make it possible to exchange information between
systems, networks and applications, even when heterogeneous. Interoperability is made possible by means of standard protocols such as those for the
exchange of files (ftp), electronic mail (smtp/mime), and so on. With
reference to databases, interoperability is guaranteed by the adoption of
suitable standards.
In this section we will look at odbc, a standard to guarantee remote access
(but not the two-phase commit protocol), and x-open dtp, a standard
specifically focused on the commit protocol. In this way, we will set down the
technological premises for the creation of a cooperative architecture between
heterogeneous databases, which will be discussed in the next section. In
Chapter 11, we will also look at the corba standard, which is concerned
with interoperability in the context of generic object-oriented applications.
378 Chapter 10
Distributed architectures
Application
ODBC
driver manager
Driver
(DBMS/OS/network)
Data source
380 Chapter 10
Distributed architectures
XA-Interface
Client-TM dialogue:
tm_init()
Session opening
tm_open()
xa_open()
Transaction starts
tm_begin()
xa_start()
tm_commit()
xa_precom()
xa_commit()
xa_abort()
xa_end()
Session close
tm_exit()
Recovery (TM-driven)
xa_close()
xa_recover()
xa_commit()
xa_abort()
xa_forget()
Figure 10.14 Interactions among client, tm and server with the x-open
dtp protocol.
When an rm is blocked because of the failure of the tm, an operator can
impose a heuristic decision (generally the abort), thus allowing the release of
the resources. The recovery procedure is guided by the tm, which calls the
rm immediately after its successful restart from the failure. At this point, the
rm process consults its log and indicates three sets of transactions:
transactions in doubt;
transactions decided by a heuristic commit;
transactions decided by a heuristic abort.
382 Chapter 10
Distributed architectures
The complexity of distributed operations is a measure of the degree of coordination necessary to carry out operations on the co-operating
databases.
The currency level indicates whether the data being accessed is up-to-date
or not. In particular, in a co-operating system there are two possible
situations: (a) direct access to up-to-date remote data; (b) access to derived
data, often managed by a system that is more easily accessible, but whose
content is typically not up-to-date.
Based on the above criteria, we can identify three architectures, which
represent the three options for guaranteeing data-based co-operation.
A first category is that of multi-database systems, shown in Figure 10.15.
In these systems, each of the participating databases continues to be used by
its respective users (programs or end users). The single systems are also
accessed by modules, called mediators, which transform and filter the
accesses, showing only the portion of database that must be exported, and
makes it available to a global manager, which carries out the integration. This
architecture presents an integrated view to the users, as if the database were
a single entity. It thus provides a high level of transparency. The currency
is also high, because data is accessed directly. At the same time, the
complexity is also high; in general, data cannot be modified by means of
mediators, because the local management of modifications at each source
system is preferable.
client
client
Global manager
client
Mediator
Mediator
Mediator
Local manager
Local manager
Local manager
DB
DB
DB
client
client
DW manager
Data Warehouse
Integrator
client
Mediator
Mediator
Mediator
Local manager
Local manager
Local manager
DB
DB
DB
client
10.7 Parallelism
Parallelism is an important component of database technology. Having
witnessed the failure of special architectures for databases (the so-called
database machines) during the eighties, parallelism was developed during
the nineties along with the spread of standard multiprocessor architectures,
that is, architectures that are not specifically dedicated to databases. From the
architectural point of view, parallelism is possible with multiprocessor
architectures both with and without shared memory. Obviously these have
different technical solutions, but in this section we will concentrate on the
aspects more specifically linked to data management without dwelling on the
technological differences of multiprocessor architectures.
384 Chapter 10
Distributed architectures
client
client
DW manager
Data Warehouse
Integrator
client
Mediator
Local manager
Local manager
DB
DB
Mediator
Mediator
Mediator
Local manager
Local manager
Local manager
DB
DB
DB
client
A typical olap query is the request for the account holders who have
carried out transactions for total amounts above 100,000 during 1998.
procedure Query6();
select AccNum, sum(Amount)
from Account join Transaction on
386 Chapter 10
Distributed architectures
Account.AccNum = Transaction.AccNum
where Date >= 1.1.1998 and Date < 1.1.1999
group by AccNum
having sum(Amount) > 100000
end procedure;
16
24
32
Number of processors
The scale-up curve characterizes both inter-query parallelism and intraquery parallelism, and measures the average cost of a single transaction
against the increase of the number of processors. Figure 10.19 illustrates an
ideal situation, in which the average costs remain almost constant with an
increase in processors. In this case, we say that the system scales in an ideal
way. In oltp systems the increase in processors permits the management of
a greater number of transactions per second and so responds to an increased
transaction load. In olap systems, the increase in processors allows an
increase in data, which occurs when the data warehouse grows. Both the
oltp systems and the olap systems guarantee services that are very close to
the ideal, and many constructors are able to show almost constant scale-up
curves.
Cost/tps
70
60
50
40
30
16
24
32
Number of processors
388 Chapter 10
Distributed architectures
Client
Client
Client
Client
Client
Server
F1
Server
copy(F2)
F2
copy(F1)
390 Chapter 10
Distributed architectures
F1
copy(F2)
DBMS 1
copy(F3)
update process
(batch)
copy(F1)
F2
DBMS 2
copy(F3)
transactions on the laptop copy. This copy is reconciled with the main copy
when the salesperson reconnects to it, at the end of the sale activity.
10.9 Bibliography
This chapter also refers to the texts by Gray and Reuter [46] and Ceri and
Pelagatti [18], mentioned in the previous chapter. Distributed databases are
described in the more recent textbook by Ozsu and Valduriez [66]. The
applications of distributed databases are described by Gray and Anderton
[44]; the two-phase commit algorithms, and in particular their optimizations,
the standardization in x-open and their use within the field of commercial
systems, are widely described by Samaras et al. [72]; distributed detection of
deadlocks is described by Obermarck [62] and Lamport clocks are defined by
Lamport [54]. The aspects relating to the co-operation of databases can be
studied more widely in the texts by Brodie and Stonebraker [11] and Kim [52]
and in the articles by Bernstein [9] and Sheth and Larson [76].
10.10 Exercises
Exercise 10.1 Consider the database:
PRODUCTION(SerialNumber, PartType, Model, Quantity, Machine)
PICKUP(SerialNumber, Lot, Client, SalesPerson, Amount)
CLIENT(Name, City, Address)
SALESPERSON(Name, City, Address)
Assume four production centres located in Dublin, San Jos, Zurich, and
Taiwan and three sales points, located in San Jos, Zurich and Taiwan. Each
production centre is responsible for one type of part; the parts are CPU,
Keyboard, Screen and Cable. Suppose also three sales points, located in San
Jos, Zurich and Taiwan. Suppose that the sales are distributed by
geographic location; thus, Zurich clients are served only by salespeople in
Zurich (assume that the sales point in Zurich also serves Dublin). Assume that
each geographic area has its own database (that is, databases are available in
Dublin, San Jos, Zurich, and Taiwan). Design the horizontal fragmentation
of the tables PRODUCTION, PICKUP, CLIENT and SALESPERSON. Express the
following queries on transparency levels of fragmentation, allocation and
language:
1. Determine the available quantity of the product 77y6878.
2. Determine the clients who have bought a lot from the retailer Wong, who
has an office in Taiwan.
3. Determine the machines used for the production of the parts type
Keyboard sold to the client Brown.
392 Chapter 10
Distributed architectures
node 1
node 2
node 3
t1
DBMS 1
t5
t6
E
t3
DBMS 2
t2
t5
E
DBMS 3
t4 E
t2
DBMS 4, version 1
t6
t1
t4
E
t4
t1
t6
DBMS 4, version 2
Exercise 10.5 Apply the warm restart protocol after the failure of a node,
assuming a two-phase commit protocol, having the following input (where
R(ti) indicates the presence of a ready record):
B(T1), B(T2), B(T3), I(T1,O1,A1), D(T2,O2,B2), B(T4), R(T1),
U(T4,O3,B3,A3), C(T1), CK(T2,T3,T4), B(T5), B(T6), U(T5,O5,B5,A5),
R(T5), B(T7), U(T7,O6,B6,A6), B(T8), U(T6,O1,B7,A7), A(T7),
R(T6), failure
Exercise 10.6 Describe the warm restart protocol after the failure of a node,
assuming a three-phase commit protocol, having the following input (where
PC(ti) indicates the presence of a pre-commit record):
B(T1), B(T2), B(T3), I(T1,O1,A1), D(T2,O2,B2), B(T4), R(T1),
U(T4,O3,B3,A3), PC(T1), C(T1), CK(T2,T3,T4), B(T5), B(T6),
U(T5,O5,B5,A5), R(T5), B(T7), U(T7,O6,B6,A6),
U(T6,O3,B7,A7),
B(T8), PC(T5), A(T7), R(T6), failure
Exercise 10.7 Given a distributed system with eight nodes, assign a
quorum necessary to decide commit and a quorum necessary to decide
abort to maximize the probability of reaching a commit decision whenever
there are four partitions with two nodes each.
Exercise 10.8 On the same database schema as in Exercise 10.1, describe an
execution schema for the following queries that maximize the inter-query
parallelism:
1. extract the sum of the production quantities, grouped according to type
and model of parts;
2. extract the average value of parts sold by the salespeople, grouped
according to type and model of parts.
Exercise 10.9 Describe an example of replicated database behaviour that
produces a data inconsistency.
Exercise 10.10 Describe an example of symmetrical replication that
produces a data inconsistency.
394 Chapter 10
Distributed architectures
Part IV
IVDatabase evolution
11
11Object databases
Object databases integrate database technology with the object-oriented
paradigm. Object orientation was originally introduced within the field of
programming languages and has become very popular as a paradigm for the
organization and design of software systems. Object databases were
originally developed in the mid eighties, in response to application demands
for which the relational model was found to be inadequate.
In object databases, each entity of the real world is represented by an
object. Classical examples of objects are:
electronic components, designed using a Computer Aided Design (cad)
system;
mechanical components, designed using a Computer Aided Manufacturing
(Cam) system;
specifications and programs, managed in a Computer Aided Software
Engineering (case) environment;
multimedia documents, which includes texts, images and sound, managed
by multimedia document managers;
spatial or geographic data, such as geometric figures or maps, managed by
Geographic Information Systems (gis).
These kinds of objects differ greatly from each other and are managed by
specialized applications and systems. A common requirement of all of these
applications is that of organizing the data as complex and unitary objects.
This demand is not satisfied by the relational model, in which each real
world object is distributed among a number of tables. To view the object in
its entirety requires the execution of complex queries that reconstruct the
various components of an object from tables in the database, by using joins.
Object databases represent real world objects by means of data objects with
complex structure and with rich semantic relationships. These are modelled
by means of constructs similar to those used for conceptual design,
398 Chapter 11
Object databases
model. The data model exhibits many of the characteristics of the EntityRelationship model, seen in Chapter 5. In displaying the characteristics of
the model, we use the syntax of a specific system ( o2); this is required
because the reference standards of odmg do not cover some aspects (for
example the implementation of methods) that are important for the
understanding of the oodbms approach.
11.1.1 Types
In an object database, types allow the definition of the properties of the
objects. In particular, types define both static properties (which describe the
structure of the objects) and dynamic properties (which describe the
behaviour of the objects, by defining the operations, or methods, applicable
to objects).
We will begin with the static part of types; the dynamic nature of types
will be discussed in Section 11.1.3. The static part of types is constructed
using type constructors and an extensive set of atomic data types, which
include the classic data types present in programming languages: for
example, integers, reals, boolean, and strings. Some systems allow the
definition of enumeration types, the values of which are explicitly listed by
the user. Atomic types include object identifiers (oid) which will be
introduced later in the chapter. Most systems support the null value
(sometimes indicated as nil) in all the atomic types. As in the relational
model, nil is a polymorphic value, that is, belonging to many types.
Each type definition associates a name to a type. For example:
Address:string is a type definition, which associates the name Address to
the string atomic type.
Complex data types Type constructors allow the definition of types
called complex data types, which dictate the structure of the instances (called
complex objects) of an object database. A recursive definition of complex data
types (based on type constructors) is as follows. Let us fix a set of atomic data
types.
The record constructor allows the definition of types whose instances are
tuples of (complex) values of possibly different types. If T1,,Tn are type
names and A1,,An are distinct labels, which we will call attributes, T =
record-of(A1 : T1,,An : Tn) is a record type.
Set, bag and list constructors allow the definition of types whose
instances are collections of (complex) values of the same type. Sets are
non-ordered collections without duplicates, bags are non-ordered
collections allowing duplicates, and lists are ordered collections, possibly
with duplicates. If T1 is a type, then T = set-of(T1) is a set type, T = bagof(T1) is a bag type and T = list-of(T1) is a list type.
Given a complex type T, an object of data type T is an instance of T. Type
constructors are orthogonal, that is, they can be applied arbitrarily, resulting
400 Chapter 11
Object databases
Given this type definition, we can show some typical values that are
compatible with the definition. In the following example, records are
contained within square brackets and sets in curved brackets:
V1: ["MI67T891", "Uno", ["Fiat", "Agnelli", {["Mirafiori",
"Torino", 10000], ["Trattori", "Modena", 1000]}),
"blue", 7000, ["1100CV", "Monroe"]]
Given a value of type record, we can gain access to its components using
the classic dot notation, which can be applied recursively. For example:
V1.Colour = "blue"
V1.Manufacturer.President = "Agnelli"
V1.MechanicalParts.ShockAbsorber = "Monroe"
Objects and values The above example demonstrates how we can satisfy
the demand for allocating an arbitrarily complex structure to a single object.
Thus, an automobile (or an integrated circuit) is described in a more detailed
and unitary manner than it would be by using, for example, the relational
model. However, this example also illustrates the limitations of a description
based on values: for each automobile, made, say, by Fiat, the description of
the manufacturer is repeated. Now, the manufacturer, in its turn, is made up
of various data, including the name of the president and the locations of the
factories. Such a description obviously introduces redundancy and goes
against the normalization principles, discussed in Chapter 8.
In order to obviate this problem, we introduce object identifiers ( oid). The
structural part of an object is made up of a pair (OID, Value). The value is an
instance of the objects type; we call it the state of an object. oids provide
the unambiguous identification of every object in the database, and allow the
construction of references between objects. In the actual systems, oids are
automatically assigned at object creation and are generally not visible to the
users. An object can include explicit references to other objects: this can be
implemented at schema level by allowing, in a data type definition, the
notation *T, which denotes oids of objects of type T. If a property of an
object has type *T, then we say it is object-valued.
The following definition introduces references to objects:
Automobile: record-of(RegistrationNumber: string,
Model: string,
Manufacturer: *Manufacturer,
Colour: string,
Price: integer,
MechanicalParts: record-of(
Motor: string,
ShockAbsorber: string)
Manufacturer: record-of(Name: string,
President: string,
Factories: set-of(*Factory))
Factory: record-of(Name: string,
City: string,
NoOfEmployees: integer)
402 Chapter 11
Object databases
Two objects 01 and 02 are identical when they have the same oid (and
obviously also the same state); in addition to identity, in the object-oriented
model, there are two notions of equality:
superficial equality (==) requires that two objects have the same state;
deep equality (===) requires that two objects have identical reachable
values obtained by recursively substituting, at each object reference, the
objects that can be reached using the oids for the oids themselves.
Note that the state of an object includes the oids of object-valued
properties, and thus superficial equality implies deep equality. Note also that
the construction of reachable values for testing deep equality could build
very large objects. It could include all of the objects in the database and
could even be endless in the presence of cyclic references. In general, many
OODBMS systems offer an operator to verify the superficial equality of two
objects, while deep equality must be programmed, for specific types, using
suitable equality predicates on their reachable values.
For example, consider the following type definitions and the following
objects:
T1: record-of(A: integer, B: *T2)
T2: record-of(C: character, D: *T3)
T3: record-of(E: integer)
O1:
O2:
O3:
O4:
O5:
O6:
O7:
<OID1,
<OID2,
<OID3,
<OID4,
<OID5,
<OID6,
<OID7,
[120,
[120,
[120,
["a",
["a",
[15]>
[15]>
OID4]> of type
OID4]> of type
OID5]> of type
OID6]> of type
OID7]> of type
of type T3
of type T3
T1
T1
T1
T2
T2
In this case:
the superficial equalities are: O1==O2, 06==07;
the deep equalities are: O1===O2, O1===O3, O2===O3, O4===O5, O6===O7.
The condition for defining deep equality of objects X and Y of type T1 can
be programmed as follows: X.A=Y.A and X.B.C=Y.B.C and X.B.D.E=Y.B.D.E .
11.1.2 Classes
A class performs the function of an object container, to and from which
objects can be dynamically added and removed. Objects belonging to the
same class are homogeneous, that is, they have the same type. In the ddl,
type definitions are typically given as a part of the class definitions. In
general, the class definition is separated into two parts.
The interface describes the type of the objects belonging to the class,
which includes the signatures of all its methods; each signature consists
of a list of the name and type of each parameter of the method.
has
contains
Object
Type
describes
has
Value
404 Chapter 11
Object databases
names to these collections (for example, the type citizen could correspond to
a same class but to different collections, called Londoner and Florentine). In
this case, the three concepts of type, class and extension would be present.
On the other hand, some oodbmss do not distinguish types or classes, in that
they unite the two concepts and give the type the role of defining extensions
and implementations of methods.
For example, let us look at how in O2 the definition of class syntactically
includes the type definition. Note the use of class names in the type
definition, which are implicitly interpreted as references:
add class Automobile
type tuple(RegistrationNumber: string,
Model: string,
Maker: Manufacturer,
Colour: string,
Price: integer,
MechanicalParts: tuple(Motor: string,
ShockAbsorber: string))
add class Manufacturer
type tuple(Name: string,
President: Person,
Factories: set(Factory))
add class Factory
type tuple(Name: string,
City: string,
NoOfEmployees: integer)
add class Person
type tuple(Name: string,
Address: string,
TaxCode: string)
11.1.3 Methods
Methods are used to manipulate the objects of an oodbms. Their presence is
the main innovative element in an oodbms when compared to a relational
database. A method has a signature, which describes the parameters of the
method and includes all the information that allows its invocation, and an
implementation, which contains the method code. Often the implementation
of methods is written in an object-oriented programming language, such as
Smalltalk or c++. The signature of the method is one of the components of
the class definition.
In general, each method is associated with a specific object class. In this
case, the method has a specific object class as target. There are, however,
systems that allow multi-target methods, which are applied to an arbitrary
Automobile
Manufacturer
RegistrationNumber: string,
Model: string,
Maker: Manufacturer,
Colour: string,
Price: integer,
MechanicalParts:[
Motor: string,
ShockAbsorber: string]
Name: string,
President: Person,
Factories: {Factory}
Factory
Name: string,
City: string,
NoOfEmployees: integer
VintageCar
SportsCar
ManufactureDate: integer
MaxSpeed: integer
Driver: Person
VintageSportsCar
PastVictories: {string}
Person
Name: string,
Address: string,
TaxCode: string
406 Chapter 11
Object databases
that should be assigned as the initial state of an object, and returns the object
itself as output parameter. In the example, the method is applied to an object
of the class AUTOMOBILE, which can be considered as an implicitly defined
input parameter. Let us look at the definition of a signature ( add method
instruction) and of implementation (body instruction) in o2.
add method init(RegistrationNumber_par: string,
Model_par: string,
Colour_par: string,
Price_par: integer): Automobile
in class Automobile is public
body init(RegistrationNumber_par: string,
Model_par: string,
Colour_par: string,
Price_par: integer):
Automobile in class Automobile
co2{self -> RegistrationNumber = RegistrationNumber_par;
self -> Model = Model_par;
self -> Colour = Colour_par;
self -> Price = Price_par;
return(self); }$
The next example shows the nested invocation of the init and Increase
methods, which are possible as the init method returns the target object as
output parameter:1
execute co2 {
o2 Automobile X;
[[X init("MI56T778", "Panda", "blue", 12000)]
Increase(2500)];}$
408 Chapter 11
Object databases
410 Chapter 11
Object databases
Automobile
SportsCar
Vintage
SportsCar
VintageCar
automatic, and requires an explicit insertion operation of that object into the
class.
Conflicts Instances of classes with multiple inheritance or objects that are
instances of more that one class can be the source of name conflicts whenever
two or more super-classes have attributes or methods with the same name. In
this case, we must define the policies for conflict resolution, to make the
mechanisms of inheritance unambiguous. We list some of the possible
solutions.
Reveal the conflict at the time of definition of the classes and do not
accept the definitions as correct. This solution has the disadvantage of
imposing a rethink of already consolidated parts of the schema. The
problem is eliminated only by changing the names of the attributes and/
or methods that cause the conflict.
Define the mechanisms to make the choice unambiguous. For example, by
using an ordering between classes defined beforehand, or by applying a
method to an object in the context of a given target, which is explicitly
defined in the method call, thus solving the conflict.
Redefine the properties and methods locally, as described in the next
section. The local redefinition eliminates the conflict.
11.1.5 Persistence
The objects defined in a program can be persistent or temporary. Temporary
objects cease to exist at the end of the execution of a program, while
persistent objects are inserted into the persistent storage space of the oodb.
In general, an object becomes persistent by means of the following
mechanisms.
412 Chapter 11
Object databases
By insertion into a persistent class. In this case, the primitive new generates
a persistent object, as described in some examples of this section.
By reachability based on another persistent object. For example, if we
have two objects of the types Automobile and Manufacturer in which the
first refers to the second and then the first is inserted into a persistent
class, then the second also becomes persistent. In this way, the state of an
object that can be reconstructed recursively by means of references is
persistent.
By denomination, that is, by giving a name to an object (called a handle),
which can be used to find it in a later invocation of the program. For
example, in o2, we can declare a variable and then to give it a name,
making the corresponding object persistent:
X SportsCar;
add name Ferrari_Testa_Rossa: X
Not all of these mechanisms for making objects persistent are supported in
all oodbmss. In some systems, persistence is defined at class level, that is, by
distinguishing between persistent classes and temporary classes. The
persistent or temporary nature of objects is defined when the object is
inserted into the class. Persistence by means of reachability guarantees
referential integrity of the oodb, that is, the automatic maintenance of
referential integrity constraints between classes that are similar to the
referential integrity constraints between tables, discussed in Section 2.2.4.
This type of persistence however, brings some difficulties into the deletion
of the object from the oodb. In practice, an object can be deleted from the
system only when it can no longer be reached by denomination or referenced
by other persistent objects. Deletion is performed by a specific subsystem
called the garbage collector.
of the house; for owner and sale conditions, it shows a schema with the
appropriate information. We can thus write code that, given a set S of
heterogeneous objects, calls the function display for it in a compact way, for
example:
for X in S do display(X)
Note that the init method, defined in the FILE class, is reused in the two
sub-classes. The operator @ is used to call the init method as implemented in
414 Chapter 11
Object databases
a different class, thus enabling the reuse of defined methods in generic classes
in the implementation of methods in more specific classes.
Note also that the use of the global variables @ThisUser, @Today and
@EndOfPeriod (also recognized by an initial @) is acceptable in the context of
the implementations of methods. Using the above method definition, we can
initialize an object corresponding to a file independently of where it belongs
in the class hierarchy. In the following code, it is necessary simply to replace
the term CLASS by any of the three class names introduced in the example.
execute co2{
o2 Class X;
X = new(Class);
[X init("myprog")]; }$
the
Observe that in the redefinition of the SOURCE class, the property Creator
is redefined: the creator is no longer a generic user, but rather a programmer.
This is an example of covariant definition of property, which can be useful
from an application point of view, if we wish to impose the condition that
only the programmers can create source files.
The init method is called with an input parameter, which indicates who
the user is, and must be correctly typed: the initialization of a generic FILE
should receive as input parameter a generic user, but the initialization of a
SOURCE must receive as input parameter a programmer. This is an example of
covariant redefinition of input parameters. It is impossible to check at
compilation time whether the invocation of the method is correct when
416 Chapter 11
Object databases
objects can migrate dynamically from the USER class to the PROGRAMMER class
and vice versa.
Finally, the output parameter of the method is also redefined, in that when
it is invoked in a sub-class it returns a more specific type. This is an example
of covariant redefinition of the output parameter, which poses no problems of
type checking.
418 Chapter 11
Object databases
Automobile
Maker
Builds
Manufacturer
Factories
SportsCar
Manufacturer
Factory
Note that the first section of the interface, which is optional, describes the
properties of types. This section is present only in the type AUTOMOBILE; the
clause extent introduces the name of the container of objects of type
AUTOMOBILE, and the clause key, as in the relational model, lists the attributes
that identify the objects belonging to the type extensions. Note that the
importance of keys in an oodb is minor in comparison with a relational
database, as the identification is provided by the object identifiers.
We next show the odl syntax for introducing sub-classes with the
example of the definition of SPORTSCAR.
interface SportsCar: Automobile
{attribute integer MaxSpeed}
language. The standard defines two bindings towards the languages c++ and
Smalltalk, for defining both the class structure and the implementations of
methods. Within the AUTOMOBILE class, the interfaces of methods init and
Increase are defined in odl as follows:
interface Automobile
{...
Automobile init (in string RegistrationNumber_par,
in string Model_par,
in string Colour_par,
in integer Price_par);
void Increase (in integer Amount) raises(ExcessivePrice);
}
The methods in general have multiple input parameters, and can return
zero or more output parameters. In odl, each parameter is characterized by
the keywords in, out, or inout (when a dual role is carried out). In addition,
a method can be characterized by a main output parameter, returned by the
method. The void clause in one of the two examples indicates the absence of
the main parameter. The raises clause indicates the presence of an exception,
which is raised whenever the price evaluated by the method is excessive.
The two examples of definition in o2 and odl show quite different styles
and syntaxes, but also show many common concepts.
420 Chapter 11
Object databases
Given the schema in Figure 11.2, it is possible to ask whether there exist
people who are both drivers and manufacturers of the same sports cars:
select x.Driver.Name
from x in VintageSportsCar
where x.Driver = x.Manufacturer.President
The result has the type bag(string); in this case there can be several
different drivers with the same name. Note that the above query requires the
identity of the person who is both a driver and president of the firm of
manufacturers. Conversely, the following query also retrieves pairs of
homonymous persons, that is, it requires equality of their names, and thus it
is an incorrect formulation of the previous query:
select x.Driver.Name
from x in VintageSportsCar
where x.Driver.Name = x.Manufacturer.President.Name
Ferrari sports cars that were constructed at Maranello and have a maximum
speed of over 250 Km/h can be expressed using three variables and two
predicates. The variables are, respectively, on SPORTSCAR, MANUFACTURER and
FACTORY. The predicates link the variables two by two and are thus used in
the same way as a join in sql:
select a.RegistrationNumber
from a in SportsCar, c in Manufacturer, s in Factory
where c = a.Manufacturer and s in c.Factories
and s.City = "Maranello" and c.Name = "Ferrari"
and a.MaxSpeed > 250
Note that in the evaluation of the query, the variable x is associated to those
sports cars that satisfy the selection condition on prices, and y is associated
with those factories that are related to the selected sports cars. The type of
the result is set(record(string,bag(record(string,integer)))) .
We can examine the use of an oql sub-query within the from clause, for
the query that extracts the number of models of cars built by manufacturers
that have a global total of employees, in all factories, higher than 4500.
select count(select distinct x.Model
from x in
(select y
from y in Automobile, z in Manufacturer
where z = y.Maker
and sum(z.Factories.NoOfEmployees) > 4500))
In this case, the aggregate function count is evaluated in the target list (on
a set without duplicates) and the function sum is evaluated in the where clause
422 Chapter 11
Object databases
of the most internal query (on a set). In general, the aggregate functions
count, min, max, avg, and sum can be applied to sets, bags or lists.
Groupings and orderings Finally, we will show some examples of the use
of grouping and ordering, which are provided in oql. For example, the
following query retrieves the list of registration numbers in the class of cars:
sort x in Automobile by x.RegistrationNumber
The next query has the same expressive power as an sql query with
grouping. It extracts the number of cars grouped according to their
manufacturers. Note the keyword partition, which denotes each partition
obtained using the group by clause.
group a in Automobile
by (constr: a.Maker)
with (AutoNumber: count(select x
from x in partition))
The result is an object consisting of a set of tuples, which list, for each
value of Maker in AUTOMOBILE, the set of AUTOMOBILE objects with that value
(denoted through their oid) and the cardinality of that set. The type of the
result is therefore set(struct(string,set(OID),integer)).
Finally, grouping can happen according to partition predicates. For
example, the next query classifies the sports cars into low, medium and high
according to the price:
group a in SportsCars
by (Low:
a.Price <
Medium: a.Price >=
a.Price <
High: a.Price >=
50000,
50000 and
100000,
100000)
In this example, the type PersType can also be used in other tables. It is thus
possible to define:
create table Industrial of type PersType
create table Driver of type PersType
Note that objects and classes in oodbmss correspond to tuples and tables
424 Chapter 11
Object databases
in ordbmss. In the context of ordmbss, the terms object and tuple are
interchangeable.
As in oodbms, it is possible to use type constructors orthogonally to
construct arbitrarily complex types. It is further possible to use references
from one type to another type, and thus create shared objects in the
database. Let us return to the example in Section 11.1 and illustrate the
definition of the corresponding tuple types, other than PersType. Note the use
of the setof constructor (as a constructor of sets) and ref (to denote a
reference to one type from another).
create row type FactoryType(
Name varchar(25),
City varchar(7),
NoOfEmployees integer)
create row type ManufacturerType(
ConstrId ref(ManufacturerType),
Name varchar(25),
President ref(PersType),
Factories setof(FactoryType))
create row type CarTypeParts(
Motor char(10),
ShockAbsorber char(5))
create row type AutoType(
RegistrationNumber char(10) primary key,
Model varchar(30),
Maker ref(ManufacturerType),
MechanicalParts CarTypeParts)
Note that the types FactoryType and CarTypeParts are used within the types
ManufacturerType and AutoType without introducing the construct ref, and
thus without the introduction of independent objects. In this way, we
construct tables that include sub-tables (at schema level) and objects that
include as components sub-objects (at instance level), by guaranteeing an
arbitrary structural complexity.
Note also that in the definition of the tuple type ManufacturerType, the
attribute ManufacturerId is a reference to ManufacturerType itself, that is, to the
type that is currently being defined. In this case, ManufacturerId carries out
the role of oid; the values for ManufacturerId are system-generated but they
can be used in the queries in the same way as any other attribute and can
carry out the role of key. If this reference mechanism is not explicitly used in
the type definition, then the system generates one oid for each object, but
oids cannot be accessed in the queries. Note moreover, that this use of
identifiers can cause the presence of dangling tuples. Whenever references to
oids are explicitly cancelled or modified by the queries, the system
guarantees the referential integrity only of those references that are not
explicitly modifiable by the users.
At this point, we can create tables for the concepts AUTOMOBILE and
Note that the scope clause limits the possible values present in the
attribute President, of the type PersType, to be a reference to the tuples
belonging to the INDUSTRIAL table. If this clause were omitted, the values of
the attribute President could be generic objects of the type PersType, present
in any table that uses this type.
Hierarchies In sql-3, we can define type and table hierarchies. Type
hierarchies are used for extending previously defined types by adding new
properties to them. For example, we can construct a tuple type
VintageCarType by adding the attribute ManufactureYear, as follows:
create row type VintageCarType(
ManufactureYear integer)
under AutoType
426 Chapter 11
Object databases
functions carry out the same role as the methods, discussed in Section 11.1.3,
and in particular include standard forms for the constructor, accessors to the
various attributes, and transformers. We can deny access privileges on the
methods, obtaining the effect of encapsulating the data.
Let us look at the definition of the abstract data type CarTypeParts, which
also includes the functions equals and greater than to express, respectively,
equality and comparison between two parts.
create type CarTypeParts(
Motor char(10),
Power integer,
Cylinders integer,
equals EqualPower,
greater than GreaterPower)
Below, we will look briefly at two new features of sql-3, called deferencing
and double dot notation, which allow access either to related objects or to
component sub-objects. We will also look at the operations for nesting and
unnesting, which enable the modification of complex object structures.
Deferencing Navigation among the references between types in sql-3
requires the deferencing operator. This operator allows access from a source
object x to an object y referenced in x as an object-valued property A in the
following way: x -> A. The following example shows the use of the deferencing operator to access the value of the attribute Name of the object of the
INDUSTRIAL table from objects of the MANUFACTURER table. In particular, it
accesses those objects that satisfy the predicate Name = 'Fiat'.
select President -> Name
from Manufacturer
where Name = 'Fiat'
In sql-3 the attributes of types oid can be used explicitly in queries, and
in particular can be compared by the equality operator with the references
to tuples of the same type. The above query can thus be expressed as follows:
select Name
from Manufacturer, Industrial
where Manufacturer.Name = 'Fiat'
and Manufacturer.President = Industrial.ManufacturerId
428 Chapter 11
Object databases
key is not available among the attributes defined by the user. Otherwise,
it is better to resort to key attributes for the identification of objects.
5. Active rules (triggers) and passive rules (integrity constraints) will
become an essential component of third generation dbmss.
6. Independently of how much this is desirable, sql is the reference
language for dbmss (it is a communication language for intergalactic
dataspeak).
Discussing the merits of the third generation manifesto, written in 1990, is
difficult. Nevertheless, we can at least state that the last prophecy has been
confirmed in the last decade relating to sql. In spite of the success of oql,
sql remains clearly the most widespread language in commercial databases.
430 Chapter 11
Object databases
particular patterns. For example, suppose we want to find all the music of
Wagner in which the same theme occurs, or the video sequences in which
there appear goals scored by the Brazilian national soccer team. In these
cases, the pattern must be reconstructed by operating on multiple frames or
temporal windows. In all of the above cases, the query must initially exploit
the structural information associated with multimedia data, in order to
extract a few multimedia objects. After this, it will be necessary to evaluate,
for each extracted object, its similarity to the criteria requested.
In general, it is possible to select a multimedia object only in probabilistic
terms. It will therefore be possible to present the result of a query starting
from data that has the highest probability of satisfying the request, by
setting a given probability threshold. Adaptive techniques allow the user to
guide the search.
432 Chapter 11
Object databases
it is relevant, that is, whether it should be part of the result of the query or
not. Then:
precision indicates the number of relevant documents extracted as a
percentage of the total extracted documents;
recall indicates the number of relevant documents extracted as a
percentage of the total documents in the database.
A good search algorithm must try to take account of both these factors.
Firstly, it must offer a good degree of precision, to present documents that are
highly relevant. Secondly, it needs a good recall, to reduce the risk of
omitting the documents that are most relevant to the user.
Several techniques can be used for describing textual documents.
Documents can be represented by using matrices, in which the rows and
columns of the matrix represent respectively the documents and the
keywords, and each element of the matrix represents the frequency of the
keyword within the document.
Alternatively, we can represent the correspondence between keywords
and documents by using inverted indexes. The indexes are organized as
trees, containing at their intermediate nodes the keywords as illustrated in
Section 9.5.5; leaves of the tree contain the identifiers of the documents
corresponding to each keyword. Using these data structures, it is easy to
extract the identifiers of documents that satisfy boolean conditions on
keywords. For example, the intersection of two keywords is obtained simply
by making an intersection of the sets of document identifiers. This is done by
running through the index twice, with the two keywords selected by the
user.
Other data structures first highlight the keywords of high frequency. For
example, the signature of a document is a compact representation of the n
keywords that appear in a document with highest frequency. Matching
techniques are used for extracting the documents that respond to a given
query by looking only at their signature; the signature can also be used to
decide whether two documents are similar.
regions that are near to a given region; or determining the points of a map
that are found along a line, and could represent the cities found along the
course of a river. Obviously, giss can describe not only the arrangement of
data in the space, but also the characteristics of each point, line or region of
the space. For example, points that describe cities have other information, as
well as their own geographical co-ordinates, such as the population and the
height above sea-level. Regions are characterized, for example, by the
prevalent type of agriculture, or by the average monthly rainfall per unit
surface. Lines can represent rivers, roads or railways. Using giss, it will be
possible to express queries in which spatial aspects are involved. The
efficient management of spatial data requires the information to be organized
using special data structures, which allow efficient execution of the queries.
In particular, various types of tree structures allow the management of
collections of points (for example, the cities in a geographical map) and they
respond efficiently to queries of the following type: Extract all the cities that
are less than a certain distance from a determined point. Each point is
represented by a tree node. Each node includes its co-ordinates X and Y, an
item of information specific to the node and the pointers to the nodes of the
successors.
In 2-d tree organization (see Figure 11.5), each node has at most two
First division
Third division
Second division
Fourth division
434 Chapter 11
Object databases
D
C
A
Third quad
division
436 Chapter 11
Object databases
To carry out a query or a program that uses this path, we must find
automobiles which are linked to cities with given values, such as London,
Boston, etc., by going along the paths connecting CITY to AUTOMOBILE in the
schema. In this way, we find the objects of the class AUTOMOBILE that satisfy
the predicate:
X.Maker.Factories.City.Name = London
A nested index directly connects the values that appear at one end of the
chain to the objects that are at the other end of the chain. Thus, from the
constant value London one immediately makes access to all the objects
of the class AUTOMOBILE that satisfy the predicate.
Finally, an index path expression allows us to navigate from the values that
appear at the end of the path to the objects of all the classes that appear
along the path. Thus, from a city name it is possible to navigate backwards to CITY objects, FACTORY objects, MANUFACTURER objects or
AUTOMOBILE objects.
Indexes introduced in this way are created with b+ trees of a particular
structure. In managing these trees, we must take into account the creation
and cancellation of intermediate objects, which can change the structure of
the indexes.
438 Chapter 11
Object databases
11.5.4 Transactions
On a transactional level, object-oriented databases offer, in addition to the
classic acid transaction properties, also less restrictive mechanisms, which
facilitate the co-operation among users. Let us look briefly at the
requirements of some of these mechanisms.
In concurrency control, check-out operations are normally defined. These
allow the loading of entire objects to the workstation on which a user is
operating, at the same time allowing other users to access the object on
the database. In this way, the user may work on an object for long work
sessions, at the same time allowing other users to have access to the object
present on the server. Obviously, the dual operation of check-in, in which
the object is returned to the server, requires the co-operation with the
other users, who must be informed of the changes to the object.
Another method for guaranteeing concurrent co-operative processes is
based on the use of versions, that is, various copies of the same object at
different times. In this case, it is also possible to define relations between
objects that represent the evolution constraints between different
versions. This problem is particularly critical in the context of case tools,
in which it is necessary to allow for and to document the evolution of
software and to guarantee that various versions constitute a unique
logical unit (for example, with respect to the process of compilation and
linking).
In some applications, long transactions can be expected. An example is the
transaction that occurs between the check-in and check-out of an object,
which can be transferred to a design workstation for days at a time; yet,
from the viewpoint of the other users, the changes constitute a unique,
long transaction.
Finally, transactions can be composed of nested transactions. This
approach, which can also be developed for relational systems, occurs
when a client process must create a unique task by communicating with
various distinct servers that do not allow the use of global transactions. In
this case, the global atomicity is obtained by means of co-ordination
440 Chapter 11
Object databases
Object
implementation
Client
Dynamic
Invocation
IDL
skeletons
Stubs in
IDL
ORB
services
Object
adaptor
user interfaces;
information management
11.6 Bibliography
Various texts are specifically dedicated to object-oriented databases, including the books by Bertino and Martino [10], Cattel [13] and Loomis [56]. The
book edited by Bancilhon, Delobel, and Kanellakis [6], contains various articles on the o2 system. The first article contains the Manifesto of object-oriented databases mentioned in the text. The standard odmg-93 and the odl and
oql languages are described Cattel [13], while the corba architecture is described in various omg publications, including Siegel [77]. A global view of
relational and object-oriented databases is offered by Stonebraker [79], while
the Manifesto on third generation databases is published as Stonebraker et al.
[81]. Multimedia databases are described in the book by Subrahmanian [83].
A classic text on spatial data structures is that by Samet [73].
442 Chapter 11
Object databases
11.7 Exercises
Exercise 11.1 Define a class GEOMETRICFIGURE and three subclasses SQUARE,
CIRCLE and RECTANGLE. Define a method Area, the implementation of which
in GEOMETRICFIGURE returns the value zero, while in the sub-classes it is
evaluated as a function of the properties of the given sub-classes. Show the
invocation of the method by a program that scans a list of geometric figures
of an arbitrary nature.
Exercise 11.2 Define a data dictionary of an object-oriented database.
Suggest the introduction of classes and hierarchies concerning various
concepts of an object-oriented schema, classes, atomic types, types
structured using various constructors, generalization hierarchies and
methods with their input/output parameters. Populate the data dictionary
with data that describes part of the schema dealing with the management of
automobiles, described in Figure 11.2. Then think of a query that allows, for
example, the extraction of a list of the classes and methods with covariant
redefinition of the output parameters.
Exercise 11.3 Consider the following schema of an o2 object-oriented
database:
add class City
type tuple(Name: string,
Nation: string,
Monuments: set(Monuments),
Hotels: list(Hotel));
add class Hotel
type tuple(Name: string,
Address: tuple(Street: string,
City: City,
Number: integer,
PostCode: string);
Stars: integer,
Features: list(string));
add class Place
type tuple(Name: string,
Photograph: Bitmap,
Address: tuple(Street: string,
City: City,
Number: integer,
PostCode: string);
ThingsToSee: set(TouristService));
add class Monument inherits Place
type tuple(ConstructionDate: date,
ClosingDays: list(string),
AdmissionPrice: integer,
Architect: Person);
444 Chapter 11
Object databases
3. Extract the names of the five star hotels in the cities in which concerts
conducted by Muti are planned.
4. Extract the names of the monuments in Paris created by Italian
architects.
5. Extract the tourist services on offer partly in Paris and partly in another
city.
6. Extract the names of the artistic directors of a theatre where no concerts
are presented.
7. Extract the title of the concert, conductor, musicians and instruments
used by each musician in the concerts of 12-2-99 in Milan.
8. Extract the cities having more than 10 monuments and fewer than five
hotels.
9. Extract the names of the French architects who are also musicians.
10.Extract the total number of concerts conducted by Muti in either Italian
or French theatres.
11. Extract the total number of concerts given in each Italian theatre.
12.Classify the monuments in Paris according to the date of construction.
Use the classifications: Renaissance (from 1450 to 1550), Baroque (from
1550 to 1800), Imperial (from 1800 to 1900), Modern (from 1900 to
today), and count the number of elements in each class.
Exercise 11.6 Use the sql-3 syntax to describe the object model presented
in Exercise 11.3 (represent o2 lists as sets).
Exercise 11.7 Considering the sql-3 database schema introduced in the
previous exercise, express the following queries in sql-3.
1. Retrieve the names of the cities having Liechtenstein as nation.
2. Retrieve the names of the musicians playing in the concerts directed by
Karajan.
3. Retrieve the names of the monuments in London constructed in the 17th
Century and closed on Mondays.
4. Retrieve the names of the directors who perform at theatres different
from those to which they are affiliated.
5. Retrieve, for each theatre, the titles of all of the concerts that are planned
for the year 2000.
Exercise 11.8 Build a 2d-tree and quadtree representation of the sequence
of bi-dimensional points: A(5,4), B(3,3) C(6,2), D(2,2), E(4,6), F(1,1), G(7,5).
446 Chapter 11
Object databases
12
12Active databases
An active database system is a dbms that supports an integrated subsystem
for the definition and management of production rules (active rules). The
rules follow the event-condition-action paradigm: each rule reacts to some
events, evaluates a condition and, based on the truth value of the condition,
might carry out an action. The execution of the rules happens under the
control of an autonomous subsystem, known as the rule engine, which keeps
track of the events that have occurred and schedules the rules for execution.
Thus, an active database system can execute either transactions, which are
explicitly initiated by the users, or rules, which are under the control of the
system. We say that the resulting system exhibits a reactive behaviour, which
differs from the typical passive behaviour of a dbms without active rules.
When a dbms is active, the part of the application that is normally
encoded by programs can also be expressed by means of active rules. As we
shall see, active rules can, for example, manage integrity constraints,
calculate derived data and manage exceptions, as well as pursue business
objectives. This phenomenon adds a new dimension to the independence of
the database, called knowledge independence: knowledge of a reactive type is
removed from the application programs and coded in the form of active rules.
Knowledge independence introduces an important advantage, because rules
are defined with the ddl and are part of the schema, and therefore they are
shared by all the applications, instead of being replicated in all the
application programs. Modifications to the reactive behaviour can be
managed by simply changing the active rules, without the need to modify
the applications.
Many prototype systems, both relational and object-oriented, provide
active rules that are particularly expressive and powerful. In this chapter, we
will concentrate on active databases supported by relational dbmss; almost
all relational systems support simple active rules, called triggers, and
therefore can be considered active databases in their own right. In this
chapter we will use the terms active rule and trigger as synonymous.
448 Chapter 12
Active databases
that the triggers are cascading. Triggers can also activate themselves one after
another indefinitely, generating a computation that does not terminate. We
will address this problem in Section 12.5.
The Mode is before or after, the Event is insert, delete, or update; update
may be followed by attribute names of the target table. The referencing
clause allows the introduction of variable names, for referring to the old and
new values of the row that is changed, with one or both of the following
clauses:
old as OldVariable
| new as NewVariable
450 Chapter 12
Active databases
particular part of the WAREHOUSE table falls below a specific reorder level
(QtyLimit):
create trigger Reorder
after update of QtyAvbl on Warehouse
when (new.QtyAvbl < new.QtyLimit)
for each row
declare
X number;
begin
select count(*) into X
from PendingOrders
where Part = new.Part;
if X = 0
then
insert into PendingOrders
values (new.Part, new.QtyReord, sysdate);
end if;
end;
452 Chapter 12
Active databases
tuple (1, 100, 10/10/1999). Suppose that next the following transaction is
carried out:
T2: update Warehouse
set QtyAvbl = QtyAvbl - 60
where Part <= 3
The trigger is thus considered for all parts, and the condition is verified for
parts 1 and 3. However, the action on part 1 has no effect, because we assume
that PENDINGORDERS still contains the tuple relating to part 1. Thus, the
execution of the trigger causes the insertion into PENDINGORDERS of the single
tuple (3, 120, 10/10/1999), relating to part 3.
where the Mode is before or after, the Event is insert, delete, or update
(update may be followed by attributes of the target table), and the Level is row
or statement. The referencing clause allows the introduction of variable
names. If the level is row, the variables refer to the tuple that is changed; they
are defined by the clauses:
old as OldTupleVar
| new as NewTupleVar
If the level is statement, then the variables refer to the table that is changed,
with the clauses:
old_table as OldTableVar
| new_table as NewTableVar
454 Chapter 12
Active databases
Supplier attribute unless it is changed to the null value. In all the other
cases, this gives an exception that forces a rollback of the primitive.
For example, the removal from the DISTRIBUTOR table of all the suppliers
located in Penang causes the violation of the referential integrity constraint.
At this point, the management policy for violations of the constraint causes
the modification to the null value of all the tuples of the PART table that
remain dangling after the deletion. This activates the two triggers
SoleSupplier and AuditPart. The first is a before-trigger, which is thus
considered first. Its evaluation, tuple by tuple, happens logically before the
modification, but it has available the N value, which describes the variation.
Thus, this value is found to be NULL, and the condition is found to be false.
Finally, the AuditPart trigger is activated, inserting into the table AUDIT a
single tuple containing the user code, the current data and the number of
modified tuples.
456 Chapter 12
Active databases
R2
R4
R3
The activation graph for this rule has only one node and a ring; thus, it
presents a cycle, which indicates the possibility that the rule is re-activated
by itself. On the other hand, whatever the initial transaction, the execution
of the rule eventually terminates, as the rule progressively reduces the
salaries until they are again within the established level. At this point, the
condition is false. However, a slightly different rule, which gives a rise of
salary rather than decreasing it, presents termination problems:
create trigger SalaryControl2
after update of Salary on Employee
then update Employee
set Salary = 1.1 * Salary
where (select avg(Salary) from Employee) > 100
The activation graph associated with this rule does not change. However,
if the rule is executed once, it will be executed an infinite number of times,
causing non-termination, as the operation carried out by the rule is unable
to make its condition false.
This example shows that the cycles give only indications of possible
causes of non-termination. A detailed analysis of cycles, which can be partly
automated, can give rise to the conclusion that a cycle is innocuous, or
instead to suggest modifications to rules that guarantee its termination.
458 Chapter 12
Active databases
Note that this assertion indicates a property that must be true for all
employees, but in an active rule, we are interested in capturing the situations
that violate the constraint. We will therefore use the negation of the assertion
illustrated above as the basis for building the condition to be included
within the active rules:
We then need to construct four active rules. Two react to each insertion in
EMPLOYEE or modification of the Dept attribute, cancelling the effect of the
Note that the condition is simpler than that shown above. It identifies as
critical those employees whose departments coincide with a department
removed by the delete operation. In fact, the condition could even be
omitted, as the action is performed on all and only the tuples that satisfy the
condition.
460 Chapter 12
Active databases
Note that in this case, too, the condition is optimized and could even be
omitted.
manager, and the insertion of a new employee. Let us suppose that among
these, the critical modification to be monitored is the increase in the salary
awarded to an employee. Let us also suppose that the reaction policy is to
block the update, and to signal this behaviour. These choices correspond to
the following trigger, written using the db2 syntax:
create trigger ExcessiveSalary
after update on Salary of Employee
for each row
when New.Salary > select Salary
from Employee
where EmpNum in
(select Director
from Department
where DeptNum = New.DeptNum)
then signal sqlstate '70005' ('Salary too high')
12.7 Bibliography
The book by Widom and Ceri [91], contains a thorough description of active
database research prototypes, as well as a general introduction to commercial
systems and applications. The book includes chapters dedicated to several
research prototypes: Postgres, Ariel, Starburst, a-rdl, Chimera, Hipac, and
Ode; two chapters also discuss commercial systems and applications.
Furthermore, one of the six parts of Zaniolo et al. [94] is dedicated to active
databases. A description of the triggers available in Oracle Server is discussed
in their manuals: [65] and [64]. Triggers in ibm db2 are described by
Cochrane, Pirahesh, and Mattos [25], as well as in the book by Chamberlin
[20] that gives a complete description of the db2 system. The possibility of
giving a declarative specification and then deriving the active rules was
introduced by Ceri and Widom [19]. A methodology for design of databases
that makes much use of active rules and also of object-oriented services is
described in the book by Ceri and Fraternali [15].
12.8 Exercises
Exercise 12.1 Given the relational schema:
EMPLOYEE(Name, Salary, DeptNum)
DEPARTMENT(DeptNum, ManagerName)
462 Chapter 12
Active databases
1. Each student must work in the same area as his or her supervisor.
2. Each student must have taken at least three courses in the subject of his
or her supervisor.
3. Each student must have taken the exam for the course taught by his or
her supervisor.
464 Chapter 12
Active databases
13
13Data analysis
Database technology, as illustrated in Chapter 9 and Chapter 10, was
developed for supporting efficient and reliable on-line data management.
Using On Line Transaction Processing (oltp) technology, companies collect
large amounts of data that enable their everyday operations. For example,
banks collect data about their clients transactions, and large supermarket
chains collect data about their daily sales. This data could be useful to the
companies not only on an operational level, but also for management,
planning and decision support. For example, it could be possible to
determine which of the financial products offered by a bank are the most
successful, or observe the variations in sales of the various products in
relation to the various promotions to evaluate their effectiveness. In practice,
past and present data allows an analysis process essential for planning the
future undertakings of the business.
For many decades, technological developments have neglected data
analysis. It was thought that query languages were sufficient for both
operational activity and for data analysis. Indeed, sql allows the
specification of complex queries, and thus offers some useful characteristics
for data analysis (such as data aggregation). However, data analysis languages
must be suitable for users who are not computer experts, and sql is not
sufficiently user-friendly for them. Moreover, it is extremely difficult to
optimize data management so that it simultaneously satisfies the demands of
analysis and operational applications; thus, the latter have generally
prevailed over the former.
At the beginning of the nineties, parallel to the development of networks
and data distribution products, a new architecture became popular,
characterized by the separation of environments: alongside the traditional
oltp systems, other systems were developed, dedicated to On-line Analytical
Processing (olap). This term states that data analysis happens on-line, that is,
by means of interactive tools. The main element of olap architecture is the
data warehouse, which carries out the same role as the database for oltp
architectures. New languages and paradigms were developed in order to
466 Chapter 13
Data analysis
Database
Data
warehouse
END USERS
On-line processing
ANALYSTS
Complex queries
data warehouse to facilitate data analysis. We will conclude the chapter with
a brief description of the main data mining problems and techniques.
Export
Data warehouse
Data mining
Data access
Refresh
Loader
Export
Export
Export
Filter
Filter
Filter
Data
source
Data
source
Data
source
468 Chapter 13
Data analysis
The export component is used for extracting data from the data sources.
The process of extraction is normally incremental: the export component
builds the collection of all the changes to the data source, which are next
imported by the dw.
The next five components operate on the data warehouse.
The loader is responsible for the initial loading of data into the dw. This
component prepares the data for operational use, carrying out both
ordering and aggregation operations and constructing the dw data
structures. Typically, loader operations are carried out in batches when
the dw is not used for analysis (for instance, at night). If the dw uses
parallelism (illustrated in Section 10.7), this module also takes care of
initial fragmentation of data. In some applications, characterized by a
limited quantity of data, this module is invoked for loading the entire
contents of the dw after each change to the data sources. More often, the
dw data is incrementally updated by the refresh component, which is
discussed next.
The refresh component updates the contents of the dw by incrementally
propagating to it the updates of the data sources. Changes in the data
source are captured by means of two techniques: data shipping and
transaction shipping. The first one uses triggers (see Chapter 12) installed
in the data source, which, transparent to the applications, records
deletions, insertions, and modifications in suitable variation files.
Modifications are often treated as pairs of insertions and deletions. The
second technique uses the transaction log (see Section 9.4.2) to construct
the variation files. In both cases, the variation files are transmitted to the
dw and then used to refresh the dw; old values are typically marked as
historic data, but not deleted.
The data access component is responsible for carrying out operations of
data analysis. In the dw, this module efficiently computes complex
relational queries, with joins, orderings, and complex aggregations. It also
computes dw-specific new operations, such as roll up, drill down and data
cube, which will be illustrated in Section 13.3. This module is paired with
client systems that offer user-friendly interfaces, suitable for the data
analysis specialist.
The data mining component allows the execution of complex research on
information hidden within the data, using the techniques discussed in
Section 13.5.
The export component is used for exporting the data present in a
warehouse to other dws, thus creating a hierarchical architecture.
In addition, DWs are often provided with modules that support their
design and management:
470 Chapter 13
Data analysis
PRODUCT
(0,N)
(1,1)
SUPERMARKET
(0,N)
(1,1)
SALE
(1,1)
(0,N)
PROMOTION
(1,1)
(0,N)
TIME
POLICY
(0,N)
(1,1)
CLIENT
(0,N)
(1,1)
PAYMENT
(1,1)
(1,1)
(0,N)
TIME
(0,N)
PROBLEM
PATIENT
(0,N)
(1,1)
DOCTOR
(0,N)
(1,1)
THERAPY
(1,1)
(0,N)
ILLNESS
(1,1)
(0,N)
HOSPITAL
472 Chapter 13
Data analysis
The products are identified by the code (ProdCode) and have as attributes
Name, Category, Subcategory, Brand, Weight, and Supplier.
The supermarkets are identified by the code (MarketCode) and have as
attributes Name, City, Region, Zone, Size, and Layout of the supermarket
(e.g., on one floor or on two floors, and so on).
The promotions are identified by the code (PromoCode) and have as
attributes Name, Type, discount Percentage, FlagCoupon, (which indicates
the presence of coupons in newspapers), StartDate and EndDate of the
promotions, Cost and advertising Agency.
Time is identified by the code (TimeCode) and has attributes that describe
the day of sale within the week (DayWk: Sunday to Saturday), of the
month (DayMonth: 1 to 31) and the year (DayYear: 1 to 365), then the week
within the month (WeekMonth) and the year (WeekYear), then the month
within the year (MonthYear), the Season, the PreholidayFlag, which indicates
whether the sale happens on a day before a holiday and finally the
HolidayFlag, which indicates whether the sale happens during a holiday.
Dimensions, in general, present redundancies (due to the lack of
normalization, see Chapter 8) and derived data. For example, in the time
dimension, from the day of the year and a calendar we can derive the values
of all the other time-related attributes. Similarly, if a city appears several
times in the SUPERMARKET relation, its region and zone are repeated.
Redundancy is introduced to facilitate as much as possible the data analysis
operations and to make them more efficient; for example, to allow the
selection of all the sales that happen in April or on a Monday before a
holiday.
The e-r schema is translated, using the techniques discussed in Chapter 7,
into a logical relational schema, arranged as follows:
SALE(ProdCode, MarketCode, PromoCode, TimeCode, Qty, Revenue)
PRODUCT(ProdCode, Name, Category, SubCategory, Brand, Weight, Supplier)
MARKET(MarketCode, Name, City, Region, Zone, Size, Layout)
PROMOTION(PromoCode, Name, Type, Percentage, FlagCoupon, StartDate,
EndDate, Cost, Agency)
TIME(TimeCode, DayWeek, DayMonth, DayYear, WeekMonth, WeekYear,
MonthYear, Season, PreholidayFlag, HolidayFlag)
The fact relation is in BoyceCodd normal form (see Chapter 8), in that each
attribute that is not a key depends functionally on the sole key of the
relation. On the other hand, as we have discussed above, dimensions are
generally non-normalized relations. Finally, there are four referential
integrity constraints between each of the attributes that make up the key of
the fact table and the dimension tables. Each of the four codes that make up
the key of the fact table is an external key, referencing to the dimension table
which has it as primary key.
(0,N)
(1,1)
PRODUCT
(1,1)
(0,N)
(1,1)
(0,N)
(1,1)
(0,N)
CATEGORY
(0,N)
(1,1)
SUPER-
MARKET
(0,N)
(1,1)
SALE
(1,1)
(1,1)
(0,N)
(0,N)
CITY
DAY
PROMOTION
MONTH
(1,1)
(0,N)
YEAR
(1,1)
(0,N)
REGION
(1,1)
(0,N)
ZONE
474 Chapter 13
Data analysis
The main advantage of the star schema is its simplicity, which, as we will
see in the next section, allows the creation of only very simple interfaces for
the formulation of queries. The snowflake schema represents the hierarchies
explicitly, thus reducing redundancies and anomalies, but it is slightly more
complex. However, easy-to-use interfaces are also available with this schema
in order to support the preparation of queries. Below, we will assume the use
of the star schema, leaving the reader to deal with the generalization in the
case of the snowflake schema.
PROMOTION.Name
Three for two
Coupon 15%
SuperSaver
SuperSaver
PRODUCT.Name
Wine
Pasta
Oil
PastaOil
PRODUCT.Name
TIME .Month
JanDec
Qty Schema
Options
FebApr
TIME .Month
Condition
sum View
In the specific case, the query constructed according to the user choices is
as follows:
select Time.Month, Product.Name, sum(Qty)
from Sale, Time, Product
where Sale.TimeCode = Time.TimeCode
and Sale.ProductCode = Product.ProductCode
and Sale.PromoCode = Promotion.PromoCode
and (Product.Name = 'Pasta' or Product.Name = 'Oil')
and Time.Month between 'Feb' and 'Apr'
and Promotion.Name = 'SuperSaver'
group by Time.Month, Product.Name
order by Time.Month, Product.Name
The query result can be presented on the olap client in matrix or in graph
form. In matrix form, dimensions correspond to the rows and columns, and
facts correspond to the cells, as in a spreadsheet (Figure 13.8).
Oil
Pasta
Feb
5K
45K
Mar
5K
50K
Apr
7K
51K
476 Chapter 13
Data analysis
Time.Month
Feb
Mar
Apr
Product.Name sum(Qty)
Pasta
45K
Pasta
50K
Pasta
51K
Product.Name
Pasta
Pasta
Pasta
Pasta
Pasta
Pasta
Pasta
Pasta
Pasta
Zone
sum(Qty)
North
18K
Centre
15K
South
12K
North
18K
Centre
18K
South
14K
North
18K
Centre
17K
South
16K
Zone
sum(Qty)
North
54K
Centre
50K
South
42K
Year
1998
1999
1998
Colour Sales
Red
50
Red
85
Red
80
478 Chapter 13
Data analysis
Make
Ferrari
Ferrari
Ferrari
Ferrari
Ferrari
Ferrari
Porsche
Porsche
Porsche
Porsche
ALL
ALL
ALL
ALL
ALL
ALL
1999
1998
Red
ALL
ALL
ALL
Ferrari
Porsche
Make
Ferrari
Ferrari
Porsche
Ferrari
Ferrari
Porsche
Ferrari
Porsche
Year
1998
1999
1998
1998
1999
1998
ALL
ALL
ALL
ALL
Colour sum(Sales)
Red
50
Red
85
Red
80
ALL
50
ALL
85
ALL
80
ALL
135
ALL
80
ALL
215
480 Chapter 13
Data analysis
As always in the case of physical design (see Chapter 9), the use of bitmap
and join indexes is subject to a costbenefit analysis. The costs are essentially
due to the necessity for constructing and storing indexes permanently, and
the benefits are related to the actual use by the dw system for the resolution
of queries.
482 Chapter 13
Data analysis
Transaction
1
1
2
2
2
3
4
4
Date
17/12/98
17/12/98
18/12/98
18/12/98
18/12/98
18/12/98
19/12/98
19/12/98
Goods
Qty Price
ski-pants 1
140
boots
1
180
T-shirt
1
25
jacket
1
300
boots
1
70
jacket
1
300
jacket
1
300
T-shirt
3
25
484 Chapter 13
Data analysis
Premise
ski-pants
boots
T-shirt
T-shirt
boots
boots
jacket
jacket
{T-shirt, boots}
{T-shirt, jacket}
{boots, jacket}
attributes of the relation that stores the phenomenon. When the phenomena
are described by a large number of attributes, the classifiers also take
responsibility for the selection of few significant attributes, separating them
from the irrelevant ones.
Suppose we want to classify the policies of an insurance company,
attributing to each one a high or low risk. Starting with a collection of
observations that describe policies, the classifier initially determines that the
sole significant attributes for definition of the risk of a policy are the age of
the driver and the type of vehicle. It then constructs a decision tree, as
shown in Figure 13.18. A high risk is attributed to all drivers below the age
of 23, or to drivers of sports cars and trucks.
POLICY(Number, Age, AutoType)
Age < 23
true
false
AutoType = Sports
true
false
AutoType = Trucks
true
false
486 Chapter 13
Data analysis
13.6 Bibliography
In spite of the fact that the multi-dimensional data model was defined
towards the end of the seventies, the first olap systems emerged only at the
beginning of the nineties. A definition of the characteristics of olap and a
list of rules that the olap systems must satisfy is given by Codd [31].
Although the sector was developed only a few years ago, many books
describe design techniques for dws. They include Inmon [49] and Kimball
[53]. The data cube operator is introduced by Gray et al. [45].
The literature on data mining is still very recent. A systematic
presentation of the problems of the sector can be found in the book edited
by Fayyad et al. [40], which is a collection of various articles. Among them,
we recommend the introductory article by Fayyad, Piatetsky-Shapiro and
Smyth, and the one on association rules by Agrawal, Mannila and others.
13.7 Exercises
Exercise 13.1 Complete the data mart projects illustrated in Figure 13.4 and
Figure 13.5, identifying the attributes of facts and dimensions.
Exercise 13.2 Design the data marts illustrated in Figure 13.4 and
Figure 13.5, identifying the hierarchies among the dimensions.
Exercise 13.3 Refer to the data mart for the management of supermarkets,
described in Section 13.2.2. Design an interactive interface for extraction of
the data about classes of products sold in the various weeks of the year in
stores located in large cities. Write the sql query that corresponds to the
proposed interface.
Exercise 13.4 Describe roll-up and drill-down operations relating to the
result of the query posed in the preceding exercise.
Exercise 13.5 Describe the use of the with cube and with roll up clauses in
conjunction with the query posed in Exercise 13.3.
Exercise 13.6 Indicate a selection of bitmap indexes, join indexes and
materialized views for the data mart described in Section 13.2.2.
Exercise 13.7 Design a data mart for the management of university exams.
Use as facts the results of the exams taken by the students. Use as
dimensions the following:
1. time;
2. the location of the exam (supposing the faculty to be organized over
more than one site);
3. the lecturers involved;
4. the characteristics of the students (for example, the data concerning preuniversity school records, grades achieved in the university admission
exam, and the chosen degree course).
Create both the star schema and the snowflake schema, and give their
translation in relational form. Then express some interfaces for analysis
simulating the execution of the roll up and drill down instructions. Finally,
indicate a choice of bitmap indexes, join indexes and materialized views.
Exercise 13.8 Design one or more data marts for railway management. Use
as facts the total number of daily passengers for each tariff on each train and
on each section of the network. As dimensions, use the tariffs, the
geographical position of the cities on the network, the composition of the
train, the network maintenance and the daily delays.
Create one or more star schemas and give their translation in relational
form.
Exercise 13.9 Consider the database in Figure 13.19. Extract the association
rules with support and confidence higher or equal to 20 per cent. Then
indicate which rules are extracted if a support higher than 50 percent is
requested.
Exercise 13.10 Discretize the prices of the database in Exercise 13.9 into
three values (low, average and high). Transform the data so that for each
transaction a single tuple indicates the presence of at least one sale for each
class. Then construct the association rules that indicate the simultaneous
presence in the same transaction of sales belonging to different price classes.
Finally, interpret the results.
488 Chapter 13
Data analysis
Transaction
1
1
2
2
2
2
3
4
4
5
5
5
Date
17/12/98
17/12/98
18/12/98
18/12/98
18/12/98
18/12/98
18/12/98
19/12/98
19/12/98
20/12/98
20/12/98
20/12/98
Goods
Qty Price
ski-pants 1
140
boots
1
180
ski-pole
1
20
T-shirt
1
25
jacket
1
200
boots
1
70
jacket
1
200
jacket
1
200
T-shirt
3
25
T-shirt
1
25
jacket
1
200
tie
1
25
14
490 Chapter 14
Databases and the World Wide Web
492 Chapter 14
Databases and the World Wide Web
Chapter 1
Section 1.1
Section 1.2
Section 1.3
Preface
Chapter 2
Section 2.1
Section 2.2
Chapter 3
Section 3.1
Node B
Text X
Text Y
Text U
Image Z
Node C
Text V
Text W
14.1.3 HTML
The documents that make up the hypertext structure of the Web are written
in html; html allows the formatting of documents and the description of
hypertext links. We will not dwell on the details of html, but will highlight
some important aspects.
html also allows the creation of pages in which the users can insert values
of parameters, to be sent to the server. These pages use the form construct,
which provides fields where users may insert data. An action is associated
with a form, specifying the URL of the program that must be executed when
494 Chapter 14
Databases and the World Wide Web
the form itself is completed (and submitted to the Web server). The data
specified by the user is transmitted to the program as parameters or as input
data. The page can be used for providing content to be stored in the wis (for
example, by inserting tuples in a database), or for retrieving content, or for
a mix of the two.
14.1.4 HTTP
The World Wide Web is operated by HTTP servers (also called Web servers),
system programs that offer their services to browsers via http, which in turn
uses tcp (and then ip) at the lower level. http consists of four phases.
1. Opening the connection: the browser (which has the role of client) contacts
the http server, at the address and with the protocol indicated in the
url, in order to verify the accuracy and availability. In particular, a tcp
connection is requested;
2. Establishment of the connection: the server (if available) accepts the
connection and sends a confirmation;
3. Request: the client sends a message to the server, with the request for a
service, the details of the resource invoked and possible parameters for
that invocation;
4. Reply: the server communicates whether the request can be satisfied and,
if so, the results; at the same time, the server terminates the connection,
without retaining any information to be used in subsequent
connections.
It is important to underline the fact that the http protocol has no memory
(it is stateless). Thus, when a client issues several http requests to the same
server, the server itself is not able to maintain information about the
operations already carried out on behalf of the same client and of their
results. This choice is motivated by the simplicity of the protocol, which
manages each request separately without keeping records of the context.
Clearly, this represents a limitation of the protocol, because it becomes
difficult to carry out procedures that require multiple interactions, such as
those necessary for database transactions. We will briefly look at how we can
overcome this limitation in Section 14.4.
14.1.5 Gateways
Web servers can invoke programs and pass parameters to them, such as the
data input by the user in a form. Let us look at the basic concept, which we
will develop in Section 14.4 with reference to database access. A gateway is
any program called by a Web server. It can be a compiled program written in
a high-level language, such as c, c++, or Java, or in interpretable languages
such as Perl or Tcl (used for operations on strings), or even in scripting
languages for operating system shells. As the name suggests, a gateway
HTTP
HTTP
server
CGI
CGI
program
...
496 Chapter 14
Databases and the World Wide Web
498 Chapter 14
Databases and the World Wide Web
and cannot be dealt with in a few pages; we will therefore give an overview
of the main issues.
Let us first clarify the extent of the design activity that we are now going
to consider.
One could think of the entire Web as a unified source of information. This
is not a helpful view, as there is no coherence in the form in which the
information is offered, in its quality, substance or usability. At the other
extreme, we could consider the Web as a set of independent pages, each of
which constitutes a specific source of information. This is the point of view
of the lists of bookmarks managed by browsers and of search engines, which
select links to single pages. Both alternatives are obviously inappropriate for
setting the scope of Web design. In fact, design has to concentrate on a
system that is dedicated to a specific application need and under the control
of a single subject, or at least of various co-ordinated subjects who share the
same design goals. Since Web sites usually enjoy this property, we set Web
sites as the extent of the design activity.
We further concentrate our attention to data-intensive Web sites. That is,
sites whose primary purpose is to deliver large amounts of information to a
variety of users. These needs are best served by sites with very regular
structures. For example, the various departments of a university, each with
its own professors and courses, often have similar Web pages,
homogeneously interconnected. Correspondingly, a database may store
information about professors and courses. Given the regularity of Web pages,
it becomes convenient to design portions of the Web with techniques similar
to those used to design databases. Our attention, therefore, will be mainly
centred on sites with at least a certain degree of regularity in the information
offered, whose goal can be described as the publication of the contents of a
database for consultation purposes.
500 Chapter 14
Databases and the World Wide Web
(0,1)
MANAGEMENT
(1,1)
Surname
Firstname
EMPLOYEE
(0,1)
MEMBERSHIP
(1,N)
Phone
DEPARTMENT
Name
Salary
(0,N)
(1,1)
PARTICIPATION
StartDate
(1,N)
Title
Budget
ReleaseDate
PROJECT
COMPOSITION
(1,N)
BRANCH
City
Address
Figure 14.4 The e-r schema and the logical schema of a database on the
employees of a company.
a page for each employee, with personal information and links to the
branch and to the projects in which he or she is participating.
The pages for projects, employees, departments, and branches, within
each of the categories, are certainly similar to each other. For example, in
Figure 14.5 we show the pages for two employees: these contain the same
information, with different values, and have the same organization. We find
here the classical distinction between schema and instance: the regular
structure of the page is the schema, while the actual content corresponding
to a given employee is an instance that conforms to the schema. Thus, it
makes sense to have the notion of a page schema to describe the common
structure of a set of pages. We can have the page schema EMPLOYEE of which
the two pages shown are instances.
Based on the above considerations, we can define a model for Web
hypertexts, which describes the page structure and the way in which the
pages are correlated. We present a simple model sufficient for the site
described above. It uses the sole construct of the page schema, which is
analogous to the notion of class in the object-oriented model shown in
Chapter 11. Each page can have atomic or complex attributes. Besides the
classic ones (integer, character, date, and so on), atomic types also include
multimedia types (for describing documents, images and sometimes videos
and sound). Complex attributes are built by means of the list constructor
(because pages have a physical order). Links connect page schemas as in the
object model, but each link instance consists of both a reference and an
In particular, note that each link is made up of an attribute with the role
of anchor (for example, City in the Branch link) and of a reference (for
example, *Branch in the above example). The anchor is made up by one or
more attributes of the referenced page schema. Alternatively, as we will see
shortly, the anchor can be a constant.
A peculiar characteristic of the Web is the fact that while in a database
each class usually has several instances, there are page schemas that have one
and only one instance. For example, the home page of each site falls into this
category. In the site described above, the pages that contain the list of all the
employees and the list of all the projects also have this characteristic. We can
say that these pages are unique; anchors pointing to unique pages from other
pages are constant strings. With the same syntax as above, the other page
schemas of the site can be defined as follows:
page-schema Company unique
(Branches: list-of (Branch: link(City: string; *Branch));
Employees: link("Employees"; *EmployeeList);
Projects: link("Projects"; *ProjectList);
)
page-schema ProjectList unique
502 Chapter 14
Databases and the World Wide Web
COMPANY
unique
Branches: list-of
Branch:
City: string
Employees:
"Employees"
Projects:
"Projects"
EMPLOYEELIST
unique
Employees: list-of
PROJECTLIST
unique
Projects: list-of
Project:
Title: string
PROJECT
BRANCH
Title: string
Budget: integer
ReleaseDate: date
Employees: list-of
City: string
Address: string
Depts: list-of
Name: string
Phone: string
Manager:
Surname: string
Employees: "Employees"
EMPLOYEE
Surname: string
Firstname: string
Salary: integer
Dept: string
Branch:
City: string
Projects: list-of
EMPLOYEESOFDEPT
Branch:
City: string
Dept: string
Employees: list-of
Project:
Title: string
StartDate: date
504 Chapter 14
Databases and the World Wide Web
Figure 14.7 Web page with the same information as one of those in
Figure 14.5, with a different presentation.
506 Chapter 14
Databases and the World Wide Web
ACTIVITIES
PERSONNEL
Address
PROJECT
Title
(1,N)
Budget ReleaseDate
PARTICIPATION
(0,N)
EMPLOYEE
Surname
Firstname Salary
City
BRANCH
(0,1)
(0,1)
MANAGEMENT
MEMBERSHIP
(1,1)
(1,N)
DEPT
Name
Phone
Requirements
analysis
Conceptual
database design
Conceptual
hypertext design
Logical
database design
Logical
hypertext design
Presentation
design
Site
generation
508 Chapter 14
Databases and the World Wide Web
among those discussed in the next section. A major point in this final phase
is the establishment of the correspondence between the logical database
schema (in the example, the relation schemas in the second part of
Figure 14.4) and the logical (and presentation) schemas of the hypertext. In
the following section we will see some examples that show various
alternative solutions.
<html>
<head><title>John Smith</title></head>
<body>
<H3>John Smith</H3>
<table>
<tr><td><em>Department</em> :</td><td> Design</td></tr>
<tr><td><em>City</em> :</td><td> London</td></tr>
<tr><td><em>Address</em> :</td><td> 354 Regent St</td></tr>
<tr><td><em>Salary</em> :</td><td> 45,000 per year</td></tr>
<tr><td><a href="/cgi-bin/Projects?Surname=Smith">Current
projects</a></td><td></td></tr>
</table>
</body>
</html>
Figure 14.11 The html source of the first page in Figure 14.8.
main(char Surname[])
{
char Firstname[20], Department[20], City[20];
char Address[60];
int Age, Salary;
$ open connection to NewCompanyDB
$ select Firstname, Department, City, Address, Salary
into :Firstname, :Department, :City, :Address, :Salary
from Employee E, Branch B
where E.Branch = B.City
and Surname = :Surname ;
$ close connection
if (sqlcode == 0){
printf("<html>\n<head><title> %s %s",Firstname,Surname,
"</title></head>\n<body>\n");
printf("<H3> %s %s",Firstname,Surname,"</H3>\n");
printf("<table>\n");
printf("<tr><td><em>Department</em>:</td><td>%s",
Department,"</td></tr>\n");
printf("<tr><td><em>City</em>:</td><td>%s",City,
"</td></tr>\n");
printf("<tr><td><em>Address</em>:</td><td>%s",Address,
"</td></tr>\n");
printf("<tr><td><em>Salary</em>:</td><td>%u",Salary,
"</td><tr>\n");
printf("<tr><td><a href=\"/cgi-bin/ProjOfEmp?Surname=%s",
Surname,"\">Current projects</a></td>
<td> </td></tr>\n");
printf("</table>\n</body>\n</html>");
}
else {
printf("<html>\n<head><title>Not found</title></head>
\n<body>\n");
printf("No employee has surname %s\n",Surname,"</body>\n
</html>");
}
}
510 Chapter 14
Databases and the World Wide Web
Figure 14.13 An html template for the construction of pages such as those
in Figure 14.8.
The template produces the same html source as the cgi program in
Figure 14.2, but without using a program in c (or other programming
language). The html code is not hidden in the output statements, but
directly specified. This is useful when debugging the structure of pages: a
browser ignores the html elements of the form <!> and can thus show the
template, with the names of the variables ($1, $2 and so on) in place of the
respective values. The tools that allow the use of templates operate in a way
that is similar to cgi programs: the urls denote a specific program, which we
could call the template manager, that receives as input the name of the
template and the needed parameters (in the example, the surname), accesses
the database and returns an html page as the result. The template in
Figure 14.3 can be invoked with a url such as the following:
http://www.nc.com/cgi-bin/tmplSQL?tmpl=Employee&Surname=Smith
512 Chapter 14
Databases and the World Wide Web
2. Note that the term push technology is also used in a different context, to refer
to tools that allow the browser to poll a server continuously in order to update
the displayed information without user intervention.
involves the use of names made up of the juxtaposition of the names of the
page schema and key values of the page itself.3
Extension of the HTTP server functionality A solution that is gaining
popularity is based on the availability of libraries associated with http
servers. This solution is denoted by the term server API (Application
Programming Interface) and indicates the modules of a specific library that
allow direct access to the database. In this case, the dbms client is the http
server itself and not another program, as with the cgi architecture. The
programs that access the database are called by the http server and are
executed within the http process itself.
The main benefit is the reduction of activation operations, with
consequent improvement of performance. The functions offered often
include the management of transactions and authorizations.
The main disadvantage of this solution is the lack of standardization; the
apis offered by the various servers, although similar to each other, are
different, and thus the applications are not easily portable. In addition, the
fact that the applications operate as sub-processes of the http server can
affect the server, in case of malfunctions of applications.
An extreme version of this approach consists of dedicated HTTP servers;
these are systems that integrate the functions of dbms and http server.
Obviously, all the services mentioned above, such as the complete
management of transactions, management of authorizations and others, such
as load balancing, are present. At the same time, there remains the
disadvantage of non-portability, accentuated by the fact that the dbms is also
fixed.
Thin CGI To meet the demand for portability and standardization, various
suppliers have suggested an intermediate solution. The technique consists of
the use of minute CGI programs (known as thin CGI), which have the sole aim
of receiving the Web server requests and of opening a connection to another
module. This module is called the CGI server, since it acts as a server for the
thin cgi requests. The thin cgi processes are subject to activation and
termination, as for each cgi. However, this concerns very small processes,
which are unlikely to cause performance problems. Conversely, the cgi
server is a permanently active process (a daemon) and thus presents no
problems of activation and termination. Furthermore, it keeps the
connection with the dbms open, with the double advantage of being able to
manage transactions and security.
3. We have not discussed the concept of keys with reference to the logical model
for hypertexts, but it is similar to that seen in earlier chapters for the relational
and the e-r model.
514 Chapter 14
Databases and the World Wide Web
Browser
HTTP
HTTP
server
HTTP
CGI
CGI
program
DB
HTTP
server
Database
client
DB
516 Chapter 14
Databases and the World Wide Web
Type 2
JVM
Type 3
JVM
Type 4
JVM
Java Application
Java Application
Java Application
Java Application
JDBC/native Bridge
JDBC/ODBC Bridge
JDBC Middleware
(Various DBMSs)
JDBC Driver
(DBMS Specific)
Native driver
(DBMS Specific)
ODBC Driver
DBMS
DBMS
DBMS
DBMS
14.5 Bibliography
There are many sources of information on the Internet and the Web, in
traditional printed form and in electronic format, available though the Web
itself. We mention a few general reference sources. A comprehensive
discussion of network protocols is provided by Comer [32]; an introductory
presentation of information services on the Internet (including the Web but
14.6 Exercises
Exercise 14.1 Consider the Web site of your university or company and
examine the services it offers, classifying them according to the types
discussed in Section 14.2.
Exercise 14.2 Find on the Internet one or more sites that fall in each of the
categories discussed in Section 14.2 (Publication and consultation;
Transactions on the Web; Electronic commerce; Co-operative and
workflow applications).
Exercise 14.3 Find on the Internet a Web site that could be used as a source
for a data farming activity.
Exercise 14.4 Consider the conceptual schema shown in Figure 5.26 and
design a Web site that can be used to publish the content of the database.
Show both the conceptual and the logical schema of the hypertext.
Exercise 14.5 We want to build a Web site for the training company
example discussed in Chapter 6 and Chapter 7. Therefore:
find the user requirements for the site, by choosing the pieces of
information that are interesting for the public (outside the company);
518 Chapter 14
Databases and the World Wide Web
Part V
VAppendices &
Bibliography
Appendix A
Microsoft Access
522 Appendix A
Microsoft Access
Figure A.1
At the start, the program asks whether we wish to create a new database
or open an existing one. It provides a list of databases that have been used
earlier by the program. The creation of a database can begin with an empty
database or with a schema selected from a series of predefined models (useful
for inexperienced users). Each database corresponds to a file with a standard
mdb extension. To open a pre-existing database, we need to select the
corresponding file. To create a new database, or open a pre-existing one, the
commands New database or Open database can also be used from the File
menu. The newly created or opened database is represented by a window,
which appears within the main window of the application. Figure A.2
illustrates this situation; we call the window listing the database components
the database window. The main window contains a set of drop-down menus
(File, Edit, etc.) and the toolbar. The menus and the toolbar vary according
to the window appearing on the foreground within Access. Figure A.2 shows
the contents of the toolbar when the database window is in the foreground
in the application.
The database window contains a list of the families of system components:
tables, queries, forms, reports, macros and modules. To move from one family
to another, the user clicks on the corresponding button. When a family is
selected, all the names of the elements of the family present in the database
appear in the window. If there are more elements than the window can
contain, a scroll bar makes it possible to navigate along the list. In
Figure A.2, we can see the set of elements of the Table family present in the
database selected. We observe that the database contains two tables, CITY and
PERSON.
At this point, for each family, we can select one of the three buttons that
appear in the right hand side of the window:
using the Open button, Access shows the contents of a selected element;
using the Design button, Access reopens the design phase on a selected
element;
Figure A.2
using the New button, Access creates a new element of the family.
Thus, the commands New and Design are for access to the information on
the design of the component (to its schema), while Open accesses its contents.
We will now describe the definition of tables and queries. Then we will
briefly illustrate the functions offered for the management of forms, reports,
and macros.
524 Appendix A
Microsoft Access
Figure A.3
In Access, the attributes are called fields, the domains types. Each attribute is
characterized by a set of field properties. The set varies according to the
domain associated with each attribute. The domains that can be associated
with an attribute are similar to the domains of standard sql, with a few
changes. The domains offered by Access are:
Text: allows the representation of strings of characters (corresponds to the
sql domain varchar).
Memo: allows the representation of strings of text as long as 64,000
characters; it is not a domain explicitly allowed by sql, but it can be
intended as a variant of varchar. (Indexes cannot be built on memo
attributes.)
Number: represents the family of numeric domains, both integer and real.
The field properties allow the specification of whether the attribute
represents precise or approximate values, and the degree of accuracy of
the representation. This domain corresponds to the sql domains numeric,
decimal, integer, smallint, float, double and real.
Date/time: represents temporal instants and corresponds to the sql
domains date, time and timestamp. It can represent only the date, only the
time or both (specified in the field properties).
526 Appendix A
Microsoft Access
number, three for boolean). Other formats can then be defined. We can
also specify how to represent values according to whether they are
positive, negative, or null.
Decimal places: (definable only for attributes of domain Single and Double)
specifies how many decimal places must be used in the representation.
Input mask: specifies the format that must be used for the input of data.
Take, for example, an attribute that holds a telephone number, made up
of a three digit prefix and of a seven digit number, separated by a dash.
We can specify an entry mask, which distinguishes the two parts and
presents the dash, allowing the user to insert only the digits. Access offers
a wizard to assist in the creation of input masks.
Caption: represents the name that can be given to the attribute when it
appears in a form or in a report. The attribute names are typically short,
in order to have a compact schema and to write concise queries. For
displaying information to the user, it is instead convenient to use an
extended name that better represents the attribute content.
Default value: specifies the default value for the attribute. It corresponds
exactly to the default option of sql. Each time a new tuple is inserted, the
default value will appear as a value for the attribute. The result of an
expression can also be used as a default value, like =Date(), which assigns
the current date to a field of the Date/Time domain.
Validation rule: describes a constraint that the attribute must satisfy.
Access automatically verifies that each value inserted belongs to the
domain of the attribute. As well as this check, Access allows the
specification of a generic constraint for each attribute (similar to the check
clause in sql). This constraint is expressed by using the syntax used for
the specification of conditions in qbe, which we will see in the next
section.
Validation text: specifies the message that must be displayed when an
insertion or an update introduces a value that does not satisfy the
integrity constraint.
Required: specifies whether the tuples must always present a value on the
attribute. This property can be true or false and corresponds to the not
null constraint in sql.
Allow zero length (valid only for attributes of the domains Text and
Memo): specifies whether empty strings (that is, strings whose length is
zero) can be allowed, or whether an empty string must be considered as a
null value. According to the context, it can be useful to manage the
empty strings differently from the null value. Bear in mind that the null
value is treated in a particular way by sql; an inequality comparison on
strings may be satisfied by an empty string, but not by a null value.
528 Appendix A
Microsoft Access
Figure A.4
the Save option in the File menu). The system at this point asks for a name to
be given to the table. The name can contain spaces.
Figure A.5
Figure A.6
Once the join path is defined, we can specify whether the path must be
associated with a referential integrity constraint. In the same window,
Access permits the definition of a policy for reaction to violations. In this
way, we can impose that updates and deletions introducing referential
integrity violations are followed by corresponding updates and deletions in
the joined tables. If the policy is not specified, each modification that
introduces a violation is simply refused. Compared with the sql-2 standard,
Access allows a limited set of reactions, corresponding to the two policies of
cascade delete and cascade update. To specify the various referential
integrity constraints, we should apply the design criteria described in
Chapter 6. Access does not allow more than one path between two tables to
appear in the graph. If more than one join path between two tables must be
defined, it is necessary to introduce more representations of the same table
into the graph (as in Figure A.5).
530 Appendix A
Microsoft Access
Figure A.7
Figure A.8
qbe query that returns the first names of the people called
Brown.
532 Appendix A
Microsoft Access
Figure A.9
The list of table attributes presented in the top half also contains an
asterisk, which, as in sql, represents all the attributes. Thus, to extract all
the attributes of the tuples of the PERSON table who are resident in their cities
of birth, we can formulate the qbe query shown in Figure A.10.
Figure A.10 Query that returns the people born in their cities of
residence.
In this query, to impose the identity of the two attributes, we put, as
selection value on the attribute City, the attribute name City of birth enclosed
in square brackets. Square brackets are the syntactic construct that Access
uses to distinguish references to schema components from string constants.
Access also allows the formulation of conditions using the normal operators
of comparison (<, >, <=, >=, <>) and the operator Like, used to compare
strings with regular expressions containing the special characters and ?
(which correspond respectively to the characters % and _ of standard sql). To
find the first names and last names of the people born before January 31st
1965 and who have a last name beginning with C, we can formulate the query
shown in Figure A.11.
Figure A.11 Query that returns the first names and last names of people
whose last names begin with C born before January 31st 1965
For each query, it is possible to specify a set of properties, at different
levels. At the level of a single column, we can specify a viewing format
different from the one chosen in the design of the table schema. Another
important property is the elimination of possible duplicates present in the
result. To specify that the duplicates must be removed, we must open the
window presenting the query properties (using the Property option in the
View menu) and assign the value Yes to the property Unique values.
To formulate queries that require more than one table, it is a good idea to
have all the tables needed by the query appear in the upper window. The
tables should be selected from the dialogue box that appears as soon as a new
query is created. The tables will appear linked by the join paths that were
defined at the end of schema design. In the table in the lower half of the
query definition window, we can add a further row, labelled Table, by
selecting the option Table names from the View menu. This row presents the
name of the table from which the attribute is extracted. The join conditions
between the tuples of the tables do not need to be specified when they are
predefined. If the query requires join conditions that are different from the
predefined ones, it is possible to modify the paths directly in the graphical
representation appearing in the top half of the window.
Suppose we wish to formulate a query that finds the people born in cities
with fewer than 100,000 inhabitants. If the join condition between the
PERSON and CITY tables is predefined by a join path, we can formulate the
query in Figure A.12.
Whenever the join path is not predefined, we must make the condition
explicit in the table, formulating a query like that in Figure A.13, which uses
references to attribute names in the Criteria rows.
It can be necessary at times to introduce into the top half tables whose
attributes are not part of the query result. An important case of this type is
that in which information must be extracted from two tables that do not have
a direct join path, but in which the join involves an intermediate table. In
this case, even if the query uses only attributes of the two unconnected
tables in the result and in the conditions, the intermediate table must be
present in the top half, with an approach similar to that used in relational
534 Appendix A
Microsoft Access
Figure A.12 Query which returns the first names and last names of the
people born in cities with fewer than 100,000 inhabitants
(using the predefined join path).
Figure A.13 Query that returns the first names and last names of the
people born in cities with fewer than 100,000 inhabitants
(without predefined joins).
algebra and calculus (Chapter 3), and also in sql (Chapter 4). In fact, without
a join path connecting the two tables, the system computes the cartesian
product of the two tables, eventually applying the conditions that appear in
the schema of the qbe query.
Let us now look at the formulation of qbe queries using aggregate
operators. The aggregate operators provided are Sum, Avg, Min, Max and
Count. They correspond to the standard sql operators with the same names.
Access also provides the operators DevSt (the standard deviation), Var (the
variance) First and Last (the value of the attributes in, respectively, the first
and last tuples). To use these operators, we must introduce a new row into
the query schema, the Total row. This row is introduced by selecting the
Totals option in the View menu. We will look at a simple example of the use
of aggregate operators, defining a query that allows us to find the number of
tuples present in the PERSON table, as shown in Figure A.14.
Note that the Count value appears in the Total row. This causes the result
of the query to be not the value of the First Name attribute of the PERSON
table, but the number of tuples. Any of the attributes of the PERSON table can
appear in the row Field. For the sake of coherence with sql, Access could
have permitted the use of the asterisk in this context (). However, since in
the evaluation of the other aggregate operators the asterisk creates
Figure A.15 Query that returns the number of people having each last
name.
Access permits the expression of conditions on the result of the aggregate
operators, corresponding to the use of the having clause in sql. This is
obtained with the simple specification of the desired values in the Criteria
row, in a way similar to the expression of simple conditions on tuples. Thus,
to find the last names that are possessed by at least two persons, we could
write the qbe query in Figure A.16, which returns the last names possessed
by more than one person, indicating for each last name the number of times
that it appears in the PERSON table.
Figure A.16 Query which returns the number of people who possess each
last name, for the last names possessed by more than one
person.
Consider now a query with grouping in which the tuples must be selected
beforehand, based on the values of attributes that are not used in the
grouping. It then becomes necessary to distinguish the conditions that must
536 Appendix A
Microsoft Access
be applied before and after the grouping. In sql this distinction occurs by
placing the preliminary conditions in the where clause and the later
conditions in the having clause. In qbe, the distinction occurs by placing the
value Where in the Total row for the attributes that are used only before the
grouping operation. The presence of the value Where is incompatible with
an active Show cell. The reason is that, as in sql, the result of the queries that
use aggregate operators may contain only the result of the evaluation of the
aggregate operators and the attributes on which the grouping is carried out.
For example, to find the persons born after January 1st 1975 having the same
last name of another person born after the same date, we can use the Count
operator and formulate the query in Figure A.17.
Figure A.17 Query which returns the homonyms in the set of people born
after January 1st 1975.
There are other ways to formulate this query that do not use the Where
term. One consists of the definition and saving in the database of a
preliminary query that extracts only the people born after January 1st 1975;
a second query, which starts from the first query result, can then be defined.
The first query result is grouped on the last name attribute in order to find
the people who share the last name with others. In fact, Access associates a
name to each saved query and a query can extract data both from the
database tables, and from the queries already defined. Each query that is
saved and made persistent can thus be considered as a definition of a view
on the database.
with the union operator. The qbe language is a powerful and friendly
language when formulating queries that use only selections, projections and
joins. In this case, the possibility of formulating queries without the need of
writing a text according to a rigid syntax, is a considerable help. On the
other hand, qbe does not provide an adequate mechanism for the
representation of complex queries, such as those that require the use of
nested queries in sql. When an sql query that uses a nested query is
translated into qbe, the translation simply returns the text of the entire
nested query in the appropriate cell of the Criteria row.
As regards the syntax recognized by the interpreter, this is a slight
variation of the standard sql syntax, with support for new functions and a
few syntactic and semantic differences. Some of the differences are as follows:
the top clause can be used to select a certain number of tuples from the
result;
square brackets are used to enclose the identifiers of the tables and
attributes (necessary when spaces or special characters appear within the
identifiers);
the join operator must always be qualified by the term inner or outer;
the evaluation of the count operator is different: if an attribute is given as
argument, the operator does not compute the distinct values of the
attribute, returning instead the number of not null values for the
attribute (as if the all option were explicitly specified); the distinct
option is not recognized inside the count argument.
For example, consider the qbe query in Figure A.18, for which the query
property is specified that only the first 10 elements have to be returned and
for which a join path is predefined. It thus corresponds the following query
in the sql dialect of Access:
Figure A.18 Query which returns the number of homonyms in a city with
more than 200,000 inhabitants.
select top 10 Lastname, count (Lastname) as HomonymNumber
from Person inner join City on Person.[City of birth]=City.Name
where [Number of inhabitants] > 200000
group by Lastname
having count (Lastname) > 1
order by count(Lastname) desc
538 Appendix A
Microsoft Access
writing the attribute values directly into the corresponding bound controls.
Forms also permit querying of the database, offering a friendly search
command (Find option in the Edit menu). To modify the value of an attribute,
the tuple is selected and the new value is written directly in the slot
presenting the old attribute value. The modification is made permanent by
moving to a different tuple (typically by pressing on the page-up or pagedown key, which are used to scroll the elements in the form). Figure A.19
shows a form on the tuples of PERSON, extended with an attribute that
contains a picture.
Forms are generally constructed on a single table. We can define forms that
contain other forms within themselves, by specifying links between the
values in the forms. In this way, we can, for example, define forms for the
management of tables with one to many relationships, in which the included
form presents all the elements of the table associated with the element in the
including form. Think of a pair of tables that describe orders and items
appearing in the orders. A form can illustrate at the most external level the
common characteristics of the order, for example, customer, date or
payment. An included form can then be used to present all the items
included in the order.
A report is defined in a similar way to a form, with almost identical tools
and concepts. The main difference lies in the fact that a report has typically
the aim of providing a description of the database contents at a summary
level. For this reason, reports typically contain calculated controls that
compute aggregate functions, and do not permit the viewing of particular
tuples. Another difference from forms is that, in general, reports are printed,
instead of being used interactively.
540 Appendix A
Microsoft Access
Let a form interact with other forms or with reports. For example,
consider a form that describes the data of clients, and one which
describes orders. We can add to the first form a button that activates the
second form, presenting the order data for the client presented on the
first form. We can also add a button that allows the printing of a report
that lists the credit balance of the client, with the total amount of
merchandise ordered, delivered and paid for by the client.
Select and group tuples automatically. In a form, we can insert a button
that allows the immediate selection of the tuples that satisfy given
conditions.
Assign values to the attributes. Using a macro, we can assign to a control
of a form a value obtained from other controls or other tables of the
database.
Guarantee the correctness of data. Macros are very useful for the
manipulation and validation of the data in the form. For example, we can
define a macro that reacts to different incorrect values of an attribute with
different messages, and guarantee that the inserted data is correct.
Set properties on forms, reports and attributes. Using macros, we can
automate changes of any property of these objects. For example, we can
make a form invisible when its contents are needed but not to be seen.
Automate the transfer of data. If we need to transfer data repeatedly
between Access and other applications (both reading and writing), we
can automate the task.
Create a customized environment. We can specify a macro that opens a set
of tables, queries, forms and reports each time a database is opened; the
toolbar can also be personalized.
Specify reactions to certain events. For each element of a form, we can
specify which macro must be executed after each event of access,
selection or modification on the element. This characteristic makes up the
basis for the definition of reactive behaviour in Access, which however
must not be confused with what is offered by active databases, described
in Chapter 12. This behaviour occurs in Access only when a particular
form is used for the manipulation of the database; the access or
modification event is not detected if the same operation is executed
directly in sql or using another form.
To define a macro, it is necessary to follow the usual steps: first, the New
option of the Macro component in the main database window must be
selected. At this point, Access presents the window for macro design. The
top half of the window contains a table with two columns: Action and
Comment. The macro is made up of a sequence of actions described in the
Action column, each on a different row, to which a brief description can be
added. In the lower half of the table a set of attributes appears, which, for
each single action, specify the parameters of the action. We can specify
conditions that must be verified when a command is executed, adding the
Condition column. We can also use simple control structures.
The available commands can be divided into families. We list the most
significant commands in each family.
Execution: RunCommand (invoke a command, like a menu option),
OpenQuery (execute a query), RunMacro (execute a macro), RunSQL
(execute an sql command), RunApp (execute an external application),
CancelEvent (disregard an event), Quit (close the session), StopMacro
(terminate the execution of the macro) and StopAllMacros (terminate the
execution of all macros).
Access to data: ApplyFilter (apply a selection condition to the tuples)
FindNext (move to next tuple), FindRecord (access a tuple), GotoControl
(position the cursor in a form control), GoToRecord (go to a particular
tuple).
Modification of data: SetValue (assign a particular value to an attribute),
DeleteObject (removes an object), OpenForm, OpenQuery, OpenReport,
OpenTable (commands that open respectively a form, a query a report and
a table).
There are other command families, such as those for the transfer of data
between Access and other applications, for the modification of the window
dimensions and to open dialogue windows with the user.
A simple macro is described in Figure A.20. The macro is associated with
modifications on the Price attribute of the ITEM table. The macro assigns to the
attribute Expensive the value Yes, if the Price is greater than 1000, otherwise
it assigns the value No to the attribute.
542 Appendix A
Microsoft Access
Appendix B DB2
Universal Database
db2 Universal Database1 belongs to a historic family of database
management systems produced by ibm. The oldest member of this family is
sql/ds, one of the first commercial systems based on the relational model,
made available by ibm at the beginning of the eighties. In its turn, sql/ds has
its roots in System R, one of the first prototypes of relational dbmss
developed, in the early seventies, in the ibm research laboratories in San
Jos. It was in the development environment of this prototype that sql was
born. This language soon became the standard for all dbmss based on the
relational model.
Together with a complete support for the relational model, db2 offers some
object-oriented features and a rich set of advanced features, including:
support for the management of non-traditional data types, such as texts,
images, sounds and video;
support for the management of data types and functions defined by the
user;
some extensions of sql that include powerful On Line Analytical
Processing (olap) operators and special constructs to specify recursive
queries;
support for parallelisms based on both shared memory configurations,
in which a database is managed by a symmetric multiprocessing ( smp)
machine, and shared nothing configurations, in which a database is
partitioned among different machines connected by a network.
1. For the rest of the chapter, we will refer to this product simply as db2.
544 Appendix B
DB2 Universal Database
The server component of db2 is available on Windows nt, os2 and several
Unix-based platforms. The client component is also available on Windows
and Macintosh environments for personal computers. Client and server can
communicate on the basis of diffuse communication protocol standards (tcp/
ip, NetBios, etc.). Moreover, db2 systems can participate in heterogeneous
and distributed multi-database environments, using a protocol called
Distributed Relational Database Architecture (drda), adopted by many other
database management systems. Finally, db2 provides support for the main
interface standards (such as odbc, jdbc) and adheres to sql-2.
In the rest of this appendix, we will describe the general characteristics of
the system, paying particular attention to its advanced functionality. We will
not discuss the db2 triggers here, because, although they form an advanced
feature of the system, they have already been presented in Chapter 12.
For a more detailed presentation of this system, we refer the reader to the
book by Chamberlin [20], one of the inventors of sql.
data warehouses (Visual Warehouse), an olap tool (db2 olap Server), and a
set of data mining applications (Intelligent Miner).
546 Appendix B
DB2 Universal Database
In the next section, we will describe how a db2 database is managed, using
the above techniques.
Figure B.1
For Windows and os2 environments, some advanced tools are also
available. They facilitate database management using windows and pop-up
menus. These tools can be profitably used in a classic client-server
architecture, in which a database resident on a Unix server is accessed and
managed by clients made up of pcs. The db2 interactive graphic tools are
described below.
Control Center This allows the administration of a database using table
creation operations, definitions of integrity constraints, authorization
controls, backups, restores, and so on. As shown in Figure B.2, the Control
Center interface has two main windows. In the one on the left, the db2
objects available are shown (instances, databases, tables and so on), according
to a hierarchical organization. These objects can be declared explicitly by the
Figure B.2
user, or the Control Center can be requested to search on the local network
for all the db2 objects to which access is possible.
In our case, we can observe that two machines containing db2 objects are
available. They are called respectively Bernina and Cad2. The information
about the first machine has been expanded, as shown by the minus symbol
next to the icon. We can see that this system contains a single db2 instance
(the default instance). This instance contains two databases: one called Emps
and another called Sample. For the first of these, all the components are
shown, whereas the information about the second is not expanded, as shown
by the plus symbol next to the icon.
The window on the right contains information on the object selected in the
window on the left. In our case, all the tables of the Emps database are shown.
This database contains two schemas: the base schema and the db2admin
schema. The tables of the sysibm schema are system tables. By clicking the
right mouse button on any object of the screen, we obtain a menu that allows
us to perform actions on the selected object. For example, by clicking on the
Tables icon of a database, we can create a new table. The system guides the
user in the execution of many of these actions. Recent releases of db2 also
provide a version of the command center that is accessible from a Web
browser.
Command Center This tool allows the user to type and execute sql
commands and to compose scripts, that is, sequences of sql statements to be
548 Appendix B
DB2 Universal Database
Figure B.3
This is a classic query that searches for employees who earn more than
their respective managers, in a schema containing the following tables:
EMPLOYEE(EmployeeId, Surname, Department, Salary)
DEPARTMENT(DepartmentId, Name, Location, Manager)
Figure B.4
clicking on the appropriate icon on the top of the window. In Figure B.5 a
portion of the access plan of the query in Figure B.3 is shown. In the smallest
window, the entire graph that represents the access plan is shown and the
portion of the graph that is being examined is highlighted. In the selected
Figure B.5
550 Appendix B
DB2 Universal Database
Figure B.6
Journal The Journal keeps track of all the events that happen in a
database. In particular, we can use the Journal to examine all the query/
update operations that have been executed (in the Jobs panel), the backup/
restore operations performed (Recovery panel), the possible signals for
attention produced automatically by the system (Alerts panel), and all the
messages generated by the system on the execution of the various operations
(Message panel).
Information Center The Information Center supplies a large amount of
information on the db2 system. Using this tool, we can obtain help on
various topics. These include subject-oriented descriptions such as creation
of a table or backup of a database, explanations of the db2 messages and of
the error codes, and a list of example programs that illustrate the various
features of db2.
552 Appendix B
DB2 Universal Database
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sqlenv.h>
exec sql include sqlca;
void main() {
char CurrDept[10];
/* Program variable declaration
exec sql begin declare section; /* Host variable declaration
char EmpSurname[10];
/* Employee surname
char DeptName[10];
/* Department name
char DeptLoc[15];
/* Department location
long EmpSalary;
/* Employee salary
char msgbuffer[500];
/* Buffer for DB2 error messages
exec sql end declare section;
exec sql declare C1 cursor for
/* Cursor declaration
select distinct emp.surname, emp.salary, dep.name
from
employee emp, department dep
where
emp.department = dep.department_id and
dep.location = :DeptLoc
order by dep.name;
exec sql whenever sqlerror go to PrintError;
exec sql connect to emps;
/* Connection to the database
printf("Enter a location:"); scanf("%s", DeptLoc);
exec sql open c1;
/* Opens the cursor
exec sql fetch c1 into :EmpSurname, :EmpSalary, :DeptName;
if (sqlca.sqlcode==100)
printf("No department in this location.\n");
while (sqlca.sqlcode == 0) {
if (strcmp(DeptName,CurrDept)!=0) {
printf("\nDepartment: %s\nEmployee\tSalary\n",DeptName);
strcpy(CurrDept,DeptName);
}
/* end if
printf("%s\t%d\n", EmpSurname, EmpSalary);
exec sql fetch c1 into :EmpSurname, :EmpSalary, :DeptName;
}
/* end of while
exec sql close c1;
/* Closes the cursor
exec sql connect reset;
/* Resets the connection
return;
PrintError:
/* Retrieves and prints an error message
sqlaintp(msgbuffer, 500, 70, &sqlca);
printf("Unexpected DB2 error: %s\n", msgbuffer);
return;
}
/* end of main
Figure B.7
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
permanent only after the execution of the commit statement. (The example
program does not include update operations so it does not use such a
statement). To carry out the planned operations, the program makes use of
the cursor technique illustrated in Section 4.6. A cursor is first defined over
an sql query that contains the host variable DeptLoc. Then, after having
stored in this variable a string read by the input, the cursor is opened and
the query is executed. Then, using the fetch statement, the result is first
copied, one tuple at a time, into the host variables and then displayed. This
operation is controlled by examining the value assumed by the system
554 Appendix B
DB2 Universal Database
Assume now that we wish to calculate a tax on the vehicles registered after
1997, based on a tariff that refers to the type of vehicle. A possible solution
is as follows, where the value is calculated on the basis of values that appear
in the Type column of the VEHICLE relation.
select RegistrationNumber,
case Type
when 'car' then 42.50 * Length
when 'truck' then 25.00 * NWheels
when 'motorbike' then 37.00
else null
end as Tax
from Vehicle
where RegistrationYear > 1997;
The case expressions can also be used for update operations. For example,
the following sql command specifies a modification of the salary in the
EMPLOYEE relation, based on the values in the Department column.
update Employee
set Salary =
case
when Department = 'Administration' then Salary * 1.1
when Department = 'Production' then Salary * 1.15
when Department = 'Distribution' then Salary * 1.12
else Salary
end;
It should be observed that in standard sql the only way to perform the
same operation would be to use multiple commands.
Nesting of queries An interesting facility offered by sql for db2 is the
ability to nest queries, not only in the where clause, but also in the from
clause. This characteristic is based on a principle of orthogonality of
programming languages, which requires that independent features can be
combined without restrictions. In the sql context, orthogonality suggests
that a select statement, returning a table, must be allowed to appear
anywhere a table is expected, including the from clause.
As an example, look again at the schema:
EMPLOYEE (EmployeeId, Surname, Department, Salary)
Suppose we wish to find the employees who earn 30% more than the
average salary in the department to which they belong. The query can be
expressed in db2 with the following sql statement.
select Surname, Salary
from Employee as Emp, (select Department, avg(Salary)
from Employee
group by Department)
as AvgSalDept(Department, AvgSalary)
where Emp.Department = AvgSalDept.Department and
Emp.Salary > AvgSalDept.AvgSalary * 1.3
556 Appendix B
DB2 Universal Database
This query can also be expressed in a standard way, by moving the nested
sql statement, which determines the average value of salaries, within the
where clause. It is however easy to show that this functionality increases the
expressive power of the language: we can write sql queries embedded in the
from clause that cannot be expressed in another way.
Suppose for example, that the schema possesses a further table with the
schema JOBS(Employee, Project), which stores the projects in which the
employees participate. The average number of projects in which the
employees of the Production department participate can be retrieved as
follows:
select avg(NumberOfProjects)
from Employee as Emp, (select Employee, count(Project)
from Jobs
group by Employee)
as NoProjects(Employee, NumberOfProjects)
where Emp.EmployeeID = NoProjects.Employee and
Emp.Department = 'Production'
This query cannot be expressed with a nesting in the where clause, because
it requires the use of the result of an aggregation (count) to evaluate another
aggregation (avg). However, in the where clause, we can use the result of a
nested query only for a comparison of values. (See Chapter 4.)
OLAP operations
As we said in Chapter 13, this could be the star schema of a data mart
constructed for specific analysis activity. In this data mart, the SALE table
stores the facts on which the analysis is focused (centre of the star), while
LOCATION and PRODUCT constitute the dimension tables.
A first sql extension provided by db2 allows the specification of
aggregations on different levels of the same dimension with the rollup
construct. For example, the statement that follows allows the calculation of
the total income of the chain, grouped by store, city and state (all levels of
the Location dimension).
select Location.Store, City, State, sum(Income) as TotIncome
from Sale, Location
where Sale.Store = Location.Store
group by rollup(State, City, Location.Store)
A possible result of the command is shown in Figure B.8. Observe that this
instruction also calculates the global total of the incomes. In the result tables
the tuple components for which there is no applicable value (for example, the
store in the third tuple) are filled by db2 with null values. As we said in
Chapter 13, these components should instead represent all the possible
values. To improve the readability of reports generated with these
operations, it is then preferable to fill these spaces with an appropriate term,
for example, all. This operation can be easily carried out using the case
construct.
Store City
State TotIncome
White Los Angeles
CA
34
Black Los Angeles
CA
32
NULL
Los Angeles
CA
66
Brown San Francisco CA
25
NULL
San Francisco CA
25
NULL
NULL
CA
91
Red
New York
NY
28
NULL
New York
NY
28
NULL
NULL
NY
28
NULL
NULL
NULL
119
Figure B.8
558 Appendix B
DB2 Universal Database
City
Los Angeles
Los Angeles
New York
New York
Los Angeles
New York
NULL
NULL
NULL
Figure B.9
Category NoOfSales
milk
2453
coffee
988
milk
789
coffee
987
NULL
3441
NULL
1776
milk
3242
coffee
1975
NULL
5217
In this command, the with clause defines the SUPERIOR table, which is
constructed recursively, starting from the SUPERVISOR table. In particular, the
construction involves a basic non-recursive query over the SUPERVISOR table
(base definition) and a recursive query that expresses a join between the
tables SUPERVISOR and SUPERIOR (recursive definition). It is interesting to
observe that the schema adopted by db2 for recursive queries is similar to
the definition of intentional predicates adopted in the logical language
Datalog, as described in Section 3.3.
Employee (
integer not null unique,
varchar(20),
decimal(7,3),
date,
blob(5M) compact,
clob(500K)
In this table, the Picture column serves to store images and the Resume
column, texts. The dimensions specified in brackets indicate a maximum
value. The compact option specifies that the data item should occupy the least
possible space on the disc, possibly trading efficiency.
There are indeed many limitations to the use of lob data in db2 sql
commands. In particular, it is not possible to compare directly columns of
lob type with operators such as =, >, < or in. It is however possible to use the
like operator. For example, the sql command
select EmployeeId, Name
from Employee
where Resume like '%DBA%'
retrieves the employees for which the DBA string appears in the Resume
column.
db2 manages lobs in a way that minimizes their movements from one
location in the memory to another. In particular, lobs can be manipulated in
applications using suitable variables called locators. These represent a lob
without physically storing it. With the appropriate use of locators, we can
defer or even avoid the loading of a lob in main memory. For example, the
copy of a lob happens simply by copying the locator. A locator is defined in
the host variable declaration section of a program with the sql command
type is TypeOfLob LocatorName. The locator can then be used like all the
other host variables to manage a lob of associated type. We can also
manipulate locators using special functions. These special functions include
posstr, which finds the position of the first occurrence of a pattern in a lob,
and substr, which returns the substring of a lob, included in the specified
positions.
As well as these basic pre-defined types, db2 provides, as external
packages, a series of extenders. These are further data types, more specific
than lobs, for the management of non-traditional data. Extenders are
560 Appendix B
DB2 Universal Database
The as clause specifies the source data type, while the with comparisons
clause specifies that all the comparison operators defined for the source type
are also allowed on the user type. Unfortunately, only pre-defined db2 data
types can be used as source types and definitions cannot be nested.
Once defined, the user types can be freely used in the creation of tables.
For example, the preceding definition of the EMPLOYEE table can be rewritten
as follows:
create table
EmployeeId
Name
Salary
HiringDate
Picture
Resume
)
Employee (
integer not null unique,
varchar(20),
money,
date,
image,
text
scalar functions: these can receive many parameters as input but return
a single value as output: if the name of the function redefines a basic
operator (for example +) then such functions can be invoked using
the infix notation;
Internal functions are mainly used together with user types to allow the
application of standard functions to user types. For example, with reference
to the user type Salary defined above, we can define some internal functions
with the following declarations.
create function "*"(Money, Decimal()) returns Money
source "*"(Decimal(), Decimal())
create Function Total(Money) returns Money
source Sum(Decimal())
Expressions that involve, for example, the sum of a salary with a decimal
number are instead not legal because the sum operator is undefined for the
Money type.
Suppose now that we wish to define an external scalar function that
calculates the salary earned by an employee, based on his length of service,
assuming that the length of service is calculated on the basis of the date of
hiring. A possible definition of such an external function is as follows:
562 Appendix B
DB2 Universal Database
Observe that the condition in the where clause is valid because: (a) the user
function returns a data item of type Money; (b) the product of this data type
by a decimal value has been defined above; and (c) the value returned by the
product is of Money type and can therefore be compared with the Salary
attribute, as established by the definition of the user type Money (see above).
The table functions constitute a particularly interesting aspect of db2.
They allow the easy transformation of an external information source into a
table that can be manipulated in sql. It is sufficient to write a program that
accesses the data source, for example a text file or an ms Excel file, possibly
filters the data based on parameters passed as input, and finally returns them
one row at a time. The return of the various rows happens as follows. The
system allocates an area of memory (called a scratchpad), which preserves its
content from one execution of the program to another. In this way, a program
can access information about the previous execution; for example, the last
position accessed in a file.
Suppose we have a function of this type that takes as input the name of a
city and returns sequences of values stored on a remote file, corresponding
to sales information of stores located in that city. The definition of the
corresponding table function could be as follows:
create function Sales(Char(20))
returns table (Store char(20),
Product char(20),
Income Integer)
external name '/usr/db2/bin/sales'
deterministic
no external action
language c parameter style db2sql no sql
scratchpad
final call disallow parallel;
Note that, apart from some specific clauses, the definition is similar to the
definition of an external scalar function. The main difference is that the result
of the function is composed of many parameters, which are interpreted as
attributes of a table.
A possible sql command that uses the table function defined above is as
follows:
select Store, sum(Income)
from table(Sales('Los Angeles')) as LASales
where Product = 'Toy'
group by Store;
This command retrieves the total income taken by the stores in Los
Angeles for sales of toys. The table queried is not stored in the database (as
indicated by the keyword table), but is generated by the table function
Sales, to which is passed, as an entry parameter, the string Los Angeles.
By using user types and user functions together, we can obtain a data
management system with object-oriented features. For example, we can
define a class Polygons using a lob user type. We can then define as user
functions a series of methods of this class for constructing a new object,
modifying it, printing it and calculating its perimeter and area.
564 Appendix B
DB2 Universal Database
Appendix C
Oracle PL/SQL
On
566 Appendix C
Oracle PL/SQL
and applications, all the tools of this family use the same repository as
was constructed by the tools in the earlier environment. This is a good
example of an integrated development environment. Among them, we
note the Forms tool for the definition of applications, based on the
definition of forms and targeted to different environments (Windows,
Mac, x/Motif or alphanumeric terminals). Other important components
are Reports (a report generator) and Procedure Builder, a support tool for
the design of pl/sql code, which provides an interpreter and a
debugger for the language. All these tools offer assistance in the
production of advanced applications, such as those for client-server
environments and graphical interfaces.
Discoverer:
568 Appendix C
Oracle PL/SQL
number. This is the most general domain for the representation of numbers
and is characterized by precision and scale. This domain permits the
representation of both integer and real values; it has the power to
describe the whole family of numeric domains of standard sql (for
example, decimal, double, float and integer). Each declaration of a
numeric domain with the sql syntax is translated by the system in a
declaration on the number domain, with appropriate values of precision
and scale.
char. When it is characterized by length, it represents strings of
characters, otherwise it defines a domain of a single character. The
maximum length of the string is 255. It corresponds exactly to the char
domain of sql.
varchar2. This allows the representation of strings of characters of
variable length. The maximum length is equal to 2000 characters. This
corresponds to the domain varchar of sql. The term varchar can actually
be used. The name varchar2 was chosen to prevent future modifications
of the varchar domain in standard sql from having an impact on
applications. A designer using the domain varchar2 has the guarantee
that the behaviour of the program in future versions of Oracle will remain
unchanged; instead, the varchar domain depends on the decisions of the
sql standardization committee. If an application is developed that must
be used only in the Oracle environment, there are no strong objections to
the use of this domain; otherwise it is preferable to write code that
respects the standard as far as possible.
long. The name would lead us to suppose that this concerns a numeric
domain (similar to the type long in c). It is in fact a domain of strings of
characters of variable length with a maximum length of 231 1. It is
therefore an extension of the varchar2 domain.
raw. This allows the representation of data in binary form, seen as strings
of bytes of variable length. It is managed as if it contained strings of
characters, except that the system never tries to interpret it for
conversion (as for example happens for characters, when transferring
from a system with ascii codes to a system with ebcdic codes). The
maximum length for an attribute is 255 bytes.
long raw. This extends the raw domain by allowing a maximum length
equal to 231 1 bytes.
date. This represents instants of time and corresponds to the timestamp
domain of standard sql. As well as the date, hours, minutes and seconds
are also represented. There is no domain corresponding to the time
domain of standard sql.
rowid. Oracle internally allocates a unique identifier to each tuple. The
identifier is a binary value using six bytes. The six bytes indicate the file
to which the tuples belong, the block number within the file, and the
position of the tuple within the block.
LOB (LargeOBject): this is a family of domains that were introduced in
version 8 of Oracle Server. These domains can be considered as extensions
of the long and long raw domains. The domains of this family are Binary
LOB (BLOB), Character LOB (CLOB), and National Character LOB (NCLOB).
There are a few differences between long and LOB domains, the main one
being that lobs are managed with locators. That is, if a table has a lob
attribute, then its value is a reference to a database area where the actual
value of the lob is kept. Locators are more compact, have fewer
restrictions than long attributes, and offer a richer access to the data
(possibly identifying a fraction of the lob content, located anywhere in
the data). A variant of the lob domain is the BFILE (binary file) domain.
The BFILE domain represents references to files kept outside the server.
Data accessed by a BFILE attribute cannot exploit the transactional
support the server offers for internal data.
570 Appendix C
Oracle PL/SQL
The definition of a typed table has a similar structure. A typed table is like
a varray, except that a table can contain an arbitrary number of elements and
that the elements are unordered and there is no direct access mechanism to
them. For example, the following command defines a typed table on
EmployeeType elements:
create type EmployeeTableType as table of EmployeeType
572 Appendix C
Oracle PL/SQL
update Employee
set Salary = Salary * 1.1
where RegNo = '575488';
else
update Employee
set Salary = Salary * 1.15
where RegNo = '575488';
end if;
commit;
exception
when no_data_found then
insert into Error
values('The RegNo does not exist', sysdate);
end;
This program updates the salary of the employee with the registration
number 575488. The program uses a local variable Sal in which the value of
the salary is held. If the salary is higher than 30 thousand, the salary is
increased by 10%, otherwise by 15%. If the select returns no tuples, a
no_data_found exception is generated and the program returns to the
execution inserting a tuple into the ERROR table. This contains a description
of the error and the time at which it occurred.
This program is composed of a single block. We distinguish among the
declaration part (enclosed between the keywords declare and begin), the
execution part (enclosed between begin and exception) and the part that
manages exceptions (between exception and end). Observe that the program
integrates SQL commands (select, update and insert) with control
structures typical of programming languages.
574 Appendix C
Oracle PL/SQL
pl/sql allows the definition of cursors. A cursor can also be used as the
basis for the use of the %rowtype keyword.
declare
cursor NewCursor is
select FirstName, LastName
from Employee;
Person NewCursor%rowtype;
This example defines a pl/sql table EmpNameTable, defined from the type
EmpNameTableType, having an attribute with domain equal to the FirstName
attribute in EMPLOYEE and a second attribute of domain binary_integer, used
to access table elements. In the procedural part, the table is filled by copying
the names of the employees one at a time. In the example, we define a record
type EmpRecordType and an EmpRecord variable of this type. The variable is
used to read the tuples in the EMPLOYEE table.
576 Appendix C
Oracle PL/SQL
if Condition then
InstructionsBlock
{elsif Condition then
InstructionsBlock}
[else InstructionsBlock]
end if
Iteration The basic construct for iteration is the loop. In its most simple
form, a sequence of instructions is placed between the keywords loop and
end loop. The sequence will be executed until it finds an exit instruction. For
a cycle with an initial condition, a while condition is inserted before the
keyword loop. The condition is evaluated before each iteration. If the
condition is false, execution terminates. The for construct is used to define
the number of times that the iteration must be repeated. The for structure
specifies a range of values and the instructions enclosed by loop and end loop
are executed once for each value. The syntax is as follows:
[while Condition |
for Counter in [reverse] StartValue..EndValue]
loop
InstructionsBlock
end loop
The same program can be written using a loop with an internal exit:
loop
fetch EmpCursor into Sal;
if EmpCursor%notfound
exit;
/* instructions to increase the Salary */
end loop;
In the example, we have seen the use of the operator %notfound on cursors.
The operator returns a boolean value true if the last operation on the cursor
is unsuccessful. In pl/sql, a cursor is used for each query and update
command. The cursor can be explicitly defined in the program; otherwise,
pl/sql uses an implicit one. The operator presented above can be applied
both to the explicit and to the implicit cursors. The implicit one is referred
to as sql. Thus, sql%notfound is an expression that returns true if the last
command has recovered no tuples.
Let us look finally at the construct for on cursors. Cursors are used to scan
the result of a query. To represent this situation compactly, pl/sql provides
a suitable structure, characterized by the following syntax:
578 Appendix C
Oracle PL/SQL
InstructionsBlock
end loop
The cursor is opened automatically when the program reaches the cycle
and a fetch instruction is implicitly executed on each iteration. The cycle
terminates when an exit instruction appears or when all the tuples returned
by the cursor have been dealt with. The earlier loop could be written:
for EmpRecord in EmpCursor loop
if EmpRecord.Salary > 60 then
/* instructions updating the Salary */
end loop;
The cursor can also be defined in line (that is, without an explicit definition
in the declaration part of a block) as happens in the following code fragment,
part of the final example in Section C.4.2.
for EmpRecord in
(select FirstName, LastName, Salary
from Employee) loop
580 Appendix C
Oracle PL/SQL
C.4.5 Procedures
Procedures, also called subprograms, constitute a fundamental tool for the
construction of large applications. Procedures permit the construction of pl/
sql programs with important properties like modularity, reusability,
maintainability and abstraction.
In the pl/sql environment, the subprograms have a still more important
role, as pl/sql is used very often as a tool for the definition of stored
procedures. These are pl/sql procedures that are stored and may be
considered a part of the database schema.
Let us look at how subprograms enrich the language. Considering the
example of a bank transaction in the preceding section, it is natural to try to
write more general code, producing instructions able to carry out a generic
debit on an arbitrary account. The following procedure satisfies the
requirement.
procedure Debit(ClientAccount char(5), Withdrawal integer) is
OverdraftError exception;
OldAmount
integer;
NewAmount
integer;
Threshold
integer;
begin
select Amount, Overdraft into OldAmount, Threshold
from BankAccount
where AccountNo = ClientAccount
for update of Amount;
NewAmount := OldAmount Withdrawal;
if NewAmount > Overdraft then
update BankAccount set Amount = NewAmount
where AccountNo = ClientAccount;
else
raise OverdraftError;
end if;
exception
when OverdraftError then
insert into OverdraftExceeded
values(ClientAccount, Withdrawal, sysdate);
when data_not_found then
insert into AccessToGhostAccounts
values(ClientAccount, sysdate);
end Debit;
The transfer command above could thus be carried out with a simple call
of the procedure:
Debit('12345',100);
LocalDeclarations
begin
Instructions
[exception
ExceptionHandlers]
end [Name]
The instructions and the exceptions are defined with the syntax
previously described. Each parameter follows the syntax:
ParameterName [in | out | in out] ParameterDomain [:= Value |
default Value]
Each parameter is characterized by a name and by a domain. The
parameters can be as follows.
Input (type in, the type assumed when no keyword is specified). In this
case, a value is assigned to the parameter when the procedure is called,
and internally to the procedure, the parameter must be considered as a
constant.
Output (type out). When the procedure is called, the parameter must be
associated to an l-value. An l-value is a term that can appear in the left
side of an assignment instruction. The l-value is generally the identifier of
a variable to which a value will be assigned at the end of the procedure.
Input/output (type in out). In this case, a variable must still be assigned to
the parameter. However, its value can be used within the procedure. A
value is assigned to the variable at the end of the execution of the
procedure.
From the syntax, we observe that we can associate a default value with a
parameter. This is possible only for input parameters, and is forbidden for
output or input/output parameters. If the procedure is called without an
input parameter, the default value is assigned to the parameter. The presence
of this feature complicates the mechanism managing the exchange of values
between the procedure and the program calling it. In most programming
languages, the correspondence between the parameters that appear in the
definition of the procedure (called the formal parameters) and the parameters
that are used in the call of the procedure (the actual parameters) is positional.
The first formal parameter corresponds to the first actual parameter, and so
on until the last actual parameter to which the last formal parameter will
correspond. If a domain incompatibility appears, the program is interrupted
by a value_error exception. If default values for parameters are allowed,
some parameters can be missing and the correspondence between formal and
actual parameters will require a different mechanism.
A first solution offered in pl/sql consists in an extension of the positional
correspondence between actual and formal parameters, permitting them to
differ in number. If the actual parameters are fewer than the formal, the
default value is assigned to the last formal parameters. This method requires,
however, that the parameters using the default value are always the last.
A further mechanism is name-based, and uses in the call of the
subprogram a direct reference to the formal parameters. Thus, with an
582 Appendix C
Oracle PL/SQL
arbitrary parameter order, the above call could also be written as:
Debit(Withdrawal => 100, AccountNo => '12345')
One of the advantages of the definition of default values for the parameters
is that it becomes possible to extend pre-existing procedures, assigning a
default value to every parameter added to the procedure. If the procedure is
called using the previous set of parameters, the assignment of default values
to the new parameters should keep the previous behaviour, without
requiring modification of all the calls to the procedure already present in the
program.
A function is a particular kind of subprogram, one that returns a value as
a result of its call. Functions have a structure identical to the procedures seen
above, with only two differences. Firstly, functions are associated with a
domain that corresponds to the domain of the result produced and, secondly,
in a function we can use the instruction return Expression. This instruction
terminates the execution of the function, returning as function value the
result of the evaluation of Expression. The syntax for the declaration of
functions is thus:
function Name [(Parameter {, Parameter})] return
FunctionDomain is
LocalDeclarations
begin
Instructions
[exception
ExceptionHandlers]
end [Name]
584 Appendix C
Oracle PL/SQL
C.4.6 Packages
Packages allow the regrouping of type definitions, procedures and cursors
into a single component. In a package, we distinguish a specification part
(the head) and an implementation part (the body). The specification part
defines all those properties that must be visible from the outside. The body
describes the implementation of subprograms and defines all that is needed
within the package and need not to be visible from the outside.
The syntax for the definition of a package is:
package PackageName is
InterfaceDeclarations
end [PackageName]
CompleteDeclarations
[begin
InitializationInstructions]
end [PackageName]
586 Appendix C
Oracle PL/SQL
ignoring the body and thus considering a set of data that is more
restricted and easier to understand.
Packages can contribute to an improvement in services, as the whole
package is loaded into the memory the first time that a reference is made
to one of its components, minimizing the transfers from disks to main
memory. (In general, when access to a function is required, it is probable
that shortly afterwards access will be gained to other functions of the
same package.) Objects that are defined in a package remain available for
the entire session, shared by all the procedures that are executed during
the session.
Packages also extend the functionality of the system. For example, the
body of a package can also contain initialization instructions, which are
executed in each session the first time that a package is accessed.
To make a package permanent, the following commands are used:
create package PackageName is
InterfaceDeclarations
end [PackageName]
CompleteDeclarations
end [PackageName]
15
Bibliography
[1] Abiteboul, S., Hull, R., Vianu, V., Foundations of Databases, AddisonWesley, Reading, Mass., .
[2] Albano, A., De Antonellis, V., Di Leva, A. (eds.), Computer-Aided
Database Design: The DATAID Project, North-Holland, Amsterdam, .
[3] Atzeni, P., De Antonellis, V., Relational Database Theory, BenjaminCummings, Menlo Park, Calif., .
[4] Atzeni, P., Mecca, G., Merialdo, P., To Weave the Web, Proceedings of
rd International Conference on Very Large Data Bases, , Athens,
Greece, Morgan Kaufmann, San Francisco.
[5] Atzeni, P., Mecca, G., Merialdo, P., Design and Maintenance of DataIntensive Web Sites, Proceedings of th International Conference on
Extending Database Technology, Valencia, Spain, Lecture Notes in
Computer Science, vol. , pp. , Springer-Verlag, Berlin, .
[6] Bancilhon, F., Delobel, C., Kanellakis, P. (eds.), Building an ObjectOriented Database System: The Story of O2, Morgan Kaufmann, San
Mateo, Calif., .
[7] Batini, C., Ceri, S., Navathe, S. B., Conceptual Database Design, an EntityRelationship Approach, Benjamin-Cummings, Menlo Park, Calif., .
[8] Bernstein, P. A., Hadzilacos, V., Goodman, N., Concurrency Control and
Recovery in Database Systems, Addison-Wesley, Reading, Mass. .
[9] Bernstein, P. A., Middleware: A Model for Distributed System Services,
Communications of the ACM, vol. , no. , pp. , .
[10]Bertino, E., Martino, L., Object-oriented Databases Systems: Concepts and
Architectures, Addison-Wesley, Reading, Mass., .
[11] Brodie, M. L., Stonebraker, M., Legacy Systems: Gateways, Interfaces &
the Incremental Approach, Morgan Kaufmann, San Mateo, Calif., .
[12]Cannan, S. J., Otten, G. A. M., SQL The Standard Handbook, McGrawHill, New York, .
588 Bibliography
Bibliography 589
590 Bibliography
Bibliography 591
592 Bibliography
Index
Page numbers in bold indicate main entries
or definitions.
$ (for embedded sql) 141142
% (special char) 105106, 532
%notfound (pl/sql) 577
%rowtype (pl/sql) 574, 575
%type (pl/sql) 574
* (asterisk)
in count 114
A
Abiteboul, S. 38, 8081
abort 133, 284285, 286307, 311320,
363377
abort (log record) 313314, 319, 371373
absence of transparency 359
absolute 141
abstract type 423, 425426
abstraction level in a database 67
Access 245, 521541
access
control 7, 136138
policy 584
manager 321
method 283, 321, 323, 333, 339, 353,
363
plan 321, 343, 548550
privilege 426
strategy, see access plan
structures, see physical access structures
table, see table of accesses
times 308
accessor 405, 426
acid properties 285286, 317, 349, 351,
438441
ack, see acknowledgement
acknowledgement 368, 371, 374
action
in an html form 493
in a trigger 447450, 458460
activation
graph 456457
of objects 440
of triggers 448457
active
database 65, 447463, 540
rule 86, 182, 381, 429, 447463
transaction 309, 319
ActiveX 515
actual parameter 415, 581582
adaptive technique 431, 482
adaptor, object 439, 440
add method 406
after row (Oracle trigger) 450
after state 314316, 449
after statement (Oracle trigger) 450
after trigger 448, 452454
after trigger option 449, 450, 452
agent 441
aggregate queries in sql 113115
aggregation 107, 116117, 125, 132, 223,
304, 337, 422, 465481, 534536,
539, 556557
Agrawal, R. 486
ahead propagation 366367
Albano, A. 211
alerter 460
algebraic representation of queries 106
107, 333
all (polymorphic value) 477479
all
123125
594 Index
atomization of selections 60
attribute 6, 1819, 22, 399
compatibility 46
in the e-r model 164, 168169, 170, 195
set of 22
Atzeni, P. 38, 80, 276, 517
audio data 397, 429430
authorization 90
authorization, see access control
automatic lock requests 294
autonomy 371, 381
autonumber (Access) 525, 527
availability 310
average 556
avg 114115, 119, 422, 534
B
b tree 327, 331332, 480
b+ tree 327, 330332, 437, 480
backup 315, 375, 389
bag constructor 399, 435
balanced tree 328, 331
Bancilhon, F. 441
bar graph 475
basic construct of the e-r model 165169,
178
basket analysis 482, 484
batch 139, 324325, 390, 468
Batini, C. 184, 211, 251
beer 483
before row (Oracle trigger) 450
before state 314316, 449
before statement (Oracle trigger) 450
before trigger 448, 452454
before trigger option 449, 450, 452
begin transaction 284285, 290, 312
begin transaction (log record) 319, 369
behavioural analysis 481
benchmark 387388
Bernstein, P. 344, 391
Bertino, E. 441
BFILE (Oracle) 569
bi-directional relationships 417
Bieber, M. 517
bill of materials 389
binary object 435
binary_integer (Oracle) 567, 575
binding transfer 125128
bit 88
bit vector 480
bitmap index 480481
BLOB (Binary lob) 569
blob (binary large object) 435, 558559,
563, 569
block, see also page (physical)
header 322
trailer 322
block in pl/sql 571572, 576, 579
body 406, 585586
body, see Datalog rule
Index 595
bookmark 499
boolean 104, 399, 574
boot 318
bot, see begin transaction
bottom-up
strategy 198201, 202, 205
transformation primitives 199201, 204
bound control (Access) 538, 539
BoyceCodd normal form 259262, 267,
269, 272, 275, 472
branch and bound 341
broadcast 370
Brodie, M. 391
browser 353, 489490, 493, 514, 547, 553
bs, see before state
buffer 299, 317, 332, 337, 339, 341, 373,
378, 437
management 284, 307311, 312318,
321, 327, 352, 371
manager, see buffer management
business rule 180182, 183184, 205, 241,
457, 460461
for a derivation 180, 181
for an integrity constraint 180, 181
byte (Access domain) 525
byte-code 515
C
c 92, 141143, 406, 544, 551, 562, 585
c++ 404, 417, 439, 544, 551
cad (Computer Aided Design) 397
calculated control (Access) 538, 539
call by copy 582
call by reference 582
Call Level Interface (cli) 553
cam (Computer Aided
Manufacturing) 397
card, see cardinality of table
cardinality
of attribute 164, 172173
of relationship 164, 170172, 175, 180,
219
of table 48, 333, 335, 480
cartesian product 16, 17, 54, 101102,
107, 125, 534
cartesian space 477
cascade
casual user 9
catalogue 99100
catastrophic failure 313, 318
Cattel, R. G. G. 441
Ceri, S. 81, 184, 211, 251, 344, 391, 461,
517
cgi (Common Gateway Interface) 495,
508509, 511, 513
chain reaction 129, 138, 286
Chamberlin, D. D. 461, 544
char 88, 568
character 88
character set 88
check clause 93, 131132, 460, 526
check option 133134
check-in and check-out 438
checkpoint 312, 314315, 362
checkpoint (log record) 313, 315, 319
checksum 322
Chen, P. P. 184, 251
Cheng, J. 517
child entity of a generalization 170, 175,
227228
Chimera 461
class 402404
hierarchy 414, 425, 435
classification (data mining) 484485
classifier 484
cleaning of data 467
cli (Call Level Interface) 553
client-server 349, 351353, 372, 378379,
383, 388, 437438, 439441, 521,
567, 573
CLOB (Character lob) 559, 569
close
of cursor 141
of file 311
of scan 336
closed formula 68
clp (Command Line Processor) 546, 553
cluster 233
co2 406
cobol 551
Cochrane, R. 461
Codd, E. F. 15, 38, 80, 276, 486
coherency of methods 408
cold restart 312, 318, 320
collection 399, 575
collection of requirements 158, 189194,
211
collisions (hashing) 325
colours (col formula) 335
column 91
COLUMNS 100
Comer, D. E. 516
Command Center of db2 547550, 553
Command Line Processor 546, 553
commercial systems 128, 146, 298, 301,
306307, 343, 374, 391, 429, 460,
538
commit as log record 313314, 319, 371
375
596 Index
D
daemon 513
dangling tuple 51, 53, 424, 454
data 2
access 468
allocation 355, 356358
analysis 350, 384, 465488, 565567
cleaning 467
cube 468, 477479, 557
definition in sql 87100
Index 597
598 Index
disaggregation 476
discovery of patterns 482
discretization 484
discussion groups 497
disjunction 337, 480
disk 352, 384
dispatcher 353, 385
display method 412413
distinct 108, 114115, 419420, 537
distinct type in db2 560
distributed
concurrency control 363368
database 284, 294, 307, 340, 349393,
353376, 436, 439441
deadlock detection 365368, 391
join 386
object 439441
queries 354
query optimization 360361, 362363
request 361
transaction 349, 360361, 363365
Distributed Relational Database
Architecture 544
distribution
of projection with respect to union 62
of selection with respect to
difference 62
of selection with respect to union 62
dml, see data manipulation language
document management 430
documentation
of a schema 179183, 205, 241245
technique 182183
domain 16, 19, 26, 29, 9091, 98, 423,
567569
independence 7273
relational calculus 67, 6872, 530
domino effect 289
dot
notation 400, 406, 426, 586
operator 103
double 89, 524, 525, 568
double dot notation 427
drda, see Distributed Relational Database
Architecture
drill-down 468, 475477
driver (odbc) 377378
driver manager (odbc) 377378
drop 97, 9899, 129130, 132, 137, 304
drop index 343
dtp (Distributed Transaction
Processing) 351, 377, 441
dummy 471
dump 312, 315, 362
dump (log record) 313, 315, 320
duplicate 18, 25, 107108, 120121, 337,
399, 420, 527, 533, 578
durability 3, 4, 285, 286, 287, 311312,
318, 349, 362, 369, 374, 389, 417,
441
dw, see data warehouse
dynamic
aspects of an application 190
cursor 145
fragmentation 385
interface 439440
linking 413
sql 142145, 352, 378, 545, 553554
E
ebcdic 568
eca, see event-condition-action
eighty-twenty rule 219
election mechanism 376
electronic commerce 490, 497498
elementary domains in sql 8790, 92
Elmagarmid, A. K. 344
ElMasri, R. A. 10
e-mail 349, 352, 377, 567
embedded
sql 138145, 551
systems 498
transaction 417
empty join 52
encapsulation 139, 283, 398, 403, 405,
416, 419, 426, 428, 572
Encina 379
encoding of data 482
end (log record) 318
end loop 576
end transaction 284285, 290
end user 9, 466
engineering process 196
entity 164, 165, 178, 195, 271, 469, 473
Entity-Relationship model 6, 157, 162,
163179, 184, 189, 194, 255, 270,
399, 408, 417, 435, 469, 471472
entry sql 86
entry-sequenced sequential
structure 323, 324, 329
enumeration type 399, 414
eot, see end transaction
equality (objects) 401402, 420, 426427
equi-join 5456, 342, 357
equivalence
of algebraic expressions 5960
of relational schemas 234
of query languages 73, 80
of schedules 291294, 300, 364
e-r model, see Entity-Relationship model
error code 301, 378
evaluation of patterns 482
event 364365, 447459, 540541, 579
expression 454455
logging 457
notification 441
event-condition-action (eca) 447448
evolution of objects 440
exact numeric domain 8889
Excel 523, 562
except 120122, 127
exception 419, 447, 450, 454, 456, 572
573, 578581
Index 599
handler 578
exchange of messages 364, 370
exclusive generalization 176
exclusive lock, see also w_lock 294, 296,
304
exec sql 551
execute 144
execute immediate 143
executequery 553
execution of active rules 448, 453
existential quantifier, see quantifier
existing documentation 190
exists 126
exit 577578
expert users 138
explicit priority (trigger) 455
exponent 89
export 468
expression (relational algebra) 5660
expressive power 125, 419, 536, 554
extend 311
extender of db2 559
extensibility 416
extension
of classes 403404
of database 6
of objects 416
extensional predicate (Datalog) 77
extent 403, 418, 435436
external 562
external
application of triggers 457
data access 383
helper 514
identifier 164, 174, 175, 233, 237, 240
key 472
level 67, 16
program 145
query 125128
schema 7, 8
subprogram 561563, 584585
table (foreign key) 9597
table (nested loop) 337338
extraction of data 467
extranet 498
F
fact 469, 470475, 480
fail-stop 318
failure 312, 316, 317318, 368, 370, 388
Fairly, R. 183
Fayyad, U. M. 486
feasibility study 158
fetch 140141, 552, 578
field (Access) 524, 531
field properties (Access) 524525
file 3, 435
file system 310311, 327
fileid, see identifier (file)
filter 467, 469
G
garbage collector 412
gateway (in the Web) 494495
generalization
hierarchy 398, 404, 408411, 412413,
416, 420, 428, 435, 441
600 Index
H
Hadzilacos, V. 344
handle 412
Hansel and Gretel 312
hard failure 368
hash join 337, 338339
hash-based structure 320321, 325327,
329, 436
hashing function 325326, 338339
having clause 118119, 135, 149, 535536
head (pl/sql package) 585
head (rule), see Datalog rule
heterogeneity 171, 354, 377, 379, 381, 413,
439, 467
heuristic decision 379, 380381
hierarchical
architecture of data warehouses 468
I
ibm 85, 360, 365, 461, 543
idempotence of undo and redo 314
identical observable behaviour of
triggers 455, 456
identifier
of entity 173175, 271, 471
of file 311
of object, see object identifier
of transaction 294, 301, 314, 319
of tuple 30
identity of objects 165, 402, 420
idl (Interface Definition Language) 439,
440441
if-then-else control structure 146147,
576
illegal use of a construct 203
image data 397, 429, 525, 539
immediate
constraint 133, 286
trigger 448, 451452
impedance mismatch 139, 407
imperative language 407, 437
implementation
of class 403
of database 159, 181
Index 601
602 Index
120121
intersection (operator) 4243, 54, 120, 480
interval 90
interval predicate 327, 331, 337
interview 190
into clause 142, 144145
intranet 489, 498, 514
intra-query parallelism 384385, 387
intra-relational constraint 29, 9394
inverse link 417
inverted index 432
invisible form (Access) 539540
ip (Internet Protocol) 490, 494
irrelevant attribute 485
is not null 64, 106
is null 64, 106
Isakowicz, T. 517
isl (intention shared lock) 303
iso 85, 150
isolation 284285, 286, 287, 304305
iteration (pl/sql) 576578
ixl (intention exclusive lock) 303
intersect
J
Java 515, 544, 551, 553
driver 516
middleware server 516
Virtual Machine, see jvm
Javascript 515
jdbc (Java DataBase Connectivity) 515
516, 544, 553
driver manager 515
jdbc/odbc bridge 515
join 42, 4956, 103, 107, 115, 123124,
126, 134, 141, 264265, 334342,
385, 420421, 427, 468, 475, 480,
533, 537, 571
and intersection 54
and keys 50
and referential constraints 50
associativity 54
commutativity 54
complete 51
empty 52
equi- 5456, 342, 357
full outer 53, 108110, 529
incomplete 51
index 480481
left outer 53
method 337339
path 242, 528529, 530534
n-ary 5354
natural 42, 49, 357
outer 53
right outer 53
theta- 42, 5456, 109
Journal of db2 550
jpeg 429
Ju, P. 517
jvm (Java Virtual Machine) 515
K
Kanellakis, P. 441
key (odl) 418
key, see also primary key
and join 50
and projection 48
access methods 323324, 327332
in the e-r model 173
of a relation 30, 31, 3238, 93, 107, 258,
264, 267269, 343, 357, 424
and null values 3334
key-sequenced structure (tree
structure) 329, 331
kill (transaction) 286, 298, 306307, 367
Kim, W. 391
Kimball, R. 486
King, R. 184
knowledge
discovery 481482
independence 447, 461
Korth, H. F. 10
L
Lamport clock 364365, 391
Lamport, L. 364, 391
lan, see local area network
language transparency 359
laptop computer 390391
large object, see blob
Larson, J. A. 391
last 140, 534
late binding 398, 413, 416
layout 179, 209
leaf node 327332
left join 109
left outer join 53, 108110, 529
legacy system 251, 467
levels
in hypertexts 502504
of a tree 328330
of refinement 196
Levy, A. 517
lexical analysis of queries 332
library 439
life cycle of information systems 158160
like 105106, 532, 559
linear complexity 292293
link 430, 491, 493, 501
list constructor 399, 435
Liu, C. 517
loader 468
lob, see blob
local check option 133134
local
area network 354, 363
deadlock 367
information systems 383
serializability 363
Index 603
M
Macintosh 544
macro 522523, 539541
Maier, D. 38, 80, 276
main
copy 388390
file 325
memory 284, 287, 302, 307311, 317,
336, 339, 349, 352, 437438
mainframe 87, 354, 387, 565
maintainability 580
maintenance of information systems 177
Malaika, S. 517
mandatory
attribute 173
participation in a relationship 171
properties of oodbmss 416417
manifesto
of oodbmss 416417, 441
of third generation databases 428429,
441
Mannila, H. 184, 486
mantissa 89
many-to-many relationship 171, 232, 234
604 Index
no-steal policy
310
not in 124
not null 93, 94, 129, 133, 526, 574
not operator 104
not-ready (message) 370
165
null in update
N
name conflict 411
name-based mechanism 581582
nappies 483
n-ary
join 5354
relationship 172, 237, 274
natural (Oracle domain) 567
natural join 42, 49, 357
natural join in sql 111
natural language 180, 190
Navathe, S. B. 10, 184, 211, 251
navigation schema 219
NCLOB (National Character lob) 569
negation 7980, 126
nested
block 572
index 437
invocation of methods 407
loop join 337338, 342, 436
query 112, 122128, 135136, 149, 421,
537, 555557
record 575
table 571
transaction 344, 438439
varray 571
nesting 427428
NetBios 544
network 349380, 388391, 428, 437441,
489516, 544, 565567, 572
data model 5, 16, 21
partitioning 368, 374, 376
new
130
null value 2627, 3334, 6264, 9294,
106, 114115, 129130, 228, 232
233, 399, 454, 459, 526, 574
and key (of a relation) 3334
and relational algebra 6264
in sql 92, 94, 114115, 129130
number 524, 525, 568
number of distinct values 334
numeric 8889, 524
O
o2 398399, 405406, 414415, 419, 441
o2 Technology 398
Obermarck, R. 391
object
adaptor 440
creation 401
data model 398419, 423427
database 139, 284, 397445, 467, 543,
553, 558563, 567, 569572
identifier (oid) 21, 322, 399, 401, 416,
418, 424, 427428, 435438, 569
572
request broker (orb) 439440, 441
server 435
view 571572
object-oriented
database (oodbms) 21, 284, 397, 398
417, 441, 423, 447, 467, 543, 553,
558563
database manifesto 416417, 441
programming languages 140, 377, 398,
404, 408
object-relational database (ordbms)
140, 398, 423429, 567, 569572
object-valued attribute 401404, 427
observation 484485
occurrence of data 164
odbc (Open Database Connectivity) 351,
377378, 515, 521, 544, 553
driver 377378, 515
Ode 461
odl 417419, 439
odmg (Object Database Management
Group) 398399, 417422, 439,
441
odmg-93 417418
ogis, see Open gis Committee
oid, see object identifier
olap (on-line analytical processing) 350,
384387, 465466, 475, 479480,
486, 543, 556557, 565566
Index 605
queue 353
overflow
chain 325327
file 325
overlapping generalization 176
overloading 398, 413, 416, 560, 583
overriding 412, 413, 416
Ozsu, T. 391
P
package 565, 585586
page (html) 491514
schema 500501
page (physical) 284, 287, 294, 308311,
317, 319, 321332, 371, 437438,
dictionary 322323
header 322
manager 322
trailer 322
Paraboschi, S. 517
parallelism 284, 303, 317, 349350, 360,
372, 383388, 468, 480, 565
parameter (subprograms) 144145, 402,
405407, 414415, 419, 426, 581
582
parametric query 353
Paredaens, J. 38, 80
parent entity of a generalization 170, 175,
227228
partial
generalization 176
rollback 133, 450, 458
participant (2pc), see rm
partition 422
partitioning
of entities 223, 230, 238, 248
of relationships 223, 232
of tables 116, 338339, 422, 428
Pascal 92
passing of privileges 137
passive
behaviour 447
rm 379
rule 429
path expression 420, 436437
pattern (data mining) 482
pattern matching (strings) 105106
pc, see personal computer
peer-to-peer 390
Pelagatti, G. 344, 391
performance
analysis on e-r schemas 218222
indicators 218219, 225
persistence, see durability
persistent
class 412
object 407, 411412
personal computer 87, 352, 521, 544, 565
physical
606 Index
Q
qbe (Query By Example) 73, 526, 530536
quadtree 434
quality
in design 159161, 189, 203205, 209,
255, 266
of data 466467
quantifier 6869, 71, 74, 79
Query By Example, see qbe
query
language 4180, 100128, 400, 403,
407, 416422, 427428, 437, 465,
530536
optimization, see optimization of queries
plan 283
sql 100128
queue 301302
quorum 376
R
r_lock 294287, 301, 305
Raiha, K. J. 184
raise 579
raises (odl) 419
Ramakhrishnan, R. 10
range list 74
raw 568, 569
reachability 402, 412
reaction to violations 9697, 132, 459
461, 529
Index 607
608 Index
rowid 568569
row-level trigger 448453
rpc, see remote procedure call
rtm 298, 299
rule
analysis 456457
Datalog 77
engine 447
set 455
Rumbaugh, J. 408
S
sag (sql Access Group) 377
Samaras, G. 391
Samet, H. 441
sampling 467, 486
scalability 386, 387, 486
scale 8889, 568
scale-up 386, 387
scan 324, 331, 336, 337339, 342, 384385
schedule 287, 291, 292300, 305, 363364
scheduler 287299, 308, 363364
scheduling of triggers 447, 453
schema 6
definition in sql 90100
documentation, see documentation of a
schema
of a relation, see relation schema
updates in sql 9799
scope 125, 579
scope clause 425
scratchpad 562
Script Center of db2 550
scroll option 140141
search
in documents 431432
on bitmap indexes 481
on trees 328329, 331332
second 8990
second normal form 268
secondary
copy 388389
index 233, 329
memory 228, 284, 287, 299, 307311,
315, 318319, 349, 352, 385, 417,
435, 437438
security 136138, 457, 521
select 101102, 107, 421, 479, 579
select privilege 137138
selection (relational operation) 42, 4547,
60, 334336, 342, 356, 386, 421,
435, 474475, 537
selection of primary identifiers 223, 233
234, 249
self variable 406
semantic
analysis of queries 332333
complexity 408, 416
error in a conceptual schema 203
Senn, J. A. 183
Index 609
Smyth, P. 486
snowflake schema 473474
soft failure 368
software engineering 183
sort (oql) 422
sort, see ordering of data
sound, see audio data
sparse index 329
spatial data 397, 432434
special indexes 480481
specialization 175, 410, 412, 415
specification of requirements 190, 191,
198
speed-up 386, 387
split (on trees) 329331
spreadsheet 352, 475, 523, 525
sql (implicit cursor) 577
sql 8, 64, 76, 85154, 182, 250, 351352,
358361, 377378, 407408, 419
420, 429, 437, 449450, 453, 458,
465, 475, 478, 509, 521, 524, 536,
529530, 534537, 540, 545, 548,
551, 565569, 572573, 578, 584
and unions 76
in Access 536537
sqlnet 566
sql/ds 543
sql-2 86, 88, 92, 99, 106, 108, 111, 131
132, 145, 150, 304, 423, 426428,
448, 460, 529, 544, 554, 557, 583
sql-3 86, 145, 150, 398, 423428, 448
data model 423426
query language 427428
sql-89 86, 106
sql-92, see sql-2
sql-99, see sql-3
sqlca 551
sqlcode 141142, 551, 553
sqlconnect() 553
sqlda 554
sqlexecute 553
sqlfetch() 553
sqlprepare() 553
stable memory 312313, 315, 318, 373, 389
stack 322323
standard (package) 586
standard deviation 115
standardization 8587, 343, 351352, 377,
381, 391, 417, 439, 448, 460, 485,
568
star schema 469472, 473474, 556
Starburst 461
starvation 307
stateless nature of http 494, 511512
statement-level trigger 448453
static
aspects of an application 190
check 415
fragmentation 385
interface (corba) 439440
properties of objects 399
sql 142, 143144, 352, 378, 545, 551
553
T
table 5, 1525
alias 104, 107, 111112, 123, 125126
definition 91
function 561563
of accesses 219, 225, 247
of operations 219, 225, 246
of volumes 219, 225, 246
variable, see table alias
wizard 523
610 Index
toolbar 522
537
top block (of the log) 313, 316
top-down
strategy 196198, 202, 205, 210
transformation primitives 196198, 204
total (qbe) 534536
total generalization 176, 228
tpc (Transaction Processing Performance
Council) 387388
tps (transactions per second) 287, 317,
384388
training set 484
transaction 9, 133, 284285, 286391,
413, 438439, 441, 447, 451452,
465, 496497, 521, 569
classification 360361
formal model of a 290
identifier 290, 369370
manager, see tm
on the Web 496497
processing system 284
record 313
shipping 468
transactions per second, see tps
transformer 405, 407, 426
transitive closure 583
translation
into a logical model 217
of a specification 195
of e-r schemas into the relational
model 234241, 249, 251
transmission cost 363
transparency 358360, 361, 381383
tree
manager 328
physical structure 323, 327332, 337,
433
structure of queries 333341
trigger 86, 182, 381, 429, 447463
trivial functional dependency 258
ts, see timestamp-based method
Tsichritzis, D 38, 184
tuning 343
tuple 1618, 19
constraint 29, 30
constructor in sql 127
dangling 51, 53
ordered 17
relational calculus 67, 73
relational calculus with range
declarations 67, 7477, 107
type 423, 425
tuple-oriented behaviour 128, 131, 139,
448
Tuxedo 379
two-dimensional spatial data 432
two-phase commit 349, 351, 361, 364,
369376, 378379, 388, 391
optimizations 374375
two-phase locking 291, 294298, 299
301, 305, 363364
two-tier architecture 353
top
Index 611
U
Ullman, J. D. 10, 38, 8081, 276
unbound control (Access) 538
under 425
undo 285, 311, 314, 315320, 373
undo set 319320
unfix 307308, 309
uniform distribution 326, 335
Uniform Resource Locator, see url
union 120121
union 4243, 4445, 120121, 135, 356,
480, 537, 558
unique constraint 9394, 95, 133
unique index 329, 343, 527, 578
universal quantifier, see quantifier
universal server 585
Unix Systems 379
unknown (truth value) 63, 106
unlock 294295, 301
unnesting 427428
unstructured data 566
update 97, 130131, 140, 141, 304, 448,
450
as log record 313314
event 449, 452
privilege 137
update
anomaly 256
loss 288, 292, 364
of key (on trees) 331
of view, see view update
update statistics 334
update-ind 324
up-to-date data 382
url (Uniform Resource Locator) 492, 508,
525
usage privilege 137
use 307308, 309
useful information (in a block) 322
user 92
user 9
function in db2 560
identification 136
interface 159, 349, 352353, 403, 441,
546, 554
requirement 157, 189190
transactions 287
type in db2 560
user-defined
domain 9192
event 454
exception 578579
user-friendly interface 465468, 474475,
521, 537
using 144145
V
val 334, 335
Valduriez, P. 391
valid page 308309
validation 159
value-based data model 2122, 3237, 49
values 128
varbit 88
varchar 88, 524, 568
varchar2 568
variable
in Datalog 7778
in domain relational calculus 6869
in embedded sql 140142
in pl/sql 565, 573, 574575, 581
in sql, see table variable
variable length strings 88
variation file 468
varray (varying array) 570, 571
varying 88
vector data structure 479
verification of data 469
version 417, 438, 457
vertical decomposition 45, 47, 230, 356,
357, 435
Vianu, V. 38, 8081
victim page 309310
video data 430, 431
view 7, 41, 6567, 9091, 131, 133136,
536
in sql query 135136
materialization 65, 480, 481
update 6566, 134, 571
recursive 65
view-equivalence 291, 292293, 364
view-serializability 292, 293, 300
violation of constraints 9698, 286, 458
459, 529
virtual relation, see view
visibility of variables, see scope
Visual Warehouse 545
Vitali, F. 517
void 419
volume of data 219
von Halle, B. 184
Vossen, G. 344
vsr, see view-serializability
W
w_lock
wait
612 Index
graph 366367
sequence 366367
state 294, 297, 299, 301302, 306307,
366368
wal, see write-ahead log
wan, see wide area network
warehouse, see data warehouse
warm restart 312, 318, 319320, 373
Web 489518, 566
Information System, see wis
publishing 496
server, see http server
site
data-intensive 499
design 498508
generation 507
materialization 512513
surfing 495
well-formed transaction
for commit or abort 285
for locking 294, 296
wfms, see workflow management system
whenever 551
where 101102, 104106, 109, 114, 118
119, 122, 125, 129130, 131, 141,
421, 536
while 576577
wide area network (wan) 354355
Widom, J. 10, 461
Wiederhold, G. 184
window of uncertainty 371, 372, 376
Windows 521522, 525, 538, 544, 566
wis 489, 494498
with 558
with check option 133134
with cube 477
with grant option 137138
with object id 571
with roll up 479
with time zone 8990
wizard 523, 526, 538
workflow 441
X
x Windows 566
xa_abort 380
xa_close 379, 380
xa_commit 380
xa_end 379, 380
xa_forget 380, 381
xa_open 379, 380
xa_precom 379, 380
xa_recover 380
xa_start 379, 380
xa-interface 379381
xl (exclusive lock, hierarchical
locking)
303
xml 430
x-open 351, 373, 377381, 391, 441
dtp 378381
xor (exclusive or) 326
Y
Yang, D. 251
year 8990
Z
Zaniolo, C.
38, 461
Contents list
The contents list in the screens left-hand panel and the contents list in the book provide links to the
chapters and sections within the book. Just click on the item name to view the appropriate page.
Cross-references
All cross-references to figures or tables in the text of the book are links to the appropriate figure or
table. Bibliographic references also act as links.
Thumbnails
If you switch to thumbnail view using the thumbnail icon, the left-hand panel will display page
thumbnails. Clicking on a thumbnail will take you to that page.
Returning
This icon will allow you to retrace your steps.
More
Acrobat navigation
You can also move around an Acrobat document by using Acrobats toolbar. For an explanation of the
Acrobat toolbar click on this link: Acrobat Toolbar Help.
Viewing options
For most purposes you will find single page viewing to be the most appropriate. To see readers
spreads you should select continuous facing pages. Both of these options are under the view
menu.
Limitations
Please note that you will not be able to print pages from this book or make any alterations to it. You
will therefore find that some of Acrobats menu items and toolbar icons are greyed out.
More
go to previous page
go back
go to first page
go to last page
go forward
Viewing pages
page only