CS134-lecture-20210615

Assignment 1 solution #

image_2021-06-15-16-01-40

Relational algebra cont. #

Intersection #

image_2021-06-15-16-08-51

They must be union compatible.

image_2021-06-15-16-10-08

\[\begin{aligned} a \leftarrow \text{student} \cap \text{instructor} \end{aligned}\]

image_2021-06-15-16-19-18

Include in weekly homework.

image_2021-06-15-16-14-50

Set difference #

image_2021-06-15-16-17-52

Must be union compatible.

image_2021-06-15-16-19-00

The result is a new relation with the same schema.

\[\begin{aligned} a \leftarrow \text{student} - \text{instructor} \end{aligned}\]

image_2021-06-15-16-20-06

\[\begin{aligned} a \leftarrow \text{instructor} - \text{student} \end{aligned}\]

This returns only John Smith.

Common properties #

image_2021-06-15-16-21-39

Cartestian product #

image_2021-06-15-16-22-57

\[\begin{aligned} R \times S \end{aligned}\]

The results from \( R \) and \( S \) are combined.

For example

image_2021-06-15-16-27-16

\[\begin{aligned} Q \leftarrow R \times S \end{aligned}\]
Note: \(R\) and \(S\) are of different schema.

It is the combination of the first row of \( R \) with each row of \( S \) ,

image_2021-06-15-16-30-03

and it continues

image_2021-06-15-16-30-42

For our amount of tuples (exhaustive combination):

\[\begin{aligned} n_R = 3 \\ n_S = 2 \\ 3 \times 2 = 6 \end{aligned}\]

Another example

image_2021-06-15-16-33-02

Use our given employee/department/etc schema for this problem.

image_2021-06-10-16-56-31

\[\begin{aligned} \text{female emp} \leftarrow \sigma_\text{sex = 'F'} (\text{ employee }) \end{aligned}\]

image_2021-06-15-16-49-40

\[\begin{aligned} \text{emp names} \leftarrow \pi_\text{fname, lname, ssn}(\text{ female emp }) \end{aligned}\]

image_2021-06-15-16-50-52

\[\begin{aligned} \text{emp dependents} \leftarrow \text{emp names} \times \text{dependent} \end{aligned}\]

image_2021-06-15-16-52-45

Many of these combinations are meaningless right now. The actual employee/dependent combinations are when ssn == essn, so we need to do a selection

\[\begin{aligned} \text{actual dependents} \leftarrow \sigma_\text{ssn=essn}(\text{ emp dependents }) \end{aligned}\]

Finally, we can project to get the done result

\[\begin{aligned} \text{result} \leftarrow \pi_\text{fname, lname, dependent name}(\text{ actual dependents }) \end{aligned}\]

image_2021-06-15-16-55-11

JOIN operation #

image_2021-06-15-16-59-12

\[\begin{aligned} R \underset{\text{condition}}{\bowtie} S \end{aligned}\]

It is the combination of a cartesian product followed by a selection.

So, looking back at our previous example

\[\begin{aligned} \text{actual dependents} \leftarrow \text{emp names} \underset{\text{ssn=essn}}{\bowtie} \text{dependents} \end{aligned}\]

It starts on the first employee, and combines with the first dependent. But we do not output to a intermediary table. It then checks if the condition is true, otherwise it ignores the tuple. It follows this same process and builds the result table.

image_2021-06-15-17-14-17

\[\begin{aligned} R \underset{\text{condition}}{\bowtie} S = Q(A_1, A_2, \ldots, A_n, B_1, B_2, \ldots, B_m) \end{aligned}\]

Conditions

  • have comparison
  • can be combined using and/or

image_2021-06-15-17-20-47

When we only have 1 condition with an equal size, it is called an equijoin, for example

\[\begin{aligned} \text{dept manager} \leftarrow \text{department} \underset{\text{mgrssn=ssn}}{\bowtie} \text{employee} \end{aligned}\]

An equijoin is guaranteed to have one or more pairs of attributes that have identical values (despite possibly not being named the same thing). If we throw out the duplicates, it is called a natural join.

image_2021-06-15-17-27-09

\[\begin{aligned} \text{dept locs} \leftarrow \text{department} * \text{dept locations} \end{aligned}\]

When the natural join is performed, any attributes with duplicate names are removed.

You could not perform this same join operation with the standard join operation \[ \underset{\text{dnumber=dnumber}}{\bowtie} \] because there is ambiguity.

Note on notation: the asterisk \(*\) is used to indicate natural join.