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

Skip to content

Commit f7dff2e

Browse files
committed
update lesson 1 plan and homework. add js files for basic SQL operations
1 parent 26c25a2 commit f7dff2e

File tree

5 files changed

+238
-73
lines changed

5 files changed

+238
-73
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: 148 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
1-
# Lesson 1: Basics of creation, insertion and retrieval of data
1+
# Lesson 1: Server setup and Basics of creation, insertion and retrieval of data
2+
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.
7+
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`
11+
12+
Before arriving to class on Sunday, please watch all of the videos in [this video playlist](https://www.lynda.com/SharedPlaylist/0299ced540444d7197460e7f1f74ddab) on Lynda.
13+
If you actually manage to watch them all and understand everything,
14+
then you are a database expert.
15+
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.
247

348
In this class, students will be introduced to
449

@@ -10,26 +55,8 @@ Objective: Students should be able to create tables,
1055
insert values in tables and
1156
retrieve data from tables using SELECT statements that include FROM, WHERE clauses.
1257

13-
## Pre-Class Readings
14-
15-
Before arriving to class on Sunday, please watch all of the videos in [this video playlist](https://www.lynda.com/SharedPlaylist/0299ced540444d7197460e7f1f74ddab) on Lynda.
16-
- What are databases?
17-
- Exploring databases and database management systems
18-
- The features of a relational database
19-
- Introduction to database modeling
20-
- Using the basic SELECT statement
21-
- Creating SQL queries
22-
- Structuring the WHERE clause
23-
- Sorting query results
24-
- Using aggregate functions
25-
- Joining tables
26-
27-
## Pre-Class Preparation
28-
- Install MySQL using the following [official docs](https://dev.mysql.com/downloads/mysql/)
29-
- MAC users may use `brew install mysql`
3058

31-
32-
## Main Topics
59+
## Topics to be covered
3360

3461
- The relational model of data
3562
- A 'database' vs. a 'DBMS' (database management system)
@@ -42,8 +69,107 @@ Before arriving to class on Sunday, please watch all of the videos in [this vide
4269
- JOIN (if time permits)
4370
- Selecting composite data from multiple tables
4471
- Compare JOIN WHERE with cartesian product
45-
- Naming Conventions: UpperCamelCase/PascalCase, lowerCamelCase, snake_case, hnHungarianNotation/HNHungarianNotation
46-
- Character Sets in Databases (hint: always use UTF-8 encoding, called 'utf8mb4' in MySQL)
72+
73+
### What is a Database ?
74+
* Definition : Organized collection of data and rules about its manipulation
75+
* Client-server architecture : E.g. (Relational) DBMS
76+
* Files as database
77+
* Data structure/object as database
78+
```js
79+
const capitals = [
80+
"Amsterdam",
81+
"Delhi",
82+
"Damascus",
83+
"Madrid"];
84+
```
85+
86+
87+
### Relations = Table
88+
89+
* What is a relation (in the following sentences)?
90+
* Delhi is the capital of India
91+
* Amsterdam is the capital of Netherlands
92+
* Damascus is the capital of Syria
93+
94+
Dan, 29, works in Amazon and lives in Seattle. His friend Ben who just celebrated
95+
his 24th birthday works in Facebook and lives in Redmond.
96+
97+
-
98+
99+
### DBMS implementations
100+
101+
* ** MySQL **
102+
* PostgreSQL
103+
* MongoDB (NoSQL)
104+
* Cassandra (NoSQL)
105+
106+
107+
### MySQL components
108+
109+
* MySQL server (runs as a service, default port: 3306)
110+
* mysql: monitor / terminal / client (to connect to the server and execute stuff)
111+
* mysqladmin: Administering a MySQL Server
112+
113+
114+
115+
### Create Table in MySQL
116+
117+
## Collection of rows and columns
118+
## SYNTAX
119+
```
120+
CREATE TABLE table_name (column_name, column_type [, column2_name, column2_type]);
121+
```
122+
123+
* INT(N) type
124+
* DATE, DATETIME and TIMESTAMP, (set time_zone = '+03:00')
125+
* BLOB (LOAD_FILE(filename))
126+
127+
128+
### Fill up the table in MySQL: INSERT rows
129+
A row (aka record or tuple) represents a single, implicitly structured data item in the table.
130+
131+
## SYNTAX
132+
```
133+
INSERT INTO table_name VALUES(value1, value2 [,value3,...]);
134+
```
135+
* INSERT INTO table_name VALUES(...values...)
136+
* INSERT INTO table_name (column names) VALUES(..values...)
137+
* INSERT INTO table_name SET column_name = {expr | DEFAULT}
138+
139+
140+
141+
### See the content : SELECT
142+
143+
* FROM clause : multiple tables
144+
* WHERE clause : Multiple conditions(AND, OR, NOT) Operators ( =, <>, BETWEEN, LIKE, IN)
145+
* Aggregation : SUM, AVG, COUNT
146+
* Joins : Natural join, inner join, left outer and right outer join
147+
## Simple SYNTAX for a single table
148+
```
149+
SELECT */column_name FROM TABLE
150+
WHERE condition1 AND/OR
151+
condition2;
152+
```
153+
154+
155+
### INSERT and SELECT together
156+
157+
```
158+
INSERT INTO 'employees' ('shop_id', 'gender', 'name', 'salary')
159+
SELECT 3,
160+
LEFT(gender, 1),
161+
CONCAT_WS(' ', first_name, last_name),
162+
salary
163+
FROM transferred_ employees
164+
WHERE transfer_date > '2008-01-01';
165+
```
166+
167+
168+
### Uniqueness and Keys
169+
170+
* Super key : set of columns that uniquely identify a row
171+
* Candidate key : minimal super key that can uniquely identify a row
172+
* Primary key : choice of candidate key chosen by database designer : cannot be null
47173

48174

49175
## Reference Material

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/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)