From 67dc6571faa164728fe608aca36059020329e9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=BClling?= <46890129+RainbowDashLabs@users.noreply.github.com> Date: Wed, 7 Sep 2022 18:47:52 +0200 Subject: [PATCH 1/3] Finish chapter 2 --- Dockerfile | 15 +++++++++ docker-compose.yml | 10 ++++++ docs/01/{index.md => .index.md} | 0 docs/02/{index.md => .index.md} | 0 docs/02/.scripts/postgres.sql | 45 +++++++++++++++++++++++++ docs/02/delete.md | 60 +++++++++++++++++++++++++++++++++ docs/02/insert.md | 11 +++--- docs/02/tables.md | 6 ++-- docs/02/update.md | 5 ++- docs/03/{index.md => .index.md} | 2 +- docs/04/{index.md => .index.md} | 0 docs/05/{index.md => .index.md} | 0 mkdocs-setup | 0 mkdocs.yml | 1 + 14 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml rename docs/01/{index.md => .index.md} (100%) rename docs/02/{index.md => .index.md} (100%) create mode 100644 docs/02/.scripts/postgres.sql rename docs/03/{index.md => .index.md} (100%) rename docs/04/{index.md => .index.md} (100%) rename docs/05/{index.md => .index.md} (100%) mode change 100644 => 100755 mkdocs-setup diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a57d946 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.10 + +RUN pip install mkdocs-material && pip install mkdocs-git-revision-date-localized-plugin + +EXPOSE 8000 + +WORKDIR "/docs" + +COPY mkdocs.yml /docs/mkdocs.yml + +COPY docs/ /docs/docs + +COPY .git/ /docs/.git/ + +ENTRYPOINT ["mkdocs", "serve"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b494f21 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.8" + +services: + docs: + volumes: + - "./docs/:/docs/" + build: + dockerfile: Dockerfile + ports: + - "80:8000" diff --git a/docs/01/index.md b/docs/01/.index.md similarity index 100% rename from docs/01/index.md rename to docs/01/.index.md diff --git a/docs/02/index.md b/docs/02/.index.md similarity index 100% rename from docs/02/index.md rename to docs/02/.index.md diff --git a/docs/02/.scripts/postgres.sql b/docs/02/.scripts/postgres.sql new file mode 100644 index 0000000..61ff223 --- /dev/null +++ b/docs/02/.scripts/postgres.sql @@ -0,0 +1,45 @@ +CREATE TABLE player +( + id INTEGER, + player_name TEXT, + last_online TIMESTAMP +); + +CREATE TABLE friend_graph +( + player_1 INTEGER, + player_2 INTEGER +); + +INSERT INTO player(id, player_name, last_online) +VALUES (1, 'Mike', '2022-05-11 00:00'::TIMESTAMP), + (2, 'Sarah', '2022-04-04 00:00'::TIMESTAMP), + (3, 'john', '2022-04-08 00:00'::TIMESTAMP), + (4, 'Lilly', '2022-04-01 00:00'::TIMESTAMP), + (5, 'Matthias', '2022-03-06 00:00'::TIMESTAMP), + (6, 'Lenny', '2022-03-08 00:00'::TIMESTAMP), + (7, 'Summer', '2022-05-22 00:00'::TIMESTAMP), + (8, 'Marry', '2022-06-04 00:00'::TIMESTAMP), + (9, 'Milana', '2022-02-12 00:00'::TIMESTAMP), + (10, 'Lexi', '2022-02-22 00:00'::TIMESTAMP); + +INSERT INTO friend_graph(player_1, player_2) +VALUES (1, 2), + (2, 3), + (4, 3), + (5, 3), + (7, 2), + (6, 1), + (6, 2), + (1, 10), + (4, 10); + +CREATE TABLE money AS ( + SELECT id, 1000.0 AS money + FROM player +); + +UPDATE money +SET money = money - 600 +WHERE id = 10 + AND money >= 600 diff --git a/docs/02/delete.md b/docs/02/delete.md index 76537e0..facf6ba 100644 --- a/docs/02/delete.md +++ b/docs/02/delete.md @@ -1 +1,61 @@ # Delete + +After inserting all our data we only miss one basic functionality. Sometimes updating or inserting data is not +enough. We also need to delete data. + +For this we use the `DELETE` keyword. The `DELETE` keyword is pretty similar to the [`SELECT` statement](select.md) we +already know. +We need to define `FROM` which table we want to delete and `WHERE` we want to delete those rows + +With this in mind we can probably assume that the general `DELETE` statement would look like this: + +```sql +DELETE +FROM table_name +WHERE column_1 = value_1; +``` + +Lets try to write a statement which would delete the user with id 10 from our users table: + + + +
+Solution + +```sql +DELETE +FROM player +WHERE id = 10; +``` + +
+ +Now we just have one more problem... Out friend graph and money table still contain references to the player with +id 10. To change this we need to delete those entries from these two tables as well. + +Write two statements. + +1. Delete all entries from friend_graph **where** the player 1 **or** player 2 has the id 10. (Do it in one statement.) +2. Delete the entry of player 10 from the `money` table (If you haven't created the money table, you can simply + ignore it.) + +
+Solution + + ```sql +DELETE +FROM friend_graph +WHERE player_1 = 10 + OR player_2 = 10; + +DELETE +FROM money +WHERE id = 10; +``` + +
+ +Of course there is a better and more save solution to avoid "dead" entries in other tables. We will learn this in +another chapter. + +Now that we know the statements for `SELECT`, `INSERT`, `UPDATE` and `DELETE` we are ready for the next chapter. diff --git a/docs/02/insert.md b/docs/02/insert.md index 51dfab5..55fbd0e 100644 --- a/docs/02/insert.md +++ b/docs/02/insert.md @@ -2,7 +2,7 @@ Now that we have created our tables we want to add data to them. -Lets start with our players +Let's start with our players | id | player\_name | last\_online | |:----|:-------------|:-----------------| @@ -43,7 +43,10 @@ use: - MariaDB/MySQL: `timestamp('2022-05-11 00:00')`\ We use the timestamp function to parse our string - SqLite: `CAST(STRFTIME('%s', '2022-05-11 00:00') AS INTEGER)`\ - We save timestamps as epoch seconds since sqlite doesnt really has a timestamp type + We save timestamps as epoch seconds since sqlite doesn't really has a timestamp type + +Now try to recreate the table from above with the previously mentioned methods and statements. Usually the time an +be ommited when it is set to 00:00.
Solution @@ -94,7 +97,7 @@ VALUES (1, 'Mike', CAST(STRFTIME('%s', '2022-05-11 00:00') AS INTEGER)),
-Lets to the same with the friend_graph +Lets to the same with the friend_graph. Try to insert following values into the `friend_graph` table. | player\_1 | player\_2 | |:----------|:----------| @@ -130,7 +133,7 @@ VALUES (1, 2), You can also use an alternative syntax to directly create a table with content. -Lets say we want to create a table with money of the players. The table should contain the id and a fixed amount of +Let's say we want to create a table with money of the players. The table should contain the id and a fixed amount of money for each player for now. ```sql diff --git a/docs/02/tables.md b/docs/02/tables.md index b2f04be..817b039 100644 --- a/docs/02/tables.md +++ b/docs/02/tables.md @@ -55,7 +55,7 @@ CREATE TABLE table_name ( col_name TYPE, col_name TYPE -) +); ``` @@ -76,7 +76,7 @@ CREATE TABLE friend_graph ( player_1 INTEGER, player_2 INTEGER -) +); ``` @@ -91,7 +91,7 @@ CREATE TABLE IF NOT EXISTS table_name ( col_name TYPE, col_name TYPE -) +); ``` diff --git a/docs/02/update.md b/docs/02/update.md index 5b61e25..1f00adc 100644 --- a/docs/02/update.md +++ b/docs/02/update.md @@ -20,10 +20,10 @@ Remember our already pretty known table of players. | 9 | Milana | 2022-02-12 00:00:00.000000 | | 10 | Lexi | 2022-02-22 00:00:00.000000 | -We are currently saving the last time the player has logged in in the `last_online` column. +We are currently saving the last time the player was online in the `last_online` column. When Lexy is online again we might need to update the `last_online` value for her again. We have now two options. The -first one is deleting the entry and inserting a new one. This is very dirty and also not a good practice. Thats why we +first one is deleting the entry and inserting a new one. This is very dirty and also not a good practice. That's why we use the `UPDATE` statement and define in the `WHERE` clause where we want to update and what we want to update. The general syntax is: @@ -62,7 +62,6 @@ WHERE id = 10;
MariaDB/MySQL -**** ```sql UPDATE player diff --git a/docs/03/index.md b/docs/03/.index.md similarity index 100% rename from docs/03/index.md rename to docs/03/.index.md index f61eb96..9ddf731 100644 --- a/docs/03/index.md +++ b/docs/03/.index.md @@ -7,10 +7,10 @@ Chapter 3 Advanced Data - Default values - Sorted indices - Conditional indices +- Auto increment - Upsert/Replace - Aggregation - Grouping -- Auto increment - Foreign keys - Basic normalization - Joins diff --git a/docs/04/index.md b/docs/04/.index.md similarity index 100% rename from docs/04/index.md rename to docs/04/.index.md diff --git a/docs/05/index.md b/docs/05/.index.md similarity index 100% rename from docs/05/index.md rename to docs/05/.index.md diff --git a/mkdocs-setup b/mkdocs-setup old mode 100644 new mode 100755 diff --git a/mkdocs.yml b/mkdocs.yml index ede9ddf..4b2470f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -29,6 +29,7 @@ nav: - 'Insert': 02/insert.md - 'Select': 02/select.md - 'Update': 02/update.md + - 'Delete': 02/delete.md theme: features: - navigation.instant From d20754e3e8ae9fe0a90553443dd5c044b9d9f1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=BClling?= <46890129+RainbowDashLabs@users.noreply.github.com> Date: Wed, 7 Sep 2022 19:12:27 +0200 Subject: [PATCH 2/3] Fix Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a57d946..fe309cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ COPY docs/ /docs/docs COPY .git/ /docs/.git/ -ENTRYPOINT ["mkdocs", "serve"] +ENTRYPOINT ["mkdocs", "serve", "-a", "0.0.0.0:8000"] From c376a5a20c313d84afefbd1f340a3b96d19cb2c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=BClling?= <46890129+RainbowDashLabs@users.noreply.github.com> Date: Wed, 7 Sep 2022 19:27:01 +0200 Subject: [PATCH 3/3] Add introduction --- docs/02/introduction.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 docs/02/introduction.md diff --git a/docs/02/introduction.md b/docs/02/introduction.md new file mode 100644 index 0000000..8c42d3f --- /dev/null +++ b/docs/02/introduction.md @@ -0,0 +1,15 @@ +# Chapter 2 + +Welcome to chapter two. Now we have set up the database of our choice and also installed a tool to access our +databases. Of course, we could start now directly with writing cool SQL queries, but first we need to talk about +some very important stuff. + +We will start with learning about operators. Especially about operators to transform and compare data. Because those +operators heavily depend on the datatypes we use we will directly head over to them and take a look at the different +types our databases offer to us. + +Before we can now finally start with creating our first database and creating our tables we will need to define some +important thing, that is naming. A clear structure in your database will make your life a lot easier and helps +others to understand what you are doing. + +After this we will take a first look on how to create tables and insert, update, select and delete data in these tables.