Databases

xkcd drop table meme

Download the introduction presentation

View the presentation on proofs or download it. The links refer to the slides of prof. Perelli and don't work on the website: you have to download the pdf in the same folder with the course's slides for the links to work.


Schema

An attribute is a pair; we can define the function on a set of names, which associates to each name a specific domain (different attributes can have the same domain)

PDF 7 slide 2

A relation schema is a set of attributes


Tuples & instances

PDF 7 slide 3 Given a relation schema , a tuple on is a function such that

Given a relation schema , a subset and a tuple on , the reduction of on is defined as

PDF 7 slide 4 Given a relation schema , a subset and tuples on

PDF 7 slide 5 Given a relation schema and tuples on , a set is an instance of


Functional dependencies

PDF 7 slide 6

Given a relation schema and we have that is a functional dependency on (noted as )

PDF 7 slide 7

Given a relation schema and a functional dependency defined on we say that an instance of satisfies the functional dependency if


Instance legality & closure

PDF 7 slide 14

Given a relation schema and a set of functional dependencies defined on , an instance of is legal if it satisfies every dependency in

PDF 7 slide 20

Given a relation schema and a set of functional dependencies defined on , the closure of is the set of functional dependencies that are satisfied by every legal instance of

doesn't necessarily have to be in


PDF 7 slide 21

Proof

By definition is legal if it satisfies every dependency given , every legal instance of satisfies


Keys

PDF 7 slide 22

Given a relation schema and a set of functional dependencies on , is a key of if

means proper subset, which implies that


Trivial dependencies

PDF 7 slide 26

Given a schema and , we have that every instance of satisfies the dependency

Proof

Given an instance of we have that

by definition as we have that
by definition

As we have that satisfies


Decomposition

PDF 7 slide 27

Given a schema and a set of functional dependencies on , we have that

Proof


PDF 8 slide 3 is a set of functional dependencies on such that

  • (refelxivity)
  • (augmentation)
  • (transitivity)

PDF 8 slide 6 derivates

  • (union)
  • (decomposition)
  • (pseudotransitivity)

PDF 8 slide 8


Derivates (Proofs)

Union

LaTeX

by augmentation by transitivity

Decomposition

by transitivity

Pseudotransitivity

by augmentation by transitivity


RFRX \subseteq RXF(X)^+_FX \subseteq (X)^+_F

Proof

\forall A \in XX \to A \in F^A \impliesA \in (X)^+_F \implies X \subseteq (X)^+_F(X)^+_FF^A(X)^+_FF^+


Lemma of closure

PDF 8 slide 10

Let RFR

Proof

X \to Y \in F^A \impliesX \to A \in F^A ; \forall A \in Y \impliesA \in (X)^+_F ; \forall A \in Y \implies Y \subseteq (X)^+_FY \subseteq (X)^+_F \implies A \in (X)^+_F ; \forall A \in Y \impliesX \to A \in F^A ; \forall A \in Y \impliesX \to Y \in F^A


F^+ = F^ARFRF^+ = F^A

Proof

Let F_iFiF_0 = F


F^A \subseteq F^+

Base case

F_0 = F \subseteq F^+ \implies F_0 \subseteq F^+

Inductive step

F_i \subseteq F^+ \implies F_{i + 1} \subseteq F^+X \to Y \in F_{i + 1}X \to Y \in F_i \impliesX \to Y \in F^+X \to Y \in F_{i + 1} \setminus F_iX \to Y has been optained through one of the axioms


F^A \subseteq F^+

Reflexivity

Y \subseteq X \impliesX \to YX \to Y \in F^+

Augmentation

Z \subseteq R, X = ZV, Y = ZW \land V \to W \in F_it_1, t_2 \in rRt_1[X] = t_2[X] \implies (t_1[V] = t_2[V] \impliest_1[W] = t_2[W]) \land t_1[Z] = t_2[Z] \implies t_1[Y] = t_2[Y]

Transitivity

X \to Z, Z \to Y \in F_i \implies\forall \text{ legal } r \text{ of } R, \forall t_1, t_2 \in r, t_1[X] = t_2[X] \implies t_1[Z] = t_2[Z] \implies t_1[Y] = t_2[Y]t_1[X] = t_2[X] \implies t_1[Y] = t_2[Y] \implies X \to Y \in F^+


F^+ \subseteq F^AX \subseteq Rr = \set{t_1, t_2}Rr(X)^+_FR \setminus (X)^+_Ft_1t_2rV \to W \in FV, W \neq \varnothingV \nsubseteq (X)^+_F \implies \exists A \in V : A \in R \setminus (X)^+_F \implies t_1[V] \neq t_2[V] \implies rV \to WV \subseteq (X)^+_FW \subseteq (X)^+_F \implies t_1[V] = t_2[V] \land t_1[W] = t_2[W] \implies rV \to WW \nsubseteq (X)^+_F \implies \exists A \in W : A \in R \setminus (X)^+_F \implies t_1[V] = t_2[V] \land t_1[W] \neq t_2[W]


F^+ \subseteq F^ArV \to W\exists V \to W \in FrV \to WV \subseteq (X)^+_F \impliesX \to V \in F^AA \in W \impliesV \to A \in F^AX \to A \in F^A \impliesA \in (X)^+_F which is a contraddiction

Legality

In the first 2 cases rV \to W \in F\implies rR


F^+ \subseteq F^AX \to Y \in F^+X \subseteq (X)^+_F \impliest_1[X] = t_2[X] \impliesrt_1[Y] = t_2[Y] \impliesY \subseteq (X)^+_F \implies X \to Y \in F^A

F^+ = F^AF_i \subseteq F^+ : \forall i \in \mathbb{N}F^+ \subseteq F^AF^+ = F^A


3NF

PDF 9 slide 14

Given a relation schema RFRR\forall X \to A \in F^+ : A \notin XAX is superkey


3NF pt.2

PDF 10 slide 4

Let RFRA \in RK\exists X \subset R : A \notin X \land X \to A \in F \land X \subset KAA \in RK\exists X \subset R : A \notin X \land X \to A \in F \land K \to X \in FXAX \subset RX \neq RR \to R \in F^A = F^+


3NF pt.3

PDF 10 slide 5

Given a schema RFRR\forall X \to A \in F^+ : A \notin XAX is superkey

Proof

TODO: I have it, I just have to write it out in \LaTeXRFRRF -->

BCNF (Boyce-Codd)

PDF 10 slide 20

A relation schema RF is a superkey. A relation that respects Boyce-Codd Normal Form is also in 3NF, but the opposite is not true.


(X)^+_F

PDF 11 slie 5

def clousure(R, F, X):
	Z = X
	S = { A ∈ R | Y → V ∈ F ∧ Y ⊆ Z ∧ A ∈ V }

	if S ⊆ Z:
		return Z

	return closure(R, F, Z ∪ S)

(X)^+_FXFR

Proof

Let's consider Z_i, S_iZSiZ_f, S_f \mid S_f \subseteq Z_fZ, SZ_i \subseteq (X)^+_F


Z_i \subseteq (X)^+_F

Base case

Inductive step Z_i \subseteq (X)^+F \implies Z{i + 1} \subseteq (X)^+FZ_i \subseteq (X)^+F \implies Z{i + 1} \subseteq (X)^+FS_i = \set{A \in R \mid Y \to V \in F \land Y \subseteq Z_i \land A \in V}Z{i + 1} = Z_i \cup S_iA \in Z{i + 1}A \in Z_i \impliesA \in (X)^+_FA \in S_i \implies\exists Y \to V \in F \mid Y \subseteq Z_i \land A \in V \impliesY \subseteq (X)^+_F \impliesX \to Y \in F^AY \to A \in F^A \impliesX \to A \in F^A \impliesA \in (X)^+_F


(X)^+_F \subseteq Z_fZ_fr = \set{t_1, t_2}RrZ_fR \setminus Z_ft_1t_2rV \to W \in FV, W \neq \varnothingV \nsubseteq Z_f \implies \exists A \in V : A \in R \setminus Z_f \implies t_1[V] \neq t_2[V] \implies rV \to WV \subseteq Z_fW \subseteq Z_f \impliest_1[V] = t_2[V] \land t_1[W] = t_1[W] \impliesrV \to WW \nsubseteq Z_f \impliest_1[V] = t_2[V] \land t_1[W] \neq t_2[W]


(X)^+_F \subseteq Z_f\exists V \to W \in F : rV \to W \impliesV \subseteq Z_f \land V \to W \in F \land A \in W \impliesS_f, : A \in Z_f which is a contraddiction

Legality

In the first 2 cases rV \to W \in F\implies rR


(X)^+_F \subseteq Z_fA \in (X)^+_FX \to A \in F^A = F^+r\implies rX \to YX \subseteq Z_f \implies t_1[X] = t_2[X] \impliest_1[A] = t_2[A] \impliesA \in Z_f

Z_f = (X)^+_FZ_i \subseteq (X)^+_F ; \forall i \in \mathbb{N}(X)^+_F \subseteq Z_fZ_f = (X)^+_F


Intersection Rule

PDF 12 slide 19

Given a relation scheme RFRRRX \to R \in F^+RR is in 3NF


Decomposition

PDF 13 slide 8

Let R\rhoR


Equivalence

PDF 13 slide 12

Let FGF \implies F^+ = F^+ \implies F \equiv FF \equiv G \implies F^+ = G^+ \implies G^+ = F^+ \implies G \equiv FF \equiv G \land G \equiv H \implies F^+ = G^+ \land G^+ = H^+ \implies F^+ = H^+ \implies F \equiv HFG


F \subseteq G^+ \implies F^+ \subseteq G^+

Base case

F_0 = F \subseteq G^+ \implies F_0 \subseteq G^+

Inductive Step

F_i \subseteq G^+ \implies F_{i + 1} \subseteq G^+X \to Y \in F_{i + 1} \implies X \to YY \subseteq X \impliesX \to YX \to Y \in G^+\exists Z \subseteq R, V \to W \in F_i \mid X = ZV, Y = ZW

  • transitivity

TODO


Preserving F

PDF 13 slide 15

Let RFR\rho = \set{R_1, R_2, ..., R_k}R\rhoFGG \subseteq F^+ \implies G^+ \subseteq F^+F \subseteq G^+


Dependency preservation

PDF 13 slide 17

def preserves_dependencies(R, F, ρ):
	for X → Y ∈ F:
		if Y ⊈ closure_G(R, F, ρ, X):
			return false

	return true

This algorithm is enough as we just have to check wether F \subseteq G^+X \to Y \in FX \to Y \in G^+ = G^A \iffY \subseteq (X)^+_G


(X)^+_GRFR\rho = \set{R_1, R_2, ..., R_k}RX \subseteq R(X)^+_G


Z_f \subseteq (X)^+_GZ_i, S_iZSiZ_0 = XS_f \subseteq Z_fZS -->

Base case

Z_0 = X \subseteq (X)^+_G \implies Z_0 \subseteq (X)^+_G by HP

Inductive step

Z_i \subseteq (X)^+G \implies Z{i+1} \subseteq (X)^+GS_i = \bigcup\limits{j = 1}^k (Z_i \cap R_j)^+F \cap R_jA \in Z{i + 1} = Z_i \cup S_i \implies \exists j : A \in (Z_i \cap R_j) \cap R_j \implies Z_i \cap R_j \to A \in G^AZ_i \subseteq (X)^+_G \implies X \to Z_i \in G^AZ_i = (Z_i \cap R_j) \cup VX \to Z_i \cap R_j \in G^A \impliesX \to A \in G^A


X \subseteq Y \implies (X)^+_F \subseteq (Y)^+_FX \subseteq Y \implies Y \to X \in F^AA \in (X)^+_F \impliesX \to A \in F^A \impliesY \to A \in F^A \ \impliesA \in (Y)^+_F

(X)^+_G \subseteq Z_fX \subseteq Z_f \implies (X)^+_G \subseteq (Z_f)^+_GZ_f = (Z_f)^+_GA \in S' = \set{A \in R \mid V \to W \in G \land V \subseteq Z_f \land A \in W} \implies \exists V \to W \in G : V \subseteq Z_f \land A \in W \implies \exists R_j \in \rho : VW \subseteq R_j \implies V \subseteq Z_f \cap R_j \land A \in R_j \implies A \in (Z_f \cap R_j)^+_F \cap R_j \implies A \in S_f \implies A \in Z_f


Loseless join


r \subseteq m_{\rho}(r)t \in r \implies t[R_i] \in \pi_{R_i}(r) ; \forall R_i \in \rho\mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r) = \set {\bigcup\limits_{i = 1}^k p_i[R_i] \mid p_i[R_i] \in \pi_{R_i}(r) \land \bigcup\limits_{i = 1}^k p_i[R_i] \text{ is a function}}\forall t \in r, ; t = \bigcup\limits_{i = 1}^k t[R_i]\rhoR = \bigcup\limits_{i = 1}^k R_it \in r \implies tt = \bigcup\limits_{i = 1}^k t[R_i] \in \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r) = m_{\rho}(r) \implies t \in m_{\rho}(r)\forall t \in r, ; t = \bigcup\limits_{i = 1}^k t[R_i] = \mathop{\bowtie}\limits_{i = 1}^k \set{ t[R_i] }t \in r \impliest[R_i] \in \pi_{R_i}(r) ; \forall R_i \in \rho \implies \set{ t[R_i] } \subseteq \pi_{R_i}(r) ; \forall R_i \in \rhot = \mathop{\bowtie}\limits_{i = 1}^k \set{ t[R_i] } \subseteq \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r) = m_{\rho}(r) \implies t \in m_{\rho}(r)\forall t \in rt[R_i], : R_i \in \rhot \in \set{ t[R_1] } \bowtie ... \bowtie \set{ t[R_k] } \subseteq \pi_{R_1}(r) \bowtie ... \bowtie \pi_{R_k}(r) = m_{\rho}(r) -->


\pi_{R_i}(m_{\rho}(r)) = \pi_{R_i}(r)t_{R_i} \in \pi_{R_i}(m_{\rho}(r))t_{R_i} \in \pi_{R_i}(r)t \in r \impliest[R_i] \in \pi_{R_i}(r) ; \forall R_i \in \rho\pi_{R_i}(m_{\rho}(r)) = \set{q[R_i] \mid q \in \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r)}

\pi_{R_i}(r) \subseteq \pi_{R_i}(m_{\rho}(r))t \in r \implies t \in m_{\rho}(r) \implies t[R_i] \in \pi_{R_i}(m_{\rho}(r))

\pi_{R_i}(m_{\rho}(r)) \subseteq \pi_{R_i}(r)q \in \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r) \impliesq = \mathop{\bowtie}\limits_{i = 1}^k \set{ p_i[R_i] } \mid p_i \in r \impliesqq[R_i] = p_i[R_i]p_i \in r \implies p_i[R_i] \in \pi_{R_i}(r)q[R_i] \in \pi_{R_i}(r)\exists p_1, p_2, ..., p_k \in r \mid p_i[R_i] \in \pi_{R_i}(r) ; \forall i = 1,..., kt_{R_i} \in \pi_{R_i}(m_{\rho}(r)) \iff \exists t' \in m_{\rho}(r) : t'[R_i] = t_{R_i} \iff\iff \exists t_1, ..., t_k \in r : t'[R_j] = t_j[R_j] \quad \forall R_j \in \rhot_{R_i} = t[R_i] \in \pi_{R_i}(r) -->


m_{\rho}(m_{\rho}(r)) = m_{\rho}(r)m_{\rho}(m_{\rho}(r)) = \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(m_{\rho}(r)) = \mathop{\bowtie}\limits_{i = 1}^k \pi_{R_i}(r) = m_{\rho}(r)m_{\rho}(m_{\rho}(r)) = \pi_{R_1}(m_{\rho}(r)) \bowtie ... \bowtie \pi_{R_k}(m_{\rho}(r)) = \pi_{R_1}(r) \bowtie ... \bowtie \pi_{R_k}(r) = m_{\rho}(r)m_{\rho}(m_{\rho}(r)) = \pi_{R_1}(m_{\rho}(r)) \bowtie ... \bowtie \pi_{R_k}(m_{\rho}(r)) = \pi_{R_1}(r) \bowtie ... \bowtie \pi_{R_k}(r) = m_{\rho}(r)t_{R_i} \in \pi_{R_i}(r)t_{R_i} \in \pi_{R_i}(m_{\rho}(r))t_{R_i} \in \pi_{R_i}(r) \land t' \in rt'[R_i]t'[R_i] \in t_{R_i} -->


Loseless join pt.2

PDF 15 slide 15 Given \rho = \set{R_1, R_2, ..., R_k}r|R|kija_jA \in R_{i}b_{ij}

def has_looseless_join(R, F, ρ):
	while !(∃ t ∈ r | ∀ A ∈ R, t[A] = a) and r changed:
		for X → Y ∈ F:
			for t1 ∈ r:
				for t2 ∈ r:
					if t1[X] = t2[X] and t1[Y] != t2[Y]:
						for A ∈ Y:
							if t1[A] = a:
								t2[A] = t1[A]
							else:
								t1[A] = t2[A]

	return ∃ t ∈ r | ∀ A ∈ R, t[A] = a

Correctness

PDF 15 slide 19

Let RFR\rho = \set{R_1, R_2, ..., R_k}R\rhor = m_{\rho}(r) \iff rar = m_{\rho}(r) \implies ra\LaTeX


Minimal cover

PDF 17 slide 7

Let RFRFG \equiv F\forall X \to Y \in G, |Y| = 1\forall X \to A \in G, \nexists X' \subset X \mid G \equiv (G - \set{X \to A}) \cup \set{X' \to A}\nexists X \to A \in G \mid G \equiv G - \set{X \to A}


Minimal cover (step 1)

F_1 = \set{X \to A \mid X \to Y \in F \land A \in Y}F \overset{A}{\to} F_1F_1 \overset{A}{\to} F_1^A \implies F \subseteq F_1^AF_1 \overset{A}{\to} FF \overset{A}{\to} F^A \implies F_1 \subseteq F^AF \equiv F_1


Minimal cover (step 2)

Given X \to A \in F_1, X' \subset X \land X' \to A \in F_1^+ \implies F_2 = (F_1 \setminus \set{X \to A}) \cup \set{X' \to A}X' \subseteq X \implies X \to X' \in F_1^+ \land X \to X' \in F_2^+X \to A \in F_1X \to A \in F_2 \implies X \to A \in F_2^+X \to A \notin F_2 \implies X \to X' \in F_2^+ \land X' \to A \in F_2^+ \implies X \to A \in F_2^+X \to A \in F_2X \to A \in F_1 \implies X \to A \in F_1^+X \to A \notin F_1 \implies X \to A \in F_1^+F_2 \equiv F_1 \implies F \equiv F_2\equiv relationship


Minimal cover (step 3)

X \to A \in F_2, ; A \in (X)^+{F_2 \setminus \set{X \to A}} \implies F_3 = F_2 \setminus \set{X \to A}X \to A \in F_2X \to A \in F_3 \implies X \to A \in F_3^+X \to A \notin F_3 \implies X \to A \in F_3^+A \in (X)^+{F_3}X \to A \in F_3X \to A \in F_2 \implies X \to A \in F_2^+X \to A \notin F_2F_3 = F_2 \setminus \set{X \to A}F_2 \equiv F_3 \implies F \equiv F_3


Decomposition

def decomposition(R, F: minimal cover):
	S = ∅
	ρ = ∅

	for A ∈ R | ∄ X → Y ∈ F : A ∈ XY:
		S = S ∪ {A}

	if S != ∅:
		R = R - S
		ρ = ρ ∪ {S}

	if ∃ X → Y ∈ F | XY = R:
		ρ = ρ ∪ {R}
	else:
		for X → A ∈ F:
			ρ = ρ ∪ {XA}

Decomposition pt.2

PDF 19 slide 5

Let RFR\rhoR\rho\rhoF