Sie sind auf Seite 1von 28

Relational Algebra

Lecture 03
Tapushe Rabaya Toma
Lecturer of SWE

7/2/2018 DIU 1
Schema Diagram

7/2/2018 DIU 2
Query Languages
• Language in which user requests information from the database.
• Categories of languages
• Procedural
• Non-procedural, or declarative
• “Pure” languages:
• Relational algebra
• Tuple relational calculus
• Domain relational calculus
• Pure languages form underlying basis of query languages that people use.

7/2/2018 DIU 3
Relational Algebra
• Procedural language
• 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.

7/2/2018 DIU 4
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

7/2/2018 DIU 5
Project Operation – Example
• Relation r: A B C

 10 1
 20 1
 30 1
 40 2

A,C (r) A C A C

 1  1
 1 =  1
 1  2
 2

7/2/2018 DIU 6
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

7/2/2018 DIU 7
Set Difference Operation – Example
• Relations r, s:
A B A B

 1  2
 2  3
 1 s
r

 r – s:
A B

 1
 1

7/2/2018 DIU 8
Cartesian-Product Operation – Example
 Relations r, s:
A B 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

7/2/2018 DIU 9
Rename Operation
• Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
• Allows us to refer to a relation by more than one name.
• Example:
 x (E)

returns the expression E under the name X


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

 x ( A ,A
1 2 ,...,An ) (E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .

7/2/2018 DIU 10
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C(r x s)
• rxs
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
• A=C(r x s)

A B C D E

 1  10 a
 2  10 a
 2  20 b
7/2/2018 DIU 11
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)

7/2/2018 DIU 12
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))

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

customer_name (borrower)  customer_name (depositor)


7/2/2018 DIU 13
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)

7/2/2018 DIU 14
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)))

 customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))

7/2/2018 DIU 15
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

7/2/2018 DIU 16
Set-Intersection Operation – Example
• Relation r, s:
A B A B
 1  2
 2  3
 1

r s

• rs
A B

 2

7/2/2018 DIU 17
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 
7/2/2018 DIU 18
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))

7/2/2018 DIU 19
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)))

7/2/2018 DIU 20
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

7/2/2018 DIU 21
Aggregate Operation – Example
• Relation r:

A B C

  7
  7
  3
  10

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

27

7/2/2018 DIU 22
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

7/2/2018 DIU 23
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
branch_name g sum(balance) as sum_balance (account)

7/2/2018 DIU 24
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.
• Uses null values:
• null signifies that the value is unknown or does not exist
• All comparisons involving null are (roughly speaking) false by
definition.
• We shall study precise meaning of comparisons with nulls later

7/2/2018 DIU 25
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

7/2/2018 DIU 26
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

7/2/2018 DIU 27
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

7/2/2018 DIU 28

Das könnte Ihnen auch gefallen