Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Chapter 6

Relational algebra and


relational calculus

1
Overview of Relational Algebra
• The basic set of operations for the relational
model is the relational algebra.
• These operations enable a user to specify
basic retrieval requests as relational algebra
expressions.
• The result of retrieval is a new relation,
which may have been formed from one or
more relations.
2
• The algebra operations thus produce new
relations, which can be further manipulated
using operations of the same algebra.
• A sequence of relational algebra operations
forms a relational algebra expression,
whose result will also be a relation that
represents the result of a database query (or
retrieval request).
3
Importance of relational
algebra
• It provides a formal foundation for
relational model operations.
• It is used as a basis for implementing and
optimizing queries in the query processing
and optimization modules that are integral
parts of relational database management
systems (RDBMSs)
• Some of its concepts are incorporated into
the SQL standard query language for4
RDBMSs.
• Fundamental operations in relational
algebra are:
• Selection ( )
• Projection (  )
• Cross/Cartesian-Product ( x )
• Set-Difference ( - )
• Union ( )
• Intersection ()
• Join
• Division operations ÷

5
• The selection and projection are unary
operations, since they operate on one
relation. The other operations work on pairs
of relations and are therefore called binary
operations.

6
7
Selection (Or Restriction)
• Selects subset of tuples/rows in a relation
that satisfy selection condition.
• The Selection operation is applied to each
tuple individually .
• The degree of the resulting relation is the
same as the original relation but the
cardinality (no. of tuples) is less than or
equal to the original relation.
8
• The Selection operator is commutative.
• σ<cond1>(σ<cond2>(R)) =
σ<cond2>(σ<cond1>(R))
• Set of conditions can be combined using
Boolean operations [(AND), (OR), and
~(NOT)]
• No duplicates in result

9
• Notation: σ<Selection Condition><Relation
Name>
• Selection condition general form:-
• <attribute name><comparison
operators><constant value>
• <attribute name><comparison operators><
attribute name>
• Comparison operators: =, >, >=, <, <=, !=
10
Example
• To select the EMPLOYEE tuples whose
department is 4
• σ Dno=4(EMPLOYEE)
• select the tuples for all employees who
either work in department 4 and make over
$25,000 per year, or work in department 5
and make over $30,000
• σ(Dno=4 AND Salary>25000) OR (Dno=5 AND
Salary>30000)(EMPLOYEE) 11
The PROJECT Operation
• Selects certain attributes while discarding
the other from the base relation.
• Creates a vertical partitioning:
• One with the needed columns (attributes)
containing results of the operation and other
containing the discarded Columns.
• Deletes attributes that are not in projection
list.
12
• Projection operator has to eliminate
duplicates! Because duplication removal is
necessary to insure that the resulting table is
also a relation.
• Notation: ∏<Selected Attributes><Relation
Name> Or ∏a1,…an(R)

13
14
Example
• to list each employee’s first and last name
and salary, we can use the PROJECT
operation as follows:
• Π Lname, Fname, Salary(EMPLOYEE)

15
• For most queries, we need to apply several
relational algebra operations one after the
other.
• Either we can write the operations as a single
relational algebra expression by nesting the
operations(in-line expression),
• Or we can apply one operation at a time and
create intermediate result relations.

16
• For example, to retrieve the first name, last
name, and salary of all employees who
work in department number 5, we must
apply a SELECT and a PROJECT
operation.
• We can write a single relational algebra
expression (in-line expression) as follows
• Π Fname, Lname, Salary(σ Dno=5(EMPLOYEE))
17
• Or we can explicitly show the sequence of
operations, giving a name to each
intermediate relation, as follows:
• DEP5_EMPS ← σ Dno=5(EMPLOYEE)
• RESULT ← Π Fname, Lname, Salary(DEP5_EMPS)

18
Union Operation
• Denoted as R S
• The union of two relations R and S defines
a relation that contains all the tuples of R or
S or both R and S, duplicate tuples being
eliminated.
• R and S must be union-compatible
• Same number of fields.
• Corresponding’ fields have the same type.
19
Example: r  s

20
Example
• to retrieve the Social Security numbers of
all employees who either work in
department 5 or directly supervise an
employee who works in department 5, we
can use the UNION operation as follows:
• DEP5_EMPS ← σ Dno=5(EMPLOYEE)
• RESULT1 ← Π Ssn(DEP5_EMPS)
• RESULT2 (Ssn) ← Π Super_ssn(DEP5_EMPS)
• RESULT ← RESULT1  RESULT2 21
Intersection Operation
• Denoted as R S
• The intersection operation defines a relation
consisting of the set of all tuples that are in
both R and S.
• R and S must be union-compatible

22
Example: r  s

23
Set Difference
• Denoted as R-S
• The set difference operation defines a
relation consisting of the tuples that are in
relation R, but not in S.
• R and S musts be Union compatible.

24
Example: r – s

25
Cartesian Product
• Denoted as R x S
• The Cartesian product operation defines a
relation that is the concatenation of every
tuple of relation R with every tuple of
relation S
• Each row of R is paired with each row of S
• The result of R x S is a relation Q with
degree n + m attributes Q(A1, A2, . . ., An,
B1, B2, . . ., Bm), in that order. 26
• The resulting relation Q has one tuple for
each combination of tuples—one from R
and one from S.
• If R has n tuples, and S has m tuples, then |
R x S | will have n* m tuples.

27
Example

28
Example
• suppose that we want to retrieve a list of
names of each female employee’s
dependents. We can do this as follows:
• FEMALE_EMPS ← σ Sex=‘F’(EMPLOYEE)
• EMPNAMES ← Π Fname, Lname,
Ssn(FEMALE_EMPS)
• EMP_DEPENDENTS ← EMPNAMES ×
DEPENDENT
• ACTUAL_DEPENDENTS ← σ
Ssn=Essn(EMP_DEPENDENTS)
• RESULT ← Π Fname, Lname,
Dependent_name(ACTUAL_DEPENDENTS)
29
Overview of tuple Relational
Calculus
• A calculus expression specifies what is to
be retrieved rather than how to retrieve it.
• Therefore, the relational calculus is considered
to be a nonprocedural language.
• This differs from relational algebra, where we
must write a sequence of operations
• It has been shown that any retrieval that can
be specified in the basic relational algebra
can also be specified in relational calculus,
and vice versa
30
• The tuple relational calculus is based on
specifying a number of tuple variables.
• Each tuple variable usually ranges over a
particular database relation, meaning that
the variable may take as its value any
individual tuple from that relation.

31
• A simple tuple relational calculus query is
of the form:
• {t | COND(t)}
• where t is a tuple variable and COND(t) is
a conditional (Boolean) expression
involving t that evaluates to either TRUE or
FALSE for different assignments of tuples to
the variable t.
32
• A general expression of the tuple relational
calculus is of the form
• {t1.Aj, t2.Ak, ..., tn.Am | COND(t1, t2, ..., tn, tn+1,
tn+2, ..., tn+m)}
• where t1, t2, ..., tn, tn+1, ..., tn+m are tuple
variables, each Ai is an attribute of the
relation on which ti ranges, and COND is a
condition or formula. of the tuple relational
calculus.
• A formula is made up of predicate calculus
atoms, which can be one of the following: 33
• An atom of the form R(ti), where R is a
relation name and ti is a tuple variable.
• This atom identifies the range of the tuple
variable ti as the relation whose name is R. It
evaluates to TRUE if ti is a tuple in the relation
R, and evaluates to FALSE otherwise.

34
• An atom of the form ti.A op tj.B, where op is
one of the comparison operators in the set
{=, <, ≤, >, ≥, ≠}, ti and tj are tuple
variables, A is an attribute of the relation on
which ti ranges, and B is an attribute of the
relation on which tj ranges.

35
• An atom of the form ti.A op c or c op tj.B,
where op is one of the comparison operators
in the set {=, <, ≤, >, ≥, ≠}, ti and tj are
tuple variables, A is an attribute of the
relation on which ti ranges, B is an attribute
of the relation on which tj ranges, and c is a
constant value.

36
Example
• to find all employees whose salary is above
$50,000, we can write the following tuple
calculus expression:
• {t | EMPLOYEE(t) AND t.Salary>50000}
• To retrieve only some of the attributes—say,
the first and last names for the above
example —we write
• {t.Fname, t.Lname | EMPLOYEE(t) AND
t.Salary>50000} 37
• Retrieve the birth date and address of the
employee (or employees) whose name is
John B. Smith.
• {t.Bdate, t.Address | EMPLOYEE(t) AND
t.Fname=‘John’ AND t.Minit=‘B’AND
t.Lname=‘Smith’}

38
• Two special symbols called quantifiers can
appear in formulas; these are the universal
quantifier (∀) and the existential quantifier
(∃).
• Informally, a tuple variable t is bound if it is
quantified, meaning that it appears in an
(∃t) or (∀t) clause; otherwise, it is free.
• The only free tuple variables in a tuple
relational calculus expression should be
those that appear to the left of the bar (|). 39
• For example, consider the following
formulas:
• F1 : d.Dname=‘Research’
• F2 : (∃t)(d.Dnumber=t.Dno)
• F3 : (∀d)(d.Mgr_ssn=‘333445555’)
• The tuple variable d is free in both F1 and
F2, whereas it is bound to the (∀) quantifier
in F3. Variable t is bound to the (∃)
quantifier in F2. 40
Interpretation of the quantifiers
• The formula (∃t)(F) is TRUE if the formula
F evaluates to TRUE for some (at least one)
tuple assigned to free occurrences of t in F;
otherwise, (∃t)(F) is FALSE.
• The formula (∀t)(F) is TRUE if the formula
F evaluates to TRUE for every tuple (in the
universe) assigned to free occurrences of t
in F; otherwise, (∀t)(F) is FALSE.
41
• List the name and address of all employees
who work for the ‘Research’ department.
• {t.Fname, t.Lname, t.Address |EMPLOYEE(t)
AND (∃d)(DEPARTMENT(d) AND
d.Dname=‘Research’ AND
d.Dnumber=t.Dno)}

42
• For every project located in ‘Stafford’, list
the project number, the controlling
department number, and the department
manager’s last name, birth date, and
address.
• {p.Pnumber, p.Dnum, m.Lname, m.Bdate,
m.Address | PROJECT(p) AND
EMPLOYEE(m) AND p.Plocation=‘Stafford’
AND ((∃d)(DEPARTMENT(d) AND
p.Dnum=d.Dnumber AND
d.Mgr_ssn=m.Ssn))}

43
Transforming the Universal
and Existential Quantifiers
• It is possible to transform a universal
quantifier into an existential quantifier, and
vice versa, to get an equivalent expression.
• One general transformation can be
described informally as follows:
• Transform one type of quantifier into the other
with negation (preceded by NOT);
• AND and OR replace one another;
• a negated formula becomes unnegated; and an
unnegated formula becomes negated. 44
Transformation rules

45
• List the names of employees who work on
all the projects controlled by department
number 5. One way to specify this query is
to use the universal quantifier as shown:
• {e.Lname, e.Fname | EMPLOYEE(e) AND
((∀x)(NOT(PROJECT(x)) OR NOT
(x.Dnum=5) OR ((∃w)(WORKS_ON(w) AND
w.Essn=e.Ssn AND x.Pnumber=w.Pno))))}
• Using the general transformation rule, we
can rephrase as follows:
46
• Exclude from the universal quantification
all tuples that we are not interested in by
making the condition true for all such
tuples.
• The first tuples to exclude (by making them
evaluate automatically to true) are those that are
not in the relation R of interest.
• In the query above, using the expression
not(PROJECT(x)) inside the universally
quantified formula evaluates to true all
tuples x that are not in the PROJECT
relation. 47
• Then we exclude the tuples we are not
interested in from R itself. The expression
not(x.DNUM=5) evaluates to true all tuples x
that are in the project relation but are not
controlled by department 5.
• Finally, we specify a condition that must
hold on all the remaining tuples in R.
• ( (∃ w)(WORKS_ON(w) and w.ESSN=e.SSN
and x.PNUMBER=w.PNO)
48
• List the names of employees who have no
dependents.
• {e.Fname, e.Lname | EMPLOYEE(e) AND
(NOT (∃d)(DEPENDENT(d) AND
e.Ssn=d.Essn))}
• Using the general transformation rule, we
can rephrase as follows:
• {e.Fname, e.Lname | EMPLOYEE(e) AND
((∀d)(NOT(DEPENDENT(d)) OR 49

NOT(e.Ssn=d.Essn)))}

You might also like