Relational algebra cont. #
Natural join cont #
Our natural join results in this table
Common mistakes when using join #
Its common to misuse join/natural join. Consider this example, if we have 2 entities, \( R \) and \( S \) :
\[\begin{aligned} R(a, b) && S(b, d, f) \end{aligned}\]If we want to join on \( b \) , we can do the natural join:
\[\begin{aligned} R * S \end{aligned}\]While the incorrect answer is
\[\begin{aligned} \text{result} \leftarrow R \underset{\text{b=b}}{\bowtie} S \end{aligned}\]because of ambiguity. Also, result will be a schema including 2 columns of \( b \) .
If we wanted to get rid of \( f \) in our joined table, we could start with a project
\[\begin{aligned} \text{new s} (\text{newb, d}) \leftarrow \pi_\text{b, d}(S) \end{aligned}\]and then get the result with a join, because the attribute names are different now:
\[\begin{aligned} \text{result} \leftarrow R \underset{\text{b=newb}}{\bowtie} \text{new s} \end{aligned}\]Join selectivity #
Complete set of relational operations #
Example queries #
Include following exercise in weekly homework
Question: For every project located in “stafford”, list the project number, the controlling department number, and the department managers’s last name, address, and birthdate.
Note:employee
andemps with deps
must be union compatible to take the difference.
Another exercise with the same schema
Include following exercise in weekly homework
List the names (first name, last name) of managers who have at least one dependent.
First approach uses set operations:
\[\begin{aligned} \text{mgr(ssn)} &\leftarrow \pi_\text{mgrssn} (\text{ department }) \\ \text{emp with dep(ssn)} &\leftarrow \pi_\text{essn} (\text{ dependent }) \\ \text{mgr with deps} &\leftarrow \text{mgr} \cap \text{emp with deps} \\ \text{result} &\leftarrow \pi_\text{lname, fname} (\text{mgr with deps} * \text{employee}) \end{aligned}\]Another approach using join:
\[\begin{aligned} \text{mgr dep} &\leftarrow \pi_\text{essn} (\text{department} \underset{\text{mgrssn=essn}}{\bowtie} \text{dependent}) \\ \text{emp mgr dep} &\leftarrow \text{employee} \underset{\text{ssn=essn}}{\bowtie} \text{mgr dep} \\ \text{result} &\leftarrow \pi_\text{fname, lname} (\text{ emp mgr dep }) \end{aligned}\]