Skip to content

Commit

Permalink
Update Tue Jun 15 17:47:50 PDT 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
zedchance committed Jun 16, 2021
1 parent b7cb801 commit f5d8295
Show file tree
Hide file tree
Showing 603 changed files with 62,032 additions and 1,829 deletions.
188 changes: 188 additions & 0 deletions content/CS134/CS134-lecture-20210614.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
title: "CS134-lecture-20210614"
# date: 2021-06-14T13:45:19-07:00
draft: false
bookToc: true
tags: ["relational algebra"]
---

{{< hint warning >}}
Announcements

- Assignment 2 is posted
{{< /hint >}}


## Relational algebra cont.

### `SELECT` cont.

![image_2021-06-14-13-59-11](/notes/image_2021-06-14-13-59-11.png)

When cascaded: the conditions can be in different order but they will produce the same output.
The conditions can be connected by logical and.

If we compose 2 select operations

{{<k display>}}
\begin{aligned}
\sigma_{\text{salary } > 7300} ( \sigma_{\text{dno = } 5 } (\text{ employee }))
\end{aligned}
{{</k>}}

We can combine the 2 conditions with a logical and

{{<k display>}}
\begin{aligned}
\sigma_{\text{salary > 7300} \text{ and } \text{ dno } = 5 } (\text{ employee })
\end{aligned}
{{</k>}}

Selection is a filter, so the input schema is the same as the output schema, however the number of tuples is equal to or less than the input.

### Exercise

{{< hint danger >}}
This is part of the weekly exercise homework.
{{< /hint >}}

![schema](/notes/image_2021-06-10-16-56-31.png)
![image_2021-06-14-14-10-10](/notes/image_2021-06-14-14-10-10.png)

1. Retrieve each female employee
{{<k display>}}
\begin{aligned}
\text{result} \leftarrow \sigma_{\text{ sex = 'F' }} ( \text{ employee })
\end{aligned}
{{</k>}}

2. Retrieve each female employee whose salary is greater than 30000
{{<k display>}}
\begin{aligned}
\text{result} \leftarrow \sigma_{\text{ sex = 'F' and salary > 30000}} ( \text{ employee })
\end{aligned}
{{</k>}}

### Projection operation

![image_2021-06-14-14-17-03](/notes/image_2021-06-14-14-17-03.png)

In the projection operation, the input schema can be different than the output schema.
It allows you to select certain columns and discard the other ones.

Projection is notated using {{<k>}} \pi {{</k>}}, for example

{{<k display>}}
\begin{aligned}
\pi_{\text{<list>}}(R)
\end{aligned}
{{</k>}}

Where
- {{<k>}} \text{list} {{</k>}} is a list of attributes, and
- {{<k>}} R {{</k>}} is the entity

For example,

{{<k display>}}
\begin{aligned}
\text{result} \leftarrow \pi_\text{<fname, lname, ssn>} (\text{ employee })
\end{aligned}
{{</k>}}

Note that the output schema only has 3 columns.

![image_2021-06-14-14-22-40](/notes/image_2021-06-14-14-22-40.png)
![image_2021-06-14-14-24-28](/notes/image_2021-06-14-14-24-28.png)

Consider

{{<k display>}}
\begin{aligned}
\text{result} \leftarrow \pi_\text{<salary>} (\text{ employee })
\end{aligned}
{{</k>}}

Our result will only have 1 columns.

![image_2021-06-14-14-27-35](/notes/image_2021-06-14-14-27-35.png)

We have 8 tuples, but 3 of them are 25000 (duplicates).
By definition, relational model is set based (and so relational algebra is too), so we do not allow duplicate tuples.
Any duplicate tuples will be removed in the returned table.

![image_2021-06-14-14-33-42](/notes/image_2021-06-14-14-33-42.png)

The attributes in the list must be a subset of the attributes of the entity.

The last projection is what really matters,

{{<k display>}}
\begin{aligned}
\text{result} &\leftarrow \pi_\text{<fname>}(\pi_\text{<ssn, fname>} (\text{ employee })) \\
&= \pi_\text{<fname>} (\text{ employee })
\end{aligned}
{{</k>}}

### Sequence of operations

If we want to compose selection and projection:

![image_2021-06-14-14-36-55](/notes/image_2021-06-14-14-36-55.png)

{{<k display>}}
\begin{aligned}
\text{result} &\leftarrow \pi_\text{<fname, lname, salary>} (\sigma_\text{dno = 5} (\text{ employee })) \\
\text{temp} &\leftarrow \sigma_\text{dno = 5} (\text{ employee }) \\
\text{result} &\leftarrow \pi_\text{<fname, lname, salary>} (\text{ temp })
\end{aligned}
{{</k>}}

### Rename operation

![image_2021-06-14-14-43-15](/notes/image_2021-06-14-14-43-15.png)

We can use this operator to rename the output of an expression.

![image_2021-06-14-14-44-32](/notes/image_2021-06-14-14-44-32.png)

If {{<k>}} R {{</k>}} is a relation, we can rename the relation to {{<k>}} S {{</k>}}, and/or rename the attributes name one by one.

{{<k display>}}
\begin{aligned}
&\text{R} \leftarrow \pi_\text{<fname, lname, salary>} (\text{ employee }) \\
& \rho_\text{S (fn, ln, sal)} (\text{R}) &\text{new relation named S}\\
& \rho_\text{(fn, ln, sal)} (\text{R}) &\text{new relation still named R}\\
& \rho_\text{S}(\text{R}) &\text{new relation named S}\\
&&\text{ but no new attribute names}
\end{aligned}
{{</k>}}

### Union operator

![image_2021-06-14-15-03-56](/notes/image_2021-06-14-15-03-56.png)

![image_2021-06-14-15-04-39](/notes/image_2021-06-14-15-04-39.png)

No duplicates allowed, it is a set.

For example,

![image_2021-06-14-15-04-05](/notes/image_2021-06-14-15-04-05.png)
![image_2021-06-14-15-06-13](/notes/image_2021-06-14-15-06-13.png)

![image_2021-06-14-15-04-14](/notes/image_2021-06-14-15-04-14.png)

Example

![image_2021-06-14-15-15-01](/notes/image_2021-06-14-15-15-01.png)

{{<k display>}}
\begin{aligned}
\text{dept5 emps} &\leftarrow \sigma_\text{dno = 5} (\text{ employee }) \\
\text{result1} &\leftarrow \pi_\text{ssn} (\text{dept5 emps}) \\
\text{result2}\underbrace{(\text{ssn})}_\text{rename} &\leftarrow \pi_\text{superssn} (\text{dept5 emps}) \\
\text{result} &\leftarrow \text{result1} \cup \text{result2}
\end{aligned}
{{</k>}}

0 comments on commit f5d8295

Please sign in to comment.