IdNameAgeRating
4312Alex433.4
3245Jamey234.1
5723Lola92
5435Charlie724
5674J.J Jameson605
^S

=

IdNameAgeRating
3453Fiona250.5
3522Ezekiel884.5
5435Charlie724
4312Alex433.4
^T

Selection

The selection operation is a unary operation that uses a selection criteria to essentially filter entries from a table/instance based on their fields/attributes. It uses the symbol sigma

Note that the selection criteria is a boolean expression which can either involve fields or constants.

The selection operation allows changing the cardinality (number of rows) of the input instance, without affecting the degree (number of columns).

Projection

The projection operation is a unary operation that allows tuples containing only specific fields to be obtained. Any other fields are ‘projected’ out. The symbol pi is used for projection.

Duplicate Elimination

Relational algebra treats all relations as sets, so if we have any fields with duplicate values, they essentially ‘disappear’ from the final output. In real life, duplicate elimination is very expensive, and some software allow the concept of Multiset. However, relational algebra assumes sets i.e. no duplicate values. See query optimisation of projection to see how a real-time software eliminates duplicates, if needed.

The projection operation allows changing the degree of the input instance, without affecting the cardinality (assuming unique records).

Set Operations

Most of the common Set Operations apply to relational instances.

Union-Compatibility

Two instances are union-compatible if they have the same number of fields and the domains (datatype).

Union

%%🖋 Edit in Excalidraw, and the dark exported image%%

  • Set union works the same way as normal sets, showing records that exist in either or .
  • Only works if both instances are union-compatible
  • Only affects cardinality, degree is unchanged
  • Schema is the same as that of
IdNameAgeRating
4312Alex433.4
3245Jamey234.1
5723Lola92
5435Charlie724
5674J.J Jameson605
3453Fiona250.5
3522Ezekiel884.5

*Note how duplicate entries Alex & Charlie only appear once!

Set Difference

|200 %%🖋 Edit in Excalidraw, and the dark exported image%%

  • Set difference has the same procedure as normal sets.
  • Only works if both instances are union-compatible
  • It is not symmetrical.
  • Only affects cardinality, degree is unchanged
  • Schema is the same as that of
Intersection

%%🖋 Edit in Excalidraw, and the dark exported image%%

  • In relational algebra, defined as a compound operator of two set differences.
  • Only affects cardinality, degree is unchanged
  • Requires both instances to be union-compatible
Cartesian Product (Cross Product)
  • Returns the cartesian product of both instances, i.e. every possible tuple of each row
  • Output has every record having fields from both sets
  • Cardinality is given by
  • Resulting schema has fields from both sets
    • Naming conflicts may arise if and have the same names for fields
    • Need to use the renaming operation to handle such conflicts.

Renaming

To avoid name conflicts where fields contain the same name, the renaming operator can be used. The syntax is as follows:

  • is the original instance/tuple which we want to rename
  • is the resultant instance/tuple which copies the same schema and values as , and **then has the rename mapping, applied
  • is the mapping function, which takes in renames of the forms:
    • where is the index of the field (STARTING FROM 1)

For example:

Renames the ‘Name’(index 1) field to ‘Last Name’, and the ‘Age’ field to ‘Years’

Joins

A more complicated operation (which is a compound operation) which allows the ‘combination’ of tables are called joins.

Natural Join
  • The natural join operation returns a joined table of tables and only if there are any attribute names and values in common between them. The process of a natural join is:
  1. Apply a cross product:
  2. Select any records where common attribute names and values exist:
    1. The selection criteria would be: where field in A has the same name as field in B
  3. Project only unique attributes, and one copy of any duplicate attributes
    1. This ensures we don’t have naming conflicts, and every attribute only shows up once in the resultant table
  • If the natural join exists, the degree is given by the number of unique attributes
Condition Join (Theta-Join)
  • A type of join that specifies one or more conditions () for joining. The conditions can be equalities or inequalities involving attributes of the relation, similar to the conditions applied in selection operation.
  • Equivalent to applying a selection over the cross product of the instances
  • Resultant schema is same as that of the cross product
Equi-Join
  • Similar to Condition Join, but the conditions can only be equalities involving attributes of the relation.
  • Equivalent to applying a selection over the cross product of the instances
  • Resultant schema is same as that of the cross product

Equivalences

The selection operation has the following properties:

  • Cascading:
  • Commutative:

The projection operation has the following properties:

  • Cascading on subsets: . Only when the ‘outer’ operator operates with columns that are a subset of inner operation.

Projection and selection commute only if the selection acts on attributes returned from the projection:

Join equivalences allow us to change the order of join operations:

  • Associative:
  • Commutative: