Skip to content

Commit 7d5ebd7

Browse files
authored
Merge pull request HackYourFuture#581 from HackYourFuture/postgresql-migration
MySQL -> PostgreSQL update
2 parents acbedd4 + 6ba2c70 commit 7d5ebd7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+6694
-17100
lines changed

README.md

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,18 @@
11
> If you are following the HackYourFuture curriculum we recommend you to start with module
2-
> 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture
2+
> 1: [CLI/GIT](https://github.com/HackYourFuture/CLI-Git). To get a complete overview of the HackYourFuture
33
> curriculum first, click [here](https://github.com/HackYourFuture/curriculum).
44
55
> Please help us improve and share your feedback! If you find better tutorials or links, please share them
66
> by [opening a pull request](https://github.com/HackYourFuture/databases/pulls).
77
8-
# Module #6 - Databases: Store and retrieve data with MySQL (Backend)
8+
# Module #6 - Databases: Store and retrieve data with PostgreSQL (Backend)
99

1010
![Databases](./assets/databases.png)
1111

1212
Have you ever thought about how your brain remembers things? It happens automatically for humans, but that's not the case for computers. Any time you go to a website and fill in some details in a form for example, that information needs to be saved somewhere. This "somewhere" is called a `database`.
1313

1414
In this module, you learn all about a fundamental part of any software application: how to (safely) store data, so that it can be used on-demand. You will learn how to structure and group data so that your apps know where to find and store the correct data.
1515

16-
## Before you start
17-
18-
**Before** your first session, you need to install the necessary software: MySQL. This differs depending on your operating system.
19-
20-
During the installation of MySQL v8, in one of the last steps, you must configure the password encryption. Here is [a screenshot of the step](./assets/MySQL-security-setup.jpg). **You must select _Legacy_ for all the given scripts to be able to connect.**
21-
22-
- For Windows, download the [MySQL Community Server](https://dev.mysql.com/downloads/mysql/);
23-
- For Linux (Ubuntu), watch the following;
24-
video: [MySQL Installation on Linux (Ubuntu)](https://www.youtube.com/watch?v=Y_LibBhOGOY)
25-
- For MacOS, watch the following video: [Installing MySQL on MacOS](https://www.youtube.com/watch?v=HxCXyxFEA0s).
26-
27-
### Setup your first database
28-
29-
In this document, you find all the instructions on how to setup your first database. Most of the commands are done in the command line, so make sure you have yours open before you start.
30-
31-
**Step 1: Logging in with the `root` user**
32-
33-
To get started with your new MySQL client, we first have to login with the `root` user.
34-
35-
> A root user, also known as a `superuser` is a special user account that has access to all commands and files of any
36-
> particular software.
37-
38-
In Windows OS, if you click on the Start menu and type `MySQL Command line Client`, then the MySQL Command Line Client gives you a `msql>` prompt after typing in your root password. Note that this password is the one you used for the `root user` of the MySQL during the installation. Linux and MAC users can execute `mysql -uroot -p` and then type your root password.
39-
40-
**Step 2: Creating a `hyfuser` account**
41-
42-
After loggin in with the root user, it's time to create the account that you will use for this module. Execute the following commands, one after the other:
43-
44-
```bash
45-
# Step 1: This command creates a user 'hyfuser' with password 'hyfpassword' for the database server at 'localhost'
46-
47-
mysql> create user 'hyfuser'@'localhost' identified with mysql_native_password by 'hyfpassword';
48-
49-
# If this does not work try the alternative command:
50-
51-
mysql> create user 'hyfuser'@'localhost' identified by 'hyfpassword';
52-
53-
# Step 2: This command gives all permissions to user 'hyfuser'. The (*.*) means every table of every database.
54-
55-
mysql> grant all privileges on *.* to 'hyfuser'@'localhost';
56-
57-
# Step 3: This command flushes all privileges so that mysql reloads the grant table and our changes are enabled
58-
59-
msyql> flush privileges;
60-
61-
# Step 4: This command creates a database named 'userdb'
62-
63-
mysql> create database userdb;
64-
```
65-
66-
**Step 3: Installing MySQL driver to use with Node.js**
67-
68-
We want to use MySQL with JavaScript and to this end, we use the following [package](https://github.com/mysqljs/mysql). You can create a project wherever you want and install it. To test that everything is working, you can use the `connection-test.js` file. If you run it it should output `The solution is: 2`.
69-
7016
## Learning goals
7117

7218
In this module, you get familiar with the complexity of storing data. By the end of it, you have learned:
@@ -75,7 +21,7 @@ In this module, you get familiar with the complexity of storing data. By the end
7521
- How to work with `Structured Query Language` (SQL);
7622
- All about the `relational model`;
7723
- How to recognise the `basic setup of a database`;
78-
- Know about `MySQL` as an example of a relational database system;
24+
- Know about `PostgreSQL` as an example of a relational database system;
7925
- Know `NoSQL` databases, with an emphasis on `MongoDB`.
8026

8127
## How to use this repository

SQL-CHEATSHEET.md

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
# SQL Cheatsheet
1+
# PostgreSQL Cheatsheet
22

3-
## Queries
3+
This cheat sheet provides a quick reference for common PostgreSQL SQL commands and operations.
4+
5+
## Basic Queries
46

57
### Specify what information to extract
68

79
```sql
810
SELECT column
911
```
1012

11-
## From which table
13+
### From which table
1214

1315
```sql
1416
FROM table
1517
```
1618

17-
## Only extract rows where the condition holds
19+
### Only extract rows where the condition holds
1820

1921
(Used with an operator: `>, <, >=, <=, =, <>, BETWEEN, LIKE, IN`)
2022

2123
```sql
2224
WHERE column = 'value'
2325
```
2426

25-
## Combining `WHERE` clauses:
27+
### Combining `WHERE` clauses:
2628

2729
(Used with: `AND, OR`)
2830

@@ -31,7 +33,7 @@ WHERE column = 'value' OR
3133
column = 'other value'
3234
```
3335

34-
## Aggregating results:
36+
### Aggregating results:
3537

3638
(Used with: `SUM, COUNT, MIN, MAX, AVG`)
3739

@@ -41,10 +43,94 @@ SELECT
4143
FROM table
4244
```
4345

44-
## Aliasing tables
46+
### Aliasing tables
4547

4648
```sql
4749
SELECT
4850
column AS alias
4951
FROM table
5052
```
53+
54+
## PostgreSQL-Specific Features
55+
56+
### Common Data Types
57+
```sql
58+
SERIAL -- Auto-incrementing integer
59+
VARCHAR(n) -- Variable-length string
60+
TEXT -- Unlimited-length string
61+
INTEGER -- 4-byte integer
62+
BIGINT -- 8-byte integer
63+
BOOLEAN -- TRUE/FALSE
64+
TIMESTAMP -- Date and time
65+
JSONB -- Binary JSON (recommended over JSON)
66+
UUID -- Universally unique identifier
67+
```
68+
69+
### Create Table with PostgreSQL Features
70+
```sql
71+
CREATE TABLE users (
72+
id SERIAL PRIMARY KEY,
73+
email VARCHAR(255) UNIQUE NOT NULL,
74+
name TEXT,
75+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
76+
active BOOLEAN DEFAULT TRUE
77+
);
78+
```
79+
80+
### Insert with RETURNING
81+
```sql
82+
INSERT INTO users (email, name)
83+
VALUES ('user@example.com', 'John Doe')
84+
RETURNING id;
85+
```
86+
87+
### UPSERT (Insert or Update)
88+
```sql
89+
INSERT INTO users (id, email, name)
90+
VALUES (1, 'user@example.com', 'John Doe')
91+
ON CONFLICT (id) DO UPDATE SET
92+
email = EXCLUDED.email,
93+
name = EXCLUDED.name;
94+
```
95+
96+
### Parameterized Queries (for Node.js pg library)
97+
```sql
98+
-- In JavaScript: client.query('SELECT * FROM users WHERE id = $1', [userId])
99+
SELECT * FROM users WHERE id = $1;
100+
```
101+
102+
### JSON Operations
103+
```sql
104+
-- Query JSON data
105+
SELECT data->>'name' FROM users WHERE data @> '{"active": true}';
106+
107+
-- Update JSON data
108+
UPDATE users SET data = data || '{"last_login": "2023-01-01"}' WHERE id = 1;
109+
```
110+
111+
### Common psql Commands
112+
```sql
113+
\l -- List databases
114+
\c database -- Connect to database
115+
\dt -- List tables
116+
\d table -- Describe table
117+
\q -- Quit psql
118+
```
119+
120+
### Useful Functions
121+
```sql
122+
-- String functions
123+
CONCAT(str1, str2) -- Concatenate strings
124+
UPPER(string) -- Convert to uppercase
125+
LOWER(string) -- Convert to lowercase
126+
LENGTH(string) -- String length
127+
128+
-- Date functions
129+
NOW() -- Current timestamp
130+
CURRENT_DATE -- Current date
131+
EXTRACT(YEAR FROM date) -- Extract part of date
132+
133+
-- Window functions
134+
ROW_NUMBER() OVER (ORDER BY column) -- Row numbering
135+
RANK() OVER (ORDER BY column) -- Ranking
136+
```

0 commit comments

Comments
 (0)