Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2b14c3e

Browse files
authoredSep 12, 2022
Chapter/02 (#36)
* Finish chapter 2 * Fix Dockerfile * Add introduction
1 parent c989cce commit 2b14c3e

15 files changed

+159
-11
lines changed
 

‎Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM python:3.10
2+
3+
RUN pip install mkdocs-material && pip install mkdocs-git-revision-date-localized-plugin
4+
5+
EXPOSE 8000
6+
7+
WORKDIR "/docs"
8+
9+
COPY mkdocs.yml /docs/mkdocs.yml
10+
11+
COPY docs/ /docs/docs
12+
13+
COPY .git/ /docs/.git/
14+
15+
ENTRYPOINT ["mkdocs", "serve", "-a", "0.0.0.0:8000"]

‎docker-compose.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3.8"
2+
3+
services:
4+
docs:
5+
volumes:
6+
- "./docs/:/docs/"
7+
build:
8+
dockerfile: Dockerfile
9+
ports:
10+
- "80:8000"

‎docs/01/index.md ‎docs/01/.index.md

File renamed without changes.

‎docs/02/index.md ‎docs/02/.index.md

File renamed without changes.

‎docs/02/.scripts/postgres.sql

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
CREATE TABLE player
2+
(
3+
id INTEGER,
4+
player_name TEXT,
5+
last_online TIMESTAMP
6+
);
7+
8+
CREATE TABLE friend_graph
9+
(
10+
player_1 INTEGER,
11+
player_2 INTEGER
12+
);
13+
14+
INSERT INTO player(id, player_name, last_online)
15+
VALUES (1, 'Mike', '2022-05-11 00:00'::TIMESTAMP),
16+
(2, 'Sarah', '2022-04-04 00:00'::TIMESTAMP),
17+
(3, 'john', '2022-04-08 00:00'::TIMESTAMP),
18+
(4, 'Lilly', '2022-04-01 00:00'::TIMESTAMP),
19+
(5, 'Matthias', '2022-03-06 00:00'::TIMESTAMP),
20+
(6, 'Lenny', '2022-03-08 00:00'::TIMESTAMP),
21+
(7, 'Summer', '2022-05-22 00:00'::TIMESTAMP),
22+
(8, 'Marry', '2022-06-04 00:00'::TIMESTAMP),
23+
(9, 'Milana', '2022-02-12 00:00'::TIMESTAMP),
24+
(10, 'Lexi', '2022-02-22 00:00'::TIMESTAMP);
25+
26+
INSERT INTO friend_graph(player_1, player_2)
27+
VALUES (1, 2),
28+
(2, 3),
29+
(4, 3),
30+
(5, 3),
31+
(7, 2),
32+
(6, 1),
33+
(6, 2),
34+
(1, 10),
35+
(4, 10);
36+
37+
CREATE TABLE money AS (
38+
SELECT id, 1000.0 AS money
39+
FROM player
40+
);
41+
42+
UPDATE money
43+
SET money = money - 600
44+
WHERE id = 10
45+
AND money >= 600

‎docs/02/delete.md

+60
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
11
# Delete
2+
3+
After inserting all our data we only miss one basic functionality. Sometimes updating or inserting data is not
4+
enough. We also need to delete data.
5+
6+
For this we use the `DELETE` keyword. The `DELETE` keyword is pretty similar to the [`SELECT` statement](select.md) we
7+
already know.
8+
We need to define `FROM` which table we want to delete and `WHERE` we want to delete those rows
9+
10+
With this in mind we can probably assume that the general `DELETE` statement would look like this:
11+
12+
```sql
13+
DELETE
14+
FROM table_name
15+
WHERE column_1 = value_1;
16+
```
17+
18+
Lets try to write a statement which would delete the user with id 10 from our users table:
19+
20+
21+
22+
<details>
23+
<summary>Solution</summary>
24+
25+
```sql
26+
DELETE
27+
FROM player
28+
WHERE id = 10;
29+
```
30+
31+
</details>
32+
33+
Now we just have one more problem... Out friend graph and money table still contain references to the player with
34+
id 10. To change this we need to delete those entries from these two tables as well.
35+
36+
Write two statements.
37+
38+
1. Delete all entries from friend_graph **where** the player 1 **or** player 2 has the id 10. (Do it in one statement.)
39+
2. Delete the entry of player 10 from the `money` table (If you haven't created the money table, you can simply
40+
ignore it.)
41+
42+
<details>
43+
<summary>Solution</summary>
44+
45+
```sql
46+
DELETE
47+
FROM friend_graph
48+
WHERE player_1 = 10
49+
OR player_2 = 10;
50+
51+
DELETE
52+
FROM money
53+
WHERE id = 10;
54+
```
55+
56+
</details>
57+
58+
Of course there is a better and more save solution to avoid "dead" entries in other tables. We will learn this in
59+
another chapter.
60+
61+
Now that we know the statements for `SELECT`, `INSERT`, `UPDATE` and `DELETE` we are ready for the next chapter.

‎docs/02/insert.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Now that we have created our tables we want to add data to them.
44

5-
Lets start with our players
5+
Let's start with our players
66

77
| id | player\_name | last\_online |
88
|:----|:-------------|:-----------------|
@@ -43,7 +43,10 @@ use:
4343
- MariaDB/MySQL: `timestamp('2022-05-11 00:00')`\
4444
We use the timestamp function to parse our string
4545
- SqLite: `CAST(STRFTIME('%s', '2022-05-11 00:00') AS INTEGER)`\
46-
We save timestamps as epoch seconds since sqlite doesnt really has a timestamp type
46+
We save timestamps as epoch seconds since sqlite doesn't really has a timestamp type
47+
48+
Now try to recreate the table from above with the previously mentioned methods and statements. Usually the time an
49+
be ommited when it is set to 00:00.
4750

4851
<details>
4952
<summary>Solution</summary>
@@ -94,7 +97,7 @@ VALUES (1, 'Mike', CAST(STRFTIME('%s', '2022-05-11 00:00') AS INTEGER)),
9497

9598
</details>
9699

97-
Lets to the same with the friend_graph
100+
Lets to the same with the friend_graph. Try to insert following values into the `friend_graph` table.
98101

99102
| player\_1 | player\_2 |
100103
|:----------|:----------|
@@ -130,7 +133,7 @@ VALUES (1, 2),
130133

131134
You can also use an alternative syntax to directly create a table with content.
132135

133-
Lets say we want to create a table with money of the players. The table should contain the id and a fixed amount of
136+
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
134137
money for each player for now.
135138

136139
```sql

‎docs/02/introduction.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Chapter 2
2+
3+
Welcome to chapter two. Now we have set up the database of our choice and also installed a tool to access our
4+
databases. Of course, we could start now directly with writing cool SQL queries, but first we need to talk about
5+
some very important stuff.
6+
7+
We will start with learning about operators. Especially about operators to transform and compare data. Because those
8+
operators heavily depend on the datatypes we use we will directly head over to them and take a look at the different
9+
types our databases offer to us.
10+
11+
Before we can now finally start with creating our first database and creating our tables we will need to define some
12+
important thing, that is naming. A clear structure in your database will make your life a lot easier and helps
13+
others to understand what you are doing.
14+
15+
After this we will take a first look on how to create tables and insert, update, select and delete data in these tables.

‎docs/02/tables.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ CREATE TABLE table_name
5555
(
5656
col_name TYPE,
5757
col_name TYPE
58-
)
58+
);
5959
```
6060
<!-- @formatter:on -->
6161

@@ -76,7 +76,7 @@ CREATE TABLE friend_graph
7676
(
7777
player_1 INTEGER,
7878
player_2 INTEGER
79-
)
79+
);
8080
```
8181

8282
</details>
@@ -91,7 +91,7 @@ CREATE TABLE IF NOT EXISTS table_name
9191
(
9292
col_name TYPE,
9393
col_name TYPE
94-
)
94+
);
9595
```
9696
<!-- @formatter:on -->
9797

‎docs/02/update.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Remember our already pretty known table of players.
2020
| 9 | Milana | 2022-02-12 00:00:00.000000 |
2121
| 10 | Lexi | 2022-02-22 00:00:00.000000 |
2222

23-
We are currently saving the last time the player has logged in in the `last_online` column.
23+
We are currently saving the last time the player was online in the `last_online` column.
2424

2525
When Lexy is online again we might need to update the `last_online` value for her again. We have now two options. The
26-
first one is deleting the entry and inserting a new one. This is very dirty and also not a good practice. Thats why we
26+
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
2727
use the `UPDATE` statement and define in the `WHERE` clause where we want to update and what we want to update.
2828

2929
The general syntax is:
@@ -62,7 +62,6 @@ WHERE id = 10;
6262

6363
<details>
6464
<summary>MariaDB/MySQL</summary>
65-
****
6665

6766
```sql
6867
UPDATE player

‎docs/03/index.md ‎docs/03/.index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Chapter 3 Advanced Data
77
- Default values
88
- Sorted indices
99
- Conditional indices
10+
- Auto increment
1011
- Upsert/Replace
1112
- Aggregation
1213
- Grouping
13-
- Auto increment
1414
- Foreign keys
1515
- Basic normalization
1616
- Joins

‎docs/04/index.md ‎docs/04/.index.md

File renamed without changes.

‎docs/05/index.md ‎docs/05/.index.md

File renamed without changes.

‎mkdocs-setup

100644100755
File mode changed.

‎mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ nav:
2929
- 'Insert': 02/insert.md
3030
- 'Select': 02/select.md
3131
- 'Update': 02/update.md
32+
- 'Delete': 02/delete.md
3233
theme:
3334
features:
3435
- navigation.instant

0 commit comments

Comments
 (0)
Please sign in to comment.