Skip to content

Commit 9928b25

Browse files
authored
Update references and code from MySQL to PostgreSQL (#59)
1 parent 9bb710e commit 9928b25

File tree

7 files changed

+113
-52
lines changed

7 files changed

+113
-52
lines changed

databases/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ console.log(musicians[0]); // prints: 'John Coltrane'
8585

8686
While this JavaScript database is a nice and simple example, it doesn't persist the data for the next day.
8787

88-
In this module you will focus primarily on persisting databases, in particular the relational database MySQL.
88+
In this module you will focus primarily on persisting databases, in particular the relational database PostgreSQL.
8989

9090
To learn more, check out the following resources:
9191

databases/sql/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,49 @@
22

33
SQL (Structured Query Language) is a programming language used for managing the data that is stored in a DBMS (DataBase Management System). The `Query` part in SQL refers to the act of "asking a database" for a certain type of information; you are `querying the database`.
44

5-
There are several implementations (software) of DBMS. Each software provides its own query language. For this course, you will learn [MySQL](https://www.mysql.com/).
5+
There are several implementations (software) of DBMS. Each software provides its own query language. For this course, you will learn [PostgreSQL](https://www.postgresql.org/).
66

77
To learn more, check out the following:
88

99
{% hyf-youtube src="https://www.youtube.com/watch?v=kqUIoOM3WEs" %}
1010

1111
## What are data types (as applied to databases)?
1212

13-
When you store data in MySQL, each datum (singular of the word data) needs to be associated with its type.
13+
When you store data in PostgreSQL, each datum (singular of the word data) needs to be associated with its type.
1414

1515
For example numbers like 42, 1636 or -345 are all associated with the type `int`.
1616

17-
The following is a list of the most frequently used data types.
17+
The following is a list of the most frequently used PostgreSQL data types.
1818

19-
| Type | Description | Example Value |
20-
| ---------- | --------------------------------------------- | ------------------------- |
21-
| int | Numbers | 42 |
22-
| float | Decimal numbers | 3.14 |
23-
| varchar(N) | String with variable maximum of N characters | "Dragon" |
24-
| text | String with fixed maximum of 65535 characters | "Positive" |
25-
| datetime | Store date and time without timezone | `2019-01-01 22:10:23` |
26-
| timestamp | Store date with timezone (e.g. last login) | `2019-01-01 22:10:23 UTC` |
27-
| BLOB | Store binary files | an image |
19+
| Type | Description | Example Value |
20+
| ------------- | --------------------------------------------- | ------------------------- |
21+
| INTEGER | Numbers | 42 |
22+
| REAL | Decimal numbers | 3.14 |
23+
| VARCHAR(N) | String with variable maximum of N characters | 'Dragon' |
24+
| TEXT | String with unlimited length | 'Positive' |
25+
| TIMESTAMP | Store date and time without timezone | `2019-01-01 22:10:23` |
26+
| TIMESTAMPTZ | Store date with timezone (e.g. last login) | `2019-01-01 22:10:23+00` |
27+
| BYTEA | Store binary data | an image |
2828

29-
There are many more data types. You can read about them [here](https://www.w3resource.com/mysql/mysql-data-types.php).
29+
There are many more data types. You can read about them [here](https://www.postgresql.org/docs/current/datatype.html).
3030

3131
For a video explanation of the above table have a look at:
3232

33-
{% hyf-youtube src="https://www.youtube.com/watch?v=PT2GXYs9FEY" %}
33+
{% hyf-youtube src="https://www.youtube.com/watch?v=HnnPgdL0snI" %}
3434

3535
## How to use SQL to Create, Read, Update and Delete (CRUD)
3636

37-
With the knowledge of all the datatypes, you can now create tables that contain the data with these datatypes.
37+
With the knowledge of all the data types, you can now create tables that contain the data with these data types.
3838

39-
Tables contain columns and columns have datatypes. For example, in a column with names of students, you cannot have numbers.
39+
Tables contain columns and columns have data types. For example, in a column with names of students, you cannot have numbers.
4040

41-
- MySQL provides a `CREATE TABLE` statement that creates a table with columns. You can choose the table name, column names but you have to choose the pre-defined datatypes supported by MySQL. For example, a column `Registration number` cannot have the data type number. It must use `int` because it represents the numeric datatype.
41+
- SQL provides a `CREATE TABLE` statement that creates a table with columns. You can choose the table name, column names but you have to choose the pre-defined data types supported by PostgreSQL. For example, a column `Registration number` cannot have the data type number. It must use `INTEGER` because it represents the numeric datatype.
4242

43-
- MySQL provides `SELECT` statement which reads (columns and rows) from a table with or without filtration.
43+
- SQL provides `SELECT` statement which reads (columns and rows) from a table with or without filtration.
4444

45-
- MySQL provides `UPDATE` statement which changes the contents of (columns and rows of) a table.
45+
- SQL provides `UPDATE` statement which changes the contents of (columns and rows of) a table.
4646

47-
- MySQL provides `DELETE` statement which can delete rows of tables. In order to delete columns, you need to use `ALTER` and `DROP` statements.
47+
- SQL provides `DELETE` statement which can delete rows of tables. In order to delete columns, you need to use `ALTER` and `DROP` statements.
4848

4949
Check out the following to learn more about how to apply SQL:
5050

databases/sql/dumps.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
# What is a database dump?
22

3-
A database `dump` (aka SQL dump) contains a record of the table structure and the data from a database which is used as a backup to restore the database in case of losing it. A database dump is usually in the form of a list of SQL statements. ([An example file named world.sql](https://github.com/HackYourFuture/databases/blob/master/Week1/world.sql) is present in the `Week1` folder)
3+
A database `dump` (aka SQL dump) contains a record of the table structure and the data from a database which is used as a backup to restore the database in case of losing it. A database dump is usually in the form of a list of SQL statements. ([An example file named world.sql](https://github.com/HackYourFuture/databases/blob/main/Week1/databases/world.sql) is present in the `Week1` folder)
4+
## Why use database dumps?
45

5-
1. Collecting the dump of an existing database from terminal `mysqldump -uroot -p database_name > dump-file.sql`
6-
2. Applying the dump from mysql command prompt (`mysql>`) `source /path/to/the/dump/file`
7-
3. Applying the dump from the terminal(with generally a dollar prompt `$`) `mysql -uroot -p [database] < /path/to/the/dump/file`
6+
Database dumps serve two primary purposes: **creating backups** to protect against data loss, and **migrating data** between different database instances or environments. Whether you're moving from development to production, switching database servers, or simply ensuring you have a recovery plan, dumps provide a reliable way to preserve and transfer your database structure and content safely.
87

9-
For a video going through the idea of a database dump and how to do it have a look at:
8+
### Backing up your database
9+
Collecting the dump of an existing database from the terminal:
10+
```bash
11+
pg_dump -U username -d database_name > dump-file.sql
12+
```
1013

11-
{% hyf-youtube src="https://www.youtube.com/watch?v=eSP3afYdIKM" %}
14+
If your database is inside a Docker container, add `docker exec container_name` before the `pg_dump` command:
15+
16+
```bash
17+
docker exec container_name pg_dump -U username -d database_name > dump-file.sql
18+
```
19+
20+
21+
### Restoring from a backup
22+
23+
Restoring the dump from the terminal
24+
```bash
25+
psql -U username database_name < /path/to/the/dump/file
26+
```
27+
28+
If your database is inside a Docker container:
29+
```bash
30+
cat /path/to/the/dump/file | docker exec -i container_name psql -U username -d database_name
31+
```
32+
33+
The command above is actually executing all the SQL commands that are in the dump file one by one to restore the database.
34+
35+
Alternatively, you can use the `pg_restore` command to restore from a dump.
36+
37+
### Alternative: Using Database management tools
38+
Many Database management tools like `pgAdmin` or `Datagrip` have the ability to export or restore a dump directly from the user interface without executing any terminal commands yourself.
39+
[Here is an example](https://www.youtube.com/watch?v=vdd66leSDa4) how you can do it with `pgAdmin`.
40+
41+
### Further reading
42+
43+
For a video going through the idea of a database dump and how to do it. Have a look at:
44+
45+
{% hyf-youtube src="https://www.youtube.com/watch?v=WB6WzuFHcP8" %}

databases/sql/identifiers.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ To define a Primary Key you can use the following syntax:
1313

1414
```sql
1515
CREATE TABLE teachers (
16-
teacher_number INT NOT NULL AUTO_INCREMENT,
16+
teacher_number SERIAL PRIMARY KEY,
1717
name VARCHAR(50),
1818
date_of_birth DATE,
1919
subject TEXT,
20-
email VARCHAR(200),
21-
PRIMARY KEY (teacher_number)
20+
email VARCHAR(200)
2221
);
2322
```
2423

@@ -36,11 +35,10 @@ To define a Foreign Key while creating the table, you can use the following synt
3635

3736
```sql
3837
CREATE TABLE students (
39-
student_number INT NOT NULL AUTO_INCREMENT,
38+
student_number SERIAL PRIMARY KEY,
4039
name VARCHAR(50),
4140
teacher_id INT,
4241
email VARCHAR(200),
43-
PRIMARY KEY (student_number),
4442
FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_number)
4543
);
4644
```
@@ -79,7 +77,7 @@ For example, in a database with students from several schools you'd expect the s
7977

8078
```sql
8179
CREATE TABLE students_across_schools (
82-
student_number INT NOT NULL AUTO_INCREMENT,
80+
student_number SERIAL,
8381
name VARCHAR(50),
8482
school_id INT,
8583
PRIMARY KEY (student_number, school_id)

databases/sql/sql-injection.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
# SQL injection
22

33
Some SQL clients accept input from the user to construct queries.
4-
A malicious user can tweak the input to acquire more information from the database or
5-
to destroy the database (literally!). See [this Demo program](https://github.com/HackYourFuture/databases/blob/master/Week3/sql-injection.js) `sql-injection.js` in the `Week3` folder.
4+
A malicious user can tweak the input to acquire more information from the database or even
5+
to destroy the database! See [this Demo program](https://github.com/HackYourFuture/databases/blob/master/Week3/scripts/sql-injection.js) `sql-injection.js` in the `Week3` folder.
66

77
Consider the following query `SELECT name, salary FROM employees where id = X`.
88

9-
## Injection to get more information
9+
### Injection to get more information
1010

1111
```sql
1212
/* If X is `101 OR 1=1`, then the query returns all records because 1=1 is always true */
1313
SELECT name, salary FROM employees where id = 101 OR 1=1;
1414
```
1515

16-
## Injection to destroy the database
16+
This query demonstrates a classic SQL injection attack. Instead of returning data for just employee ID 101, the malicious input `101 OR 1=1` transforms the WHERE clause into a condition that's always true. Since `1=1` is always true, the OR operator makes the entire condition evaluate to true for every row and exposing all employee records including sensitive salary information.
17+
18+
### Injection to destroy the database
1719

1820
```sql
1921
/* If X is `101; DROP database mydb`, then the query will delete the entire database */
2022
SELECT name, salary FROM employees where id = 101; DROP database mydb;
2123
```
2224

23-
To prevent SQL injection you have to use prepared statements. The diagram below summarizes nicely how prepared statements work:
24-
25-
![SQL injection](https://pics.me.me/prepared-statements-sol-injections-let-me-in-adult-swim-sol-62056759.png)
25+
To prevent SQL injection you have to use **prepared statements**.
2626

27+
## Prepared statements
2728
With prepared statements, we instruct the database to treat certain parts of a query only as a string and nothing else. Even if the string is a valid command it will not be evaluated or executed. The syntax for prepared statements is:
2829

2930
Make sure that the database already contains the `employees` table
3031
```sql
31-
PREPARE example FROM 'SELECT name, salary FROM employees where id = ?';
32-
SET @id = 5;
33-
EXECUTE example USING @id
32+
-- Prepare the statement
33+
PREPARE example AS SELECT name, salary FROM employees WHERE id = $1;
34+
35+
-- Execute with a parameter
36+
EXECUTE example(5);
37+
38+
-- Clean up (optional)
39+
DEALLOCATE example;
3440
```
3541

36-
If you try to provide an extra command in the input `set @id='2; show tables'`, it will not be executed.
42+
If you try to provide an extra command in the input `EXECUTE('5 OR 1=1')`, it will not be executed.
3743

3844
To increase your understanding check the following materials:
3945

4046
- [What is SQL injection?](https://www.youtube.com/watch?v=ciNHn38EyRc)
41-
- [Prepared statements](https://www.databasejournal.com/features/mysql/a-guide-to-mysql-prepared-statements-and-parameterized-queries.html)
47+
- [Prepared statements](https://www.postgresql.org/docs/current/sql-prepare.html)
4248

4349
# Bonus video
4450

4551
A bonus video going over this made by one of our mentors ([Unmesh](https://github.com/unmeshvrije)):
4652

4753
{% hyf-youtube src="https://www.youtube.com/watch?v=wS24JiRK4vo" %}
54+
55+
*Note: This video uses MySQL but the concept is still the same and can be easily translated to PostgreSQL*

databases/sql/transactions.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,37 @@ Imagine writing a program for transferring money from one bank account to anothe
66

77
![he did not use transactions](https://i.imgflip.com/3hkxnl.jpg)
88

9-
To start a transaction in MySQL we use the keyword `START TRANSACTION;`. Then we execute a series of commands. More concretely, in our money transfer example: `UPDATE account SET balance = balance - 100 WHERE account_no = 987654 ;` and `UPDATE account SET balance = balance + 100 WHERE account_no = 123456 ;`. If there are no errors we use the command `COMMIT;` which finalizes the changes from both update commands. If there was an error we can use the command `ROLLBACK;` which will _undo_ the changes from all commands in the transaction.
9+
To start a transaction in PostgreSQL we use the command:
10+
```sql
11+
BEGIN;
12+
```
13+
Then we execute a series of commands. More concretely, in our money transfer example:
14+
15+
```sql
16+
-- Update 1
17+
UPDATE account SET balance = balance - 100 WHERE account_no = 987654 ;
18+
19+
-- Update 2
20+
UPDATE account SET balance = balance + 100 WHERE account_no = 123456 ;
21+
```
22+
23+
If there are no errors we use the command:
24+
```sql
25+
COMMIT;
26+
```
27+
This command finalizes the changes from both update commands. If there was an error we can use the command
28+
```sql
29+
ROLLBACK;
30+
```
31+
Which will _undo_ the changes from all commands in the transaction.
1032

1133
Transactions are essentials when building applications since it is very rare that a certain complex functionality can be written as a single SQL command. To do anything useful, several SQL commands need to be executed and in that case transactions are there to ensure that if something fails halfway, then the data does not stay in an inconsistent state.
1234

1335
To increase your understanding, study the following materials:
1436

15-
- [Transaction examples](https://www.mysqltutorial.org/mysql-transaction.aspx/)
37+
- [Transaction examples](https://www.postgresql.org/docs/current/tutorial-transactions.html)
1638

17-
# Bonus video
39+
# Extra videos
1840

19-
A bonus video going over this made by one of our mentors ([Unmesh](https://github.com/unmeshvrije)):
20-
21-
{% hyf-youtube src="https://www.youtube.com/watch?v=mzAs7YQYNog" %}
41+
* [What is a Database Transaction?](https://www.youtube.com/watch?v=wHUOeXbZCYA)
42+
* [How to implement Transactions (COMMIT, ROLLBACK, SavePoint) in PostgreSQL.](https://www.youtube.com/watch?v=DvJq4L41ru0)

node-js/user-registration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ First, we need a user database. The simplest solution can be defining an array i
3131
```javascript
3232
const usersDatabase = [];
3333
```
34-
In real world applications, we use a real database server like MySQL or MongoDB to store all the users.
34+
In real world applications, we use a real database server like PostgreSQL or MongoDB to store all the users.
3535

3636
Next, lets create the endpoint:
3737

0 commit comments

Comments
 (0)