Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Relational Algebra Operators:

– Union
• adds tuples from one relation to another relation to form a third relation
• A UNION B or A ∪ B

– Difference
• produces a third relation that contains the tuples that appear in the first
relation, but not the second
• A DIFFERENCE B or A – B

– Intersection
• will produce a third relation that contains the tuples (rows) that are
common to the relations involved.
• This is similar to the logical operator ‘AND’
• A INTERSECT B or A ∩ B

Remember: union, difference and intersection must be union compatible

– Product
• is a concatenation of every tuple in one relation with every tuple in a
second relation (Cartesian product)
• The resulting relation will have x times y tuples, where…
x = the number of tuples in the first relation and
y = the number of tuples in the second relation
• A PRODUCT B or A TIMES B or A x B

– Projection
• produces a second relation that is a subset of the first.
• The subset is in terms of attributes (i.e. columns).
• A[a,b,c] or PROJECT a,b,c (A)
• or πa,b,c (A)

– Selection (Restriction)
• is similar to the projection operator. It produces a second relation that
is a subset of the first using a condition.
• produces a subset of tuples (rows).
• A WHERE store > 24 or RESTRICT A WHERE store > 24
• or σstore>24 (A)

– Join
• is a combination of the product, selection, and projection operators.
• There are several variations of the join operator…Equi-join, Natural join
and Theta-join.
• It is best practice to use equi-join (and state the join condition) as in the
example given:
• A JOIN (StaffNo = StaffId) B or A ⋈staffNo=staffId
staffNo=staffId B
• Or use Natural Join only when attribute names match,
• i.e. A * B or A NATURAL JOIN B

- Rename
• renames attributes
• applies to the current operation only, i.e. the original relation retains the
original attribute names.
• A RENAME (StaffNo = NewStaffNumber, ...)
• Or ρstaffno/newstaffnumber(A)

Merging Relational Algebra Operations


If you need to merge operators then you use nested brackets, for example:

• (A UNION B) [x,y]
o will perform a union, then a projection of the result

• ((A DIFFERENCE B) WHERE z > 10) [z, w]


o will perform a DIFFERENCE, then a restriction on the result, and
finally project

If you need to assign an operation to a result, then you use the following
syntax:

• RESULT ← ( (A DIFFERENCE B) WHERE z > 10) [x, y, z]

Will create a new relation ‘RESULT’ which you can use in further queries

You might also like