Relational Algebra: Monday, May 10, 2010
Relational Algebra: Monday, May 10, 2010
Relational Algebra
Monday, May 10, 2010
Data
Dan Independence
Suciu -- 444 Spring 2010 3
SQL = WHAT
Product(pid, name, price)
Purchase(pid, cid, store)
Customer(cid, name, city)
SELECT DISTINCT x.name, z.name
FROM Product x, Purchase y, Customer z
WHERE x.pid = y.pid and y.cid = y.cid and
x.price > 100 and z.city = ‘Seattle’
T1(pid,name,price,pid,cid,store) cid=cid
R1 ∪ R2
R1 – R2
R1 ∩ R2 = R1 – (R1 – R2)
• Derived using join (will explain later)
R1 ∩ R2 = R1 ⨝ R2
σc(R)
• Examples
– σSalary > 40000 (Employee)
– σname = “Smith” (Employee)
• The condition c can be =, <, ≤, >, ≥, <>
Dan Suciu -- 444 Spring 2010 14
Employee
SSN Name Salary
1234545 John 200000
5423341 Smith 600000
4352342 Fred 500000
Π A1,…,An (R)
• Example: project social-security number
and names:
– Π SSN, Name (Employee)
– Answer(SSN, Name)
Which is Dan
more efficient to implement ?
Suciu -- 444 Spring 2010 17
Cartesian Product
• Each tuple in R1 with each tuple in R2
R1 × R2
Name
SSN
EmpSSN
DepName
John
999999999
999999999
Emily
Tony
777777777
777777777
Joe
Employee ✕ Dependent
Name
SSN
EmpSSN
DepName
John
999999999
999999999
Emily
John
999999999
777777777
Joe
Tony
777777777
999999999
Emily
Tony
777777777
777777777
Joe
Dan Suciu -- 444 Spring 2010 19
Renaming
• Changes the schema, not the instance
ρ B1,…,Bn (R)
• Example:
– ρN, S(Employee) Answer(N, S)
R1 ⨝ R2
• Meaning: R1⨝ R2 = ΠA(σ(R1 × R2))
• Where:
– The selection σ checks equality of all
common attributes
– The projection eliminates the duplicate
common attributes
A B C
R ⨝ S =
X Z U
ΠABC(σR.B=S.B(R × S))
X Z V
Y Z U
Y Z V
Z V W
Dan Suciu -- 444 Spring 2010 22
Natural Join
• Given the schemas R(A, B, C, D), S(A, C,
E), what is the schema of R ⨝ S ?
R1 ⨝θ R2 = σ θ (R1 × R2)
R ⋉C S = Π A1,…,An (R ⨝C S)
29
Semijoins in Distributed
Databases
Dependent
Employee
EmpSSN DepName Age Stuff
SSN Name Stuff ... ... ... ....
... ... .... network
• WHY ?
• Grouping γ
γA,sum(B) = select A,sum(B) from R group by A
• Sorting τ
Dan Suciu -- 444 Spring 2010 33
Complex RA Expressions
γ u.name, count(*)
x.ssn=y.buyer-ssn
y.pid=u.pid
y.seller-ssn=z.ssn
Π ssn Π pid
σname=fred σname=gizmo
Person x Purchase y Person z Product u
Dan Suciu -- 444 Spring 2010 34
RA = Dataflow Program
• Several operations, plus strictly
specified order