Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 724aebe

Browse files
authored
Merge pull request HackYourFuture#184 from HackYourFuture/unmesh
update README of lesson 1
2 parents 1bac04e + 9f80cd6 commit 724aebe

File tree

6 files changed

+245
-121
lines changed

6 files changed

+245
-121
lines changed

Week1/MAKEME.md

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
11
# Homework week 1
2-
3-
## Draw a database design for a TODO App
4-
5-
If you want to use software to do this use MySQL workbench or LucidCharts
6-
(online option) And a tutorial on how to create an Entity-Relation-Diagram
7-
(ERD) (<https://www.youtube.com/watch?v=9L0CMnDGuLg>). Otherwise pen and paper
8-
is just fine, but take a picture put it in the homework directory!
9-
10-
*(Note: It's not important you learn how to use a specific drawing tool but it
11-
is important you learn how to design a database!)*
12-
13-
Design a database for an app that keeps track of TODO lists of its users. In
14-
this app a user can:
15-
16-
- Create a TODO item with some text in it
17-
- Add tags to the TODO items, like `homework`, `groceries` etc..
18-
- Mark a TODO item as done
19-
20-
**Bonus round:**
21-
22-
Write a `todo.sql` file and put it in the homework folder where you create the
23-
database structure from your ERD
24-
<https://www.w3schools.com/sql/sql_create_table.asp>. Make sure you can use the
25-
`source` command in MySQL to create the database.
26-
27-
## World database queries!
28-
29-
Create a database called `world` and `source` the SQL data into the database [databases/world.sql](databases/world.sql).
30-
31-
*(Remember: first `create` the database, then `use` it, and then `source` the `world.sql` file.)*
32-
33-
Some SQL refresh tutorial (if needed):
34-
35-
- <https://www.w3schools.com/sql/sql_select.asp>
36-
- <http://www.geeksforgeeks.org/sql-tutorial/>
37-
38-
Answer the following questions using the `world` database and put your answers
39-
in [homework/world-queries.txt](homework/world-queries.txt). You will have to
40-
use keywords that we did not see in class: `ORDER BY`, `LIMIT`, `GROUP BY`:
41-
42-
*(Hint: Use `SHOW tables;` and `DESCRIBE <table>;` to see what the table structure looks like)*
43-
44-
1. What are the names of all the cities in the Netherlands?
45-
2. What is the population of Rotterdam ?
46-
3. What's the name of all the countries on the continent ‘North America’ ?
47-
4. What's the top 10 countries by SurfaceArea ?
48-
5. What's the top 10 most populated cities?
49-
6. Select the top 3 countries by population that their names start with the letter ‘P’
50-
7. What is the population in Asia?
51-
8. Which languages are spoken in the Region of ‘South America’
52-
9. What are the languages spoken on all cities named ‘Barcelona’ (you may need to join 3 tables =) )
2+
```
3+
Write a node-JS program to
4+
1. create a database world
5+
2. create a table country
6+
3. create a table city
7+
4. Insert 10-20 rows in each table with relevant fields.
8+
9+
Choose the fields/columns such that you should be able to answer the following queries:
10+
(Queries 6-10 are bonus queries and require the knowledge of joins, group by and having clauses)
11+
12+
Write a node-JS program to query (using select statements) the world database to answer following questions
13+
1. What are the names of countries with population greater than 8 million
14+
2. What are the names of countries that have “land” in their names ?
15+
3. What are the names of the cities with population in between 500,000 and 1 million ?
16+
4. What's the name of all the countries on the continent ‘Europe’ ?
17+
5. List all the countries in the descending order of their surface areas.
18+
6. What are the names of all the cities in the Netherlands?
19+
7. What is the population of Rotterdam ?
20+
8. What's the top 10 countries by Surface Area ?
21+
9. What's the top 10 most populated cities?
22+
10. What is the population of the world ?
23+
24+
```

Week1/README.md

Lines changed: 155 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,167 @@
1-
# Lesson 1: Retrieving Data
1+
# Lesson 1: MySQL and Node setup! Create, Insert and Select !
22

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.
3+
Objective : This module aims to incorporate JavaScript code to operate the MySQL database.
4+
MySQL client can be used to demonstrate SQL queries however, students should know how to
5+
make a MySQL database connection from JavaScript, run queries from JavaScript and
6+
capture results of queries in JavaScript.
47

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 Readings
8+
## Pre-Class Preparation
9+
- Install MySQL using the following [official docs](https://dev.mysql.com/downloads/mysql/)
10+
- MAC users may use `brew install mysql`
811

912
Before arriving to class on Sunday, please watch all of the videos in [this video playlist](https://www.lynda.com/SharedPlaylist/0299ced540444d7197460e7f1f74ddab) on Lynda.
10-
- What are databases?
11-
- Exploring databases and database management systems
12-
- The features of a relational database
13-
- Introduction to database modeling
14-
- Using the basic SELECT statement
15-
- Creating SQL queries
16-
- Structuring the WHERE clause
17-
- Sorting query results
18-
- Using aggregate functions
19-
- Joining tables
13+
If you actually manage to watch them all and understand everything,
14+
then you are a database expert.
2015

21-
## Pre-Class Preparation
22-
- Install MySQL windows guys can follow the [official docs](https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html) unix users have it easyier as usual 😉
16+
## Setup
17+
18+
### MySQL setup
19+
This setup assumes MySQL version 8.0.
20+
Windows users should use **Microsoft MySQL Command Line** client.
21+
Linux and MAC users should use **gnome-terminal** and **Terminal** respectively.
22+
**Microsoft MySQL Command Line** client gives you a **msql>** prompt after typing in your root password.
23+
Note that this password is the one you used for root user of the mysql.
24+
Linux and MAC users can execute `mysql -uroot -p` and then type the password.
25+
Following commands should be run under the **mysql>** prompt:
26+
```
27+
mysql> create user ‘hyfuser’@‘localhost’ identified with mysql_native_password by ‘hyfpassword’;
28+
# This command creates a user 'hyfuser' with password 'hyfpassword' for
29+
# the database server at 'localhost'
30+
31+
mysql> grant all privileges on *.* to ‘hyfuser’@‘localhost’;
32+
# This command gives all permissions to user 'hyfuser'.
33+
(*.*) means every table of every database.
34+
35+
mysql> create database userdb;
36+
This command creates a database 'userdb'
37+
```
38+
39+
### Node setup
40+
This setup assumes that you have Node.js 0.6 or higher.
41+
We use **mysqljs** driver which can be installed using `npm install mysql`
42+
43+
### Verification of the correct setup
44+
Run `node connection-test.js` from VScode(Windows) or the terminal(Linux or MAC).
45+
The output should be `The solution is: 2`.
46+
connection-test.js can be found in the Week1 folder.
47+
48+
In this class, students will be introduced to
49+
50+
* Basics of relational databases: Concepts of tables, rows, columns, primary key, foreign key.
51+
* Creation of a database table and insertion of values.
52+
* Retrieving data from a MySQL database using SELECT queries.
53+
54+
Objective: Students should be able to create tables,
55+
insert values in tables and
56+
retrieve data from tables using SELECT statements that include FROM, WHERE clauses.
57+
58+
59+
## Topics to be covered
60+
61+
### What is a Database ?
62+
* Definition : Organized collection of data and rules about its manipulation
63+
* Client-server architecture : E.g. (Relational) DBMS
64+
* Files as database
65+
* Data structure/object as database
66+
```js
67+
const capitals = [
68+
"Amsterdam",
69+
"Delhi",
70+
"Damascus",
71+
"Madrid"];
72+
```
73+
74+
### Relations = Table
75+
76+
* What is a relation (in the following sentences)?
77+
* Delhi is the capital of India
78+
* Amsterdam is the capital of Netherlands
79+
* Damascus is the capital of Syria
80+
81+
Dan, 29, works in Amazon and lives in Seattle. His friend Ben who just celebrated
82+
his 24th birthday works in Facebook and lives in Redmond.
83+
84+
### DBMS implementations
85+
86+
* **MySQL**
87+
* PostgreSQL
88+
* MongoDB (NoSQL)
89+
* Cassandra (NoSQL)
90+
91+
### MySQL components
92+
93+
* MySQL server (runs as a service, default port: 3306)
94+
* mysql: monitor / terminal / client (to connect to the server and execute stuff)
95+
* mysqladmin: Administering a MySQL Server
96+
97+
### Create a table in MySQL
98+
99+
#### Collection of rows and columns
100+
#### SYNTAX
101+
```
102+
CREATE TABLE table_name (column_name, column_type [, column2_name, column2_type]);
103+
```
104+
105+
* INT(N) type
106+
* DATE, DATETIME and TIMESTAMP, (set time_zone = '+03:00')
107+
* BLOB (LOAD_FILE(filename))
108+
109+
110+
### Fill up a table in MySQL: INSERT rows
111+
A row (aka record or tuple) represents a single, implicitly structured data item in the table.
112+
113+
#### SYNTAX
114+
```
115+
INSERT INTO table_name VALUES(value1, value2 [,value3,...]);
116+
```
117+
* INSERT INTO table_name VALUES(...values...)
118+
* INSERT INTO table_name (column names) VALUES(..values...)
119+
* INSERT INTO table_name SET column_name = {expr | DEFAULT}
120+
121+
### See the content of a table in MySQL: SELECT
122+
123+
#### SYNTAX
124+
```
125+
SELECT */column_1,column_2...
126+
FROM table_1
127+
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
128+
WHERE conditions
129+
GROUP BY group 
130+
HAVING group_conditions
131+
ORDER BY column_1 [ASC | DESC]
132+
LIMIT offset, row_count
133+
 
134+
The SELECT statement is composed of several clauses:
135+
 
136+
  -  SELECT chooses which columns of  the table you want to get the data.
137+
  -  FROM specifies the table from which you get the data.
138+
  -  JOIN gets data from multiple table based on certain join conditions.
139+
  -  WHERE filters rows to select.
140+
  -  GROUP BY group rows to apply aggregate functions on each group.
141+
  -  HAVING filters group based on groups defined by GROUP BY clause.
142+
  -  ORDER BY specifies the order of the returned result set.
143+
  -  LIMIT constrains number of returned rows.
144+
```
145+
146+
### INSERT and SELECT together
23147

148+
```
149+
Example:
24150
25-
## Main Topics
151+
INSERT INTO 'employees' ('shop_id', 'gender', 'name', 'salary')
152+
SELECT 3,
153+
LEFT(gender, 1),
154+
CONCAT_WS(' ', first_name, last_name),
155+
salary
156+
FROM transferred_ employees
157+
WHERE transfer_date > '2008-01-01';
158+
```
26159

27-
- The relational model of data
28-
- A 'database' vs. a 'DBMS' (database management system)
29-
- The concept of a schema
30-
- The properties of an 'entity' (or 'row')
31-
- Basic entity relationship diagrams
32-
- A basic SELECT statement
33-
- Constructing more complex SELECT statements
34-
- JOIN
35-
- Selecting composite data from multiple tables
36-
- Compare JOIN WHERE with cartesian product
37-
- Naming Conventions: UpperCamelCase/PascalCase, lowerCamelCase, snake_case, hnHungarianNotation/HNHungarianNotation
38-
- Character Sets in Databases (hint: always use UTF-8 encoding, called 'utf8mb4' in MySQL)
160+
### Uniqueness and Keys
39161

162+
* Super key : set of columns that uniquely identify a row
163+
* Candidate key : minimal super key that can uniquely identify a row
164+
* Primary key : choice of candidate key chosen by database designer : cannot be null
40165

41166
## Reference Material
42167

Week1/create-table.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var mysql = require('mysql');
2+
var connection = mysql.createConnection({
3+
host : 'localhost',
4+
user : 'hyfuser',
5+
password : 'hyfpassword',
6+
database : 'class17'
7+
});
8+
9+
connection.connect();
10+
11+
var create_query = "create table teachers (teacher_number int, teacher_name varchar(50), date_of_birth date, subject text, gender enum('m', 'f'))"
12+
13+
connection.query(create_query, function (error, results, fields) {
14+
if (error) {
15+
throw error;
16+
}
17+
console.log("the reply is ", results[0]);
18+
});
19+
connection.end();

Week1/homework/world-queries.txt

Lines changed: 0 additions & 40 deletions
This file was deleted.

Week1/insert-values.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var mysql = require('mysql');
2+
var connection = mysql.createConnection({
3+
host : 'localhost',
4+
user : 'hyfuser',
5+
password : 'hyfpassword',
6+
database : 'class17'
7+
});
8+
9+
connection.connect();
10+
var insert_queries = [
11+
"insert into students values (103, 'Ahmad', '1986-01-11', 8.3, 'm')",
12+
"insert into students values (104, 'Rim', '1979-11-01', 8.7, 'f')",
13+
"insert into students values (105, 'Rabia', '1978-05-30', 8.1, 'f')",
14+
"insert into students values (106, 'Karam', '1994-02-02', 8.8, 'm')"
15+
]
16+
17+
for(var i in insert_queries){
18+
console.log("Going to run ", insert_queries[i])
19+
connection.query(insert_queries[i], function (error, results, fields) {
20+
if (error) {
21+
throw error;
22+
}
23+
console.log("the reply is ", results[0]);
24+
});
25+
}
26+
connection.end();

Week1/select-star.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var mysql = require('mysql');
2+
var connection = mysql.createConnection({
3+
host : 'localhost',
4+
user : 'hyfuser',
5+
password : 'hyfpassword',
6+
database : 'class17'
7+
});
8+
9+
connection.connect();
10+
11+
var select_query = "select * from students"
12+
13+
console.log("Going to run ", select_query)
14+
connection.query(select_query, function (error, results, fields) {
15+
if (error) {
16+
throw error;
17+
}
18+
for (i in results) {
19+
console.log(results[i]);
20+
}
21+
});
22+
connection.end();

0 commit comments

Comments
 (0)