|
1 |
| -# Lesson 2: Practical Database Usage |
| 1 | +# Lesson 2: Group by, Having and Joins. Promisification of JS client with prepared statements |
2 | 2 |
|
3 |
| -In this class, students will learn how to use more complex SQL queries to retrieve information across tables, and interact with data including write operations. |
4 |
| - |
5 |
| -Objective: Students should be able to build CRUD functionality using SQL statements, including INSERT INTO, UPDATE, etc. |
| 3 | +Objective: This class introduces more clauses (group by, having) in the |
| 4 | +select statement. MySQL joins (inner, self, left and right) should be explained |
| 5 | +with demonstration (Employee table with **reportsTo** field and Department |
| 6 | +table with its PK in Employee table is suitable for this demonstration). |
| 7 | +Promise based JavaScript program with SQL prepared statements should be |
| 8 | +understood students. The program can be found in the Week2 folder (Credits: |
| 9 | +@remarcmij |
6 | 10 |
|
7 | 11 | ## Pre-Class Readings
|
8 | 12 |
|
9 | 13 | Before arriving to class on Sunday, please watch all of the videos in [this video playlist](https://www.lynda.com/SharedPlaylist/0d62f3e4428e44ada89466cdbc296fc0) on Lynda.
|
10 |
| -- Inserting, Updating, and Deleting |
11 |
| -- Understanding Write Conflicts |
12 |
| -- Planning Your Database |
13 |
| -- The Data Definition Language |
14 |
| -- Understanding Stored Procedures and Injection Attacks |
15 | 14 |
|
16 | 15 | Also, please read the following page that explains the ACID database model.
|
17 | 16 | - [The ACID Database Model](https://www.thoughtco.com/the-acid-model-1019731)
|
18 | 17 |
|
19 |
| -## Main Topics |
20 |
| - |
21 |
| -- INSERT INTO |
22 |
| -- UPDATE |
23 |
| -- DELETE |
24 |
| -- Writing SQL in your application |
25 |
| - - Raw SQL strings |
26 |
| - - Parameter validation |
27 |
| - - Escaping |
28 |
| - - Prepared statements |
29 |
| - - Stored procedures |
30 |
| -- Security |
31 |
| - - SQL Injection |
32 |
| - - User GRANTS |
33 |
| - - Enumeration |
| 18 | +## Topics to be covered |
| 19 | + |
| 20 | +### NOT NULL and default values in CREATE table statement |
| 21 | + |
| 22 | +Following links are worth reading. |
| 23 | +- [Working with nulls] (https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html) |
| 24 | +- [TO DEFAULT or TO NULL] (https://blog.jooq.org/2014/11/11/have-you-ever-wondered-about-the-difference-between-not-null-and-default/) |
| 25 | + |
| 26 | +### foreign key |
| 27 | + |
| 28 | +Creating foreign key while creating the table |
| 29 | +``` |
| 30 | +CREATE TABLE Employee ( |
| 31 | +other fields, |
| 32 | +dept_id int, |
| 33 | +foreign key (dept_id) |
| 34 | +references Department(id) |
| 35 | +); |
| 36 | +``` |
| 37 | + |
| 38 | +Creating the foreign key by explicitly adding the constraint |
| 39 | +``` |
| 40 | +ALTER TABLE Employee ADD CONSTRAINT fk_dept foreign key (dept_id) references Department(id); |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +### Database dump |
| 45 | + |
| 46 | +A database dump (aka SQL dump) contains a record of the table structure |
| 47 | +and/or the data from a database and is usually in the form of a list of SQL statements. |
| 48 | + |
| 49 | +- Collecting the dump of an existing database from terminal `mysqldump -uroot -p database > dump-file.sql` |
| 50 | +- Applying the dump from mysql command prompt `source /path/to/the/dump/file` |
| 51 | +- Applying the dump from the terminal `mysql -uroot -p [database] < /path/to/the/dump/file` |
| 52 | + |
| 53 | +### Group by and Having clauses |
| 54 | + |
| 55 | +- *Group by* clause is used to group rows with same values. |
| 56 | +- It can be used in conjunction with aggregate functions (E.g. min, max). |
| 57 | +- The queries that contain the *group by* clause only return a single row for every grouped item. |
| 58 | +- *Having* clause restricts the query results of *group by* clause. |
| 59 | + |
| 60 | +### INSERT INTO table SET syntax |
| 61 | + |
| 62 | +``` |
| 63 | +INSERT INTO Department SET dept_id=101, dept_name='fun', dept_head='unmesh'; |
| 64 | +``` |
| 65 | +### Promise based program demo |
| 66 | + |
| 67 | +### Relationships between tables : 1-M, M-M |
| 68 | + |
| 69 | +- One to One (one user has one profile) |
| 70 | +- One to Many (one department has many employees) |
| 71 | +- Many to Many (book(s) and author(s)) |
| 72 | + |
| 73 | +### Adding a column to the table |
| 74 | +``` |
| 75 | +alter table Employee add column dept_id int |
| 76 | +``` |
| 77 | + |
| 78 | +### Update table (add a department head for a department) |
| 79 | + |
| 80 | +``` |
| 81 | +update Department set dept_head = 'Lucas' where dept_id = 3; |
| 82 | +``` |
| 83 | + |
| 84 | +### JOINs : CROSS, left, right, self, inner |
| 85 | + |
| 86 | +- A comma (,) after **FROM** is equivalent to the CROSS join. |
| 87 | +- Implicit inner join (when the keyword **JOIN** is not used), **WHERE** clause has conditions. |
| 88 | +- self join use case : Employee table with (*eid* field and *reports_to* field) |
| 89 | +- left and right join : reverse of each other |
| 90 | +- [Join manual](https://dev.mysql.com/doc/refman/8.0/en/join.html) |
34 | 91 |
|
35 | 92 | ## Reference Material
|
36 | 93 |
|
|
0 commit comments