[DMDB] relational model, relational algebra, SQL very basics

This commit is contained in:
2026-06-22 15:28:27 +02:00
parent cef70242ce
commit cf0bdbaff8
19 changed files with 157 additions and 18 deletions
+9
View File
@@ -0,0 +1,9 @@
\acrshort{sql}, or unabbreviated, \acrlong{sql}, is a \gls{declarative} programming language, used to describe and operate on databases.
This is is the point to mention \hlhref{https://xkcd.com/927/}{xkcd standards comic}, because, as is the case with almost any standard,
people deviate from it and new standards form.
Even though \textit{most} \acrshort{sql} databases support the standard SQL operations, many have added a lot of features on top.
\sql\ is split up into the query language itself, used to operate on data, as well as the Data Definition Language,
used to create, modify and delete schemas.
+30
View File
@@ -0,0 +1,30 @@
\subsection{Data Definition Language (DDL)}
The DDL is used to describe the schema of relations, in \sql\ called tables.
For that, we provide a table name, the columns and their data types.
\sql\ supports the following data types (plus more):
\begin{multicols}{2}
\begin{itemize}[itemsep=-2pt]
\item \texttt{char(n)}
\item \texttt{varchar(n)}
\item \texttt{integer}
\item \texttt{blob} or \texttt{raw} (for large binaries)
\item \texttt{date}
\end{itemize}
\end{multicols}
\subsubsection{Creating Tables}
Use the DDL \texttt{CREATE TABLE} keyword to create a table. We can set default values for certain attributes,
or can add constraints to them
% TODO: Constraints (such as NOT NULL)
\inputcodewithfilename{sql}{}{code/sql/ddl/create.sql}
\subsubsection{Updating / Altering Tables}
\subsubsection{Deleting Tables}
@@ -0,0 +1,33 @@
\subsection{Operations}
\begin{tables}{ll}{Keyword & Description}
\texttt{SELECT val, val2} & returns the columns \texttt{val} and \texttt{val2} \\
\texttt{FROM table d, table2 c} & selects the target tables. Can use range variables (\texttt{d, c} here) \\
\texttt{WHERE} & Filter results. Can also target non-acquired columns \\
\texttt{DISTINCT} & Returns elements without duplicates \\
\texttt{IN} & Checks if the element is present in subquery \\
\texttt{UNION} & Set union (also removes duplicates) \\
\texttt{INTERSECT} & Set intersection \\
\texttt{EXCEPT} & Set difference \\
\texttt{JOIN} & Join tables, always requires a ON clause \\
\texttt{ON} & Specify on which column with what condition to join the tables \\
\texttt{DATE} & Used to do comparisons against fixed Dates (followed by Datestring) \\
\texttt{AS} & Rename a column \\
\texttt{AVG(column)} & Computes the average of the specified column \\
\texttt{SUM(column)} & Computes the sum of the specified column \\
\end{tables}
We can directly add new columns to our result using statements like (also note that we do not actually return salary and employment columns)
\mint{sql}{SELECT name, salary * employment / 100 AS RealSalary FROM employees;}
When applying \texttt{GROUP BY}, COUNT and the like operate on each group, not entire dataset.
\texttt{NATURAL JOIN} doesn't require a ON clause (it looks for columns with same name)
After \texttt{GROUP BY} we can't use \texttt{WHERE}, instead we can use \texttt{HAVING}, which operates on groups instead of on rows.
We can compare against a query with a single result column and row using a \texttt{WHERE} clause.
The \texttt{CROSS JOIN} is the both-sided extension to \texttt{LEFT JOIN} and \texttt{RIGHT JOIN}
We can compute the number of all passed grades for a person using something like
\mint{sql}+SUM(CASE Grade >= 1 THEN 1 ELSE 0 END) AS PassedSubjectsCount+
+6
View File
@@ -0,0 +1,6 @@
\newsection
\section{SQL}
\input{parts/01_sql/00_intro.tex}
\input{parts/01_sql/01_ddl.tex}
\input{parts/01_sql/01_operations.tex}
% \input{parts/00_sql/}