CS134-lecture-20210610

Relational model cont. #

Weak entity #

image_2021-06-10-16-10-52 image_2021-06-10-16-12-22

Notice that the employee will be the owner of the depending weak entity. The primary key will be part of the foreign key and the weak attribute.

image_2021-06-10-16-15-35

Remember,

  • primary keys are underlined
  • foreign keys have arrows pointing to the other reference

Multivalues attributes #

image_2021-06-10-16-19-07

Consider this ER-diagram,

image_2021-06-10-16-23-45

We can handle this in our relational model by creating another table called dept_locations,

image_2021-06-10-16-27-37

Note that the primary key of dept_locations is the combination of both dnumber and dlocation.

A bigger example #

Consider

image_2021-06-10-16-30-13

Its good to start at a strong entity, so we’ll start with employee

image_2021-06-10-16-33-42

Then we’ll tackle the recursive supervision relationship, by creating a foreign key superssn that references to the ssn,

image_2021-06-10-16-34-29

Next we’ll do department

  • we need to make dept_location to handle the multivalued attribute
  • to handle the derived attribute we can either
    1. use a trigger to handle the derived attribute (like number_of_employees), (taught in more depth in CS174)
    2. Ignore the derived attributes for now, which is what we’ll do

image_2021-06-10-16-40-22

Now that we have both of those entities setup, we’re ready to do the 2 relationships works_for and manages,

image_2021-06-10-16-43-23

Note: The colors don’t have any meanings in relational models, just a preference from the Professor.

Next we need to do the entity project before we can do the relationship controls

image_2021-06-10-16-44-52

Note: The order of your attributes don’t have any meaning, you can list pnumber first if preferable.

We need a foreign key in project to note which department controls it

image_2021-06-10-16-46-51

Next we can add the many-to-many relationship works_on with a new table

image_2021-06-10-16-49-36

  • we added the primary key of works_on to be both essn and pno

We can now tackle the weak entity dependent

image_2021-06-10-16-51-46

  • we added a foreign key essn to reference to the employee
  • primary key is essn and d_name

Overall,

image_2021-06-10-16-56-31

University ER exercise #

Consider this ER-diagram

image_2021-06-10-17-00-58 image_2021-06-10-17-03-59 image_2021-06-10-17-05-00 image_2021-06-10-17-06-52 image_2021-06-10-17-08-52

Note: This is part of the weekly homework.

Relational algebra #

File: Relational algebra slides

Studying relational algebra will make our SQL skills better. It also gives a deeper understanding on how queries are processed. It is low level algebra presentation of a high level query.

image_2021-06-10-17-13-43

Recall that relational model is set based and bag based, so we don’t allow duplicates. It enables users to specify basic retrieval requests.

image_2021-06-10-17-18-17

SELECT #

image_2021-06-10-17-18-31

For example if we’re looking for the employees from department number 5,

\[\begin{aligned} \text{result } \leftarrow \sigma_{\text{dno} = 5}(\text{ employee }) \end{aligned}\]

and the result it stored.

Consider that result for this table

image_2021-06-10-17-21-42

The result emp_5 will be a new table (though not physically saved on the disk, just as a return result). For select, the structure of the new relation is the same as the input (this is not true for all operators).

image_2021-06-10-17-24-47

So for each given tuple in the input, we are looking to see if it satisfies the condition dno = 5,

image_2021-06-10-17-27-10

The result is a subset of the tuples.

There can be many conditions in each select operation. Multiple conditions can be put together with boolean operations.

For example if we want the department of the employee to be number 5, with salary > 30000

\[\begin{aligned} \text{result } \leftarrow \sigma_{\text{dno} = 5 \text{ and salary } > 30000}(\text{ employee }) \end{aligned}\]

image_2021-06-10-17-30-43

2 of the employees are returned (in blue).

We can do more complex selections, ie

\[\begin{aligned} \text{result } \leftarrow \sigma_{(\text{dno} = 5 \text{ and salary } > 30000) \text{ or } (\text{ dno} = 4 \text{ and salary } > 25000)}(\text{ employee }) \end{aligned}\]

image_2021-06-10-17-32-35