Professional Documents
Culture Documents
Slides-7-Relational Algebra
Slides-7-Relational Algebra
Relational Algebra
1
Outline
• Relational Algebra (Section 6.1)
2
Relational Algebra
• Formalism for creating new relations from
existing ones
• Its place in the big picture:
Declarative
Declarative
query
query Algebra
Algebra Implementation
Implementation
language
language
4
1. Union and 2. Difference
• R1 R2
Example:
– ActiveEmployees RetiredEmployees
• R1 – R2
Example:
– AllEmployees − RetiredEmployees
5
What about Intersection ?
• It is a derived operator
R1 R2 = R1 – (R1 – R2)
6
3. Selection
• Returns all tuples which satisfy a condition
• Notation: c(R)
• Examples
– Salary > 40000 (Employee)
– name = “Smith” (Employee)
• The condition c can be =, <, , >, , <>
Employee
SSN Name DepartmentID Salary
999999999 John 1 30,000
777777777 Tony 1 32,000
888888888 Alice 2 45,000
Find all employees with salary more than $40,000.
(Employee)
Salary > 40000
8
4. Projection
• Eliminates columns, then removes duplicates
• Notation: A1,…,An (R)
• Example: project to social-security number and
names:
– SSN, Name (Employee)
– Output schema: Answer(SSN, Name)
9
Projection Example
Employee
SSN Name DepartmentID Salary
999999999 John 1 30,000
777777777 Tony 1 32,000
888888888 Alice 2 45,000
SSN, Name (Employee)
SSN Name
999999999 John
777777777 Tony
888888888 Alice
10
5. Cartesian Product
• Combine each tuple in R1 with each tuple in R2
• Notation: R1 R2
• Example:
– Employee Dependents
• Very rare in practice; mainly used to express
joins
11
Cartesian Product Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
EmployeeSSN Dname
999999999 Emily
777777777 Joe
Employee x Dependents
Name SSN EmployeeSSN Dname
John 999999999 999999999 Emily
John 999999999 777777777 Joe
Tony 777777777 999999999 Emily
Tony 777777777 777777777 Joe
12
Relational Algebra
• Five operators:
– Union:
– Difference: -
– Selection:
– Projection:
– Cartesian Product:
• Derived or auxiliary operators:
– Intersection, complement
– Joins (natural,equi-join, theta join, semi-join)
– Renaming:
13
Renaming
• Changes the schema, not the instance
• Schema: R(A1, …, An )
• Notation: B1,…,Bn (R)
• Example:
– LastName, SocSocNo (Employee)
– Output schema: Answer(LastName, SocSocNo)
Employee
Name SSN
John 999999999
Tony 777777777
• Where:
– The selection C checks equality of all common attributes
– The projection eliminates the duplicate common
attributes
[in SQL: SELECT DISTINCT R1.A, R1. B, R2.C FROM R1, R2
WHERE R1.B = R2.B
Schema: R1(A,B), R2(B,C)]
16
Natural Join Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
SSN Dname
999999999 Emily
777777777 Joe
Employee Dependents =
Name, SSN, Dname( SSN=SSN2(Employee x SSN2, Dname(Dependents))
Name SSN Dname
John 999999999 Emily
Tony 777777777 Joe
17
Natural Join
• R= A B S= B C
X Y Z U
X Z V W
Y Z Z V
Z V
A B C
• R ⋈ S= X Z U
X Z V
Y Z U
Y Z V
Z V W 18
Natural Join
• Given the schemas R(A, B, C, D), S(A, C, E),
what is the schema of R ⋈ S ?
19
Theta Join
• A join that involves a predicate
• R1 ⋈ R2 = (R1 R2)
• Here can be any condition
20
Eq-join
• A theta join where is an equality
R1 ⋈A=B R2 = A=B (R1 R2)
• Example:
– Employee ⋈SSN=SSN Dependents
22
Semijoins in Distributed
Databases
• Semijoins are used in distributed databases
Dependents
Employee
SSN Dname Age
SSN Name ... ...
... ... network
Employee
Employee ⋈⋈ssn=ssn
ssn=ssn
(
( (Dependents))
age>71 (Dependents))
age>71
buyer-ssn=ssn
pid=pid
seller-ssn=ssn
ssn pid
name=fred name=gizmo
sname
sid=sid
29
Formulating queries in RA
• Specify the following queries in relational
algebra
– List the number of courses (Course#) taken by all
students named ‘John Smith’ in Winter 1999 (i.e.,
Quarter = W99)
– List any department which has all its adopted books
published by ‘BC Publishing’
30
Formulating Queries in RA
Course# (Quarter=W99 ((Name= ‘John Smith’ (STUDENT) ⋈
ENROLL))
OtherDept = Dept ((Publisher <> ‘PS Publishers’
(BOOK_ADOPTION ⋈ TEXT)) ⋈ COURSE)
AllDept = Dept (BOOK_ADOPTION ⋈ COURSE) WHY?
Answer = AllDept - OtherDept
31