Skip to content

Commit

Permalink
Update Thu Jul 1 12:50:19 PDT 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
zedchance committed Jul 1, 2021
1 parent 993a7e3 commit da5bc15
Show file tree
Hide file tree
Showing 630 changed files with 87,742 additions and 2,431 deletions.
5 changes: 0 additions & 5 deletions content/CS134/CS134-lecture-20210624.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,4 @@ WHERE (SELECT COUNT(*)
This is a special case where you can compare the table directly to the interger 2.
- you cannot use a comparison in a `SELECT` statement

List each employee(ssn, fname, lname) who has more than 3 daughters and the salary of the employee is less than 60000.

```sql

```

150 changes: 150 additions & 0 deletions content/CS134/CS134-lecture-20210628.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
title: "CS134-lecture-20210628"
# date: 2021-06-28T13:52:38-07:00
draft: false
bookToc: true
tags: ["SQL"]
---

## SQL cont.

### Aggregate functions cont.

{{< hint danger >}}
Include in weekly assignment.
{{< /hint >}}

List each employee(ssn, fname, lname) who has more than 3 daughters and the salary of the employee is less than 60000.

```sql
SELECT e.ssn, e.fname, e.lname
FROM employee AS e
WHERE e.salary < 60000 AND
(SELECT COUNT(*)
FROM dependent AS dep
WHERE e.ssn=dep.essn AND
dep.relationship='daughter') > 3;
```

### `GROUP BY` clause

![image_2021-06-28-13-58-35](/notes/image_2021-06-28-13-58-35.png)

A clause `GROUP BY` is used to create sub groups.
This allows us to do some operations within a specific sub group.

The attributes being grouped by must appear in the select clause:

```sql
SELECT x, y
-- from ...
GROUP BY x, y
```

For each department, retrieve the department number, the number of employees in the department, and their average salary.

```sql
SELECT dno, COUNT(*), AVG(salary)
FROM employee
GROUP BY dno
```

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

### `HAVING` clause

![image_2021-06-28-14-18-49](/notes/image_2021-06-28-14-18-49.png)

Since the `WHERE` clause comes before the `GROUP BY` clause, if we want to filter the results of the grouping, we can use the `HAVING` clause.
This allows us to filter the results of the grouping by making sure each sub group satisfies the condition after the `HAVING` clause.

- it is okay to have a `GROUP BY` clause without a `HAVING`
- it is **not** okay to have a `HAVING` clause without a `GROUP BY`

For each department which has more than two employees, retrieve the department number, the number of employees in the department, and their average salary.

```sql
SELECT dno, COUNT(*), AVG(salary)
FROM employee
GROUP BY dno
HAVING COUNT(*) > 2;
```

### Summary of SQL queries

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

The optional clauses are surrounded in square brackets `[]`.

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

### In class exercise

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

Retrieve the department name if the lowest employee salary of the department is greater than 50000.
List department name and lowest salary.

```sql
SELECT dname, MIN(salary)
FROM department, employee
WHERE dno=dnumber
GROUP BY dname
HAVING MIN(salary) > 50000;
```

### Another solution to a previous problem

Our previous problem can be solved using our new clauses:

List each employee(ssn, fname, lname) who has more than 3 daughters and the salary of the employee is less than 60000.

```sql
SELECT ssn, fname, lname
FROM employee, department
WHERE ssn=essn AND
relationship='daughter' AND
salary < 60000
GROUP BY ssn, fname, lname
HAVING COUNT(*) > 3;
```

### Returning to earlier problems we skipped

Before we skipped the delete statement with the subquery.

```sql
DELETE FROM employee
WHERE dno IN (SELECT dnumber
FROM department
WHERE dname='Research');
```

We can also use a subquery in an `UPDATE`:

```sql
UPDATE employee
SET salary=salary*1.1
WHERE dno IN (SELECT dnumber
FROM department
WHERE dname='Research');
```

We can use a subquery in an insert statement:

```sql
CREATE TABLE dept_info(
dept_name VARCHAR(10),
no_of_emps INTEGER,
total_sal INTEGER
);

INSERT INTO dept_info (dept_name, no_of_emps, total_sal)
SELECT dname, COUNT(*), SUM(salary)
FROM department, employee
WHERE dnumber=dno
GROUP BY dname;
```

- the result of the subquery will be inserted into the `dept_info` table's values.

206 changes: 206 additions & 0 deletions content/CS134/CS134-lecture-20210629.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
---
title: "CS134-lecture-20210629"
# date: 2021-06-29T17:46:25-07:00
draft: false
bookToc: true
tags: ["SQL", "functional dependency", "normalization"]
---

## SQL cont.

![image_2021-06-29-17-47-40](/notes/image_2021-06-29-17-47-40.png)

Subqueries can be used inside insert statements:

![image_2021-06-29-17-47-46](/notes/image_2021-06-29-17-47-46.png)

This insert statement will update the numbers dynamically based on the response from the subquery.
The next time an employee is inserted, we can run this insert statement to update `DEPTS_INFO` using a trigger.

Or, we can use views...

### Virtual table view

```sql
CREATE VIEW dept_info_view AS
SELECT dname,
COUNT(*) AS no_of_emps,
SUM(salary) AS total_sal
FROM department, employee
WHERE dnumber=dno
GROUP BY dname;
```

This results in a self updating table, called a view:

![image_2021-06-29-18-01-59](/notes/image_2021-06-29-18-01-59.png)

We can query the view the same way we'd query a table.

```sql
SELECT * FROM dept_info_view;
```

The views are maintained by the DBMS.
The views will be automatically updated, to main consistency between the view and the table being viewed.
This is a way we can have derived attributes.

{{< hint info >}}
Note: Views are covered more in depth in CS174.
{{< /hint >}}

## Functional dependencies and normalization

{{< hint info >}}
File: [*Normalization slides*](/notes/134-7.pdf)
{{< /hint >}}

![image_2021-06-29-18-28-58](/notes/image_2021-06-29-18-28-58.png)

Formal measures are also called normal forms.
To understand normal forms, we will start with functional dependency.

### Functional dependencies

![image_2021-06-29-18-32-14](/notes/image_2021-06-29-18-32-14.png)
![image_2021-06-29-18-33-26](/notes/image_2021-06-29-18-33-26.png)

{{<k display>}}
\begin{aligned}
X \to Y
\end{aligned}
{{</k>}}

- {{<k>}} X {{</k>}} and {{<k>}} Y {{</k>}} are sets of attributes

The functional dependency means that if 2 tuples share the same {{<k>}} X {{</k>}} value for an attribute, it means
they also share the same {{<k>}} Y {{</k>}} value for an attribute.

![image_2021-06-29-18-35-24](/notes/image_2021-06-29-18-35-24.png)

When {{<k>}} X {{</k>}} and {{<k>}} Y {{</k>}} are sets of a single item, they may be commonly notated like

{{<k display>}}
\begin{aligned}
\text{ssn} \to \text{ename}
\end{aligned}
{{</k>}}

So if 2 tuples share the same {{<k>}} \text{ssn} {{</k>}}, they will share the same {{<k>}} \text{ename} {{</k>}}.

![image_2021-06-29-18-40-44](/notes/image_2021-06-29-18-40-44.png)
![image_2021-06-29-18-42-38](/notes/image_2021-06-29-18-42-38.png)

### Armstrong's inferences rules

![image_2021-06-29-18-48-05](/notes/image_2021-06-29-18-48-05.png)
![image_2021-06-29-18-49-05](/notes/image_2021-06-29-18-49-05.png)

- For reflexivity, if
{{<k display>}}
\begin{aligned}
\{\text{ssn}\} \subseteq \{\text{ssn,ename}\}
\end{aligned}
{{</k>}}
then
{{<k display>}}
\begin{aligned}
\{\text{ssn,ename}\} \to \text{ssn}
\end{aligned}
{{</k>}}

- For augmentation, if
{{<k display>}}
\begin{aligned}
\text{ssn} \to \text{ename}
\end{aligned}
{{</k>}}
we can add something to both sides, and it still holds
{{<k display>}}
\begin{aligned}
\{\text{ssn, address}\} \to \{\text{ename, address}\}
\end{aligned}
{{</k>}}

- For transitivity,
![image_2021-06-29-18-55-31](/notes/image_2021-06-29-18-55-31.png)

![image_2021-06-29-18-49-18](/notes/image_2021-06-29-18-49-18.png)

- For decomposition, if
{{<k display>}}
\begin{aligned}
\text{ssn} \to \{\text{ename, bdate, address}\}
\end{aligned}
{{</k>}}
then we also know
{{<k display>}}
\begin{aligned}
\text{ssn} &\to \text{ename} \\
\text{ssn} &\to \text{bdate} \\
\text{ssn} &\to \text{address} \\
\text{ssn} &\to \{\text{ename, bdate}\} \\
\text{ssn} &\to \{\text{bdate, address}\} \\
\text{ssn} &\to \{\text{ename, address}\} \\
\end{aligned}
{{</k>}}
- Union is the opposite of the decomposition above
- Pseudotransitivity is the transitivity between decomposed sets above

### Closure

![image_2021-06-29-19-01-31](/notes/image_2021-06-29-19-01-31.png)
![image_2021-06-29-19-02-47](/notes/image_2021-06-29-19-02-47.png)

Each element of the set {{<k>}} F {{</k>}} is a functional dependency.
The set {{<k>}} F^+ {{</k>}} is all the dependencies in {{<k>}} F {{</k>}}, plus all the dependencies
that can be inferred from {{<k>}} F {{</k>}}.

The set {{<k>}} X^+ {{</k>}} is the set of attributes that are functionally deteremined by {{<k>}} X {{</k>}} based on {{<k>}} F {{</k>}}.

![image_2021-06-29-19-06-45](/notes/image_2021-06-29-19-06-45.png)
![image_2021-06-29-19-06-52](/notes/image_2021-06-29-19-06-52.png)

### Equivalence

![image_2021-06-29-19-08-53](/notes/image_2021-06-29-19-08-53.png)
![image_2021-06-29-19-11-32](/notes/image_2021-06-29-19-11-32.png)

### Minimal sets

![image_2021-06-29-19-11-08](/notes/image_2021-06-29-19-11-08.png)
![image_2021-06-29-19-15-34](/notes/image_2021-06-29-19-15-34.png)
![image_2021-06-29-19-12-22](/notes/image_2021-06-29-19-12-22.png)
![image_2021-06-29-19-12-31](/notes/image_2021-06-29-19-12-31.png)

## Normalization

![image_2021-06-29-19-17-18](/notes/image_2021-06-29-19-17-18.png)

There are 4 normal forms we will study: 1NF, 2NF, 3NF, BCNF.
The higher the number, the more strict the form.

![image_2021-06-29-19-18-02](/notes/image_2021-06-29-19-18-02.png)

### Keys and superkeys

![image_2021-06-29-19-23-28](/notes/image_2021-06-29-19-23-28.png)

- any key is a superkey

![image_2021-06-29-19-25-56](/notes/image_2021-06-29-19-25-56.png)

### First normal form

![image_2021-06-29-19-27-02](/notes/image_2021-06-29-19-27-02.png)
![image_2021-06-29-19-29-30](/notes/image_2021-06-29-19-29-30.png)

Since the Research department has a set of locations, it is not in first normal form.
We can put it in first normal form by using decomposition.

![image_2021-06-29-19-32-17](/notes/image_2021-06-29-19-32-17.png)

- The disadvantage to solution 2 is that the department name and manager's ssn is repeated for each location.
- The disadvantage to solution 3 is that any tuple that doesn't have exactly 3 locations will have a lot of null values in the table.
Also the schema would needed to be modified if a department needed 4 locations.

2 changes: 1 addition & 1 deletion content/CS152/CS152-lecture-20210627.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ This is what the Diffie-Hellman exchange is based on.
>
> Given {{<k>}} g,n {{</k>}} and {{<k>}} g^x \mod n {{</k>}}, find {{<k>}} x {{</k>}}.
If {{<k>}} g^x {{</k>}} is large enough, since its wrapping around the ring multiple times, there is no way to hone in on the target like in our binary search technique before.
If {{<k>}} g^x {{</k>}} is large enough it will wrap around the modulus ring multiple times, and there will be no way to hone in on the target like in our binary search technique before.
The modulus {{<k>}} n {{</k>}} is usually thousands of bits long.

### How the exchange works
Expand Down

0 comments on commit da5bc15

Please sign in to comment.