You are on page 1of 30

Relational Algebra

Schema Diagram for University Database


Union Operation (∪)
• It performs binary union between two given relations
• Notion − r U s
• Where r and s are either database relations or relation
result set (temporary relation).
• For a union operation to be valid, the following
conditions must hold −

• r and s must have the same number of attributes.


• Attribute domains must be compatible.
• Duplicate tuples are automatically eliminated.
Example

• ∏ author (Books) ∪ ∏ author (Articles)

• Output − Projects the names of the


authors who have either written a
book or an article or both.
Example
• Find the names of all bank customers
who have either an account or a loan or
both?
Example
• Find the names of all bank customers who
have either an account or a loan or both.

• Divide this query into two…..

• Find the names of all customers With a loan in


the bank.
• Find the names of all customers with an
account in the bank.
Example
• Find the names of all bank
customers who have either an
account or a loan or both.
• Divide this query into two…..
• Find the names of all customers
With a loan in the bank.
• Π customer - name( depositor )
Example
• Find the names of all bank customers
who have either an account or a loan or
both.

• Divide this query into two…..


• Find the names of all customers with an
account in the bank.
• Π customer – name ( borrower )
Example

• Find the names of all


bank customers who have either
an account or a loan or both.

• Π customer – name ( borrower )


∪ Π customer – name ( depositor )
Set Difference (−)
• The result of set difference operation is
tuples, which are present in one relation
but are not in the second relation.
• Notation : r − s
• Finds all the tuples that are present in r but
not in s.
• r and s must have the same number of
attributes.
• Attribute domains must be compatible.
Example

• ∏ author (Books) − ∏ author (Articles)


• Output − Provides the name of
authors who have written books but
not articles.
Example
• Find all customers of the bank who
have an account but not a loan?
Example
• Find all customers of the bank who have an
account but not a loan.
• Π customer –name ( depositor ) −
Π customer –name ( borrower )
Cartesian Product (Χ)
• Combines information of two different
relations into one.
• Notation − r Χ s
• Where r and s are relations.
Cartesian Product (Χ)
• r = borrower × loan

loan relation
Cartesian Product (Χ)
• The relation schema for r = borrower × loan is:
(borrower.customer-name , borrower.loan-
number , loan.loan-number,loan.branch-
name, loan.amount)
• We can then write the relation schema for r as
(customer-name, borrower.loan-number ,
loan.loan-number, branch-name, amount)
Cartesian Product (Χ)
Example

• σauthor = 'tutorialspoint'(Books Χ Articles)


• Output − Yields a relation, which
shows all the books and articles
written by tutorialspoint.
Example
• Find the names of all customers who have a
loan at the Perryridge branch.

loan relation
Step 1
• Find the names of all customers who have a loan at the
Perryridge branch.
• σ branch- name = “ Perryridge ” ( borrower × loan ) .
Step 2

• Find the names of all customers who


have a loan at the Perryridge branch.

• σ borrower .loan- number = loan.loan- number


( σ branch- name = “ Perryridge
”( borrower × loan ))
Step 3
• Find the names of all customers who have a
loan at the Perryridge branch.

• Π customer - name (σ borrower .loan-


number = loan.loan- number( σ branch- name
= “ Perryridge ”( borrower × loan )))
Rename Operation (ρ)
• The results of relational algebra are also relations
but without any name. The rename operation
allows us to rename the output relation.
• 'rename' operation is denoted with small Greek
letter rho ρ.
• Notation − ρ x (E)
• Where the result of expression E is saved with
name of x.
Example
• Find the largest account balance in the bank?
Example
• Find the largest account balance in the
bank.
• Our strategy is to (1) compute first a
temporary relation consisting of those
balances that are not the largest and
• (2) take the set difference between the
relation Π balance ( account) and the
temporary relation just computed, to obtain
the result.
Step 1
• We need to compare the values of all
account balances. We do this comparison
by computing the Cartesian product
account × account.
• We can now write the temporary relation
that consists of the balances that are not
the largest:
• Π account .balance( σ account .balance < d .balance
( account × ρd ( account)))
Step 2
• The query to find the largest account
balance in the bank can be written as

• Π balance ( account) −
Π account .balance( σ account .balance <
d .balance ( account × ρd ( account)))
Home Task…!
• Besides fundamental operations other
relational algebra are as follows −
• Namely
• Set intersection
• Natural join
• Division
• Assignment

You might also like