Professional Documents
Culture Documents
Chapter7-Relational Algebra
Chapter7-Relational Algebra
Query languages
Allow manipulation and retrieval of data from a database.
Relational model supports simple, powerful QLs:
Strong formal foundation based on logic.
Allows for much optimization.
Query Languages != programming languages!
QLs not intended to be used for complex calculations.
QLs support easy, efficient access to large data sets.
Formal Relational Query Languages
Two mathematical Query Languages form the basis
for “real” languages (e.g. SQL):
Relational Algebra: More operational, very useful for
representing execution plans.
Relational Calculus: Lets users describe what they want,
rather than how to compute it. (Non-operational,
declarative.)
Understanding Algebra & Calculus is key to
understanding SQL, query processing!
Preliminaries
A query is applied to relation instances, and the result
of a query is also a relation instance (called closer
property).
Schemas of input relations for a query are fixed (but
query will run regardless of instance!)
The schema for the result of a given query is also
fixed! Determined by definition of query language
constructs.
Relational Algebra
Similar to normal algebra (as in 2+3*x-y), except we
use relations as values instead of numbers, and the
operations and operators are different.
A procedural query language; Based on algebraic
concepts (Algebra – studies quantities, refers to
relations and properties by symbols)
Not used as a query language in actual DBMSs. (SQL
instead.)
The inner, lower-level operations of a relational DBMS
are, or are similar to, relational algebra operations.
Operations in Relational Algebra
Six fundamental operations:
select (unary)
project (unary)
rename (unary)
cartesian product (binary)
union (binary)
set-difference (binary)
Several other operations, defined in terms of the fundamental
operations:
set-intersection
natural join
division
assignment
Notations
Operation Symbol
Operation Symbol
Selection Join
Assignment Semijoin
Select Operation
Works on a single relation R and defines a relation
that contains only those tuples (rows) of R that satisfy
the specified condition (predicate).
Notation: p(R)
p is called the selection predicate
Formally, select operation is defined as:
p(r) = {t | t r(R) and p(t)}
Where p is a formula in propositional calculus consisting of
terms connected by: (and), (or), (not)
Result has the same attributes as the base relation.
Select Operation
Each term is one of:
<attribute> op <attribute>
<attribute> op <constant>
TEMP DNO=5(EMPLOYEE)
TEMP1 (PNO=1)(WORKS_ON)
RESULT1 SSN(TEMP1)
TEMP2 (PNO=2)(WORKS_ON)
RESULT2 SSN(TEMP2)
RESULT RESULT1 – RESULT2
Set Intersection Operation
Defines a relation consisting of the set of all
tuples that are in both R and S.
Notation: r s
Defined as:
R s = {t | t r and t s}
Assumes:
R and S have the same arity
attributes of R and S are compatible
Note: r s = r - (r - s)
Example
List all those managers who have at least one
dependent
MGRS(SSN) MGRSSN(DEPARTMENT)
EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT)
MGRS_WITH_DEPS MGRS EMP_WITH_DEPS
Cartesian Product Operation
Defines a relation that is the concatenation of every
tuple of relation R with every tuple of relation S.
Notation: r s
Defined as:
r x s = {t q | t r and q s}
Assume that attribute names of r(R) and s(S) are
disjoint.
If attributes of r(R) and s(S) are not disjoint, then
renaming must be used
Example
R S
StID Name Address Sec_no C_Code
980001 Ali Ma 1 ITCS385
980002 Ahmed Mu 2 ITCS100
3 ITCs101
RxS
Temp r s
Result A1=B1(Temp)
Can be replaced with a single JOIN operation
ACTUAL_DEPENDENTS EMPNAMES SSN=ESSN DEPENDENT
Equijoin and Natural Join
Equijoin
If the condition in the join operator is only equality, it
is called, equijoin.
Natural Join
Natural join is basically an equijoin followed by
removal of the superfluous attributes.
The two tables must have at least one common
attribute.
Example
Retrieve the name and address of all employees who
work for the ‘Research’ department.
Modification of the Database
The content of the database may be modified
using the following operations:
Deletion
Insertion
Updating
All these operations are expressed using the
assignment operator.
Deletion
A delete request is expressed similarly to a query,
except instead of displaying tuples to the user, the
selected tuples are removed from the database.
We can delete only whole tuples; cannot delete
values on only particular attributes
A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra
query.
Examples
Example 1: Delete all account records in the
Perryridge branch.
account account – branch-name = “Perryridge” (account)
Example 2: Delete all loan records with amount in the
range of 0 to 50
1 John 100
5 Sarah 300
7 Tom 100
salary
select salary
100
salary(E)
from E
300
nr salary
7 100
E
nr name salary
1 John 100
5 Sarah 300
7 Tom 100
select *
nr name salary
from E 1 John 100 salary < 200(E)
where salary < 200
7 Tom 100
select *
from E nr name salary
where salary < 200 7 Tom 100 salary < 200 and nr >= 7 (E)
and nr >= 7
E
nr name salary
1 John 100
5 Sarah 300
7 Tom 100
D
dnr dname
A Marketing
B Sales
C Legal