You are on page 1of 25

Chapter 4

Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus

Domain Relational
Calculus

Chapter 4
Formal Relational Query Languages

Yonas Y.

Addis Ababa Institute of Technology

yonas.yehualaeshet@aait.edu.et
AAiT - SECE 1
Chapter 4

Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


The Relational Algebra Calculus

Domain Relational
Calculus

The Tuple Relational Calculus

Domain Relational Calculus

AAiT - SECE 2
Chapter 4

Relational Algebra Yonas Y.

Outline

The Relational Algebra


The relational algebra is a procedural query language. The Tuple Relational
Calculus
Six fundamental basic operators
Domain Relational
Calculus
I select: σ
Q
I project:
I union: ∪
I set difference: −
I cartesian product: ×
I rename: ρ

The operators take one or two relations as inputs and produce a


new relation as a result.

AAiT - SECE 3
Chapter 4

The Select Operation Yonas Y.

Outline

The Relational Algebra


The select operation selects tuples that satisfy a given predicate.
The Tuple Relational
Calculus
I Notation: σp (r )
Domain Relational
I p is called the selection predicate. Calculus

I Defined as:
σp (r ) = {t | t ∈ r and p(t)}

Where p is a formula in propositional calculus consisting of


terms connected by : ∧ (and), ∨ (or), ¬ (not).

I Each term is one of:


< attribute > op < attribute > or < constant >

where op is one of: =, 6=, >, ≥, <, ≤.

AAiT - SECE 4
Chapter 4

Yonas Y.

Outline

The Relational Algebra


Example: To select those tuples of the instructor relation where
The Tuple Relational
the instructor is in the ”Electrical Engineering” department, we Calculus

write: Domain Relational


Calculus

σdept name=”Elec.Eng .” (instructor )

Example: To find the instructors in Electrical Engineering with a


salary greater than 90,000, we write:

σdept name=”Elec.Eng .”∧salary >90,000 (instructor )

AAiT - SECE 5
Chapter 4

The Project Operation Yonas Y.

Outline

The Relational Algebra


The project operation is a unary operation that returns its
The Tuple Relational
argument relation, with certain attributes left out. Calculus

Domain Relational
I Notation: Calculus

Q
A1 ,A2 ,··· ,Ak (r )
I

where A1 , A2 are attribute names and r is a relation name.


I The result is defined as the relation of k columns obtained by
erasing the columns that are not listed.
I Duplicate rows removed from result, since relations are sets.

Example: To eliminate the dept name attribute of instructor


Q
ID,name,salary (instructor )

AAiT - SECE 6
Chapter 4

The Union Operation Yonas Y.

Outline

I Notation: r ∪ s The Relational Algebra

The Tuple Relational


I Defined as: Calculus

r ∪ s = {t | t ∈ r or t ∈ s} Domain Relational
Calculus

For r ∪ s to be valid.

1. r , s must have the same arity (same number of attributes)


2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)

Example: To find all courses taught in the Fall 2009 semester, or


in the Spring 2010 semester, or in both.
Q
(σsemester =”Fall”∧year =2009 (section)) ∪
Qcourse id
course id (σsemester =”Spring ”∧year =2010 (section)).
AAiT - SECE 7
Chapter 4

The Set-Difference Operation Yonas Y.

Outline
The set-difference operation, allows us to find tuples that are in The Relational Algebra

one relation but are not in another. The Tuple Relational


Calculus
I Notation: r − s Domain Relational
Calculus
I Defined as:
r − s = {t | t ∈ r and t ∈
/ s}

Set differences must be taken between compatible relations.

1. r , s must have the same arity.


2. The attribute domains must be compatible.

Example: To find all courses taught in the Fall 2009 semester,


but not in the Spring 2010 semester.
Q
(σsemester =”Fall”∧year =2009 (section)) −
Qcourse id
course id (σsemester =”Spring ”∧year =2010 (section)).
AAiT - SECE 8
Chapter 4

Set-Intersection Operation Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus
I Notation: r ∩ s
Domain Relational
Calculus
I Defined as:
r ∩ s = {t | t ∈ r and t ∈ s}

Check compatibility.

1. r , s must have the same arity.


2. The attribute domains must be compatible.

Note: r ∩ s = r - (r - s).

AAiT - SECE 9
Chapter 4

The Cartesian-Product Operation Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


The Cartesian-product operation, allows us to combine Calculus

information from any two relations. Domain Relational


Calculus

I Notation: r × s
I Defined as:
r × s = {t q | t ∈ r and q ∈ s}

Assume that attributes of r (R) and s(S) are disjoint. (That is,
R ∩ S = ∅).

I If attributes of r (R) and s(S) are not disjoint, then renaming


must be used.

AAiT - SECE 10
Chapter 4

The Rename Operation Yonas Y.

Outline

The Relational Algebra


Allows us to name, and therefore to refer to, the results of
The Tuple Relational
relational-algebra expressions. Calculus

Domain Relational
I Allows us to refer to a relation by more than one name. Calculus

Example:

ρx (E )
I returns the expression E under the name x.

If a relational-algebra expression E has arity n, then

ρx(A1 ,A2 ,··· ,An ) (E )

returns the result of expression E under the name x, and with the
attributes renamed to A1 , A2, · · · , An .

AAiT - SECE 11
Chapter 4

Yonas Y.

Outline
Example: Consider the query ”Find the highest salary in the
The Relational Algebra
university.”
The Tuple Relational
Calculus
1. Step 1: Compute first a temporary relation consisting of
Domain Relational
those salaries that are not the largest. Calculus
Q
instructor .salary (σinstructor .salary <d.salary (instructor ×
ρd (instructor )))

2. Step 2: The query to find the largest salary in the university


can be written as:
Q
(instructor ) -
Qsalary
instructor .salary (σinstructor .salary <d.salary (instructor ×
ρd (instructor )))

AAiT - SECE 12
Chapter 4

The Tuple Relational Calculus Yonas Y.

Outline

The Relational Algebra

The Tuple Relational

A nonprocedural query language, where each query is of the form Calculus

Domain Relational
Calculus

{t | P(t)}

I It is the set of all tuples t such that predicate P is true for t.


I t is a tuple variable, t[A] denotes the value of tuple t on
attribute A.
I t ∈ r denotes that tuple t is in relation r .
I P is a formula similar to that of the predicate calculus.

AAiT - SECE 13
Chapter 4

Predicate Calculus Formula Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


1. Set of attributes and constants. Calculus

Domain Relational
2. Set of comparison operators: (e.g., <, ≤, =, 6=, >, ≥). Calculus

3. Set of connectives: and (∧), or (∨) not (¬).



4. Implication (⇒): x ⇒ y , if x is true, then y is true.
x ⇒ y ≡ ¬x ∨ y

5. Set of quantifiers:
I ∃t ∈ r (Q(t)) ≡ ”there exists” a tuple t in relation r such
that predicate Q(t) is true.
I ∀t ∈ r (Q(t)) ≡ Q is true ”for all” tuples t in relation r .

AAiT - SECE 14
Chapter 4

Yonas Y.

Example: Find the ID, name, dept name, salary for instructors Outline

whose salary is greater than 80,000. The Relational Algebra

The Tuple Relational


Calculus
{t | t ∈ instructor ∧ t[salary ] > 80000}
Domain Relational
Calculus
I Notice that a relation on schema (ID, name, dept name,
salary ) is implicitly defined by the query.

Example: As in the previous query, but output only the ID


attribute value.

{t | ∃s ∈ instructor (t[ID] = s[ID] ∧ s[salary ] > 80000}


I Notice that a relation on schema (ID) is implicitly defined by
the query.

AAiT - SECE 15
Chapter 4

Yonas Y.

Example: Find the names of all instructors whose department is Outline

in the Watson building. The Relational Algebra

The Tuple Relational

{t | ∃s ∈ instructor (t[name] = s[name] ∧ ∃u ∈ Calculus

department(u[dept name] = s[dept name] ∧ u[building ] = Domain Relational


Calculus

”Watson”))}

Example: Find the set of all courses taught in the Fall 2009
semester, or in the Spring 2010 semester, or both.

{t | ∃s ∈ section(t[course id] = s[course id]∧


s[semester ] = ”Fall” ∧ s[year ] = 2009)
∨ ∃u ∈ section(t[course id] = u[course id]∧
u[semester ] = ”Spring ” ∧ u[year ] = 2010)}

AAiT - SECE 16
Chapter 4

Universal Quantification Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus

Domain Relational
Calculus
Example: Find all students who have taken all courses offered in
the Biology department.

{t | ∃r ∈ student(t[ID] = r [ID]∧
(∀u ∈ course(u[dept name] = ”Biology ” ⇒ ∃s ∈
takes(t[ID] = s[ID] ∧ s[course id] = u[course id]))}

AAiT - SECE 17
Chapter 4

Safety of Expressions Yonas Y.

Outline

The Relational Algebra

It is possible to write tuple calculus expressions that generate The Tuple Relational
Calculus

infinite relations. Domain Relational


Calculus

I For example, {t | ¬t ∈ r } results in an infinite relation if the


domain of any attribute of relation r is infinite.
I To guard against the problem, we restrict the set of allowable
expressions to safe expressions.
I An expression {t | P(t)} in the tuple relational calculus is
safe if every component of t appears in one of the relations,
tuples, or constants that appear in P.
I NOTE: this is more than just a syntax condition.

AAiT - SECE 18
Chapter 4

Yonas Y.

Outline

The Relational Algebra


Example: Consider again that query to find all students who have
The Tuple Relational
taken all courses offered in the Biology department. Calculus

Domain Relational
Calculus
{t | ∃r ∈ student(t[ID] = r [ID]∧
(∀u ∈ course(u[dept name] = ”Biology ” ⇒ ∃s ∈
takes(t[ID] = s[ID] ∧ s[course id] = u[course id]))}

Without the existential quantification on student, the above query


would be unsafe if the Biology department has not offered any
courses.

AAiT - SECE 19
Chapter 4

Domain Relational Calculus Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus

Domain Relational
A nonprocedural query language equivalent in power to the tuple Calculus

relational calculus.

I Each query is an expression of the form:


{< x1 , x2 , · · · , xn >| P(x1 , x2 , · · · , xn )}
I x1 , x2 , · · · , xn represent domain variables.
I P represents a formula similar to that of the predicate
calculus.

AAiT - SECE 20
Chapter 4

Example Queries Yonas Y.

Outline

The Relational Algebra


Example: Find the ID, name, dept name, salary for instructors
The Tuple Relational
whose salary is greater than 80,000. Calculus

Domain Relational
{< i, n, d, s >|< i, n, d, s >∈ instructor ∧ s > 80000} Calculus

Example: As in the previous query, but output only the ID


attribute value.

{< i >|< i, n, d, s >∈ instructor ∧ s > 80000}

Example: Find the names of all instructors whose department is


in the Watson building.

{< n > ∃i, d, s(< i, n, d, s >∈ instructor ∧ ∃b, a(< d, b, a >∈


department ∧ b = ”Watson”))}

AAiT - SECE 21
Chapter 4

Yonas Y.

Outline

Example: Find the set of all courses taught in the Fall 2009 The Relational Algebra

semester, or in the Spring 2010 semester, or both The Tuple Relational


Calculus

{< c >| ∃a, s, y , b, r , t(< c, a, s, y , b, r , t >∈ section ∧ Domain Relational


Calculus

s = ”Fall” ∧ y = 2009)
∨ ∃a, s, y , b, r , t(< c, a, s, y , b, r , t >∈ section ∧
s = ”Spring ” ∧ y = 2010)}

I This case can also be written as

{< c >| ∃a, s, y , b, r , t(< c, a, s, y , b, r , t >∈ section ∧


((s = ”Fall” ∧ y = 2009) ∨ (s = ”Spring ” ∧ y = 2010))}

AAiT - SECE 22
Chapter 4

Safety of Expressions Yonas Y.

Outline

The expression: The Relational Algebra

The Tuple Relational

{< x1 , x2 , · · · , xn >| P(x1 , x2 , · · · , xn )} Calculus

Domain Relational
Calculus
is safe if all of the following hold:

1. All values that appear in tuples of the expression are values


from dom(P) (that is, the values appear either in P or in a
tuple of a relation mentioned in P).

2. For every ”there exists” subformula of the form ∃x(P1 (x)),


the subformula is true if and only if there is a value of x in
dom(P1 ) such that P1 (x) is true.

3. For every ”for all” subformula of the form ∀x(P1 (x)), the
subformula is true if and only if P1 (x) is true for all values x
from dom(P1 ).
AAiT - SECE 23
Chapter 4

Universal Quantification Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus
Example: Find all students who have taken all courses offered in
Domain Relational
the Biology department. Calculus

{< i >| ∃n, d, tc(< i, n, d, tc >∈ student∧


(∀ci, ti, dn, cr (< ci, ti, dn, cr > course ∧ dn = ”Biology ” ⇒
∃si, se, y , g (< i, ci, si, se, y , g >∈ takes))}

Note that without the existential quantification on student, the


above query would be unsafe if the Biology department has not
offered any courses.

AAiT - SECE 24
Chapter 4

Yonas Y.

Outline

The Relational Algebra

The Tuple Relational


Calculus

Domain Relational

End of Chapter 4 Calculus

Questions?

AAiT - SECE 25

You might also like