|
1 |
| -# Lesson 1: Retrieving Data |
| 1 | +# Lesson 1: MySQL and Node setup! Create, Insert and Select ! |
2 | 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. |
| 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. |
4 | 7 |
|
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` |
8 | 11 |
|
9 | 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.
|
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. |
20 | 15 |
|
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 |
23 | 147 |
|
| 148 | +``` |
| 149 | +Example: |
24 | 150 |
|
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 | +``` |
26 | 159 |
|
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 |
39 | 161 |
|
| 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 |
40 | 165 |
|
41 | 166 | ## Reference Material
|
42 | 167 |
|
|
0 commit comments