You are on page 1of 16

Relational algebra

Functional aspect of relational


model
Reminder
1. Use this form to confirm your presence at a lecture
2. Today will be a test
Operators
Operations
Name Function Examples

Select (σ) Filtering tuples in relation to a σyear > 2016 (Person)


given condition (predicate):
σid = 20 or pages>100 (Book)
σ predicate (R), R – relation.
σmark = ‘A’ and stud_id=1 (Mark)
Example
Приклад: σyear > 2016 (Person)

Person
id name year

1 P1 2010

2 P2 2019

3 P3 2020
Operations
Projection (∏) Selection of relationship attributes ∏email, username (User)
and removal of duplicate tuples:
∏name (Book)
Πattr1, . . . , attrn(R), де attr1..attrn –
attributes being selected, R – ∏title, comment (Post)
relation.
Example
id1 id2

Example: ∏name (Person) 1 1 id1

1 2 1
Person
id name year

1 P1 2010

2 P2 2019

3 P3 2020
Operations

Union (∪) Tuples union of two relations, User U Person


compatible in number and types of
corresponding attributes (domains): ∏city (City) U ∏city (Capital)

R ∪ S, where R and S – relations.


Operations

Difference or Difference of tuples of relations R User – Student


Minus and S: such tuples from R which are
not present in S. User - σonline (User)
(-)
R-S
Operations

Cartesian Concatenation of each tuple of R User X User


product (X) with each tuple of S
User X Student X Book
RXS
Operations

Rename (ρ) Rename a relation or its attribute: ρ(School, University)

ρ (new name, old name) ρ(Attr, Attribute)


Operations

Natural Join A JOIN is a derivative operation of a GoodType ⋈ Good


(⋈) Cartesian product combined with a
SELECTION operation. A natural Group ⋈ Student
JOIN uses a selection with the
condition that the values common to
the attribute and domain are equal.

R⋈S
Operations
Intersection Selection of identical tuples of two Part in warehouse ∩
(∩) relations, compatible in number and
types of corresponding attributes Part in shop
(domains):

R ∩ S, where R and S – relations. Todo_Items on Monday ∩


Can be expressed through basic Todo_Items on Tuesday
operations:

R ∩ S = R – (R – S)
Operations
Division (÷) For the relations R (A, B) and S (B)
returns such tuples from A that for
each b from B there is a tuple (a, b)
in R. If RXS = T, then T ÷ R = S and
T ÷ S = R.

Rules are:

1) T1=∏A (R);

2) T2=T1 X S;

3) T3= T2 – R;

4) T = T1 - ∏A (T3)
Division example
R S

Student Tests Tests

S1 KR1 KR1 Student


➗ =
S1 KR2 KR2 S1

S1 LR1 LR1

S2 KR1
Find students that have
S2 KR2 done all of the tests
Examples
The Relational Algebra examples are here

You might also like