CIS3530L06
CIS3530L06
CIS3530L06
Concepts
L06
Chapter 6
The Relational Algebra and
Relational Calculus
Topics to be covered
Outline
Introduction to Databases
Data Modeling Using ER-Mod
Constraints
Relational Algebra & Relational Calculus
Structured Query Language
Optimization
Concurrency Control and Recovery
Object & Object-Relational Databases
Database Security, Adv. Modeling & Distribution
2
Outline
Languages for dBs
Relational Algebra
– RA Operations From Set Theory
• Union, Intersection, Difference
• Divide (Read)
– Other Relational Operators
• Select, Project,
• Join (Read)
Examples (COMPANY)
3
Languages for dBs
DDL: Data Definition Language
– Describes operations on the DB structure (DB
schema).
• table creation, modification, deletion, …
• definition of attribute domains, primary keys, integrity
constraints, (either intra-relational or inter-relational).
– Mainly used by DB designers during the database
implementation.
DML: Data Manipulation Language
– Describes operations on the data stored in the DB
(DB Instance)
– The Operations here include:
• Update (changes content of dB without changing the schema)
• Query (i.e. a fxn, that given the dB, produces a relation.)
4
Query Languages for Relational Databases
Foundations of DB languages can be studied with
reference to query languages.
There are two different kinds of query languages:
– Procedural Languages:
• One describes the procedure that may be followed to obtain
the results (“how”)
• Relational Algebra
– Declarative Languages
• Describe the properties of the result, rather than the procedure
used to obtain it (“what”).
• Relational Calculus (theoretical)
• Structured Query Language – SQL (real but only partially
declarative).
• Query by Example – QBE (real). 5
Relational Algebra
A collection of operators that:
– are defined on relations
– produce relations as results
– can be combined to form complex expressions.
Operators
– traditional set theory: union, intersection, difference;
– more specific operators:
renaming, selection, projection
– The most important: join
• Natural join
• Cartesian product
• Theta-join
6
Union, Intersection & Difference
Relations are sets, so we can apply set
operators;
However, we want the results to be
relations (homogeneous sets of tuples).
Therefore it is meaningful to apply union,
intersection, difference only to pairs of
relations defined over the same attributes.
Note that these do not alter the original
relations, but produce new relations.
7
Relational Algebra Operations
From Set Theory
Type Compatibility
– The operand relations:
R1(A1, A2, ..., An) and
R2(B1, B2, ..., Bn)
– must have the same number of attributes, and
the domains of corresponding attributes must be
compatible; that is, dom(Ai)=dom(Bi) for i=1, 2,
..., n.
Graduates
Number Surname Age
7274 Robinson 37
7432 O'Malley 39 Graduates Managers
9824 Darkes 38 Number Surname Age
7274 Robinson 37
Managers 7432 O'Malley 39
Number Surname Age 9797 O'Malley 56
9797 O'Malley 56 9824 Darkes 38
7432 O'Malley 39
9824 Darkes 38
Intersection
The intersection of two relations R and S is the
set of tuples t such that t R t S, T = R S.
Graduates
Number Surname Age
7274 Robinson 37
7432 O'Malley 39 Graduates Managers
9824 Darkes 38 Number Surname Age
7432 O'Malley 39
Managers 9824 Darkes 38
Number Surname Age
9797 O'Malley 56
7432 O'Malley 39
9824 Darkes 38
Difference
The difference of two relations R and S is the set
of tuples t such that t R t S, i.e. T = R – S.
Graduates
Number Surname Age
7274 Robinson 37
7432 O'Malley 39 Graduates – Managers
9824 Darkes 38 Number Surname Age
7274 Robinson 37
Managers –
Number Surname Age
9797 O'Malley 56
7432 O'Malley 39
9824 Darkes 38
Properties of Operators
Commutative Property
RS=SR
RS=SR
Associative Property
R (S T) = (R S) T
R (S T) = (R S) T
Set Difference is not Commutative
R–SS–R
12
A meaningful but impossible Union
15
Renaming and Union
Renaming and Union with more attributes
Unary Relational Operations (cont.)
Rename Operation (cont.)
The rename operator is
The general Rename operation can be expressed by any
of the following forms:
S (B1, B2, …, Bn ) (R) is a renamed relationS based on R with
column names B1, B1, …..Bn
S ( R) is a renamed relationS based on R (which does
not specify column names).
18
Select, Project, and Join
Now let us turn to the other three
Relational Algebra Operators
– Select
– Project, and
– Join
Note that Select & Project are
Unary operators.
19
Select
Select operation works on a single relation R
and defines a relation that contains only those
tuples (rows) of R that satisfy the specified
condition (predicate).
21
Examples
1. Select the EMPLOYEE tuples whose department
number is 4: DNO = 4 (EMPLOYEE)
Examples
1. Select the EMPLOYEE tuples whose department
number is 4: DNO = 4 (EMPLOYEE)
Examples
1. Select the EMPLOYEE tuples whose department
number is 4: DNO = 4 (EMPLOYEE)
Examples
2. Select the employee tuples whose salary is greater
than $40,000: SALARY 40,000 (EMPLOYEE)
SELECT Operation Properties
The SELECT operation <selection condition>(R)
produces a relation S that has the same schema
(same attributes) as R
SELECT is commutative:
<condition1>( < condition2> (R)) =
<condition2> ( < condition1> (R))
Because of commutativity property, a cascade
(sequence) of SELECT operations may be
applied in any order:
<cond1>(<cond2> (<cond3> (R)) =
<cond2> (<cond3> (<cond1> ( R))) 26
SELECT Operation Properties
A cascade of SELECT operations may be
replaced by a single selection with a
conjunction of all the conditions:
34
Class Exercise
Use Relational Algebra to specify the
following queries on the COMPANY
dB:
– Retrieve the SSNs of all employees who
work more than 10 hrs/wk on ProductX.
– Find the names of all employees who are
directly supervised by ‘Franklin Wong.’
– Find the names of all employees who earn
either 25k or 45k.
36
Database State for COMPANY
All examples discussed below refer to the COMPANY database shown here.
39