Relational Algebra: Database Principles
Relational Algebra: Database Principles
Relational Algebra: Database Principles
Database Principles
What is Relational Algebra?
Database Principles
Supplier-Part Example
supplies
Supplier Part
PK Sno PK Pno
(0,n) (1,n)
Sname Pdesc
Location O_date Colour
Supplier Part
Sno Sname Location Pno Pdesc Colour
Supplies
Sno Pno O_date
s1 p1 nov 3
s2 p2 nov 4
s3 p1 nov 5
s3 p3 nov 6
s4 p1 nov 7
s4 p2 nov 8
s4 p4 nov 9
Database Principles
SELECTION:
alternatively
σ <condition> (table_name)
Find all suppliers from Boston.
Supplier Answer
Sno Sname Location
Sno Sname Location
Select Supplier
s1 Acme NY
s2 Ajax Bos
where Location = ‘Bos’ s2 Ajax Bos
s3 Apex Chi
s4 Ace LA σ Location = ‘Bos’ (Supplier)
s5 A-1 Phil
Database Principles
SELECTION Exercise:
alternatively
• Observations:
– There is only one input table.
– Both Cardholder and the answer table have the same
schema (list of columns)
– Every row in the answer has the value ‘Modena’ in the
b_addr column.
Database Principles
SELECTION:
same schema
Answer
Database Principles
PROJECTION:
alternatively
Supplier Answer
Sname
Sno Sname Location
Project Supplier over Sname
Acme
s1 Acme NY
Ajax
s2 Ajax Bos π Sname (Supplier) Apex
s3 Apex Chi
Ace
s4 Ace LA
A-1
s5 A-1 Phil
Database Principles
PROJECTION Exercise:
alternatively
π b_addr (Cardholder)
• Observations:
– There is only one input table.
– The schema of the answer table is the list of columns
– If there are many Cardholders living at the same
address these are not duplicated in the answer table.
Database Principles
PROJECTION:
schema of answer table
is the same as the list of
columns in the query
Answer
Database Principles
CARTESIAN PRODUCT:
Database Principles
CARTESIAN PRODUCT:
5 rows 4 rows
Supplier Part
Sno Sname Location Pno Pdesc Colour
Database Principles
CARTESIAN PRODUCT Exercise:
Names x Addresses
Database Principles
UNION:
alternatively
∩
Table1 Table2
• Observations:
– This operation is impossible unless both tables involved
have the same schemas. Why?
– Because rows from both tables must fit into a single
answer table; hence they must “look alike”.
– Because some rows might already belong to both tables
Database Principles
UNION Example:
Part1Suppliers
union
Part1Suppliers Part2Suppliers Part2Suppliers
Sno Sno Sno
s1 s2 s1
s3 s4 s2
s4 s3
s4
Database Principles
UNION Exercise:
Database Principles
INTERSECTION:
alternatively
Table1 ∩ Table2
• Observations:
– This operation is impossible unless both tables involved
have the same schemas. Why?
– Because rows from both tables must fit into a single
answer table; hence they must “look alike”.
Database Principles
INTERSECTION Example:
Part1Suppliers = project (select Supplies where Pno = ‘p1’) over Sno
Part2Suppliers = project (select Supplies where Pno = ‘p2’) over Sno
Answer = Part1Suppliers
∩ Part2Suppliers
Part1Suppliers
intersect
Part1Suppliers Part2Suppliers
Part2Suppliers
Sno Sno
Sno
s1 s2
s3 s4 s4
s4
Database Principles
INTERSECTION Exercise:
Database Principles
SET DIFFERENCE:
alternatively
Table1 \ Table2
• Observations:
– This operation is impossible unless both tables involved
have the same schemas. Why?
– Because it only makes sense to calculate the set
difference if the two sets have elements in common.
Database Principles
SET DIFFERENCE Example:
Part1Suppliers = project (select Supplies where Pno = ‘p1’) over Sno
Part2Suppliers = project (select Supplies where Pno = ‘p2’) over Sno
Part1Suppliers
minus
Part1Suppliers Part2Suppliers Part2Suppliers
Sno Sno Sno
s1 s2
s4
s1
s3
s4 s3
Database Principles
SET DIFFERENCE Exercise:
Database Principles
JOIN:
Database Principles
JOIN Example:
supplies
• Suppose we want to know the names of all parts ordered
Supplier Part
PK Pno
between Nov 4 and Nov 6.
PK Sno
(0,n) (1,n)
Sname Pdesc
Location Colour
related
O_date
Supplier Part
Sno Sname Location Pno Pdesc Colour
Supplies
Sno Pno O_date
s1 p1 nov 3
s2
s3
s3
p2
p1
p3
nov 4
nov 5
nov 6
} What we know is here?
s4 p1 nov 7
s4 p2 nov 8
s4 p4 nov 9
Database Principles
JOIN Example:
Supplies x Part
Sno Pno O_date Pno Pdesc Colour
Database Principles
JOIN Example:
• Step 2: Let’s get rid of all the noise rows from the
Cartesian Product.
A = select (Supplies x Part) where Supplies.PNo = Part.PNo
Database Principles
JOIN Example:
Database Principles
JOIN Example:
Database Principles
JOIN Example:
Supplies Part =
project(select (Supplies x Part) where Supplies.Pno = Part.Pno)
over Sno, Supplies.Pno, O_date, Pdesc, Colour
Sno Pno O_date Pdesc Colour
Database Principles
JOIN Example:
• Summary:
– Used when two tables are to be combined into one
– Most often, the two tables share a column
– The shared column is often a primary key in one of the
tables
– Because it is a primary key in one table the shared
column is called a foreign key in any other table that
contains it
– JOIN is a combination of
• Cartesian Product
• Select
• Project
Database Principles
JOIN Example (Finishing Up):
Database Principles
JOIN Example (Finishing Up):
Answer
• Final Answer: Pdesc
Database Principles
JOIN Summary:
R.A θ S.B
where θ is any comparison operator except =
Database Principles
JOIN Exercise:
Answer
author title
Brookes MMM
Database Principles
QUOTIENT
Database Principles
QUOTIENT
Database Principles
QUOTIENT
SuppliedParts
Sno Pno
SuppliedParts = project Supplies over Sno, Pno
s1 p1
s2 p2
s3 p1
s3 p3
7 rows s4 p1
s4 p2
s4 p4
p1
p4
AllSuppliers
Sno
AllSuppliers = project Supplier over Sno
s1
s2
s3
s4
s5
Database Principles
AllSuppliers x RedParts
QUOTIENT
Sno Pno
s1 p1 s1 p1
s2 p1 s2 p2
s3 p1 s3 p1
s4 p1 s3 p3
s5 p1 s4 p1
s1 p4 s4 p2
s2 p4
s4 p4
s3 p4
s4 p4
s5 p4
AllSuppliers x RedParts \
• Next calculate: RedParts
Sno Pno NOTE: These are the
NonSuppliedRedParts = “noise” rows of the
s2 p1
(AllSuppliers x RedParts) \ SuppliedParts Cartesian Product. We
s5 p1
s1 p4 know that for every row
s2 p4 in this table, the supplier
s3 p4 mentioned did NOT
s5 p4 supply the red part
mentioned.
Database Principles
QUOTIENT
s4
Database Principles
QUOTIENT
∩
the QUOTIENT of R by S (written R/S) is defined to be
the largest table (call it Q) such that Q x S R.
∩
x
SuppliedParts FinalAnswer =
Sno Pno SuppliedParts / RedParts
s1 p1 FinalAnswer
RedParts
s2 p2
=
Sno
/
s3 p1 Pno
s3 p3
s4 p1 p1 s4
s4 p2 p4
s4 p4
Database Principles
How to Use QUOTIENT
Database Principles
How to Use QUOTIENT
Database Principles
QUOTIENT Exercise:
Q is the answer
• NOTE:
– We only use key attributes. This is important
Database Principles