0% found this document useful (0 votes)
49 views35 pages

Relational Algebra

The document discusses relational algebra operations including select, project, union, set difference, cartesian product, join, and aggregate functions. Relational algebra provides basic operations to retrieve data from relational databases through selection, projection, joins and other set operations.

Uploaded by

jovape4609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views35 pages

Relational Algebra

The document discusses relational algebra operations including select, project, union, set difference, cartesian product, join, and aggregate functions. Relational algebra provides basic operations to retrieve data from relational databases through selection, projection, joins and other set operations.

Uploaded by

jovape4609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Relational Algebra

An example of Company database


Relational Algebra
• The basic set of operations for the relational model is
known as the relational algebra. These operations
enable a user to specify basic retrieval requests.
• The result of a retrieval is a new relation, which may
have been formed from one or more relations.
Select operation
• SELECT operation is used to select a subset of the tuples
from a relation that satisfy a selection condition.

Example: To select the EMPLOYEE tuples whose


department number is four or those whose salary is greater
than $30,000 the following notation is used:
DNO = 4 (EMPLOYEE)
SALARY > 30,000 (EMPLOYEE)
In general, the select operation is denoted by  <selection
condition>(R) where the
symbol  (sigma) is used to denote the select operator,
and the selection condition is a Boolean expression
specified on the attributes of relation R
SELECT Operation Properties
• The SELECT operation  <selection condition>(R) produces a
relation S that has the same schema as R.
• The SELECT operation  is commutative; i.e.,
 <condition1>( < condition2> ( R)) =  <condition2> ( < condition1> ( R))
• A cascaded SELECT operation may be applied in any
order; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> ( R)))

• A cascaded SELECT operation may be replaced by a


single selection with a conjunction of all the conditions;
i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition1> AND < condition2> AND < condition3> ( R)))
PROJECT Operation
• This operation selects certain columns from the table and discards
the other columns. The PROJECT creates a vertical partitioning –
one with the needed columns (attributes) containing results of the
operation and other containing the discarded Columns.

• Example: To list each employee’s first and last name and salary, the
following is used:
LNAME, FNAME,SALARY(EMPLOYEE)


• The general form of the project operation is <attribute list>(R)
where  (pi) is the symbol used to represent the project operation
and <attribute list> is the desired list of attributes from the attributes
of relation R.
• The project operation removes any duplicate tuples, so the result
of the project operation is a set of tuples and hence a valid
relation.
Rename Operation
• We may want 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, or we can apply one operation at a time and create
intermediate result relations. In the latter case, we must give names to the
relations that hold the intermediate results.

• 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 as follows:
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

Or We can explicitly show the sequence of operations, giving a name to each


intermediate relation:
DEP5_EMPS   DNO=5(EMPLOYEE)
RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
Rename Operation contd.
The rename operator is 

The general Rename operation can be expressed by any of the following forms:

•  S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B1,

…..Bn.

• S( R) is a renamed relation S based on R (which does not specify column names).

•  (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B1, …..Bn which does

not specify a new relation name.


Union Operation
• The result of this operation, denoted by R  S, is a
relation that includes all tuples that are either in R or in
S or in both R and S. Duplicate tuples are eliminated.

• E.g. to find all customers with either an account or


a loan
▫ Πcustomer-name(depositor) ∪ Πcustomer-
name(borrower)
Union Operation Example

STUDENTINSTRUCTOR
Type Compatibility for Set Operations
• Type Compatibility
▫ The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of attributes,
and the domains of corresponding attributes must be
compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n.
INTERSECTION OPERATION
• The result of this operation, denoted by R  S, is a relation
that includes all tuples that are in both R and S. The two
operands must be "type compatible"
• Example: The result of the intersection operation (figure
below) includes only those who are both students and
instructors.
Set Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type
compatible”.
• Example: The figure shows the names of students who are not instructors,
and the names of instructors who are not students.

STUDENT-INSTRUCTOR

INSTRUCTOR-
STUDENT
Set Operations Properties
• Notice that both union and intersection are commutative
operations; that is
R  S = S  R, and R  S = S  R

• Both union and intersection can be treated as n-ary operations


applicable to any number of relations as both are associative
operations; that is
R  (S  T) = (R  S)  T, and (R  S)  T = R  (S  T)

• The minus operation is not commutative; that is, in general


R-S≠S–R
CARTESIAN (or cross product) Operation
• This operation is used to combine tuples from two relations in a
combinatorial fashion. In general, the result of R(A1, A2, . . ., An) x
S(B1, B2, . . ., Bm) is a relation Q with degree n + m attributes Q(A1,
A2, . . ., An, B1, B2, . . ., Bm), in that order. The resulting relation Q
has one tuple for each combination of tuples—one from R and one
from S.
• Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS tuples,
then
| R x S | will have nR * nS tuples.
• The two operands do NOT have to be "type compatible”
Example:

FEMALE_EMPS   SEX=’F’(EMPLOYEE)
EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)

EMP_DEPENDENTS  EMPNAMES x DEPENDENT


Example
• branch (branch-name, branch-city, assets)
• customer (customer-name, customer-street, customer-only)
• account (account-number, branch-name, balance)
• loan (loan-number, branch-name, amount)
• depositor (customer-name, account-number)
• borrower (customer-name, loan-number)
Example Queries
• Find all loans of over $1200
σ amount> 1200(loan)
• Find the loan number for each loan of an
amount greater than $1200
Π loan-number(σamount> 1200(loan))
Example Queries
• Find the names of all customers who have a
loan, an account, or both, from the bank.
Πcustomer-name(borrower) ∪ Πcustomer-name(depositor)

• Find the names of all customers who have a loan


and an account at bank.
Πcustomer-name(borrower) ∩ Πcustomer-name(depositor)
Example Queries
• Find the names of all customers who have a loan at the
Perryridge branch.
▫ Πcustomer-name(σ branch-name=“Perryridge” (σborrower.loan-number =
loan.loan-number(borrower x loan)))

• Find the names of all customers who have a loan at the


Perryridge branch but do not have an account at any
branch of the bank.
• Πcustomer-name(σbranch-name = “Perryridge” (σborrower.loan-number =
loan.loan-number(borrower x loan))) – Πcustomer-name (depositor)
JOIN Operation
• The sequence of cartesian product followed by select is
used quite commonly to identify and select related tuples
from two relations, a special operation, called JOIN. It is
denoted by a
• This operation is very important for any relational
database with more than a single relation, because it
allows us to process relationships among relations.
• The general form of a join operation on two relations R(A1,
A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
where R and S can be any relations that result from general
relational algebra expressions.
JOIN Operation
• EQUIJOIN Operation
The most common use of join involves join conditions with equality comparisons
only. Such a join, where the only comparison operator used is =, is called an
EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of
attributes (whose names need not be identical) that have identical values in every
tuple.
• NATURAL JOIN Operation

Denoted by *, the standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have the same name in
both relations. If this is not the case, a renaming operation is applied first.
Example
• To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to
write:
DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS
Complete Set of Relational Operations
• The set of operations including select , project  ,
union , set difference - , and cartesian product X
is called a complete set because any other relational
algebra expression can be expressed by a
combination of these five operations.
• For example:
R  S = (R  S ) – ((R - S)  (S - R))
R <join condition> S =  <join condition> (R X S)
Aggregate functions
Use of the Functional operator g
 g MAX (Salary) (Employee) retrieves the maximum salary value from
the Employee relation
 g MIN (Salary) (Employee) retrieves the minimum Salary value from
the Employee relation
 g (Employee) retrieves the sum of the Salary from the
SUM (Salary)

Employee relation
 g (Employee) groups employees by
DNO COUNT (SSN), AVERAGE (Salary)

DNO (department number) and computes the count of employees


and average salary per department.
[ Note: count just counts the number of rows, without removing
duplicates]
The OUTER JOIN Operation
• In NATURAL JOIN tuples without a matching (or related) tuple are
eliminated from the join result. Tuples with null in the join
attributes are also eliminated. This amounts to loss of information.
• A set of operations, called outer joins, can be used when we want to
keep all the tuples in R, or all those in S, or all those in both
relations in the result of the join, regardless of whether or not they
have matching tuples in the other relation.
• The left outer join operation keeps every tuple in the first or left
relation R in R S; if no matching tuple is found in S, then the
attributes of S in the join result are filled or “padded” with null
values.
• A similar operation, right outer join, keeps every tuple in the second
or right relation S in the result of R S.
• A third operation, full outer join, denoted by keeps all tuples
in both the left and the right relations when no matching tuples are
found, padding them with null values as needed.
Outer Join Example
• Relation loan

• Relation borrower
• Inner join

• Left outer join


• Right outer join

• Full outer join


Deletion
• A deletion is expressed in relational algebra by:
r← r– E
where r is a relation and E is a relational algebra query.

• Delete all loan records with amount in the range


of 0 to 50
▫ loan ←loan– σ amount ≥ 0 and amount ≤50 (loan)
• Delete all account records in the Perryridge
branch.
▫ account ←account – σ branch-name = “Perryridge” (account)
Insertion
• In relational algebra, an insertion is expressed by:
r ←r ∪ E
where r is a relation and E is a relational algebra expression.

• Insert information in the database specifying that Smith


has $1200 in account A-973 at the Perryridge branch.
▫ account ←account ∪ {(“Perryridge”, A-973, 1200)}
▫ depositor ←depositor ∪ {(“Smith”, A-973)}
Updating
• Use the generalized projection operator to do this task
r← ΠF1, F2, …, FI,.. (r)

• Make interest payments by increasing all balances by 5


percent.
account ←ΠAN, BN, BAL * 1.05 (account)
where AN, BN and BAL stand for account-number,
branch-name and balance, respectively.
Exercise 1:
Consider a schema with two relations, R(A, B) and S(B, C), where all
values are integers. Make no assumptions about keys. Consider the
following three relational algebra expressions. Identify the odd one
out.
Two of the three expressions are equivalent (i.e., produce the same
answer on all databases), while one of them can produce a different
answer. Which query can produce a different answer?
• Let R be Let S be
A B B C
4 1 1 6
5 2 2 7
3 8
• Results for
a. 4 6

b. 4 6

c. 4 6
5 6

You might also like