View
226
Download
0
Embed Size (px)
Citation preview
Is this the Algebra you know?Algebra -> operators and atomic operandsExpressions -> 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 ofa 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-fieldnotation more readable.– Both used in SQL
Relational Algebra OperationsThe 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!
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 E1, c1 is new name of
column
Cartesian-Product Operation – Example
Relations r, s:
r x s:
A B
1
2
A B
11112222
C D
1010201010102010
E
aabbaabb
C D
10102010
E
aabbr
s
Cross Product
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
c(t-num1, t-name1, t-num2, t-name2)(Teacher X Teacher)
t-num1, t-name1, t-num2, t-name2
• How to resolve????
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 E1, c1 is new name of column
• 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))⋈ ⋈
• An even more efficient solution:πname(Stars ⋈πname(AppearIn (π⋈ movieIdσyear=1994(Movies)))A query optimizer can find this, given the first solution!
• A more efficient solution:πname(Stars AppearIn (σ⋈ ⋈ year=1994( Movies))
Aggregate
• Functions on more than one tuple
• Samples:– Sum– Count-distinct– Max– Min– Count– Avg
• Use “as” to renamebranchname g sum(balance) as totalbalance (account)
Aggregate Operation – Example
• Relation account grouped by branch-name:
branch_name g sum(balance) (account)
branch_name account_number balance
PerryridgePerryridgeBrightonBrightonRedwood
A-102A-201A-217A-215A-222
400900750750700
branch_name sum(balance)
PerryridgeBrightonRedwood
13001500700
Outer Join – Example• Relation loan
Relation borrower
customer_name loan_number
JonesSmithHayes
L-170L-230L-155
300040001700
loan_number amount
L-170L-230L-260
branch_name
DowntownRedwoodPerryridge
Outer Join – Example• Inner Join
loan Borrower
loan_number amount
L-170L-230
30004000
customer_name
JonesSmith
branch_name
DowntownRedwood
JonesSmithnull
loan_number amount
L-170L-230L-260
300040001700
customer_namebranch_name
DowntownRedwoodPerryridge
Left Outer Join
loan Borrower
Outer Join – Example
loan_number amount
L-170L-230L-155
30004000null
customer_name
JonesSmithHayes
branch_name
DowntownRedwoodnull
loan_number amount
L-170L-230L-260L-155
300040001700null
customer_name
JonesSmithnullHayes
branch_name
DowntownRedwoodPerryridgenull
Full Outer Join
loan borrower
Right Outer Join
loan borrower
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
• attribute1g function (attribute2) (E1) : perform function on attribute2 whenever
attribute1 changes