mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-07-27 21:29:09 +02:00
[DMDB] SQL intro (DML, DDL)
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
CREATE TABLE TableName (
|
CREATE TABLE TableName (
|
||||||
Attribute integer,
|
Attribute integer,
|
||||||
OtherAttribute varchar (30),
|
OtherAttribute varchar (30),
|
||||||
NextAttribute character (2) default "AP",
|
NextAttribute character (2) default "AP", -- default value, if unset on insert
|
||||||
PRIMARY KEY (Attribute)
|
PRIMARY KEY (Attribute) -- primary key, as in RA
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE TableName;
|
||||||
|
|||||||
@@ -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;
|
||||||
Binary file not shown.
@@ -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.
|
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,
|
\sql\ is split up into the following parts:
|
||||||
used to create, modify and delete schemas.
|
\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}
|
||||||
|
|||||||
@@ -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):
|
\sql\ supports the following data types (plus more):
|
||||||
\begin{multicols}{2}
|
\begin{multicols}{2}
|
||||||
\begin{itemize}[itemsep=-2pt]
|
\begin{itemize}[itemsep=-2pt]
|
||||||
\item \texttt{char(n)}
|
\item \texttt{char(n)}
|
||||||
\item \texttt{varchar(n)}
|
\item \texttt{varchar(n)}
|
||||||
\item \texttt{integer}
|
\item \texttt{integer}
|
||||||
@@ -16,15 +16,21 @@ For that, we provide a table name, the columns and their data types.
|
|||||||
|
|
||||||
\subsubsection{Creating Tables}
|
\subsubsection{Creating Tables}
|
||||||
Use the DDL \texttt{CREATE TABLE} keyword to create a table. We can set default values for certain attributes,
|
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)
|
% TODO: Constraints (such as NOT NULL)
|
||||||
|
|
||||||
\inputcodewithfilename{sql}{}{code/sql/ddl/create.sql}
|
\inputcodewithfilename{sql}{}{code/sql/ddl/create.sql}
|
||||||
|
|
||||||
|
|
||||||
|
% TODO: Make sure all the sql statements actually execute in pgsql
|
||||||
\subsubsection{Updating / Altering Tables}
|
\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}
|
\subsubsection{Deleting Tables}
|
||||||
|
|
||||||
|
\inputcodewithfilename{sql}{}{code/sql/ddl/delete.sql}
|
||||||
|
|||||||
@@ -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}
|
||||||
@@ -2,5 +2,6 @@
|
|||||||
\section{SQL}
|
\section{SQL}
|
||||||
\input{parts/01_sql/00_intro.tex}
|
\input{parts/01_sql/00_intro.tex}
|
||||||
\input{parts/01_sql/01_ddl.tex}
|
\input{parts/01_sql/01_ddl.tex}
|
||||||
|
\input{parts/01_sql/02_dml.tex}
|
||||||
\input{parts/01_sql/01_operations.tex}
|
\input{parts/01_sql/01_operations.tex}
|
||||||
% \input{parts/00_sql/}
|
% \input{parts/00_sql/}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user