profile pic
⌘ '
raccourcis clavier

Database Design

Finding Keys

a

Consider a relation schema R(A,B,C,D,E)R(A, B, C, D, E) and the set of functional dependencies

F={ABC,CDE,BD,EA}\mathbf{F} = \{ A \rightarrow BC, \, CD \rightarrow E, \, B \rightarrow D, \, E \rightarrow A \}

Find all candidate keys (minimal keys) of relation RR. Show all the steps you took to derive each key, and clearly state which of Armstrong’s axioms are used in each step.

Closure of AA

ABCA \rightarrow BC means A+={A,B,C}(Decomposition)A^{+} = \{A, B, C\} (\text{Decomposition})

BDB \rightarrow D means A+={A,B,C,D}(Transitivity)A^+ =\{A,B,C,D\} \text{(Transitivity)}

CDECD \rightarrow E means A+={A,B,C,D,E}(Transitivity)A^+ =\{A,B,C,D,E\} \text{(Transitivity)} (CDACD \in A)

AA is a candidate key

Closure of BB

B+={B}B^+ = \{B\}

BDB \rightarrow D means B+={B,D}B^+ = \{B, D\}

No other closure can be applied thus BB is not a key

Closure of CC

We can’t derive all attributes, thus CC is not a key

Closure of DD

No applicable dependencies, thus DD is not a key

Closure of EE

EAE \rightarrow A means E+={E,A}(FD)E^+ = \{E, A\} \text{(FD)}

ABCA \rightarrow BC means E+={E,A,B,C}Decomposition and TransitivityE^+ = \{E, A, B, C\} \text{Decomposition and Transitivity}

BDB \rightarrow D means E+={E,A,B,C,D}TransitivityE^+ = \{E, A, B, C, D\} \text{Transitivity}

EE is a candidate key

Closure of CDCD

Initial CDCD gives (CD)+={C,D}(CD)^+ = \{C, D\}

CDECD \rightarrow E gives (CD)+={C,D,E}FD(CD)^+ = \{C, D, E\} \text{FD}

EAE \rightarrow A gives (CD)+={C,D,E,A}Transitivity(CD)^+ = \{C, D, E, A\} \text{Transitivity}

ABCA \rightarrow BC gives (CD)+={C,D,E,A,B,C}Transitivity and Decomposition(CD)^+ = \{C, D, E, A, B, C\} \text{Transitivity and Decomposition}

CDCD is a candidate key

Closure of BCBC

Initial BCBC gives (BC)+={B,C}(BC)^+ = \{B, C\}

BDB \rightarrow D gives (BC)+={B,C,D}FD(BC)^+ = \{B, C, D\} \text{FD}

CDECD \rightarrow E gives (BC)+={B,C,D,E}Transitivity(BC)^+ = \{B, C, D, E\} \text{Transitivity}

EAE \rightarrow A gives (BC)+={B,C,D,E,A}Transitivity(BC)^+ = \{B, C, D, E, A\} \text{Transitivity}

BCBC is a candidate key

conclusion

Final candidate keys are A,E,CD,BCA, E, CD, BC

b

Is the FD ABCAB \rightarrow C entailed by F? Show your work that supports your answer

We will find closure of ABAB under FF

(AB)+={A,B}(AB)^+ = \{A, B\}

ABCA \rightarrow BC entails ABA \rightarrow B and ACA \rightarrow C (Decomposition)

with augmentation on ACA \rightarrow C we have ABCBAB \rightarrow CB

Decomposition to ABCBAB \rightarrow CB gets ABCAB \rightarrow C

Therefore ABCAB \rightarrow C is entailed by FF

Minimal Cover

Question

Given the relational schema T(A,B,C,D)T(A, B, C, D) and the FDs: F{ABCD,CDA,CAB,ADC,CDB}F \{ABC \rightarrow D, CD \rightarrow A, CA \rightarrow B, AD \rightarrow C, CD \rightarrow B \}, compute the minimal cover FF^{'} of FF. Show all your work (derivation) to compute FF^{'}

We have the following FD

ABCDCDACABADCCDB\begin{aligned} ABC &\rightarrow D \\ CD &\rightarrow A \\ CA &\rightarrow B \\ AD &\rightarrow C \\ CD &\rightarrow B \end{aligned}
  1. RHS of FDs into single attributes
  • Already in this form
  1. minimize LHS by removing extraneous

FD1: ABCDABC \rightarrow D

  • BB is extraneous given that ACDAC \rightarrow D holds:
    • (AC)+={A,C}(AC)^+ = \{A, C\}
    • CABCA \rightarrow B then add BB to closure
    • ABCDABC \rightarrow D then add DD to closure

Update FD1: ACDAC \rightarrow D

FD2: CDACD \rightarrow A

  • no FD is applied is either C or D is assume extraneous, therefore remained unchanged

FD3: CABCA \rightarrow B

  • can’t reduce CABCA \rightarrow B given that neither CBC \rightarrow B and ABA \rightarrow B holds

FD4: ADCAD \rightarrow C

  • can’t reduce ADCAD \rightarrow C given that neither ACA \rightarrow C and DCD \rightarrow C holds

FD5: CDBCD \rightarrow B

  • can’t reduce CDBCD \rightarrow B given that neither CBC \rightarrow B and DBD \rightarrow B holds
  1. Remove redudant FDs

FD5: CDBCD \rightarrow B

Can be calculated from CDACD \rightarrow A and CABCA \rightarrow B:

Closure of CDCD is (CD)+={C,D}(CD)^+ = \{C,D\}, CDACD \rightarrow A gives {C,D,A}\{C,D,A\} and CDBCD \rightarrow B gives {C,D,A,B}\{C,D,A,B\}

thus this is redudant

Final minimal cover is

F={ACD,CDA,CAB,ADC}F^{'} = \{AC \rightarrow D, CD \rightarrow A, CA \rightarrow B, AD \rightarrow C\}

Armstrong’s Axioms

a

Given the relational schema R(A,B,C,D,E,F)R(A,B, C, D, E, F) and FDs F1:{ABC,AD,CDEF}F_1: \{AB \rightarrow C, A \rightarrow D, CD \rightarrow EF\}. Show that ABFAB \rightarrow F

AD(Given)ABDB(Augmentation w/B)ABDABBDecomposition)ABCD(Union with 2 and ABC)ABEF(Transitivity with 3 and CDEF)ABF(Decomposition)\begin{align} A &\rightarrow D &\text{(Given)} \\ AB &\rightarrow DB &\text{(Augmentation w/B)} \\ AB &\rightarrow D \cup AB \rightarrow B &\text{Decomposition)} \\ AB &\rightarrow CD &\text{(Union with 2 and } AB \rightarrow C) \\ AB &\rightarrow EF &\text{(Transitivity with 3 and } CD \rightarrow EF) \\ AB &\rightarrow F &\text{(Decomposition)} \\ &\because \end{align}

b

Given the relational schema R(A,B,C,D,E,F)R(A,B, C, D, E, F) and FDs F1:{CD,BEA,BEFC}F_1: \{C \rightarrow D, BE \rightarrow A, BEF \rightarrow C \}. Show that BEFBEF is a key

Proof: BEF+={A,B,C,D,E,F}BEF^{+} = \{A,B,C,D,E,F\} and BEFBEF is minimal

  1. Proving closure of BEFBEF

We have BEF+={B,E,F}BEF^{+} = \{B,E,F\}

BEFCBEF \rightarrow C and BEFBEF+BEF \in BEF^{+} by reflexivity, we add CC to the closure BEF+={B,E,F,C}BEF^{+} = \{B,E,F, C\}

CDC \rightarrow D and with Transitivity of BEFCBEF \rightarrow C gives BEFDBEF \rightarrow D. Add DD to closure BEF+={B,E,F,C,D}BEF^{+} = \{B,E,F, C, D\}

BEABE \rightarrow A thus add AA to the closure BEF+={B,E,F,C,D,A}BEF^{+} = \{B,E,F, C, D, A\}

Therefore by union we have prove closure of BEFBEF

  1. minimal of BEFBEF

Case 1: Remove BB from BEFBEF:

  • Compute EF+EF^{+}, and there is no FD to prove this transition, therefore EFEF does not determine all attributes

Case 2: Remove EE from BEFBEF:

  • Compute BF+BF^{+}, and there is no FD to prove this transition, therefore EFEF does not determine all attributes

Case 3. remove FF from BEFBEF:

  • Closure of BEBE is BE+={B,E}BE^{+} = \{B, E\}
  • BEABE \rightarrow A means BE+={B,E,A}BE^{+} = \{B, E, A\}
  • No further can be added

Therefore BEF is minimal

BEF is a key

3NF, BCNF

1

List all functional dependencies and keys that can be inferred from this information

  1. functional dependencies

For Company table:

FD1: companyIDcompanyName, cityName, countr, assets\text{companyID} \rightarrow \text{companyName, cityName, countr, assets}

FD2: companyName, cityNamecompanyID, country, assets\text{companyName, cityName} \rightarrow \text{companyID, country, assets}

Candidate key: companyID\text{companyID} (minimal key based on FD1) and companyName, cityName\text{companyName, cityName} (based on FD2)

For Department table:

FD3: deptIDdeptName, companyID, cityName, country, deptMgrID\text{deptID} \rightarrow \text{deptName, companyID, cityName, country, deptMgrID}

FD4: companyID, depthNamedeptID, cityName, country, deptMgrID\text{companyID, depthName} \rightarrow \text{deptID, cityName, country, deptMgrID}

FD5: deptMgrIDdeptID\text{deptMgrID} \rightarrow \text{deptID}

Candidate key: deptID\text{deptID} and companyID, depthName\text{companyID, depthName}

For City table:

FD6: cityIDcityName, country\text{cityID} \rightarrow \text{cityName, country}

FD7: cityName, countrycityID\text{cityName, country} \rightarrow \text{cityID}

Candidate key: cityID\text{cityID} and cityName, country\text{cityName, country}

2

schemas satisfies either BCNF or 3NF

Note that for both Company and City tables, it satisifies BCNF. however, for Department table:

For FD3, deptID\text{deptID} is a candidate key, thus satisfies BCNF

For FD4, companyID, depthName\text{companyID, depthName} is a candidate key, thus satisfies BCNF

But for FD5 given that deptMgrID\text{deptMgrID} is not a candidate key, thus violate BCNF

Improvement

  • Create a new table DeptManager deptMgrID, deptID\text{deptMgrID, deptID} with decomposition

  • remove deptMgrID\text{deptMgrID} from the original table (now deptName, companyID, cityName, country\text{deptName, companyID, cityName, country})

Thus should satisfy BCNF

Transactions and Concurrency

Schedules

Consider schedules S1,S2,S3S_{1}, S_{2}, S_{3} State which of the following properties holds (or not) for each schedule: strict, avoid cascading aborts, recoverability. Provide brief justification for each answer

a

S1:r1(X); r2(Z); r1(Z); r3(X); r3(Y); w1(X); c1; w3(Y); c3; r2(Y); w2(Z); w2(Y); c2S_{1}: \text{r1(X); r2(Z); r1(Z); r3(X); r3(Y); w1(X); c1; w3(Y); c3; r2(Y); w2(Z); w2(Y); c2}

  • strict: no because r3\text{r3} reads X before T1T_{1} commits, and r2\text{r2} reads Y before T3T_{3} commits
  • avoid cascading aborts: no, because r3\text{r3} reads X before T1T_{1} commits
  • recoverability: yes, since T2T_{2} reads data written by T3T_{3} has committed

b

S2:r1(X); r2(Z); r1(Z); r3(X); r3(Y); w1(X); w3(Y); r2(Y); w2(Z); w2(Y); c1; c2; c3S_{2}: \text{r1(X); r2(Z); r1(Z); r3(X); r3(Y); w1(X); w3(Y); r2(Y); w2(Z); w2(Y); c1; c2; c3}

  • strict: no because T2T_{2} reads uncommitted data from T3T_{3} before committed
  • avoid cascading aborts: no because r2(Y)\text{r2}(Y) reads an uncommitted value from T3T_{3}
  • recoverability: no because T2T_{2} reads Y written by T3T_{3} but commits before T3T_{3} commits

c

S3:r1(X); r2(Z); r3(X); r1(Z); r2(Y); r3(Y); w1(X); w2(Z); w3(Y); w2(Y); c3; c1; c2S_{3}: \text{r1(X); r2(Z); r3(X); r1(Z); r2(Y); r3(Y); w1(X); w2(Z); w3(Y); w2(Y); c3; c1; c2}

  • strict: no, because T2T_{2} writes to YY after it has been modified by uncommitted T3T_{3}
  • avoid cascading aborts: yes, because all reads are from initial state, not from uncommitted transaction
  • recoverability: yes, because T2T_{2} is committed after T3T_3

Serialisability

Which of the following schedules is (conflict) serializable? For each serializable schedule, find the equivalent serial schedules.

a

r1(X); r3(X); w1(X); r2(X); w3(X)\text{r1(X); r3(X); w1(X); r2(X); w3(X)}

r1(X)w3(X):T1 reads X before T3 writes it    T1T3r3(X)w1(X):T3 reads X before T1 writes it    T3T1w1(X)r2(X):T1 writes X before T2 reads it    T1T2w1(X)w3(X):T1 writes X before T3 writes it    T1T3r2(X)w3(X):T2 reads X before T3 writes it    T2T3\begin{align*} r_1(X) \rightarrow w_3(X) &: T_1 \text{ reads } X \text{ before } T_3 \text{ writes it} \implies T_1 \rightarrow T_3 \\ r_3(X) \rightarrow w_1(X) &: T_3 \text{ reads } X \text{ before } T_1 \text{ writes it} \implies T_3 \rightarrow T_1 \\ w_1(X) \rightarrow r_2(X) &: T_1 \text{ writes } X \text{ before } T_2 \text{ reads it} \implies T_1 \rightarrow T_2 \\ w_1(X) \rightarrow w_3(X) &: T_1 \text{ writes } X \text{ before } T_3 \text{ writes it} \implies T_1 \rightarrow T_3 \\ r_2(X) \rightarrow w_3(X) &: T_2 \text{ reads } X \text{ before } T_3 \text{ writes it} \implies T_2 \rightarrow T_3 \end{align*}

The precedence graph contains a cycle between T1T_{1} and T3T_{3}, thus this is not conflict serializable

b

r3(X); r2(X); w3(X); r1(X); w1(X)\text{r3(X); r2(X); w3(X); r1(X); w1(X)}

Note that there are no conflict between T2T_{2} and other nodes given that it only read

This is conflict serializable with the following equivalent serial schedules:

T3T1T2T3T2T1\begin{aligned} & T_{3} \to T_{1} \to T_{2} \\ & T_{3} \to T_{2} \to T_{1} \end{aligned}

Locking

Question

Consider the following locking protocol:

  • Before a transaction T writes a data object A, T has to obtain an exclusive lock on A.
  • For a transaction T, we hold these exclusive locks until the end of the transaction.
  • If a transaction T reads a data object A, no lock on A is obtained.

State which of the following properties are ensured by this locking protocol: serializability, conflict-serializability, recoverability, avoids cascading aborts, avoids deadlock. Explain and justify your answer for each property.

  1. serializability
  • Not ensured given that reads aren’t controlled by locks, therefore two transaction can read the same data item and write to in different order (example: r1(X); r2(X); w2(X); w1(X)\text{r1(X); r2(X); w2(X); w1(X)})
  1. conflict-serializability
  • Not ensured, same reason as above
  1. recoverability
  • not ensured given that if a transaction TjT_j reads data written by TiT_i, then TjT_j should commit only after TiT_i commit. however, in this protocol, transaction can read uncommitted data, given that read is not locked (dirty read.)
  1. avoid cascading aborts
  • not ensured given that dirty read can happen (example: w1(X); r2(X)\text{w1(X); r2(X)}. In this case if T1T_{1} aborts, T2T_{2} will need to aboart, causing cascade aborts)
  1. avoid deadlock
  • ensured, given that each transaction have to obtain exclusive lock on A, as transactions can’t wait for each other in a scycle since reads don’t require locks.