Lecture 3/1 The Relational Model: Relational Algebra and Relational Calculus • T. Connolly, and C. Begg, “Database Systems: A Practical Approach to Design, Implementation, and Management”, 5th edition, Addison-Wesley, 2009. ISBN: 0-321-60110-6, ISBN-13: 978-0-321-60110-0 (International Edition). • T. Connolly, and C. Begg, “Database Systems: A Practical Approach to Design, Implementation, and Management”, 4th edition, Addison-Wesley, 2004. ISBN: 0-321-21025-5. • R. Elmasri and S. B. Navathe, “Fundamentals of Database Systems”, 5th ed., Pearson, 2007, ISBN: 0-321-41506-X. ITM661 – Database Systems
60
Embed
Lecture 3/1 The Relational Model: Relational …mit.wu.ac.th/mit/images/editor/files/itm661-lecture03...Lecture 3/1 The Relational Model: Relational Algebra and Relational Calculus
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Lecture 3/1 The Relational Model:
Relational Algebra and
Relational Calculus
• T. Connolly, and C. Begg, “Database Systems: A Practical Approach to Design, Implementation, and Management”, 5th edition,
Addison-Wesley, 2009. ISBN: 0-321-60110-6, ISBN-13: 978-0-321-60110-0 (International Edition).
• T. Connolly, and C. Begg, “Database Systems: A Practical Approach to Design, Implementation, and Management”, 4th edition,
Addison-Wesley, 2004. ISBN: 0-321-21025-5.
• R. Elmasri and S. B. Navathe, “Fundamentals of Database Systems”, 5th ed., Pearson, 2007, ISBN: 0-321-41506-X.
ITM661 – Database Systems
ITS322 - DBMSs The Relation Model 2
Objectives Overview of the relational model, e.g., how tables
represent data.
The connection between mathematical relations and
relations in the relational model.
Properties of database relations
How to identify candidate, primary, and foreign keys.
The meaning of entity integrity and referential integrity.
How to form queries in relational algebra.
How relational calculus queries are expressed.
The purpose and advantages of views in relational
systems.
ITS322 - DBMSs The Relation Model 3
An Example (Branch and Staff Relations)• A relation is a table with
columns and rows.Only applies to logical structure of
the database, not the physical
structure.
• An attribute is a named
column of a relation.
• A domain is a set of
allowable values for one or
more attributes.
• A tuple is a row of a
relation.
• A degree is a number of
attributes in a relation.
• A cardinality is a number of
tuples in a relation.
• A relational database is a
collection of normalized
relations.
ITS322 - DBMSs The Relation Model 4
Terminology for Relational Model and Attribute Domain
Terminology
Attribute Domain
ITS322 - DBMSs The Relation Model 5
Mathematical Relations (I) Mathematical definition of relation
Consider two sets D1 = {2, 4} and D2 = {1, 3, 5}.
Cartesian product is D1D2, the set of all ordered pairs, 1st element is member of D1 and 2nd element is member of D2.
Alternative way is to find all combinations of elements with first from D1 and second from D2.
Mathematical Relations (II) Any subset of Cartesian product is a relation.
R = {(2, 1), (4, 1)}
May specify which pairs are in relation using some condition for selection. For example, the second element is 1
R = {(x, y) | x D1, y D2, and y = 1}
Using same sets, form another relation S, where first element is always twice the second.
S = {(x, y) | x D1, y D2, and x = 2y}
Only one ordered pair in the Cartesian Product satisfies this condition.
S = {(2, 1)}
ITS322 - DBMSs The Relation Model 7
Mathematical Relations (III) Consider three sets D1, D2, and D3 with Cartesian
Product D1D2D3. For example
D1 = {1, 3} , D2 = {2, 4} , D3 = {5, 6}
D1D2D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6),
(3,2,5), (3,2,6), (3,4,5), (3,4,6)}
Any subset of these ordered triples is a relation.
T = {(x, y, z) | x D1, y D2, z D3 and y = 2x
and z = 3y}
T = { (1, 2, 6) }
ITS322 - DBMSs The Relation Model 8
Mathematical Relations (IV) To define a general relation on n domains…let D1,
D2, . . ., Dn be n sets with Cartesian product defined as
D1 D2 . . . Dn = {(d1, d2, . . . , dn) | d1 D1, d2 D2, . . . , dn Dn} usually written as
In defining relations we specify the sets, or domains, from which we chose values.
i
n
i
D1
ITS322 - DBMSs The Relation Model 9
Database Relations and Their Properties Relation schema
Named relation defined by a set of attribute and domain name pairs.
Relational database schema Set of relation schemas, each with a distinct name.
Relation name is distinct from all other relations.
Each cell of relation contains exactly one atomic (single) value.
Each attribute has a distinct name.
Values of an attribute are all from the same domain.
Order of attributes has no significance.
Each tuple is distinct; there are no duplicate tuples.
Order of tuples has no significance, theoretically.
ITS322 - DBMSs The Relation Model 10
Relational Keys (I) Superkey
An attribute or a set of attributes that uniquely
identifies a tuple within a relation.
Candidate Key
A superkey (K) such that no proper subset is a
superkey within the relation.
In each tuple of R, the values of K uniquely
identify that tuple (uniqueness).
No proper subset of K has the uniqueness property
(irreducicility).
ITS322 - DBMSs The Relation Model 11
Relational Keys (II) Primary Key
Candidate key selected to identify tuples uniquely
within relation.
Alternate Keys
Candidate keys that are not selected to be the
primary key.
Foreign Key
An attribute or set of attributes within one relation
that matches candidate key of some (possibly
same) relation.
ITS322 - DBMSs The Relation Model 12
Relational Integrity (I) Null
Represents a value for an attribute that is currently
unknown or is not applicable for this tuple.
Deals with incomplete or exceptional data.
represents the absence of a value and is not the
same as zero or spaces, which are values.
ITS322 - DBMSs The Relation Model 13
Relational Integrity (II) Entity Integrity
In a base relation, no attribute of a primary key can be null.
Referential Integrity
If foreign key exists in a relation, either the foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null.
Enterprise Constraints
Additional rules specified by users or database administrators.
ITS322 - DBMSs The Relation Model 14
Relational Algebra and Calculus Relational Algebra
Unary Relational Operations
Relational Algebra Operations From Set Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
Relational Calculus
Tuple Relational Calculus
Domain Relational Calculus
ITS322 - DBMSs The Relation Model 15
Relational Algebra and Calculus Relational algebra and relational calculus are formal
languages associated with the relational model.
Informally,
Relational algebra is a (high-level) procedural
language and
Relational calculus a non-procedural language.
However, formally both are equivalent to one
another.
A language that produces a relation that can be
derived using relational calculus is relationally
complete.
ITS322 - DBMSs The Relation Model 16
Relational Algebra Relational algebra operations work on one or more
relations to define another relation without changing
the original relations.
Thus, both operands and results are relations, so
output from one operation can become input to
another operation.
This allows expressions to be nested, just as in
arithmetic. This property is called closure.
ITS322 - DBMSs The Relation Model 17
Relational Algebra (Overview) Relational Algebra consists of several groups of operations
When domain relational calculus is restricted to safe
expressions, it is equivalent to tuple relational
calculus restricted to safe expressions, which is
equivalent to relational algebra.
This means every relational algebra expression has an
equivalent relational calculus expression, and vice
versa.
ITS322 - DBMSs The Relation Model 59
Other Languages Transform-oriented languages are non-procedural
languages that use relations to transform input data into outputs (e.g. SQL).
Graphical languages provide the user with a picture or illustration of the structure of the relation. The user fills in an example of what is wanted and the system returns the required data in that format (e.g QBE).
Fourth-generation languages (4GLs) can create a complete customized application using a limited set of commands in a user-friendly, often menu-driven environment.
Some systems accept a form of natural language, sometimes called a fifth-generation language (5GL). This development is still in its infancy.
ITS322 - DBMSs The Relation Model 60
Exercise The following tables form a part of a database held in a relational