select *
from instructor
where dept_name = 'Physics'| id | name | dept_name | salary |
|---|---|---|---|
| 22222 | Einstein | Physics | 95000 |
| 33456 | Gold | Physics | 87000 |
select *
from instructor
where dept_name = 'Physics'| id | name | dept_name | salary |
|---|---|---|---|
| 22222 | Einstein | Physics | 95000 |
| 33456 | Gold | Physics | 87000 |
\[\sigma_{\texttt{salary > 90000}}(\texttt{instructor})\]
select *
from instructor
where salary > 90000| id | name | dept_name | salary |
|---|---|---|---|
| 22222 | Einstein | Physics | 95000 |
| 83821 | Brandt | Comp. Sci. | 92000 |
\[\sigma_{\texttt{dept\_name = 'Physics'} \wedge \texttt{salary > 90000}}(\texttt{instructor})\]
select *
from instructor
where salary > 90000 and dept_name = 'Physics'| id | name | dept_name | salary |
|---|---|---|---|
| 22222 | Einstein | Physics | 95000 |
\[\sigma_{\texttt{dept\_name = building}}(\texttt{department})\]
select *
from department
where name = building| name | building | budget |
|---|
\[\Pi_{\texttt{ID, name, salary}}(\texttt{instructor})\]
select i.id, i.name, i.salary
from instructor i| id | name | salary |
|---|---|---|
| 10101 | Srinivasan | 65000 |
| 12121 | Wu | 90000 |
| 15151 | Mozart | 40000 |
| 22222 | Einstein | 95000 |
| 32343 | El Said | 60000 |
| 33456 | Gold | 87000 |
| 45565 | Katz | 75000 |
| 58583 | Califieri | 62000 |
| 76543 | Singh | 80000 |
| 76766 | Crick | 72000 |
select id, name, salary / 12 as month_salary
from instructor| id | name | month_salary |
|---|---|---|
| 10101 | Srinivasan | 5416.667 |
| 12121 | Wu | 7500.000 |
| 15151 | Mozart | 3333.333 |
| 22222 | Einstein | 7916.667 |
| 32343 | El Said | 5000.000 |
| 33456 | Gold | 7250.000 |
| 45565 | Katz | 6250.000 |
| 58583 | Califieri | 5166.667 |
| 76543 | Singh | 6666.667 |
| 76766 | Crick | 6000.000 |
\[\Pi_{\texttt{name}}(\sigma_{\texttt{dept\_name = 'Physics'}}(\texttt{instructor}))\]
select name
from instructor
where dept_name = 'Physics'| name |
|---|
| Einstein |
| Gold |
let \(r[R]\) and \(s[S]\). If \(R \cap S = \emptyset\), then \(r \times s\) is simply:
\[(r\times s)[R \cup S] := \{t[R \cup S] \mid t[R] \in r \wedge t[S] \in s\}\]
If \(R \cap S \neq \emptyset\), equally named attributes must be distinguished. Let
\[R \tilde{+} S := R \oplus S \bigcup_{x \in R \cap S}\{R.x, S.x\} \] Then,
\[(r \times s)[R \tilde{+} S] := \{t[R \tilde{+} S] \mid t[(R\setminus S) \cup \bigcup_{x\in R \cap S}\{R.x\}] \in t[R] \wedge t[(S\setminus R) \cup \bigcup_{x\in R \cap S}\{S.x\}] \in t[S] \}\]
Problem when \(r \times r\). We must use rename.
A whole relation can be renamed:
\[\beta_{s}(r)\]
Attributes of a relation can be renamed:
\[\beta_{b_1 \leftarrow a_1, b_2 \leftarrow a_2}(r)\]
Above the attributes \(a_1\) and \(a_2\) of \(r\) are renamed to \(b_1\) and \(b_2\).
Using rename we can perform cross product of a relation with itself:
\[\beta_{s}(r) \times r\]
sql version:
select *
from r, r as sExample illustrating rename:
\[\Pi_{\texttt{i.ID, i.name}}\Bigl( \sigma_{\texttt{i.salary > wu.salary}} (\\\beta_{\texttt{i}}(\texttt{instructor} \times \beta_{\texttt{wu}}(\sigma_{\texttt{id = 12121}}(\texttt{instructor})))) \Bigr)\]
for \(r[R]\) and \(s[S]\) natural join is defined as:
\[r \bowtie s := \{t[R \cup S] \mid t[R] \in r \wedge t[S] \in s\}\]
e.g. \(\texttt{instructor} \bowtie \texttt{teaches}\) gives all information about instructors and courses they teach:
General \(\theta\)-join for a predicate \(\theta\) is defined as:
\[r \bowtie_{\theta} s := \sigma_{\theta}(r \times s)\] join can be expressed in terms \(\theta\)-join with appropriate rename and projection operations.
for relations \(r\) and \(s\) with compatible schemes \(R\) and \(S\) (compatible means same arities and corresponding domains) simply
examples:
\[\begin{align*} &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Fall'}\wedge\texttt{year = 2017}}(\texttt{section})) \cup\\ &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Spring'}\wedge\texttt{year = 2018}}(\texttt{section})) \end{align*}\]
\[\begin{align*} &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Fall'}\wedge\texttt{year = 2017}}(\texttt{section})) \cap\\ &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Spring'}\wedge\texttt{year = 2018}}(\texttt{section})) \end{align*}\]
\[\begin{align*} &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Fall'}\wedge\texttt{year = 2017}}(\texttt{section})) \setminus\\ &\Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Spring'}\wedge\texttt{year = 2018}}(\texttt{section})) \end{align*}\]
For convenience we can name intermediate results of relational algebraic operations, by assigning them variable names:
\[\begin{align*} & \texttt{r} := \Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Fall'}\wedge\texttt{year = 2017}}(\texttt{section})) \\ & \texttt{s} := \Pi_{\texttt{course\_id}}(\sigma_{\texttt{semester = 'Spring'}\wedge\texttt{year = 2018}}(\texttt{section})) \\ & \texttt{r} \cup \texttt{s} \end{align*}\]