0% found this document useful (0 votes)
11 views

Relational Algebra1

DBMS

Uploaded by

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

Relational Algebra1

DBMS

Uploaded by

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

Relational Algebra –

Basis for Relational Query


Languages
Based on presentation by Juliana Freire
Formal relational query languages
Is this the Algebra you know?
Algebra -> operators and atomic operands
Expressions -> applying operators to atomic operands and/or other
expressions

Algebra of arithmetic: operands are variables and constants, and


operators are the usual arithmetic operators
E.g., (x+y)*2 or ((x+7)/(y-3)) + x

Relational algebra: operands are variables that stand for relations and
relations (sets of tuples), and operations include union, intersection,
selection, projection, Cartesian product, etc
– E.g., (π c-ownerChecking-account) ∩ (π s-ownerSavings-account)
What is a query?
A query is applied to relation instances, and the result of
a query is also a relation instance. (view, query)
– Schemas of input and output fixed, but instances not.

• Operators refer to relation attributes by position or name:


– E.g., Account(number, owner, balance, type)

– Positional notation easier for formal definitions, named-field


notation more readable.
– Both used in SQL
Relational Algebra Operations
The usual set operations: union, intersection, difference

•Operations that remove parts of relations:


selection, projection
•Operations that combine tuples from two relations:
Cartesian product, join
•Since each operation returns a relation,
operations can be composed!
Removing Parts of Relations
• Selection – rows
• Projection - columns
Selection: Example
Another selection
Example of Projection
Projection removes duplicates
Set Operations
• Union
• Intersection
• Difference
What happens when sets unite?
Union
• Relations r, s:
Operation – Example
A B A B

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

A B

 r  s:  1
 2
 1
 3
Union Example
Union Compatibility
Intersection
Set Difference Operation –
Example
• Relations r, s:
A B A B

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

 r – s:
A B

 1
 1
Difference
Another way to show intersection?
Summary so far:
• E1 U E2 : union
• E1 - E2 : difference
• E1 x E2 : cartesian product
 c(E1) : select rows, c = condition (book has p for predicate)
• IIs(E1) : project columns : s =selected columns
  x(c1,c2) (E1) : rename, x is new name of E , c1 is new name of
1

column
Combining Tuples of Two Relations

• Cross product (Cartesian product)


• Joins
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
Cross Product Example
Cross Product

• How to resolve????
Renaming operator: 

Rename whole relation: Teacher X secondteacher(Teacher) 


Teacher.t-num, Teacher.t-name, secondteacher.t-num, secondteacher.t-name
OR rename attribute before combining:

Teacher X secondteacher(t-num2, t-name2)(Teacher)


t-num, t-name, t-num2, t-name2
OR rename after combining

Join: Example
Join : Example
Condition Join
Equi and Natural Join
Divide operator
Divide Operation
Divide Definition
When to divide?
Division Example
Dividing without division sign
Working out an example
Assignment operation
Why would we use Relational
Algebra?????
Equivalencies help
ER vs RA
• Both ER and the Relational Model can be
used to model the structure of a database.
• Why is it the case that there are only
Relational Databases and no ER
databases?
RA vs Full Programming Language
• Relational Algebra is not Turing complete.
There are operations that cannot be
expressed in relational algebra.
• What is the advantage of using this
language to query a database?
Summary of Operators updated
• Summary so far:
• E1 U E2 : union
• E1 - E2 : difference
• E1 x E2 : cartesian product
 c(E1) : select rows, c = condition (book has p for predicate)
• IIs(E1) : project columns : s =selected columns
  x(c1,c2) (E1) : rename, x is new name of E , c1 is new name of column
1

• E1  E2 : division
• E1 E2 : join, c = match condition
Practice
• Find names of stars who’ve appeared in a 1994
movie
• Information about movie year available in Movies; so
need an extra join:
σyear=1994(πname(Stars ⋈ AppearIn ⋈ Movies))

• A more efficient solution:


πname(Stars ⋈ AppearIn ⋈ (σyear=1994( Movies))

• An even more efficient solution:


πname(Stars ⋈
πname(AppearIn ⋈ (πmovieIdσyear=1994(Movies)))
A query optimizer can find this, given the first solution!
Extended Relational Algebra
Operations

• Generalized projection
• Outer join
• Aggregate functions
Generalized projection – calculate
fields
Aggregate Operation – Example
• Relation r:
A B C

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

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

27
Aggregate
• Functions on more than one tuple
• Samples:
– Sum
– Count-distinct
– Max
– Min
– Count
– Avg

• Use “as” to rename


branchname g sum(balance) as totalbalance (account)
Aggregate Operation – Example
• Relation account groupedaccount_number
branch_name by branch-name: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
Outer Join
• Keep the outer side even if no join
• Fill in missing fields with nulls
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
Outer Join – Example
• Inner 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
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
Summary of Operators - Full
• E1 U E2 : union
• E1 - E2 : difference
• E1 x E2 : cartesian product
 c(E1) : select rows, c = condition (book has p for predicate)
• IIs(E1) : project columns : s =selected columns separated by commas,
can have calculations included
  x(c1,c2) (E1) : rename, x is new name of E1, c1 is new name of column

• E1  E2 : division

• E1 E2 : join, c = match condition

• E1 E2 : outer join, c = match condition, keep the side with the arrows
  : assignment – give a new name to an expression to make it easy to read
• as : rename a calculated column

• attribute1 g function (attribute2) (E ) : perform function on attribute2 whenever


1

attribute1 changes

You might also like