CS134-lecture-20210609

Relational database schemas #

image_2021-06-09-16-36-39

IC = integrity constraints

Recall, a state is a collection of all the instances.

Example of schema and state #

image_2021-06-09-16-40-35 image_2021-06-09-16-42-12

Valid/invalid state #

image_2021-06-09-16-43-40

Update operations on relations #

image_2021-06-09-16-44-48

Whenever we do any of these update operations, there is a chance that the constraints may be violated. These violations may propagate to other tables.

image_2021-06-09-16-46-29

REJECT is the most common action taken. CASCADE is also common, but requires setup in DBMS.

image_2021-06-09-16-50-43

Respective examples of these are

  • Domain constraints are common from type mismatches
  • Key constraints happen if you try to insert another tuple with an already used key
  • Null values where they aren’t allowed
  • Null values in a primary key slot
  • References to things that don’t exist

image_2021-06-09-16-56-43 image_2021-06-09-16-57-00

Note: We will study these last 2 slides more later.

ER-to-relational model mapping #

File: Ch 3 Slides

This chapter is about going from our ER models to the relational model.

image_2021-06-09-17-01-15

Entities #

image_2021-06-09-17-01-24

For example, consider this ER-diagram

image_2021-06-09-17-04-44

This would be

employee(_ssn, salary, fname, minit, lname)

as a relational model. We only use the composite attribute name in the conceptual level for semantics.

Note: There is only one candidate key: ssn. I indicate the key with a leading underscore.

At this point, it is not case sensitive because we are still at the design level.

Consider this ER-diagram

image_2021-06-09-17-14-45

This would be

classroom(_building, _roomNo, capacity)

as a relational model.

Note on notation: I prepend an underscore on both building and roomNo to indicate they are a composite key. Normally they would both be underlined.

Recall only 1 primary key is allowed per relation.

Relationships #

image_2021-06-09-17-17-55

Consider this employee ER-diagram,

image_2021-06-09-17-25-40

We will choose the entity with total participation to hold the foreign key,

employee(                   _ssn)
                            ^
department(dname, _dnumber, mgrssn)
Note on notation: The foreign key mgrssn should have an arrow drawn to ssn on employee. I indicate the arrow using the carrot ^.

If the relationship itself has a simple attribute, ie

image_2021-06-09-17-29-35

then the attribute mgrstartdate should go in department (the entity with the foreign key).

employee(                   _ssn)
                            ^
department(dname, _dnumber, mgrssn, mgrstartdate)

image_2021-06-09-17-18-16

The entity at the many side must include the foreign key to the other.

Consider

image_2021-06-09-17-36-40

employee(_ssn, salary, dno)
                       v
department(dname,      _dnumber)

The foreign key dno references to the primary key dnumber.

If the relationship works_for has an attribute, ie

image_2021-06-09-17-38-46

Then that attribute should also be where the foreign key is, ie

employee(_ssn, salary, dno, empstartdate)
                       v
department(dname,      _dnumber)

The reason we don’t have the department reference to the employee is because we will have many employees, and the department only has 1 foreign key.

image_2021-06-09-17-18-21 image_2021-06-09-17-46-21 image_2021-06-09-17-46-26

image_2021-06-09-17-18-25 image_2021-06-09-17-54-59 image_2021-06-09-17-55-49

The reason we don’t have employee have the foreign key for a project is because then it wouldn’t reflect the many-to-many relationship (an employee could only work on 1 project).

Weak entities #

image_2021-06-09-17-17-36