Skip to content

Commit 190bdb3

Browse files
committed
Import 'databases'
1 parent 350320a commit 190bdb3

39 files changed

Lines changed: 3935 additions & 2 deletions

legacy/databases/README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1-
# databases
1+
# Databases
22

3-
TODO
3+
## Relational and Non-Relational Data and Database Systems
4+
5+
### About
6+
7+
This three-week program aims to introduce the fundamental concepts of data entities and data storage.
8+
9+
### Key Objectives
10+
11+
By the end of this module, students should have a familiarity with and basic understanding of the following:
12+
13+
- Entities
14+
- The relational model
15+
- The Structured Query Language (SQL)
16+
- The construction of a database system
17+
- MySQL as an example of a relational database system
18+
- Non-relational data and NoSQL
19+
- MongoDB as an example of a NoSQL database
20+
21+
## Lesson Plan
22+
23+
### [Lesson 1](lesson1/): Retrieving Data
24+
25+
In this class, students will be introduced to retrieving data from a MySQL database using SELECT queries.
26+
27+
Objective: Students should be able to retrieve data from a database table using SELECT statements that include WHERE, GROUP BY, ORDER BY, LIMIT, and JOIN.
28+
29+
### [Lesson 2](lesson2/): Data Models, Relationships, and Schemas
30+
31+
In this class, the students will learn how to use more complex SQL queries to retrieve information across tables, and interact with data including write operations.
32+
33+
Objective: the students should be able to build CRUD functionality using SQL statements, including INSERT INTO, UPDATE and DELETE. The students should also be able to create an entity relationship diagram based on a qualitative description of data requirements, and translate that into a MySQL database schema.
34+
35+
### [Lesson 3](lesson3/): Security and Non-Relational Databases
36+
37+
In the final week, the students will be introduced to SQL injection and how to use MySQL permissions to avoid it. MongoDB, as an example of a non-relational database, will also be introduced, as well as the benefits and drawbacks of relational and non-relational models.
38+
39+
Objective: the students should know how SQL injection happens, and how to define user permissions in MySQL to avoid part of it. The students should also be able to compare and contrast relational (like MySQL) and NoSQL databases (considering their benefits and drawbacks).

legacy/databases/lesson1/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Lesson 1: Retrieving Data
2+
3+
In this class, students will be introduced to relational data terminology (row, column), the function of a primary key, and retrieving data from a MySQL database using SELECT queries.
4+
5+
Objective: Students should be able to retrieve data from a database table using SELECT statements that include WHERE, GROUP BY, and ORDER BY.
6+
7+
## Pre-Class Preparation
8+
9+
Before the class you should Docker and MySQL VScode plugin on your computer.
10+
11+
### Installing Docker and the MySQL VScode plugin
12+
13+
- Install docker by following the instructions here: <https://drive.google.com/file/d/1zVV9SyqxkHPnieQ3f2gned7JAljnvZtt/view?usp=drive_link>
14+
- Install the MySQL VSCode plugin by following the instructions here: <https://drive.google.com/file/d/1FS50CCVUgRAXe9SfS8IMyCY9tFqCXna8/view?usp=drive_link>
15+
16+
### To learn what Docker is, you can read these articles
17+
18+
- <https://medium.com/@SaadAAkash/docker-for-dummies-literally-ab3fc6362d5f>
19+
- <https://www.freecodecamp.org/news/docker-simplified-96639a35ff36/>
20+
21+
Docker is widely used by software developers these days, i’d say it’s been a
22+
game changer, so it’s good for you if you at least know what it is :slightly_smiling_face:
23+
24+
## Main Topics
25+
26+
- The relational model of data
27+
- A 'database' vs. a 'DBMS' (database management system)
28+
- The concept of a schema
29+
- The properties of an 'entity' (or 'row')
30+
- Basic entity relationship diagrams
31+
- A basic SELECT statement
32+
- Constructing more complex SELECT statements
33+
- Pattern matching with LIKE
34+
- Limiting and ordering results with LIMIT and ORDER BY
35+
- Grouping results with GROUP BY
36+
- Aggregate functions like AVG, COUNT, SUM, MIN and MAX
37+
- JOIN
38+
- Selecting composite data from multiple tables
39+
- Compare JOIN WHERE with cartesian product
40+
- Naming Conventions: UpperCamelCase/PascalCase, lowerCamelCase, snake_case, hnHungarianNotation/HNHungarianNotation
41+
- Character Sets in Databases (hint: always use UTF-8 encoding, called 'utf8mb4' in MySQL)
42+
43+
## Reference Material
44+
45+
- [TutorialsPoint MySQL Introduction](http://www.tutorialspoint.com/mysql/mysql-introduction.htm)
46+
- [w3schools tutorial (easy to find how specific commands work)](https://www.w3schools.com/sql/default.asp)
47+
- [Official MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/)
48+
- [Official MySQL Tutorial (pretty dense)](https://dev.mysql.com/doc/refman/8.0/en/tutorial.html)
49+
- [Joel Spolsky - Character Sets and Unicode](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/)
50+
51+
## Homework
52+
53+
### Start the homework
54+
55+
Need to brush up on the homework setup process? Check [this](https://github.com/HackYourFuture-CPH/Git/blob/main/homework-submission.md) out before you get into some git confusion!
56+
57+
### The task
58+
59+
Design queries that retrieve the following data sets. Compare with classmates to see if your answers are correct.
60+
61+
1. Find out how many tasks are in the task table
62+
2. Find out how many tasks in the task table do not have a valid due date
63+
3. Find all the tasks that are marked as done
64+
4. Find all the tasks that are not marked as done
65+
5. Get all the tasks, sorted with the most recently created first
66+
6. Get the single most recently created task
67+
7. Get the title and due date of all tasks where the title or description contains **database**
68+
8. Get the title and status (as text) of all tasks
69+
9. Get the name of each status, along with a count of how many tasks have that status
70+
10. Get the names of all statuses, sorted by the status with most tasks first
71+
72+
Watch the previous session recorded on video here:
73+
74+
Part 1 : <https://www.youtube.com/watch?v=gAeww1z5774>
75+
76+
Part 2 : <https://www.youtube.com/watch?v=u77w6Lw2gOY>
77+
78+
Part 3 : <https://www.youtube.com/watch?v=Mag9n6olmRU>
79+
80+
### Hand in homework
81+
82+
Need to brush up on the homework hand-in process?
83+
84+
Check [this resource](https://github.com/HackYourFuture-CPH/Git/blob/main/homework-submission.md) to remember how to hand in the homework correctly!
564 KB
Binary file not shown.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# SQL Joins
2+
3+
> An SQL JOIN clause is used to combine rows from two or more tables,
4+
> based on a common field between them.
5+
6+
There are different types of joins available in SQL:
7+
8+
**INNER JOIN**: returns rows when there is a match in both tables.
9+
10+
**LEFT JOIN**: returns all rows from the left table, even if there are no matches in the right table.
11+
12+
**RIGHT JOIN**: returns all rows from the right table, even if there are no matches in the left table.
13+
14+
**FULL JOIN**: It combines the results of both left and right outer joins.
15+
16+
The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side.
17+
18+
**SELF JOIN**: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.
19+
20+
**CARTESIAN JOIN**: returns the Cartesian product of the sets of records from the two or more joined tables.
21+
22+
WE can take each first four joins in Details :
23+
24+
## Our example data
25+
26+
We have two tables with the following values.
27+
28+
`TableA`:
29+
30+
id firstName lastName
31+
.......................................
32+
1 arun prasanth
33+
2 ann antony
34+
3 sruthy abc
35+
6 new abc
36+
37+
`TableB`:
38+
39+
id2 age Place
40+
................
41+
1 24 kerala
42+
2 24 usa
43+
3 25 ekm
44+
5 24 chennai
45+
46+
## Details
47+
48+
### INNER JOIN
49+
50+
**Note** :it gives the intersection of the two tables, i.e. rows they have common in TableA and TableB
51+
52+
Syntax
53+
54+
SELECT table1.column1, table2.column2...
55+
FROM table1
56+
INNER JOIN table2
57+
ON table1.common_field = table2.common_field;
58+
59+
Apply it in our sample table :
60+
61+
SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
62+
FROM TableA
63+
INNER JOIN TableB
64+
ON TableA.id = TableB.id2;
65+
66+
Result Will Be
67+
68+
firstName lastName age Place
69+
..............................................
70+
arun prasanth 24 kerala
71+
ann antony 24 usa
72+
sruthy abc 25 ekm
73+
74+
### LEFT JOIN
75+
76+
**Note** : will give all selected rows in TableA, plus any common selected rows in TableB.
77+
78+
Syntax
79+
80+
SELECT table1.column1, table2.column2...
81+
FROM table1
82+
LEFT JOIN table2
83+
ON table1.common_field = table2.common_field;
84+
85+
Apply it in our sample table :
86+
87+
SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
88+
FROM TableA
89+
LEFT JOIN TableB
90+
ON TableA.id = TableB.id2;
91+
92+
Result
93+
94+
firstName lastName age Place
95+
...............................................................................
96+
arun prasanth 24 kerala
97+
ann antony 24 usa
98+
sruthy abc 25 ekm
99+
new abc NULL NULL
100+
101+
### RIGHT JOIN
102+
103+
**Note** : will give all selected rows in TableB, plus any common selected rows in TableA.
104+
105+
Syntax
106+
107+
SELECT table1.column1, table2.column2...
108+
FROM table1
109+
RIGHT JOIN table2
110+
ON table1.common_field = table2.common_field;
111+
112+
Apply it in our sample table :
113+
114+
SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
115+
FROM TableA
116+
RIGHT JOIN TableB
117+
ON TableA.id = TableB.id2;
118+
119+
Result
120+
121+
firstName lastName age Place
122+
...............................................................................
123+
arun prasanth 24 kerala
124+
ann antony 24 usa
125+
sruthy abc 25 ekm
126+
NULL NULL 24 chennai
127+
128+
### FULL JOIN
129+
130+
**Note** :It will return all selected values from both tables.
131+
132+
Syntax
133+
134+
SELECT table1.column1, table2.column2...
135+
FROM table1
136+
FULL JOIN table2
137+
ON table1.common_field = table2.common_field;
138+
139+
Apply it in our sample table :
140+
141+
SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
142+
FROM TableA
143+
FULL JOIN TableB
144+
ON TableA.id = TableB.id2;
145+
146+
Result
147+
148+
firstName lastName age Place
149+
...............................................................................
150+
arun prasanth 24 kerala
151+
ann antony 24 usa
152+
sruthy abc 25 ekm
153+
new abc NULL NULL
154+
NULL NULL 24 chennai
155+
156+
## Interesting Fact
157+
158+
For INNER joins the order doesn't matter
159+
160+
For (LEFT, RIGHT or FULL) OUTER joins,the order matter
161+
162+
Better to go check this **[Link][1]** it will give you interesting details about join order
163+
164+
[1]: https://stackoverflow.com/questions/9614922/does-the-join-order-matter-in-sql
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Class exercises
2+
3+
Implement the following queries:
4+
5+
1. Select the names and phones of all users;
6+
2. Select the name of the user with `id=10`;
7+
3. Find how many users exist in the database;
8+
4. Select the names of the first 5 users in the database;
9+
5. Select the names of the last 3 users in the database;
10+
6. Sum all the ids in the `user` table;
11+
7. Select all users and order them alphabetically by name;
12+
8. Find all tasks that include `SQL` either on the title or on the description;
13+
9. Find the title of all tasks that the user `Maryrose` is responsible for;
14+
10. Find how many tasks each user is responsible for;
15+
11. Find how many tasks with a `status=Done` each user is responsible for;
31.5 KB
Loading

0 commit comments

Comments
 (0)