diff --git a/semester4/dmdb/code/sql/ddl/alter.sql b/semester4/dmdb/code/sql/ddl/alter.sql index e69de29..ee29451 100644 --- a/semester4/dmdb/code/sql/ddl/alter.sql +++ b/semester4/dmdb/code/sql/ddl/alter.sql @@ -0,0 +1,3 @@ +ALTER TABLE TableName ADD COLUMN (col integer); +ALTER TABLE TableName ADD COLUMN (AnotherColumn integer default ""); +ALTER TABLE TableName DROP COLUMN AnotherColumn; diff --git a/semester4/dmdb/code/sql/ddl/create.sql b/semester4/dmdb/code/sql/ddl/create.sql index c7e7836..911a73e 100644 --- a/semester4/dmdb/code/sql/ddl/create.sql +++ b/semester4/dmdb/code/sql/ddl/create.sql @@ -1,6 +1,6 @@ CREATE TABLE TableName ( Attribute integer, OtherAttribute varchar (30), - NextAttribute character (2) default "AP", - PRIMARY KEY (Attribute) + NextAttribute character (2) default "AP", -- default value, if unset on insert + PRIMARY KEY (Attribute) -- primary key, as in RA ); diff --git a/semester4/dmdb/code/sql/ddl/delete.sql b/semester4/dmdb/code/sql/ddl/delete.sql index e69de29..3ccfa83 100644 --- a/semester4/dmdb/code/sql/ddl/delete.sql +++ b/semester4/dmdb/code/sql/ddl/delete.sql @@ -0,0 +1 @@ +DROP TABLE TableName; diff --git a/semester4/dmdb/code/sql/dml.sql b/semester4/dmdb/code/sql/dml.sql new file mode 100644 index 0000000..cd33d90 --- /dev/null +++ b/semester4/dmdb/code/sql/dml.sql @@ -0,0 +1,11 @@ +INSERT INTO TableName ( + Attribute, + OtherAttribute +) VALUES ( 1000, 'Value' ); + +DELETE FROM TableName WHERE Attribute > 50; + +UPDATE TableName SET Attribute = Attribute + 1; + +-- Import data from a CSV file (this is pgsql specific syntax) +COPY TableName FROM '/data.csv' WITH FORMAT csv; diff --git a/semester4/dmdb/data-modelling-databases-summary.pdf b/semester4/dmdb/data-modelling-databases-summary.pdf index f3de0b6..ff135d3 100644 Binary files a/semester4/dmdb/data-modelling-databases-summary.pdf and b/semester4/dmdb/data-modelling-databases-summary.pdf differ diff --git a/semester4/dmdb/parts/01_sql/00_intro.tex b/semester4/dmdb/parts/01_sql/00_intro.tex index a85fd1b..be7c846 100644 --- a/semester4/dmdb/parts/01_sql/00_intro.tex +++ b/semester4/dmdb/parts/01_sql/00_intro.tex @@ -5,5 +5,9 @@ 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. +\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 +\end{itemize} diff --git a/semester4/dmdb/parts/01_sql/01_ddl.tex b/semester4/dmdb/parts/01_sql/01_ddl.tex index 66ba79c..714b09a 100644 --- a/semester4/dmdb/parts/01_sql/01_ddl.tex +++ b/semester4/dmdb/parts/01_sql/01_ddl.tex @@ -4,7 +4,7 @@ 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] + \begin{itemize}[itemsep=-2pt] \item \texttt{char(n)} \item \texttt{varchar(n)} \item \texttt{integer} @@ -16,15 +16,21 @@ For that, we provide a table name, the columns and their data types. \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 +or can add constraints to them. % TODO: Constraints (such as NOT NULL) \inputcodewithfilename{sql}{}{code/sql/ddl/create.sql} - +% TODO: Make sure all the sql statements actually execute in pgsql \subsubsection{Updating / Altering Tables} +Use the DDL \texttt{ALTER TABLE} keyword to update a table's schema. +Be aware that if we ADD a column to the schema, unless a default value is provided, the column is set to \texttt{NULL} (see \ref{sec:sql-null}) + +\inputcodewithfilename{sql}{}{code/sql/ddl/alter.sql} \subsubsection{Deleting Tables} + +\inputcodewithfilename{sql}{}{code/sql/ddl/delete.sql} diff --git a/semester4/dmdb/parts/01_sql/02_dml.tex b/semester4/dmdb/parts/01_sql/02_dml.tex new file mode 100644 index 0000000..202d75c --- /dev/null +++ b/semester4/dmdb/parts/01_sql/02_dml.tex @@ -0,0 +1,8 @@ +\newpage +\subsection{Data Manipulation Language (DML)} +The DML is used to insert, update and delete data from the database. + +Below some examples showing the use of the common operations. +Note that you can use a \texttt{WHERE} clause from the query language to filter data for both \texttt{DELETE} and \texttt{UPDATE} statements. + +\inputcodewithfilename{sql}{}{code/sql/dml.sql} diff --git a/semester4/dmdb/parts/01_sql/03_query-language/00_intro.tex b/semester4/dmdb/parts/01_sql/03_query-language/00_intro.tex new file mode 100644 index 0000000..e69de29 diff --git a/semester4/dmdb/parts/01_sql/main.tex b/semester4/dmdb/parts/01_sql/main.tex index 5ade079..58403be 100644 --- a/semester4/dmdb/parts/01_sql/main.tex +++ b/semester4/dmdb/parts/01_sql/main.tex @@ -2,5 +2,6 @@ \section{SQL} \input{parts/01_sql/00_intro.tex} \input{parts/01_sql/01_ddl.tex} +\input{parts/01_sql/02_dml.tex} \input{parts/01_sql/01_operations.tex} % \input{parts/00_sql/} diff --git a/semester4/dmdb/postgres-language-server.jsonc b/semester4/dmdb/postgres-language-server.jsonc new file mode 100644 index 0000000..010eb53 --- /dev/null +++ b/semester4/dmdb/postgres-language-server.jsonc @@ -0,0 +1,17 @@ +{ + "$schema": "https://pg-language-server.com/latest/schema.json", + "vcs": { + "enabled": false, + "clientKind": "git", + "useIgnoreFile": false + }, + "files": { + "ignore": [] + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + } +}