[DMDB] Completed normal forms, start query optimization

This commit is contained in:
2026-06-30 16:21:35 +02:00
parent 4b4b74fcb6
commit 59e4bcd5e9
19 changed files with 168 additions and 13 deletions
@@ -1,4 +1,5 @@
\subsection{Functional Dependency}
\label{sec:functional-dependency}
As with relational algebra, we again define a schema to be a relation, e.g. $\cR(A:D_A, B:D_B, C:D_C, D: D_D)$,
with an instance of it being $R \subseteq D_A \times D_B \times D_C \times D_D$.
@@ -12,9 +13,10 @@ and $R \vdash \alpha \rightarrow \beta$ if the same applies syntactically (i.e.
\inlineintuition This means that there is a function that maps the values of columns $\alpha$ to the values of columns $\beta$.
\inlinedefinition $\alpha \subseteq \R$ is a \bi{superkey} if and only if $\alpha \rightarrow \cR$.
\inlinedefinition $\alpha \subseteq \R$ is a \gls{superkey} if and only if $\alpha \rightarrow \cR$.
\inlineintuition This means that if we know the values of columns $\alpha$, we know the value of the rest of the columns in $\cR$
\inlineintuition This means that if we know the values of columns $\alpha$, we know the value of the rest of the columns in $\cR$.
It is a superset of some candidate keys (see \ref{sec:relations-keys})
\inlinedefinition $\alpha \rightarrow \beta$ is minimal if and only if $\forall A \in \alpha : (\alpha - \{ A \}) \centernot{\rightarrow} \beta$.
@@ -26,7 +26,7 @@ We can use that to check the following:
\item $F \vdash \alpha \rightarrow \gamma$: Calculate $\alpha^+$ and check $\gamma \in \alpha^+$
\item $F \vdash G$: For each $\alpha \rightarrow \gamma \in G$, check $F \vdash \alpha \rightarrow \gamma$
\item $F$ equivalent to $G$: Check $F \vdash G$ and $G \vdash F$
\item $K \subseteq \cR$ superkey for $F$: Check $F \vdash K \rightarrow \cR$:
\item $K \subseteq \cR$ \gls{superkey} for $F$: Check $F \vdash K \rightarrow \cR$:
\end{itemize}
@@ -1,5 +1,5 @@
\subsubsection{First Normal Form}
In the first normal form, only atomic domains are allowed.
In the first normal form, only atomic domains are allowed, i.e. keys are not allowed to be arrays.
\inlineintuition This makes the concept of a key easier to define, or well defined
@@ -1,10 +1,16 @@
\subsection{Second Normal Form}
The two dependencies $\{ \text{Legi}, \text{Nr} \} \rightrightarrow \{ \text{Name}, \text{Semester} \}$,
$\{ \text{Legi} \} \rightrightarrow \{ \text{Name}, \text{Semester} \}$ state the following:
% The two dependencies $\{ \text{Legi}, \text{Nr} \} \rightrightarrow \{ \text{Name}, \text{Semester} \}$,
% $\{ \text{Legi} \} \rightrightarrow \{ \text{Name}, \text{Semester} \}$ state the following:
%
% \begin{itemize}
% \item Legi determines the name and semester of a student.
% \item The first map has to happen, even though mapping $\{ \text{Legi}, \text{Nr} \}$ to $\{ \text{Legi} \}$ would be enough. We thus will have redundancy.
% However, this is the exact relationship that the table is meant to capture
% Thus, $\{ \text{Name}, \text{Semester} \}$ should not be in the table at all.
% \end{itemize}
\begin{itemize}
\item Legi determines the name and semester of a student.
\item The first map has to happen, even though mapping $\{ \text{Legi}, \text{Nr} \}$ to $\{ \text{Legi} \}$ would be enough. We thus will have redundancy.
However, this is the exact relationship that the table is meant to capture
Thus, $\{ \text{Name}, \text{Semester} \}$ should not be in the table at all.
\end{itemize}
For a relation to be in 2NF, every non-key attribute needs to minimally dependent on \bi{every} key, i.e. no attribute depends on part of a key.
If relations are not in 2NF, they may experience insert, update and delete anomalies,
which can lead to incorrect, redundant or inconsistent updates of the relations.
However, some relations can still suffer from update and / or delete anomalies
@@ -0,0 +1,11 @@
\subsubsection{Third Normal Form}
A relation $R$ is in 3NF if and only if for all $\alpha \rightarrow B$, at least one of the following conditions holds:
\begin{itemize}
\item $B \in \alpha$ (then $\alpha \rightarrow B$ is a trivial FD)
\item $B$ is an attribute of at least one key
\item $\alpha$ is a \gls{superkey} of $R$
\end{itemize}
\inlineintuition if $\alpha \rightarrow B$ does not satisfy any of these conditions, $\alpha$ is a concept in its own right,
thus, the 3NF tries to get rid of ``transitive dependencies'' (e.g. $A \rightarrow B, B \rightarrow C$)
The 3NF can still experience update and delete anomalies.
@@ -0,0 +1,13 @@
\subsubsection{Boyce-Codd Normal Form (BCNF)}
$R$ is in BCNF if and only if for all $\alpha \rightarrow B$ at least one of the following conditions holds:
\begin{itemize}
\item $B \in \alpha$ (thus, $\alpha \rightarrow B$ is a trivial FD)
\item $\alpha$ is a \gls{superkey} of $R$
\end{itemize}
\inlineintuition In each relation, you only store the same information once.
The following FD are okay in both 3NF and BCNF because both left hand sides are candidate keys for the table.
\begin{itemize}
\item $\{ \text{PersNr} \} \rightarrow $ Name, Level, Room, City, Street, Canton
\item $\{ \text{Room} \} \rightarrow $ PersNr
\end{itemize}
@@ -0,0 +1,25 @@
\subsubsection{BCNF Decomposition Algorithm}
\begin{algorithm}
\caption{Decomposition algorithm}
\begin{algorithmic}[1]
\Procedure{Decomposition}{$\cR$}
\State \texttt{result} $\gets \{ \cR \}$
\While{$\exists \cR_i$ in \texttt{result} such that $\cR_i$ is not in BCFN}
\State let $\alpha \rightarrow \beta$ be the evil FD
\State $\cR_i^1 \gets \alpha \cup \beta$
\State $\cR_i^2 \gets \cR_i - \beta$
\State $\texttt{result} \gets (\texttt{result} - \cR_i) \cup \{ \cR_i^1, \cR_i^2 \}$
\EndWhile
\State \Return \texttt{result}
\EndProcedure
\end{algorithmic}
\end{algorithm}
In words, this algorithm takes as input a schema $\cR$ and outputs a list of schemas $\cL = \cR_1, \ldots, \cR_n$,
such that $\cL$ is a lossless decomposition of $\cR$ (i.e. joining them together will perfectly recover the information in $\cR$)
and each of the $\cR_i$ are in BCNF.
Of note is that this does \bi{not} preserve the functional dependencies and some data redundancies will still remain,
only the ones caused by functional dependencies.
In addition, the order of picking the evil FD matters, so we use heuristics, which pick the one with the largest right hand side.
@@ -0,0 +1,19 @@
\subsubsection{3NF Synthesis Algorithm}
\begin{algorithm}
\caption{Synthesis algorithm}
\begin{algorithmic}[1]
\Procedure{Synthesis}{$\cR$}
% TODO: what is the minimal basis?
\State Compute the minimal basis $F_c$ of $F$
\For{\textbf{all} $\alpha \rightarrow \beta \in F_c$}
\State create $R_{\alpha \cup \beta}(\alpha \cup \beta)$
\EndFor
\State If none of the above relations contains a superkey, add a relation with a key
\State Eliminate $R_\alpha$ if there exists $R_\alpha'$ such that $\alpha \subseteq \alpha'$
\EndProcedure
\end{algorithmic}
\end{algorithm}
In words, this algorithm takes as input a schema $\cR$ and outputs a list of schemas $\cL = \cR_1, \ldots, \cR_n$,
such that $\cL$ is a lossless decomposition of $\cR$ and each of the $\cR_i$ are in 3NF.
This algorithm preserves all functional dependencies, however does not remove redundancies because it is not BCNF.
@@ -14,6 +14,8 @@
\input{parts/02_theory-background/01_normal-forms/02_second-nf.tex}
\input{parts/02_theory-background/01_normal-forms/03_third-nf.tex}
\input{parts/02_theory-background/01_normal-forms/04_bncf.tex}
\input{parts/02_theory-background/01_normal-forms/05_decomposition-algorithm.tex}
\input{parts/02_theory-background/01_normal-forms/06_synthesis-algorightm.tex}
% \input{parts/02_theory-background/01_normal-forms/}
% \input{parts/02_theory-background/}