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

DBMS Module 2.1 Relational Algebra

The document describes six basic relational algebra operators: select, project, union, set difference, Cartesian product, and rename. It provides examples of how each operator works when applied to relations. The operators take one or two relations as inputs and produce a new relation as the output. More complex expressions can be built by combining multiple operators in a sequence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views32 pages

DBMS Module 2.1 Relational Algebra

The document describes six basic relational algebra operators: select, project, union, set difference, Cartesian product, and rename. It provides examples of how each operator works when applied to relations. The operators take one or two relations as inputs and produce a new relation as the output. More complex expressions can be built by combining multiple operators in a sequence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

DBMS module 2.

Relational Algebra

Database Engineering 4th SEM CSE


Relational Algebra
• Six basic operators
– select: σ
– project: ∏
– union: ∪
– set difference: –
– Cartesian product: x
– rename: ρ
• The operators take one or two relations as
inputs and produce a new relation as a result.

Database Engineering 4th SEM CSE


Select Operation – Example
Relation r
A B C D

α α 1 7
α β 5 7
β β 12 3
β β 23 10

σA=B ^ D > 5 (r)


A B C D

α α 1 7
β β 23 10

Database Engineering 4th SEM CSE


Project Operation – Example
A B C
Relation r
α 10 1
α 20 1
β 30 1
β 40 2

∏A,C (r) A C A C

α 1 α 1
α 1 = β 1
β 1 β 2
β 2
Database Engineering 4th SEM CSE
Set Difference Operation – Example
Relations r, s: A B A B

α 1 α 2
α 2 β 3
β 1 s
r

A B
r -s:

α 1
β 1

Database Engineering 4th SEM CSE


Union Operation – Example
Relations r, s: A B A B

α 1 α 2
α 2 β 3
β 1 s
r

A B
r ∪ s:
α 1
α 2
β 1
β 3

Database Engineering 4th SEM CSE


Cartesian-Product Operation –
Example
Relations r, s:
AA BB C D E

α 1 α 10 a
β 10 a
β 2
β 20 b
r γ 10 b
s
r x s:
A B C D E
α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
Database Engineering 4th SEM CSE
Composition of Operations
• Can build expressions using multiple operations
• Example: σA=C(r x s) A B C D E
• rxs α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b

• σA=C(r x s) A B C D E

α 1 α 10 a
β 2 β 10 a
β 2 β 20 b
Database Engineering 4th SEM CSE
Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street,
customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)

Database Engineering 4th SEM CSE


Database Engineering 4th SEM CSE
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))

Find the names of all customers who have a loan, an account, or both, from
the bank

∏customer_name (borrower) ∪ ∏customer_name (depositor)

Database Engineering 4th SEM CSE


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)

Database Engineering 4th SEM CSE


Bank Example Queries
 Find the largest account balance
 Strategy:
 Find those balances that are not the largest
 Rename account relation as d so that we can compare each
account balance with all others
 Use set difference to find those account balances that were not
found in the earlier step.
 The query is:

∏balance(account) - ∏account.balance
(σaccount.balance < d.balance (account x ρd (account)))

Database Engineering 4th SEM CSE


Additional Operations

• Additional Operations
– Set intersection
– Natural join
– Aggregation
– Outer Join
– Division
• All above, other than aggregation, can be
expressed using basic operations we have seen
earlier.

Database Engineering 4th SEM CSE


Set-Intersection Operation – Example
 Relation r, s: A B
A B
α 1 α 2
α 2 β 3
β 1

r s

A B
 r∩s
α 2

Database Engineering 4th SEM CSE


Natural-Join Operation
Notation: r s
 Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R ∪ S obtained as
follows:
 Consider each pair of tuples tr from r and ts from s.
 If tr and ts have the same value on each of the attributes in R ∩
S, add a tuple t to the result, where
 t has the same value as tr on r
 t has the same value as ts on s
 Example:
R = (A, B, C, D)
S = (E, B, D)
 Result schema = (A, B, C, D, E)
 r s is defined as:
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B ∧ r.D = s.D (r x s))
Database Engineering 4th SEM CSE
Natural Join Operation – Example
Relations r, s:

A B C D B D E

α 1 α a 1 a α
β 2 γ a 3 a β
γ 4 β b 1 a γ
α 1 γ a 2 b δ
δ 2 β b 3 b ∈
r s

r s A B C D E
α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ
Database Engineering 4th SEM CSE
Database Engineering 4th SEM CSE
Bank Example Queries

Find the names of all customers who have a loan


and an account at bank.
∏customer_name (borrower) ∩ ∏customer_name (depositor)

Find the name, loan amount of all customers who have


a loan at the bank and an account at bank.

∏customer_name, loan_number, amount (Depositor (loan borrower))

Database Engineering 4th SEM CSE


Aggregate Functions and Operations
• Aggregation function takes a collection of values and returns a
single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
• Aggregate operation in relational algebra
G1 ,G2 ,,Gn ϑF ( A ), F ( A ,, F ( A ) ( E )
1 1 2 2 n n

E is any relational-algebra expression


– G1, G2 …, Gn is a list of attributes on which to group (can be empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name

Database Engineering 4th SEM CSE


Aggregate Operation – Example
Relation r:
A B C

α α 7
α β 7
β β 3
β β 10

g sum(c) (r) sum(c )

27

Database Engineering 4th SEM CSE


Aggregate Operation – Example
Relation account grouped by branch-name:

branch_name account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)

branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700

Database Engineering 4th SEM CSE


Aggregate Functions (Cont.)
• Result of aggregation does not have a name
– Can use rename operation to give it a name
– For convenience, we permit renaming as part of
aggregate operation
g (account)
branch_name sum(balance) as sum_balance

Database Engineering 4th SEM CSE


Outer Join
• An extension of the join operation that
avoids loss of information.
• Computes the join and then adds tuples
form one relation that does not match
tuples in the other relation to the result of
the join.

Database Engineering 4th SEM CSE


Outer Join – Example
Relation loan

loan_number branch_name amount


L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155

Database Engineering 4th SEM CSE


Outer Join – Example
Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

Left Outer Join


loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null

Database Engineering 4th SEM CSE


Outer Join – Example
Right Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
Full Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes

Database Engineering 4th SEM CSE


Null Values
• It is possible for tuples to have a null value, denoted by null, for
some of their attributes
• null signifies an unknown value or that a value does not exist.
• The result of any arithmetic expression involving null is null.
• Aggregate functions simply ignore null values (as in SQL)
• For duplicate elimination and grouping, null is treated like any
other value, and two nulls are assumed to be the same (as in
SQL)

Database Engineering 4th SEM CSE


Division Operation
• Property
– Let q = r ÷ s
– Then q is the largest relation satisfying q x s ⊆ r
• Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S ⊆ R

r ÷ s = ∏R-S (r ) – ∏R-S ( ( ∏R-S (r) x s ) – ∏R-S,S(r ) )

To see why
– ∏R-S,S (r) simply reorders attributes of r

– ∏R-S (∏R-S (r ) x s ) – ∏R-S,S(r) ) gives those tuples t in

∏R-S (r ) such that for some tuple u ∈ s, tu ∉ r.

Database Engineering 4th SEM CSE


Division Operation – Example
 Relations r, s:
A B B
α 1 1
α 2
α 3 2
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
 r ÷ s: A r
α
β
Database Engineering 4th SEM CSE
Division Operation – Example
 Relations r, s:
A B C D E D E

α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1 s
β a γ a 1
β a γ b 3
γ a γ a 1
γ a γ b 1
γ a β b 1
r
 r ÷ s:
A B C

α a γ
γ a γ
Database Engineering 4th SEM CSE
Bank Example Queries
• Find all customers who have an account at
all branches located in Brooklyn city.
∏customer_name, branch_name (depositor account)
÷ ∏branch_name (σbranch_city = “Brooklyn” (branch))

Database Engineering 4th SEM CSE

You might also like