UNIT II - Chapter I
UNIT II - Chapter I
Chapter -I
Syllabus:
Formal Relational Query Languages: The Relational Algebra, The Tuple
Relational Calculus, The Domain Relational Calculus.
Relational Algebra
➢ Relational algebra is a procedural query language. It consists of a set of operations
that take one or two relations as input and produce a new relation as their result.
➢ The fundamental operations in relational algebra are select, project, union, set
difference, Cartesian product, and rename.
➢ In addition to the fundamental operations, there are several other operations namely,
set intersection, natural join, and assignment.
Fundamental Operations
➢ The select, project, and rename operations are called unary operations because they
operate on one relation.
➢ The other three operations operate on pairs of relations and are, therefore, called binary
operations.
Notation: σ p(r)
Where:
Examples:
i. Select the tuples of the instructor relation where the instructor is in the Physics
department.
we write:
Fig. The instructor relation
The project operation is a unary operation that returns the relation with required
attributes and the rest of the attributes are eliminated from the relation. Projection is
denoted by the uppercase Greek letter pi (∏). We list those attributes that we wish to
appear in the result as a subscript to ∏. The argument relation follows in parentheses.
Examples:
i. Display the ID, name, and salary attributes from instructor relation
The result of above projection statement is
i. The relations R and S must be of the same arity. That is, they must have
the same number of attributes.
ii. The domains of the ith attribute of R and the ith attribute of S must be the
same, for all i.
Example:
Find the set of all courses taught in the Fall 2009 semester, the Spring 2010 semester,
or both.
Fig. The Section relation
The set-difference operation, denoted by −, allows us to find tuples that are in one
relation but are not in another. The expression R − S produces a relation containing
those tuples in R but not in S. As with the union operation, we must ensure that set
differences are taken between compatible relations.
Example: Find all the courses taught in the Fall 2009 semester but not in Spring 2010
semester.
➢ The rename operator, denoted by the lowercase Greek letter rho (ρ) is used to
rename the output relation. Given a relational-algebra expression E, the
expression ρ x (E) returns the result of expression E under the name x.
➢ A relation r by itself is considered a (trivial) relational-algebra expression. Thus,
we can also apply the rename operation to a relation r to get the same
relation under a new name.
➢ A second form of the rename operation is as follows:
Assume that a relational algebra expression E has arity n. Then, the expression
ρ x(A1, A2,...,An) (E) returns the result of expression E under the name x, and
with the attributes renamed to A1, A2, . . . , An.
The natural join is a binary operation that allows us to combine certain selections and a
Cartesian product into one operation. It is denoted by the join symbol. The
natural-join operation forms a Cartesian product of its two arguments, performs
selection-forcing equality on those attributes that appear in both relation schemas, and
finally removes duplicate attributes.
Example: Find the names of all instructors together with the course id of all courses they
taught.
Since the schemas for instructor and teaches have the attribute ID in common, the
natural-join operation considers only pairs of tuples that have the same value on ID. It
combines each such pair of tuples into a single tuple on the union of the two schemas;
that is, (ID, name, dept name, salary, course id).
➢ The theta join operation is a variant of the natural-join operation that allows us to
combine a selection and a Cartesian product into a single operation. Consider relations
r (R) and s(S), and let ϴ be a predicate on attributes in the schema R ∪ S. The theta
join operation r s is defined as follows:
The evaluation of an assignment does not result in any relation being displayed to the
user. Rather, the result of the expression to the right of the ← is assigned to the relation
variable on the left of the←. This relation variable may be used in subsequent
expressions.
4. Outer join Operations
➢ The outer join operation works in a manner similar to the natural join operation but
preserves those tuples that would be lost in a join by creating tuples in the result
containing null values.
➢ There are actually three forms of the operation: left outer join, denoted ; right outer
join, denoted ; and full outer join, denoted . All three forms of outer join
compute the join, and add extra tuples to the result of the join.
➢ The left outer join ( ) takes all tuples in the left relation that did not match with
any tuple in the right relation, pads the tuples with null values for all other attributes
from the right relation, and adds them to the result of the natural join. All information
from the left relation is present in the result of the left outer join.
➢ The right outer join ( ) is symmetric with the left outer join: It pads tuples
from the right relation that did not match any from the left relation with nulls
and adds them to the result of the natural join. All information from the right
relation is present in the result of the right outer join.
➢ The full outer join ( ) does both the left and right outer join operations,
padding tuples from the left relation that did not match any from the right
relation, as well as tuples from the right relation that did not match any from
the left relation, and adding them to the result of the join.
Extended Relational-Algebra Operations
Extended relational-algebra operations provide the ability to write queries that cannot
be expressed using the basic relational algebra operations. There are two extended relational
algebra operations: Generalized Projection and Aggregation.
1. Generalized Projection:
➢ It extends the projection operation by allowing operations such as arithmetic and string
functions to be used in the projection list.
➢ Example:
gives the ID, name, dept name, and the monthly salary of each instructor
2. Aggregation:
➢ In the above example, t is a free variable. Tuple variable s is said to be a bound variable.
➢ A tuple-relational-calculus formula is built up out of atoms. An atom has one of the
following forms:
• s∈ r, where s is a tuple variable and r is a relation.
• s[x] ϴ u[y], where s and u are tuple variables, x is an attribute on which s is
defined, y is an attribute on which u is defined, and ϴ is a comparison operator
(<, ≤, =, _=, >, ≥).
• s[x] ϴ c, where s is a tuple variable, x is an attribute on which s is defined, ϴ is
a comparison operator, and c is a constant in the domain of attribute x.
➢ We build up formulae from atoms by using the following rules:
• An atom is a formula.
• If P1 is a formula, then so are ¬P1 and (P1).
• If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
• If P1(s) is a formula containing a free tuple variable s, and r is a relation, then
∃ s ∈ r (P1(s)) and ∀ s ∈ r (P1(s)).
are also formulae.
➢ In the tuple relational calculus, the following equivalences are available:
1. P1 ∧ P2 is equivalent to ¬ (¬(P1) ∨ ¬(P2)).
2. ∀ t ∈ r (P1(t)) is equivalent to ¬ ∃ t ∈ r (¬P1(t)).
3. P1 ⇒ P2 is equivalent to ¬(P1) ∨ P2.
➢ If a TRC expression returns the finite number of tuples then we say that is safe otherwise
unsafe.
➢ A DRC query has the form {〈x1, x2, . . . , xn〉 | p(〈x1,x2,.. ., xn〉)}, where each xi is
either a domain variable or a constant and p(〈x1,x2,.. ., xn〉) denotes a DRC formula
whose only free variables are the variables among the xi, 1 ≤ i ≤ n. The result of this
query is the set of all tuples 〈x1, x2,...,xn〉 for which the formula evaluates to true.
➢ A DRC formula is defined in a manner that is very similar to the definition of a TRC
formula. The main difference is that the variables are now domain variables. Let op
denote an operator in the set {<, >, =, ≤, ≥, =} and let X and Y be domain variables.
➢ An atom in the domain relational calculus has one of the following forms:
• < x1, x2, . . . , xn > ∈ r, where r is a relation on n attributes and x1, x2, . . . , xn are
domain variables or domain constants.
• x ϴ y, where x and y are domain variables and ϴ is a comparison operator (<, ≤, =,
!=, >, ≥). We require that attributes x and y have domains that can be compared by ϴ.
• x ϴ c, where x is a domain variable, ϴ is a comparison operator, and c is a constant in
the domain of the attribute for which x is a domain variable.
➢ We build up formulae from atoms by using the following rules:
• An atom is a formula.
• If P1 is a formula, then so are ¬P1 and (P1).
• If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
• If P1(x) is a formula in x, where x is a free domain variable, then ∃ x (P1(x)) and
∀ x (P1(x)) are also formulae.
➢ Examples:
1. Find the instructor ID, name, dept name, and salary for instructors whose salary
is greater than $80,000.
{< i, n, d, s > | < i, n, d, s > ∈ instructor ∧ s > 80000}
2. Find all instructor ID for instructors whose salary is greater than $80,000.
{< n > | ∃ i, d, s (< i, n, d, s > ∈ instructor ∧ s > 80000)}
3. Find the set of all courses taught in the Fall 2009 semester, the Spring 2010
semester, or both:
{< c > | ∃ s (< c, a, s, y, b, r, t >∈ section ∧s = “Fall” ∧ y = “2009”
∨∃u (< c, a, s, y, b, r, t >∈ section
∧ s = “Spring” ∧ y = “2010”
4. Find all students who have taken all courses offered in the Biology department:
{< i > | ∃ n, d, t (< i, n, d, t > ∈ student) ∧
∀ x, y, z,w (< x, y, z,w > ∈ course ∧ z = “Biology” ⇒
∃ a, b (< a, x, b, r, p, q > ∈ takes ∧ < c, a > ∈ depositor ))}