mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-09-10 19:15:25 +02:00
[DMDB] Fix errors
This commit is contained in:
Binary file not shown.
@@ -1,13 +1,13 @@
|
||||
\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,
|
||||
This is is the point to mention the \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 following parts:
|
||||
\begin{itemize}
|
||||
\item Data Definition Language, used to create, modify and delete schemas.
|
||||
\item Data Manipulation Language, used to insert, update and delete data
|
||||
\item Query Language, used to retrieve data
|
||||
\item Data Definition Language, used to create, modify and delete schemas.
|
||||
\item Data Manipulation Language, used to insert, update and delete data
|
||||
\item Query Language, used to retrieve data
|
||||
\end{itemize}
|
||||
|
||||
@@ -27,7 +27,7 @@ Since the primary key must be unique for each entry, it may be useful to configu
|
||||
|
||||
\inputcodewithfilename{sql}{}{code/sql/ddl/create.sql}
|
||||
|
||||
Note that PostgreSQL doesn't support \texttt{AUTO\_INCREMENT} constraints, instead use the \texttt{SERIAL} (or \texttt{BIGSERIAL} type)
|
||||
Note that PostgreSQL doesn't support \texttt{AUTO\_INCREMENT} constraints, instead use the \texttt{SERIAL} (or \texttt{BIGSERIAL}) type
|
||||
|
||||
|
||||
% TODO: Make sure all the sql statements actually execute in pgsql
|
||||
@@ -37,6 +37,8 @@ Be aware that if we ADD a column to the schema, unless a default value is provid
|
||||
|
||||
\inputcodewithfilename{sql}{}{code/sql/ddl/alter.sql}
|
||||
|
||||
Instead of a \texttt{ADD COLUMN} or \texttt{DROP COLUMN}, we can also use \texttt{ADD CONSTRAINT} and \texttt{DROP CONSTRAINT} to update constraints
|
||||
|
||||
|
||||
\subsubsection{Deleting Tables}
|
||||
|
||||
|
||||
@@ -18,20 +18,21 @@ To rename, we can set \texttt{I = Column as Name, Column2 as Name2}, etc
|
||||
|
||||
Since \sql\ implements \gls{bag} and not set semantics, there is a \texttt{DISTINCT} keyword, which is used to remove duplicates.
|
||||
It is to be applied in the \texttt{SELECT} clause (\texttt{I} above), e.g.
|
||||
\mint{sql}|SELECT DISTINCT name FROM Data;|
|
||||
\mint{sql}|SELECT DISTINCT name FROM data_table;|
|
||||
|
||||
For the last three operations, \texttt{R1} and \texttt{R2} typically are other \sql\ statements, example:
|
||||
For the last three operations in the table above, \texttt{R1} and \texttt{R2} typically are other \sql\ statements, example:
|
||||
\mint{sql}|(SELECT name FROM FirstTable) UNION (SELECT name FROM SecondTable)|
|
||||
In addition, to apply \textit{bag semantics}, as opposed to \textit{set semantics} append \texttt{ALL} to the expression (e.g. \texttt{UNION ALL})
|
||||
In addition, to apply \textit{bag semantics}, as opposed to \textit{set semantics} append \texttt{ALL} to the \texttt{UNION} expression.
|
||||
|
||||
We can also name the tables, e.g. $\texttt{T} = \texttt{One o, Two t}$, and then access columns from a specific table using
|
||||
$\texttt{I} = \texttt{o.Col, b.Col}$, or the like.
|
||||
|
||||
String concatenation works using \texttt{CONCAT('string', 'string', 'string', ...)}, or using \texttt{'string' || 'string' || \dots}. Note that SQL uses single quote for Strings,
|
||||
and double quotes for renaming columns (using \texttt{AS} statements, or in \texttt{SELECT})
|
||||
String concatenation works using \texttt{CONCAT('string', 'string', 'string', ...)}, or using \texttt{'string' || 'string' || \dots}.
|
||||
Note that SQL uses single quotes for Strings, and double quotes for renaming columns (using \texttt{AS} statements, or in \texttt{SELECT})
|
||||
|
||||
Dates work as you'd expect. We can create a new date using \texttt{Date('ISO-date-string')}. \texttt{DATETIME} combines date and time and \texttt{TIME} is just the time.
|
||||
To compute time delta, we can use \texttt{DATEDIFF('interval', DateOne, DateTwo)}, where the interval can be:
|
||||
\texttt{year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond}.
|
||||
To get just the year from an existing date object (or string), use \texttt{YEAR}. The result is an integer and thus you can use comparison operations with numbers.
|
||||
|
||||
The current date, time, year, etc is provided using \texttt{current\_date} (etc).
|
||||
|
||||
@@ -6,8 +6,8 @@ The following \sql\ logical operators are available (may not be exhaustive for a
|
||||
\begin{tables}{p{4.5cm}p{12cm}}{Operator & Description}
|
||||
\texttt{P1 AND P2} & TRUE if \texttt{P1} and \texttt{P2} both are true \\
|
||||
\texttt{P1 OR P2} & TRUE if one of \texttt{P1} or \texttt{P2}, or both are true \\
|
||||
\texttt{C cop ALL query} & TRUE if all values \texttt{x} returned in \gls{subquery}\ \texttt{query} fulfil \texttt{C cop x} \\
|
||||
\texttt{C cop ANY query} & TRUE if one (or more) values \texttt{x} returned in \gls{subquery}\ \texttt{query} fulfil \texttt{C cop x}.
|
||||
\texttt{C cop ALL query} & TRUE if all values \texttt{x} returned in \gls{subquery}\ \texttt{query} fulfil \texttt{C cop x} \\
|
||||
\texttt{C cop ANY query} & TRUE if one (or more) values \texttt{x} returned in \gls{subquery}\ \texttt{query} fulfil \texttt{C cop x}.
|
||||
\texttt{SOME} is logically equivalent \\
|
||||
\texttt{C BETWEEN low AND high} & TRUE if numerical, text or date value of \texttt{C} is between \texttt{low} and \texttt{high}. Both ends inclusive, negatable \\
|
||||
\texttt{C IN list} & TRUE if value of \texttt{C} is in the provided \texttt{list}.
|
||||
@@ -24,12 +24,16 @@ The following \sql\ logical operators are available (may not be exhaustive for a
|
||||
|
||||
\shade{gray}{Arithmetic Operators} $+, -, *, /, \%$ are defined as usual.
|
||||
|
||||
This also means that we can use \acrshort{sql} as a calculator, for example::
|
||||
\mint{sql}|SELECT a * b|
|
||||
|
||||
We can use the arithmetic operators like this:
|
||||
\mint{sql}|SELECT Value * 1.1 FROM Data;|
|
||||
or in \texttt{WHERE} clauses like this:
|
||||
\mint{sql}|SELECT Value FROM (Data a, Data b) WHERE a.Value = b.Value - 1;|
|
||||
|
||||
\inlineremark You may have noticed that in the above query, the same table was used twice.
|
||||
This is referred to as a \bi{Self-Join} and can come in handy when comparing values in a single table.
|
||||
This is referred to as a \bi{Self-Join} and can come in handy when comparing values in a single table,
|
||||
or to build pairs.
|
||||
|
||||
We can use comparison operators also with subqueries containing aggregations, if they return a single result. \TODO Check that this doesn't work with more than a single result
|
||||
We can use comparison operators also with subqueries containing aggregations, if they return a single result, for multiple results, need \texttt{ANY}, \texttt{ALL}, etc (see above)
|
||||
|
||||
@@ -18,7 +18,10 @@ These are parenthesized normal SQL queries.
|
||||
We can create an alias for them using an \texttt{AS name} clause after the parenthesis as follows
|
||||
\mint{sql}|SELECT r.name FROM (SELECT * FROM Table) AS r;|
|
||||
or alternatively, using a \texttt{WITH} clause (replace the dummy queries with real queries):
|
||||
\mint{sql}|WITH CteName (SELECT * FROM Table) SELECT name FROM CteName;|
|
||||
\mint{sql}|WITH CteName AS (SELECT * FROM Table) SELECT name FROM CteName;|
|
||||
Note that it is possible to have multiple with statements as follows:
|
||||
\mint{sql}|WITH with1 AS (SELECT * FROM Table1), with2 AS (SELECT * FROM with1) SELECT name FROM with2|
|
||||
\texttt{WITH} clauses are an implementation of \bi{Common Table Expressions} (CTE)
|
||||
|
||||
|
||||
\paragraph{Join Operations}
|
||||
@@ -28,10 +31,11 @@ If we don't specify the kind of join, an \texttt{INNER JOIN} is executed. The fo
|
||||
\mint{sql}|SELECT * FROM Student JOIN Tests USING (PersNr);|
|
||||
For the latter of the three, the parenthesis around the column are important, as omitting them is invalid syntax.
|
||||
|
||||
Other types of \texttt{JOIN} are:
|
||||
Other types of \texttt{JOIN} are (the latter two are considered to be \texttt{OUTER JOIN}s):
|
||||
\begin{itemize}
|
||||
\item \texttt{NATURAL JOIN} (no \texttt{ON} required), joins on columns with same name
|
||||
\item \texttt{LEFT/RIGHT/FULL OUTER JOIN} (requires \texttt{ON}), also returns non-matching rows from left, right or both sides, respectively
|
||||
\item \texttt{LEFT/RIGHT JOIN} (requires \texttt{ON}), also returns non-matching rows from left or right side, respectively
|
||||
\item \texttt{FULL JOIN} (or \texttt{FULL OUTER JOIN}; requires \texttt{ON}), also returns non-matching rows from both sides
|
||||
\end{itemize}
|
||||
|
||||
\begin{center}
|
||||
|
||||
@@ -29,6 +29,7 @@ It applies a comparison to a grouping column or an aggregate as specified in the
|
||||
\paragraph{Sorting}
|
||||
We can sort the output of any query using the \texttt{ORDER BY column DIRECTION} clause, where \texttt{DIRECTION} is to be replaced with \texttt{ASC} or \texttt{DESC}:
|
||||
\mint{sql}|SELECT name FROM Students ORDER BY name DESC;|
|
||||
We can also specify more than one order by condition, separated with a comma.
|
||||
|
||||
|
||||
\paragraph{Limit}
|
||||
|
||||
@@ -11,7 +11,7 @@ Usability is also often much better due to having less complex queries to write
|
||||
|
||||
\shade{orange}{How a view is evaluated}
|
||||
This is quite simple in essence, the \texttt{VIEW}'s query is inserted into the query using the view.
|
||||
It then is then optimized as it normally would be, too. Of course, it is likely that more optimization can and has to be done on views.
|
||||
It is then optimized as it normally would be, too. Of course, it is likely that more optimization can and has to be done on views.
|
||||
|
||||
|
||||
\paragraph{Updatable Views}
|
||||
|
||||
Reference in New Issue
Block a user