CH 4
CH 4
CH 4
Chapter 4
t p(t)
f1 ∧ f2 " is true if and only if " f1 " is true and " f2 "
is true,
" f1 ∨ f2 " is true if and only if " f1 " is true or " f2 "
is true or both are true,
" ¬ f " is true if and only if " f " is not true,
Tuple calculus
ENG ELHAM MOH'D
Algebra -> operators and atomic operands
Expressions -> applying operators to atomic operands and/or
other expressions
Relational Algebra
ENG ELHAM MOH'D
• There are two groups of operations:
• Mathematical set theory based relations:
UNION, INTERSECTION, DIFFERENCE, and
CARTESIAN PRODUCT.
• Special database operations:
SELECT (not the same as SQL SELECT),
PROJECT, and JOIN.
Relational Algebra
ENG ELHAM MOH'D
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:
•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!
Projection - columns
• Relational SELECT
• SELECTION, σ - Selects the rows, or
tuples, from a relation, which satisfy the
specified constraints or restrictions
Name=“Fred” (Employee)
Relational Algebra
ENG ELHAM MOH'D
Projection
Eliminates columns, then removes
duplicates
Notation: A1,…,An (R)
Example: project social-security
number and names:
◦ SSN, Name (Employee)
◦ Output schema: Answer(SSN, Name)
SSN Name Salary
1234545 John 200000
5423341 John 600000
4352342 John 200000
Name,Salary (Employee)
Name Salary
John 20000
John 60000
Relational Algebra
SELECT and PROJECT
SELECT and PROJECT can be combined
together. For example, to get a list of
employee numbers for employees in
department number 1:
Π empno , σ dpno=1 (employee)
Cross product
ENG ELHAM MOH'D
Cross Product Example
Each tuple in R1 with each tuple in R2
Notation: R1 R2
Example:
◦ Employee Dependents
Very rare in practice; mainly used to
express joins
Cartesian Product
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
Set operation-semantic
• Consider two relations R and S.
• UNION of R and S
the union of two relations is a relation that
includes all the tuples that are either in R
or in S or in both R and S. Duplicate tuples
are eliminated.
• INTERSECTION of R and S
the intersection of R and S is a relation that
includes all tuples that are both in R and S.
• DIFFERENCE of R and S
the difference of R and S is the relation that
contains all
ENG ELHAM MOH'D
Relations r, s:
A B A B
1 2
2 3
1 s
r
A B
r s: 1
2
1
3
Union Operation – Example
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
Example to illustrate the result of
UNION, INTERSECT, and DIFFERENCE
Slide 6- 33
Some additional operations are:
• Intersection “∩ “
• Join “ “
• Renaming “ρ “
Example Projection
ENG ELHAM MOH'D
Example PROJECTION
name tel_no
• πc.name, c.tel_noCustomer
Anne 01789277883
Wetherby
Peter Smith 01788 266777
Peter 01789277883
Michaels
Renaming
Renaming Example
Employee
Name SSN
John 999999999
Tony 777777777
Supplier
supplier
name address contact_name tel_no
_no
Phones 4 Everyone 01637
1 Unit 10 Count Drive Northampton NH10 4EX Mr Smith
Ltd 890226
Mrs. 01456
2 MP Tradesales 3 Clarence Drive Wolverhampton WX10 2GT
Applegate 789267
Peter 01287
3 Buzz Mobiles Ltd 10 Green Gate Norwich NA2 6DT
Taverham 383991
Phone Accessories 01275
4 Warwick Crescent Leicester LE10 3AT Mr Studley
Direct 339482
2 Lindridge Industrial Estate Lindridge LD12 01534
5 Orion Express Mr Wheathill
4TD 729467
Unit 8 Knowle Industrial Estate Knowle KN22 01237
6 Activity Phones Ltd. Mr Solihull
3AX 228999
And Purchase
Activity
Unit 8 Knowle Industrial Estate 01237
6 Phones Mr Solihull 24 6 2005-09-12
Knowle KN22 3AX 228999
Ltd.
JOIN-natural join
ENG ELHAM MOH'D
Invariably the JOIN involves an equality test,
and thus is often described as an equi-join.
Such joins result in two attributes in the
resulting relation having exactly the same
value. A `natural join' will remove the duplicate
attribute(s).
In most systems a natural join will require that
the attributes have the same name to identify
the attribute(s) to be used in the join. This may
require a renaming mechanism.
If you do use natural joins make sure that the
relations do not have two attributes with the
same name by accident
JOIN
ENG ELHAM MOH'D
S
R
A 1 A 1 R JOIN S
B 2 R.COLA=S.COLA
C 2
C 3 D 3
D 4
F 4
E 5
A 1 A 1
C 3 C 2
D 4 D 3
EXAMPLE JOIN
ENG ELHAM MOH'D
The equivalent relational algebra expression
follows:
Supplier
S.supplier_no=PO.supplier_no Purchase_Order
EXAMPLE JOIN
ENG ELHAM MOH'D
Contains those tuples of the cross product
that satisfy some condition
The result schema is the same as that of
the cross product
However there are fewer tuples than cross
product ~ might be computationally more
efficient
E.g. S S1.supplier_no < P.supplier_no P
CONDITION JOIN
ENG ELHAM MOH'D
A special case of condition join where the
condition cond contains only equalities
EQUI-JOIN
ENG ELHAM MOH'D
Taking into account the sailors
Sailors(sid, sname, rating, age)
DELETE EXAMPLE
ENG ELHAM MOH'D
To insert data into a relation, we either:
specify a tuple to be inserted
write a query whose result is a set of
tuples to be inserted
in relational algebra, an insertion is
expressed by:
r r E
where r is a relation and E is a
relational algebra expression.
INSERT
ENG ELHAM MOH'D
Insert information in the database
specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
account account {(“Perryridge”, A-
973, 1200)}
depositor depositor {(“Smith”, A-
973)}
INSERT
ENG ELHAM MOH'D
Make interest payments by increasing all
balances by 5 percent.
account AN, BN, BAL * 1.05 (account)
where AN, BN and BAL stand for account-
number, branch-name and balance,
respectively.