Link Search Menu Expand Document

Save this note as PDF

Introduction

So far, we’ve learned how to use already built databases: SQL queries! We also learned the internals of a database management system which includes many abstraction levels. But what happens if we are given a high-level description of what data we want to store with our database? How do we design a database to fit out needs? We will discuss these question in this module!

Entity-Relationship Models

When designing a database, we often use Entity-Relationship Models (aka "E-R" models). These models have 2 main components. The first main component is an entity: a real-world object described by a set of attribute values. An entity is usually depicted as a rectangle in our E-R model. In addition, the attributes/fields associated with the entity are depicted as ovals. For example, the following figure shows an employee as an entity in our E-R model with the following fields: a SSN number, a name, and a lot. Note that the attribute "ssn" is underlined because it is the identifying attribute for this entity! In a schema, this would be the primary key.

image

The second main component is a relationship: association among two or more entities. A relationship is usually depicted as a diamond in our E-R model. Again, the attributes associated with the relationship are depicted as ovals. For example, the following figure shows "works in" as a relationship between the entities employee and department. The "works in" relationship has the attribute "since."

image

Note that the same entity set can participate in different relationship sets, or in different “roles” in the same relationship set.

Relationship Constraints

We have connected our entities to relationships with a thin black line up until now. This line means denotes a many-to-many relationship. This means each entity can participate in 0 or more times in the relationship and vice versa. For example, for the figure above, this means that many employees can work in many departments (0 or more) and departments can have many employees (0 or more). In contrast, we want our model to reflect that each department has at most one manager. We do this by using a key constraint, which denotes an at most one, or 1-to-many relationship. The key constraint is depicted by a thin arrow. It is important to note the direction of the arrow, which points from the entity with 0 or 1, towards the entity with many. For example, for the following figure, this means that each department has at most one manager (0 or 1) while employees can be managers for many (0 or more) departments.

image

What if we want our model to show that each employee works in at least one department? We do this by using a participation constraint, which denotes at least one relationship. The participation constraint is depicted by a thick line. For example, in the following figure, we added two thick lines. One of them denotes that each employee works in at least one department. The other denotes that each department has at least one employee.

image

In addition, you can see we added a thick arrow pointing from departments to manages. This is both a key and participation constraint: each department has exactly one manager. A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entities and weak entities must participate in a one-to-many relationship (one owner, many weak entities). The weak entity must have total participation in this identifying relationship set. We depict a weak entity by bolding the rectangle and relationship as shown below. In addition, weak entities have only a “partial key” (dashed underline). In the example below, we say that every dependent is uniquely identified by their pname and an employees’ ssn and that each employee has 0 or more dependents attached to them.

image

Now that we have our E-R Model drawn out, how do we actually organize it into relations? We can translate each entity and relationship to its own table and have primary keys and foreign keys depending on the relationship between tables. In general, there are many ways to set up relations to represent the same E-R model.

FDs and Normalization

Now that we have set up our database, we want to optimize! One thing we want to avoid is redundancy in our schema. This is wasted storage and also leads to insert/delete/update anomalies. Our solution to this problem is going to be Functional Dependencies. A functional dependency \(X \xrightarrow{} Y\) means that the \(X\) column determines \(Y\) column in a table \(R\). This means given any two tuples in table \(R\), if their \(X\) values are the same, then their \(Y\) values must be the same (but not vice versa). Let’s look at a couple of examples to better understand functional dependencies and what "determines" means. In the example below, two of the rows have the same X value (\(1\)) but different Y values (\(5\) and \(6\)), so we cannot say that X determines Y:

X Y
1 5
1 6
2 1

For the following example, we see that all rows with the same X value have the same Y value. More specifically, all the rows with \(X=1\) have \(Y=2\), all the rows with \(X=2\) have \(Y=4\), and all the rows with \(X=3\) have \(Y=4\). In this case, it is possible that X determines Y (aka \(X \xrightarrow{} Y\)):

X Y
1 2
1 2
1 2
2 4
3 4

Note that in the above example, even though \(Y=4\) for two rows, their X values do not have to be the same to say that X determines Y. We only care if the Y values for a particular X value are the same, not vice versa! Primary keys are special cases of FDs. A superkey1 is a set of columns that determine all the columns in the table. If \(X\) is a superkey of table \(R\), then \(X \xrightarrow[]{} R\).

A candidate key is a set of columns that determine all the columns in the table such that if we remove any of the columns in a candidate key, the resulting set will not be a superkey of the relation. You can think of it as the minimum set of columns that is a superkey for the table. If \(X\) is a candidate key of \(R\), then \(X \xrightarrow[]{} R\) and for every strict subset \(Y \subset X\), \(Y \nrightarrow{} R\).

For example, if columns \(K, L\) are the columns in a table and \(K\) is a primary key of the table (aka column \(K\) alone determines all the columns in the table) then \(KL\) is superkey and \(K\) is a superkey and a candidate key.
We will decompose our relation to avoid redundancies by using functional dependencies. But first, we define the closure of \(F\), denoted as \(F+\), to be the set of all FDs that are implied by \(F\). In broader terms, a closure is the full set of table relationships that can be determined from known FDs.

Let’s take a look at a couple of examples to better understand these definitions. Given a relation \(R\) with columns \(\{A,B,C,D,E\}\) and functional dependencies \(F=\{B \xrightarrow{} CD, D \xrightarrow{} E, B \xrightarrow{} A, E \xrightarrow{} C, AD \xrightarrow{} B\}\), let’s examine the following questions:

  • Is \(B \xrightarrow{} E\) in \(F+\)? Yes! To see why, we will evaluate the closure of \(B\). \(B+ = \{ B, C, D, E, A\}\), we get this because \(B\) determines \(CD\) from the first functional dependency from the set \(F\). Then we see that \(D\) determines \(E\) so \(E\) is in the closure of \(B\). Thus, from the given implications in F, we see that \(B \xrightarrow{} E\), so the answer is YES.

  • Is \(D\) a superkey for relation \(R\)? We will determine the answer to this by evaluating the closure of \(D\). \(D+ = \{ D, E, C\}\). We get this because \(D\) determines itself. From the second FD in \(F\), we see that \(D\) determines \(E\). Then we see that \(E\) determines \(C\). Since \(D+\) is not equal to all the columns of the relation \(R\), we know that \(D\) is NOT a superkey for \(R\) because knowing \(D\) does not determine all columns in \(R\). The answer is NO.

  • Is \(AD\) a superkey for \(R\)? We again evaluate the closure of \(AD+\). Using the same procedure as the last two, we get that \(AD+ = \{ A,D,E,C,B\}\). Therefore, the answer is YES.

  • Is \(AD\) a candidate key for \(R\)? We must see if \(AD\) is the minimum subset to imply \(R\) by checking the closure of each subset. So we check \(A+ = \{A\}\) and \(D+ = \{ D,E,C\}\). Since none of them are \(R\) and that they together make up all columns of \(R\), we know that \(AD\) is a candidate key! The answer is YES.

  • Is \(ADE\) a candidate key for \(R\)? We know that \(AD\) determines all the columns for \(R\) already so \(ADE\) is NOT a candidate key. The answer is NO.

BCNF Decomposition

We will now decompose the relation into Boyce-Codd Normal Form (BCNF). Relation \(R\) with FDs \(F\) is in BCNF if for all \(X \xrightarrow{} A\) in \(F+\), either of the following conditions holds true:

  • \(A \subseteq X\) (called a trivial FD)

  • \(X\) is a superkey for \(R\) (implies \(X+ = R)\)

The BCNF Decomposition algorithm is as follows:

  1. Input R: Relation

  2. Input F: FDs for R

  3. \[R = \{R\}\]
  4. If there is a relation \(r \in R\) that is not BCNF and has \(> 2\) attributes (note that two attribute relations are always in BCNF):

    1. Pick a violating FD \(f\): \(X \xrightarrow{} A\) such that \(X, A \in\) attributes of \(r\).

    2. Compute \(X+\).

    3. Let \(R_1 = X+\). Let \(R_2 = X \ \cup \ (r - X+)\).

    4. Remove \(r\) from \(R\).

    5. Insert \(R_1\) and \(R_2\) into \(R\).

    6. Recompute \(F\) as FDs over all relations \(r \in R\).

  5. Repeat step 4 until necessary.

Once these steps are completed, our relation is in Boyce-Codd Normal Form.

Let’s take a look at an example to see how this works. Let us look at relation \(R = \{C,S,J,D,P,Q,V \}\) with the superkey \(C\) and \(F = \{ JP \xrightarrow{} C, SD \xrightarrow{} P, J \xrightarrow{} S\}\).

  • \(JP \xrightarrow{} C\) does not violate the BCNF constraints, since \(C\) is a superkey, therefore \(JP\) is also a superkey.

  • The FD \(SD \xrightarrow{} P\) is in violation of BCNF since \(SD\) is not a superkey nor is \(P \subseteq SD\).

    • Compute \(SD+ = \{S, D, P\}\).

    • Decompose \(R\) into \(R_1 = SD+ = \{S, D, P\}\) and \(R_2 = SD \ \cup \ (R - SD+) = \{S, D\} \ \cup \{C, J, Q, V\} = \{S, D, C, J, Q, V\}\).

    • We now have 2 relations: \(R_1 = \{S, D, P\}\) and \(R_2 = \{S, D, C, J, Q, V\}\).

  • The FD \(J \xrightarrow{} S\) is in violation of BCNF since \(J\) is not a superkey for \(R_2\) nor is \(S \subseteq J\). \(R_1\) does not have this dependency so it is not decomposed further. Applying the same steps as above we see that \(R_2\) gets decomposed into \(\{J, S\}\) and \(\{D, C, J, Q, V\}\).

The BCNF form of this relation is \(SDP\), \(JS\), and \(DCJQV\).

Lossless Decomposition

One problem that can arise from decomposing relations is that after breaking up the table into smaller relations, we may be unable to reconstruct the original relation through these decomposed relations. This means that our decomposition is lossy.

For example, consider the following relation R=ABC, which is decomposed into X=AB and Y=BC. This is a lossy decomposition since attempting to reconstruct R by joining X and Y doesn’t give us the original relation, but rather a superset of R with some extra junk data. As we can see in this example, lossy decompositions don’t "lose" data, but rather generate bad data.\

image



Instead, we want our decompositions to be lossless, meaning that if R is decomposed into X and Y, \(X \bowtie Y = R\). A decomposition of a relation R into X and Y is lossless with respect to F (a set of FDs) if and only if the closure of F (\(F^+\)) contains:

  • \(X \cap Y \xrightarrow[]{} X\) (\(X \cap Y\) is a superkey of X), or

  • \(X \cap Y \xrightarrow[]{} Y\) (\(X \cap Y\) is a superkey of Y)


Looking back at our previous example (R = ABC, X=AB, Y=BC), we can apply this test to confirm it is lossy. \(X \cap Y = B\) , and B is not a superkey of either X or Y (because in X, B = 2 maps to two different values for A; in Y, B = 2 maps to two different values for C).

BCNF decompositions are always lossless, so we don’t have to worry about lossiness if we are decomposing a relation into BCNF.

Dependency Preserving Decompositions

Another problem that can arise from decomposing relations is that dependency checking may require joins. If we make an insertion to a table and need to check that functional dependencies are still satisfied, we may have to join together the tables we decomposed, making every insertion we do costly.

We can avoid this problem through dependency preserving decompositions. Suppose we decompose R into X, Y, and Z. This decomposition is dependency preserving if enforcing FDs individually on each of X, Y, and Z implies that all FDs that held on R must also hold. Now, when making an insertion to a table, we will only need to check the FDs are satisfied on that relation (avoiding joins between decomposed relations) since this will be sufficient for enforcing all FDs that held on the original relation.

Formally, a decomposition of a relation R into X and Y is dependency preserving if \((F_X \cup F_Y)^+ = F^+\). If we take the dependencies that can be checked in X without considering Y and the dependencies that can be checked in Y without considering X, the closure of their union will be the closure of the original set of FDs (\(F^+\)).

Note that although decompositions into BCNF are lossless, they are not necessarily dependency preserving! To demonstrate this, let’s revisit the BCNF decomposition example we examined earlier, where the relation \(R = \{C,S,J,D,P,Q,V \}\) with the superkey \(C\) and \(F = \{ JP \xrightarrow{} C, SD \xrightarrow{} P, J \xrightarrow{} S\}\) was decomposed into X = \(SDP\), Y = \(JS\), and Z = \(DCJQV\).

Here, \(F_X = \{SD \xrightarrow{} P\}\), \(F_Y = \{J \xrightarrow{} S\}\), and \(F_Z = \{\}\). Thus, \((F_X \cup F_Y \cup F_Z) = \{SD \xrightarrow{} P, J \xrightarrow{} S\}\), and the closure of this set of FDs does not include \(JP \xrightarrow{} C\). Thus, this BCNF decomposition is not dependency preserving.

Practice Questions

  1. Consider a set of functional dependencies F = {X \(\rightarrow{}\) Y, Y \(\rightarrow{}\) Z}. For each of the following symbols or expressions, indicate whether it is (a) an attribute, (b) a set of attributes, (c), a set of sets of attributes, (d) a functional dependency, (e) a set of functional dependencies, or (f) none of the above.

    1. X

    2. XY

    3. X \(\rightarrow{}\) Y

    4. F

    5. F+

    6. X+

    7. Armstrong’s reflexivity axiom

  2. Let’s say we have two entities, Students and School, connected by the relationship "Attend s". If a student can attend at most one school, but does not have to attend, what kind of line would we want from Students to Attends?

  3. From the previous question, we also know that each school has at least one student. What kind of line would we want from School to Attends?

  4. Instead, we decide to keep the Student entity but replace school with a Semester entity and connect them with the relationship "Enrolled". A student can be enrolled for up to 10 semesters. What kind of line would we want from Students to Enrolled?

  5. We know that D and AB are both superkeys of the table R. Can both of them also be candidate keys? Are they both guaranteed to be candidate keys?

  6. Decompose ABCDEFGH into BCNF in the order of the following FDs: \(\{G \xrightarrow{} H, EF \xrightarrow{} B, EA \xrightarrow{} C, FH \xrightarrow{} D\}\)

  7. Decompose ABCDEF into BCNF in the order of the following FDs: \(\{A \xrightarrow{} B, B \xrightarrow{} CD, DE \xrightarrow{} F, D \xrightarrow{} A\}\)

  8. Is the decomposition in #6 lossless? Is it dependency preserving?

  9. Suppose we have a relation "tv_shows". Suppose we also have a relation "awards". The awards entity is also a weak entity connected to the tv_shows entity through a "won" relationship. What kind of edge will be drawn between the won relationship and the awards entity set?

Solutions

  1. Consider a set of functional dependencies F = {X \(\rightarrow{}\) Y, Y \(\rightarrow{}\) Z}. For each of the following symbols or expressions, indicate whether it is (a) an attribute, (b) a set of attributes, (c), a set of sets of attributes, (d) a functional dependency, (e) a set of functional dependencies, or (f) none of the above.

    1. X [(b) a set of attributes (or a set of columns)]{style=”color: red”}

    2. XY [(b) a set of attributes]{style=”color: red”}

    3. X \(\rightarrow{}\) Y [(d) a functional dependency]{style=”color: red”}

    4. F [(e) a set of functional dependencies]{style=”color: red”}

    5. F+ [(e) a set of functional dependencies]{style=”color: red”}

    6. X+ [(b) a set of attributes]{style=”color: red”}

    7. Armstrong’s reflexivity axiom [(f) an axiom]{style=”color: red”}

  2. We know a student can attend at most one school, so the line between Students and Attends should be a thin arrow.

  3. We know each school has at least one student, so the line between School and Attends should be a thick line.

  4. We know a student can be enrolled for 0 or more semesters, so the line between Students and Enrolled should be a thin line.

  5. Both D and AB are possible candidate keys. AB is a possible candidate key if removing A or B causes it to no longer be a superkey. Only D is guaranteed to be a candidate key. This is because there is nothing we can remove from the set of columns and possibly have a smaller set that is a superkey because D is only one column. On the other hand, AB is not guaranteed to be a candidate key because removing A or B might result in a smaller set that is still a superkey.

  6. At step 1, we get ABCDEFG and GH.

    At step 2, we get ACDEFG and EFB and GH.

    At step 3, we get ADEFG and EAC and EFB and GH.

  7. First, we consider \(A \rightarrow{} B\). A+ = ABCD so we get ABCD and AEF.

    Next, we consider \(B \rightarrow{} CD\). Since B is a superkey (B+ = ABCD), BCNF is not violated and we do not need to decompose.

    Next, we consider \(DE \rightarrow{} F\). We do not need to decompose as there are no relations with D, E, and F in them.

    Next, we consider \(D \rightarrow{} A\). Since D is a superkey (D+ = ABCD), BCNF is not violated and we do not need to decompose.

  8. In #6, we split R = ABCDEF into X = ABCD and Y = AEF. This decomposition is lossless, as all BCNF decompositions are lossless. We can confirm this by noticing that \(X \cap Y = A\), and A is a superkey of X.

    However, this decomposition is not dependency preserving. \(F_X = \{ A \rightarrow{} B, B \rightarrow{} CD, D \rightarrow{} A\}\) and \(F_Y = \{\}\). \((F_X \cup F_Y)^+\) does not contain \(DE \rightarrow{} F\), an FD that originally held on R.

  9. Since "awards" is a weak entity, we will draw a bolded arrow from "awards" to "won". We will also bold the "awards" rectangle and "won" diamond.

Past Exam Problems

Fa22 Midterm 2 Q3: Ground School (6.5 points)

Answer the following questions to fill out this Entity-Relationship Model according to the given constraints.

ER diagram with Traveler, Airline, Flights, Pilots and the Passenger, Owns, Communicates, and is Assigned relationships

Constraints

  • Each airline owns at least one flight
  • Each flight is owned by exactly one airline
  • Each flight is assigned exactly one pilot
  • Each pilot can be assigned to any number of flights
  • Each traveler can be a passenger for at most one flight
  • Each flight will have at least one passenger

For parts (a)–(f), choose from:

  • A. Thin arrow (key constraint)
  • B. Thin line (no constraint)
  • C. Bold arrow (key constraint with total participation)
  • D. Bold line (participation constraint)

(a) (0.5 pt) What type of edge should be drawn at 1 between Airline and Owns?

Solution

D. Bold line (participation constraint)

(b) (0.5 pt) What type of edge should be drawn at 2 between Flights and Owns?

Solution

C. Bold arrow (key constraint with total participation)

(c) (0.5 pt) What type of edge should be drawn at 3 between Traveler and Passenger?

Solution

A. Thin arrow (key constraint)

(d) (0.5 pt) What type of edge should be drawn at 4 between Flights and Passenger?

Solution

D. Bold line (participation constraint)

(e) (0.5 pt) What type of edge should be drawn at 5 between Pilots and Is Assigned?

Solution

B. Thin line (no constraint)

(f) (0.5 pt) What type of edge should be drawn at 6 between Flights and Is Assigned?

Solution

C. Bold arrow (key constraint with total participation)

(g) (0.5 pt) We now decide to convert Flights into a weak entity identified by its corresponding Airline. Choose all of the following that must occur.

  • A. Set the edge between Airline and Owns to a Thick Arrow
  • B. Bold the Diamond for Owns relationship
  • C. Bold the Rectangle for Flights
  • D. Set the edge between Flights and Owns to a Thick Line
Solution

B and C.

(h) (1 pt) What attribute(s) uniquely identify the weak entity in this ER diagram from part g? If there are multiple attributes, separate them with commas (order does not matter). Write your answer in the form table_name.attribute_name.

Solution

Flights.flight_id, Airlines.airline_id

The flight’s id becomes a partial key, and it must be appended to the corresponding airline’s id because Flights are now a weak entity uniquely identified by a specific Airline. As such, the primary key for Flights needs the attributes “Flights.flight_id, Airlines.airline_id”.

(i) (2 pt) Due to some CS161 students’ exploits, the control center at this airport is no longer functioning. As such, all flights now need to be able to communicate with all other flights to land or take off safely. You may assume that there will always be at least 2 flights in the communication network at any time. To satisfy all possible communication networks with the added relationship Communicates, what edge type(s) are used to properly fill in both edge X and edge Y? Select all that apply.

  • A. Thin arrow (key constraint)
  • B. Thin line (no constraint)
  • C. Bold arrow (key constraint with total participation)
  • D. Bold line (participation constraint)
  • E. Bold arrow with weak entity
Solution

D. Bold line (participation constraint)

Fa22 Midterm 2 Q4: Flight School (9.5 points)

Consider the relation R = PLANES with the following functional dependencies:

  • L → P
  • P → L
  • AN → E
  • PL → NE
  • S → PAE

(a) (1 pt) Select all of the following that are candidate keys for the relation R.

  • A. P
  • B. S
  • C. AN
  • D. PL
  • E. PA
  • F. PS
  • G. None of the above
Solution

B. S

In the list of options, only S is a candidate key. This is because the closure of S consists of PLANES. By applying S → PAE, P → L, and PL → NE, we get that S determines all other columns of R = PLANES, making S first a superkey. Not only that, if we were to remove S, we no longer have any columns so we cannot fully determine R. Hence, S is a candidate key.

PS also fully determines R, so it is a superkey. However, if we were to remove P, S alone still can determine all of R. As such, PS is a superkey, but not a candidate key. All other remaining options do not fully determine R.

(b) (1 pt) Select all of the following that are superkeys for the relation R.

  • A. L
  • B. AN
  • C. PES
  • D. PAE
  • E. PLAS
  • F. PLANE
  • G. None of the above
Solution

C. PES and E. PLAS

From the previous part, we already know that S is both a superkey and a candidate key. Thus, any option that contains S can determine all columns of R and be a superkey as well. Hence, PES and PLAS are both superkeys.

(c) (1 pt) Which of the functional dependencies violate BCNF?

  • A. L → P
  • B. P → L
  • C. AN → E
  • D. PL → NE
  • E. S → PAE
  • F. None of the above
Solution

A, B, C, and D.

A functional dependency X → Y violates BCNF if Y is not a subset of X AND X is not a superkey of the relation R. In our case:

  • P is not a subset of L and L is not a superkey
  • L is not a subset of P and P is not a superkey
  • E is not a subset of AN and AN is not a superkey
  • NE is not a subset of PL and PL is not a superkey
  • S is a superkey of R so S → PAE does not violate BCNF

(d) (2 pt) Which of the following relations are in the BCNF decomposition after applying L → P?

  • A. PL
  • B. LAS
  • C. PLNE
  • D. NES
  • E. LANES
  • F. PAN
  • G. None of the above
Solution

B. LAS and C. PLNE

L+, the closure of L, is PLNE because L → P and PL → NE. So, the first relation we have is PLNE. Then the second relation consists of L union with the original relation R - L+ = L union R - PLNE = L union AS = LAS. Thus, the two relations after applying L → P are LAS and PLNE.

(e) (2 pt) Which of the following relations are in the BCNF decomposition after applying all dependencies in the order in which they are listed in the question’s description?

  • A. P
  • B. PL
  • C. PNE
  • D. ANE
  • E. LAS
  • F. LANS
  • G. PLANE
  • H. None of the above
Solution

E. LAS

From applying L → P in the previous question:

  • Relation 1: PLNE
  • Relation 2: LAS

P → L: The FD P → L doesn’t break BCNF because P is a superkey for relation 1 from the previous part since P → L and PL → NE results in PLNE. As such, this FD doesn’t need to be applied to decompose the previous relations.

AN → E: Nothing is done here because ANE aren’t within the same relation in any of the relations.

PL → NE: Nothing is done here because PL is a superkey for PLNE, so this FD doesn’t violate BCNF.

S → PAE: Nothing is done here because SPAE aren’t within the same relation in any of the relations.

Final solution:

  • Relation 1: PLNE
  • Relation 2: LAS

Thus, the only relation in the decomposition from the options is E. LAS.

(f) (0.5 pt) Is the BCNF decomposition from the previous question lossless?

  • Yes
  • No
Solution

Yes. BCNF decompositions are always lossless.

(g) (2 pt) Your flight school instructor uses their own algorithm and comes up with the decomposition of PL, ANS, E for the relation R. They claim that their decomposition is dependency preserving for the relation R. Explain if their claim is right or wrong in one sentence.

Solution

No, it violates AN → E, PL → NE, S → PAE.

Sp22 Final Q11: Cal Day! (7 points)

Justin decides to collect some data about 186 students’ attendance in recent Berkeley events. He decides to use ER Diagrams to model their attendance and would like your help completing the diagram.

(a) First Try

Some rules for the ER Diagram:

  • A student can either go to an event or perform at an event but not both.
  • At each event, at least one CS186 student is spotted in the audience.
  • Each Berkeley student cannot perform in more than 1 event.
  • For each event, there has to be at least 1 performer.

ER diagram with CS186 and Cal_events connected by Go_to and Perform

For each part, choose from: A. Thin arrow, B. Thin line, C. Bold arrow, D. Bold line.

i. (1 pt) What type of edge should be drawn between CS186 and Go_to?

Solution

B. Thin line. There is no constraint on any CS 186 student, no one has to go to any event, and if they’d like they could go to multiple of them.

ii. (1 pt) What type of edge should be drawn between Go_to and Cal_events?

Solution

D. Bold line. There is at least one CS 186 student in the audience for each seat, so it’s a participation constraint.

iii. (1 pt) What type of edge should be drawn between CS186 and Perform?

Solution

A. Thin arrow. Each student cannot perform in more than 1 event, so this is a key constraint.

iv. (1 pt) What type of edge should be drawn between Perform and Cal_events?

Solution

B. Thin line. There’s nothing required of CS 186 students to perform on stage, so it’s also a thin line. (Note: option D was also given full credit.)

(b) Talented Students

It’s just discovered that there are some very talented 186 students who are involved in multiple performances in one event. For example, they could both perform martial arts and sing on Cal Day. So Justin wants to update the ER to reflect the changes and make sure these students’ talents do not go unnoticed. He adds in a “performance” entity and hopes you could help him finish the edges. (Assume each performance is solo).

The rules from the previous question will still hold, and pay attention to how each entity relates to both of the other entities.

ER diagram with CS186, Cal_events, and Performance connected by Perform

i. (1 pt) What type of edge should be drawn between CS186 and Perform?

Solution

B. Thin line. Now a student can perform multiple performances in one event. So the key constraint does not hold on “performance”.

ii. (1 pt) What type of edge should be drawn between Perform and Cal_events?

Solution

B. Thin line. Each event could still have 0 contribution from CS 186 group body, so there’s no constraint. (Note: option D was also given full credit.)

iii. (1 pt) What type of edge should be drawn between Perform and Performance?

Solution

C. Bold arrow. Each performance is played by exactly one student at one event, so both key constraint and participation constraint.

Fa21 Midterm 2 Q1: ER Game (8 points)

Answer the following questions to fill out this Entity-Relationship Model according to the given constraints:

ER diagram with Player, Game, and Staff_Member connected by Plays, Wins, and Oversees

Constraints

  • Each player must play in at least one game, and each game may have any number of players
  • Each player may win any number of games, and each game may have any number of winners
  • Each game must have exactly one staff member that oversees it, and each staff member may oversee at most one game

For parts (a)–(f), choose from:

  • A. Thin arrow (key constraint)
  • B. Thin line (no constraint)
  • C. Bold arrow (key constraint with total participation)
  • D. Bold line (participation constraint)

(a) (1 pt) What type of edge should be drawn at 1 between Player and Plays?

Solution

D. Bold line (participation constraint)

(b) (1 pt) What type of edge should be drawn at 2 between Player and Game?

Solution

B. Thin line (no constraint)

(c) (1 pt) What type of edge should be drawn at 3 between Player and Wins?

Solution

B. Thin line (no constraint)

(d) (1 pt) What type of edge should be drawn at 4 between Wins and Game?

Solution

B. Thin line (no constraint)

(e) (1 pt) What type of edge should be drawn at 5 between Staff_Member and Oversees?

Solution

A. Thin arrow (key constraint)

(f) (1 pt) What type of edge should be drawn at 6 between Oversees and Game?

Solution

C. Bold arrow (key constraint with total participation)

(g) (1 pt) We now decide to convert Game to a weak entity identified by its corresponding Staff_Member. Choose all of the following that must be bolded.

  • A. Rectangle surrounding “Game”
  • B. Oval surrounding “name”
  • C. Rectangle surrounding “Staff_Member”
  • D. Diamond surrounding “Oversees”
Solution

A and D. (The official solutions only fill in A, but label D as “CORRECT”: the identifying relationship of a weak entity is also bolded.)

(h) (1 pt) Continuing from part g, write down the attributes in the primary key for a game. Use dot notation to name the attributes and separate them by commas. Order does not matter.

Solution

Game.name, Staff_Member.id

The game’s name becomes a partial key, and it must be appended to the corresponding staff member’s id. Therefore, the primary key needs the attributes “Game.name, Staff_Member.id”.

Sp21 Midterm 2 Q1: Get your boba! (6 points)

We want to model boba franchises, their affiliated locations, and customers they have sold boba to. Consider the ER diagram below.

ER diagram with Location, Franchise, and Customer connected by Affiliates and Buys from

Assumptions:

  • Each franchise’s id is a unique identifier.
  • Each location is affiliated with exactly one franchise, each franchise must have at least 1 location.
  • Each franchise has at most one location in a given zipcode, but multiple franchises can have locations in the same zipcode.
  • Customers can buy boba from multiple locations and can buy any number of drinks, including no drinks.

For parts (a)–(d), choose from: Thin line, Bold line, Thin arrow, Bold arrow.

(a) (1 pt) What type of edge should be drawn at 1 from Franchise to Affiliates?

Solution

Bold line

(b) (1 pt) What type of edge should be drawn at 2 from Location to Affiliates?

Solution

Bold arrow

(c) (1 pt) What type of edge should be drawn at 3 from Location to Buys from?

Solution

Thin line

(d) (1 pt) What type of edge should be drawn at 4 from Customer to Buys from?

Solution

Thin line

(e) (2 pt) What attribute(s) should be part of the Location relation’s primary key? If there are multiple attributes, separate them with commas.

Solution

zipcode, Franchise.id

We store affiliates as part of Location as it is a weak entity.

Fa20 Final Q5: Normalize Normalization (19 points)

Consider a relation R(A,B,C,D,E) with the following functional dependencies:

  1. A -> C
  2. BC -> D
  3. CD -> E
  4. E -> A

(a) (4 pt) Enumerate all of the (minimal) candidate keys in this relation. Separate them by commas, with no spaces in between. When listing keys, list them in alphabetically increasing order. (Thus, if your answers are ABC and DCB, list them as: ABC,BCD)

Solution

AB,BC,BE

(b) (3 pt) Which of the functional dependencies above are BCNF violations? List the corresponding FD numbers in increasing order. Thus, if your answer is A->C and CD->E, list them as: 1,3

Solution

1,3,4

(c) (3 pt) Using the BCNF decomposition algorithm described in class, and the first FD you listed in the previous question, provide the first step of your decomposition. Simply list the schemas of the two relations that result. When listing schemas, list them in alphabetically increasing order. Thus, if your answer is R(A, B, C) and S(A, C, E, D), then list: ABC,ACDE

Solution

ABDE,AC

(d) (3 pt) Using your answer from the previous question, continue decomposing the relations until you arrive at a BCNF decomposition. Simply list the schemas of the eventual relations. When listing schemas, list them in alphabetically increasing order. Thus, if your answer is R(A, B), S(C, D), and T(E, D, A), then list: AB,ADE,CD

Solution

AC,AE,BDE

(e) (3 pt) Are there any other BCNF decompositions? If not, simply answer “No”. If yes, list the schemas of the eventual relations. When listing schemas, list them in alphabetically increasing order. Thus, if your answer is R(A, B), S(C, D), and T(E, D, A), then list: AB,ADE,CD

Solution

Yes. AC,AE,BCD,DE using 3, then 1, then 4.

(f) (3 pt) There is a single FD of the form X->Y where X and Y are both single attributes such that by adding that FD, there are no longer any violations in the original set. What is that FD? [Answer this as “X->Y”, replacing X and Y each with one of A–E.]

Solution

C->B or A->B

  1. Sometimes we refer to a superkey as a key. ↩