RelationalAlgebra RelationalCalculus SQL
RelationalAlgebra RelationalCalculus SQL
1
Agenda
1 Session Overview
2
Session Agenda
Session Overview
Relational Algebra and Relational Calculus
Relational Algebra Using SQL Syntax
Summary & Conclusion
3
What is the class about?
Textbooks:
» Fundamentals of Database Systems (6th Edition)
Ramez Elmasri and Shamkant Navathe
Addition Wesley
ISBN-10: 0-1360-8620-9, ISBN-13: 978-0136086208 6th Edition (04/10)
4
Icons / Metaphors
Information
Common Realization
Knowledge/Competency Pattern
Governance
Alignment
Solution Approach
55
Agenda
1 Session Overview
6
Agenda
7
The Relational Algebra and Relational Calculus
Relational algebra
Basic set of operations for the relational model
Relational algebra expression
Sequence of relational algebra operations
Relational calculus
Higher-level declarative language for specifying
relational queries
8
Unary Relational Operations: SELECT and PROJECT (1/3)
9
Unary Relational Operations: SELECT and PROJECT (2/3)
Example:
10
Unary Relational Operations: SELECT and PROJECT (3/3)
Selectivity
Fraction of tuples selected by a selection
condition
SELECT operation commutative
Cascade SELECT operations into a single
operation with AND condition
11
The PROJECT Operation
Degree
Number of attributes in <attribute list>
Duplicate elimination
Result of PROJECT operation is a set of
distinct tuples
12
Sequences of Operations and the RENAME Operation
In-line expression:
Sequence of operations:
13
Relational Algebra Operations from Set Theory (1/2)
14
Relational Algebra Operations from Set Theory (2/2)
INTERSECTION
R∩S
Includes all tuples that are in both R and S
SET DIFFERENCE (or MINUS)
R–S
Includes all tuples that are in R but not in S
15
The CARTESIAN PRODUCT (CROSS PRODUCT) Operation
CARTESIAN PRODUCT
CROSS PRODUCT or CROSS JOIN
Denoted by ×
Binary set operation
Relations do not have to be union compatible
Useful when followed by a selection that
matches values of attributes
16
Binary Relational Operations: JOIN and DIVISION (1/2)
17
Binary Relational Operations: JOIN and DIVISION (2/2)
THETA JOIN
Each <condition> of the form Ai θ Bj
Ai is an attribute of R
Bj is an attribute of S
Ai and Bj have the same domain
θ (theta) is one of the comparison operators:
• {=, <, ≤, >, ≥, ≠}
18
Variations of JOIN: The EQUIJOIN and NATURAL JOIN (1/2)
EQUIJOIN
Only = comparison operator used
Always have one or more pairs of attributes
that have identical values in every tuple
NATURAL JOIN
Denoted by *
Removes second (superfluous) attribute in an
EQUIJOIN condition
19
Variations of JOIN: The EQUIJOIN and NATURAL JOIN (2/2)
Join selectivity
Expected size of join result divided by the
maximum size nR * nS
Inner joins
Type of match and combine operation
Defined formally as a combination of
CARTESIAN PRODUCT and SELECTION
20
A Complete Set of Relational Algebra Operations
21
The DIVISION Operation
Denoted by ÷
Example: retrieve the names of employees
who work on all the projects that ‘John
Smith’ works on
Apply to relations R(Z) ÷ S(X)
Attributes of R are a subset of the attributes of
S
22
Operations of Relational Algebra (1/2)
23
Operations of Relational Algebra (2/2)
24
Notation for Query Trees
Query tree
Represents the input relations of query as leaf
nodes of the tree
Represents the relational algebra operations
as internal nodes
25
Sample Query Tree for Relational Algebra Expression
26
Additional Relational Operations (1/2)
Generalized projection
Allows functions of attributes to be included in
the projection list
27
Additional Relational Operations (2/2)
28
Sample Aggregate Function Operation
29
Recursive Closure Operations
30
OUTER JOIN Operations
Outer joins
Keep all tuples in R, or all those in S, or all
those in both relations regardless of whether or
not they have matching tuples in the other
relation
Types
• LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL
OUTER JOIN
Example:
31
The OUTER UNION Operation
32
Examples of Queries in Relational Algebra (1/3)
33
Examples of Queries in Relational Algebra (2/3)
34
Examples of Queries in Relational Algebra (3/3)
35
The Tuple Relational Calculus
Declarative expression
Specify a retrieval request
Non-procedural language
Any retrieval that can be specified in basic
relational algebra
Can also be specified in relational calculus
36
Tuple Variables and Range Relations
Tuple variables
Ranges over a particular database relation
Satisfy COND(t):
Specify:
Range relation R of t
Select particular combinations of tuples
Set of attributes to be retrieved (requested
attributes)
37
Expressions and Formulas in Tuple Relational Calculus
38
Existential and Universal Quantifiers
39
Sample Queries in Tuple Relational Calculus
40
Notation for Query Graphs
41
Transforming the Universal and Existential Quantifiers
42
Using the Universal Quantifier in Queries
43
Safe Expressions
44
The Domain Relational Calculus (1/2)
45
The Domain Relational Calculus (2/2)
QBE language
Based on domain relational calculus
46
Summary
47
Agenda
1 Session Overview
48
Agenda
50
Key Differences Between SQL And “Pure” Relational Algebra
SQL data model is a multiset not a set; still rows in tables (we
sometimes continue calling relations)
» Still no order among rows: no such thing as 1st row
» We can (if we want to) count how many times a particular row appears
in the table
» We can remove/not remove duplicates as we specify (most of the time)
» There are some operators that specifically pay attention to duplicates
» We must know whether duplicates are removed (and how) for each
SQL operation; luckily, easy
Many redundant operators (relational algebra had only one:
intersection)
SQL provides statistical operators, such as AVG (average)
» Can be performed on subsets of rows; e.g. average salary per
company branch
51
Key Differences Between Relational Algebra And SQL
p a, b SELECT a, b
pq FROM p, q
p a, b s (d > e) (f =g ) (p q) SELECT a, b
FROM p, q
WHERE d > e AND f = g;
{must always have SELECT even if all
attributes are kept, can be written as:
SELECT *}
renaming AS {or blank space}
pq SELECT *
FROM p
UNION
SELECT *
FROM q
p−q SELECT *
FROM p
EXCEPT
SELECT *
FROM q
Sometimes, instead, we have DELETE
FROM
pq SELECT *
FROM p
INTERSECT
SELECT *
FROM q
54
Sets And Operations On Them
55
Relations in Relational Algebra
R A B R B A
1 10 20 2
2 20 10 1
20 2
20 2
57
Many Empty Relations
58
Relational Algebra Versus Full SQL
59
Operations on relations
R A B C D
1 10 100 1000
1 20 100 1000
1 20 200 1000
SQL statement B A D
SELECT B, A, D 10 1 1000
20 1 1000
FROM R
20 1 1000
A B C D
5 5 7 4
4 5 4 4
62
Selection
63
Cartesian Product
RA B S C B D
1 10 40 10 10
2 10 50 20 10
2 20
SQL statement
SELECT A, R.B, C, S.B, D
FROM R, S; (comma stands for Cartesian product)
A R.B C S.B D
1 10 40 10 10
1 10 50 20 10
2 10 40 10 10
2 10 50 20 10
2 20 40 10 10
2 20 50 20 10
64
A Typical Use Of Cartesian Product
SQL statement:
SELECT ID#, R.Room#, Size
FROM R, S
WHERE R.Room# = S.Room#;
ID# R.Room# Size
40 1010 140
50 1020 150
65
A Typical Use Of Cartesian Product
R A B S A B
1 10 1 10
2 20 3 20
SQL statement
(SELECT * A B
FROM R) 1 10
UNION
2 20
(SELECT *
FROM S); 3 20
R A B S A B
1 10 1 10
2 20 3 20
SQL statement
(SELECT *
FROM R)
MINUS A B
(SELECT *
2 20
FROM S);
Union compatibility required
EXCEPT is a synonym for MINUS
70
Intersection
R A B S A B
1 10 1 10
2 20 3 20
SQL statement
(SELECT *
FROM R) A B
INTERSECT 1 10
(SELECT *
FROM S);
Union compatibility required
Can be computed using differences only: R – (R – S)
71
From Relational Algebra to Queries
73
Relational Implementation
PK,FK2 W PK N PK,FK1 P
PK,FK2 C
FK1 H S
A
PK,FK1 H PK N PK,FK1 P
PK,FK2 C
FK2 W S
A
74
Microsoft Access Database
75
Microsoft Access Database
The database and our queries (other than the one with
MINUS at the end) are the appropriate “extras” directory
on the class web in “slides”
» MINUS is frequently specified in commercial databases in a
roundabout way
» We will cover how it is done when we discuss commercial
databases
Our sample Access database: People.mdb
The queries in Microsoft Access are copied and pasted
in these notes, after reformatting them
Included copied and pasted screen shots of the results
of the queries so that you can correlate the queries with
the names of the resulting tables
76
Our Database With Sample Queries - Open In Microsoft Access
77
Our Database
Person N S A Birth P C
Albert M 20 Dennis Albert
Dennis M 40 John Mary
Evelyn F 20 Mary Albert
John M 60 Robert Evelyn
Mary F 40 Susan Evelyn
Robert M 60 Susan Richard
Susan F 40
Marriage H W A
Dennis Mary 20
Robert Susan 30
78
Our Instance In Microsoft Access
79
A Query
81
A Query
N
Evelyn
82
The Query In Microsoft Access
83
A Query
84
A Query
Answer:=
SELECT P, C AS Daughter
FROM Person, Birth
WHERE C = N AND S = ‘F’;
P Daughter
John Mary
Robert Evelyn
Susan Evelyn
85
Cartesian Product With Condition: Matching Tuples Indicated
Person N S A
Albert M 20
Dennis M 40
Evelyn F 20
John M 60
Mary F 40
Robert M 60
Susan F 40
Birth P C
Dennis Albert
John Mary
Mary Albert
Robert Evelyn
Susan Evelyn
Susan Richard
86
The Query In Microsoft Access
87
A Query
Answer :=
SELECT P AS Father, C AS Daughter
FROM Person, Birth, Person AS Person1
WHERE P = Person.N AND C = Person1.N
AND Person.S = ‘M’ AND Person1.S = ‘F’;
Father Daughter
John Mary
Robert Evelyn
89
Cartesian Product With Condition: Matching Tuples Indicated
Person N S A Person N S A
Albert M 20 Albert M 20
Dennis M 40 Dennis M 40
Evelyn F 20 Evelyn F 20
John M 60 John M 60
Mary F 40 Mary F 40
Robert M 60 Robert M 60
Susan F 40 Susan F 40
Birth P C
Dennis Albert
John Mary
Mary Albert
Robert Evelyn
Susan Evelyn
Susan Richard
90
The Query In Microsoft Access
91
A Query
F_I_L S_I_L
John Dennis
92
The Query In Microsoft Access
93
A Query
Produce a relation:
Answer(Grandparent,Grandchild)
A classroom exercise, but you can see
the solution in the posted database
G_P G_C
John Albert
94
Cartesian Product With Condition: Matching Tuples Indicated
Birth P C Birth P C
Dennis Albert Dennis Albert
John Mary John Mary
Mary Albert Mary Albert
Robert Evelyn Robert Evelyn
Susan Evelyn Susan Evelyn
Susan Richard Susan Richard
95
The Query In Microsoft Access
96
Further Distance
97
Relational Algebra Is Not Universal:Cannot Compute (Ancestor,Descendant)
99
A Sample Query
SELECT
A FROM Person
MINUS
SELECT
A FROM MARRIAGE;
100
The Query In Microsoft Access
101
It Does Not Matter If We Remove Duplicates
Removing duplicates
A A
A
20
40
- 20
30
= 40
60
60
R A B C D
1 10 100 1000
1 20 100 1000
1 20 200 1000
107
: Cartesian Product
R A B S C B D
1 10 40 10 10
2 10 50 20 10
2 20
SQL statement Relational Algebra
SELECT A, R.B, C, S.B, D RS
FROM R, S
A R.B C S.B D
1 10 40 10 10
1 10 50 20 10
2 10 40 10 10
2 10 50 20 10
2 20 40 10 10
2 20 50 20 10
108
A Typical Use Of Cartesian Product
R A B S A B
1 10 1 10
2 20 3 20
R A B S A B
1 10 1 10
2 20 3 20
R A B S A B
1 10 1 10
2 20 3 20
114
Agenda
1 Session Overview
115
Summary
116
Assignments & Readings
Readings
» Slides and Handouts posted on the course web site
» Textbook: Chapters 6
Assignment #3
» Textbook exercises: Textbook exercises: 9.3, 9.5, 10.23, 6.16, 6.24, 6.32
117
Next Session: Standard Query Language (SQL) Features
118
Any Questions?
119