Objective: This class introduces more clauses (group by, having) in the select statement. MySQL joins (inner, self, left and right) should be explained with demonstration (Employee table with reportsTo field and Department table with its PK in Employee table is suitable for this demonstration). Promise based JavaScript program with SQL prepared statements should be understood by students. The program can be found in the Week2 folder (Credits: @remarcmij)
Before arriving to class on Sunday, please watch all of the videos in this video playlist on Lynda.
Also, please read the following page that explains the ACID database model.
Following links are worth reading.
- [Working with nulls] (https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html)
- [TO DEFAULT or TO NULL] (https://blog.jooq.org/2014/11/11/have-you-ever-wondered-about-the-difference-between-not-null-and-default/)
Creating foreign key while creating the table
CREATE TABLE Employee (
other fields,
dept_id int,
foreign key (dept_id)
references Department(id)
);
Creating the foreign key by explicitly adding the constraint
ALTER TABLE Employee ADD CONSTRAINT fk_dept foreign key (dept_id) references Department(id);
A database dump (aka SQL dump) contains a record of the table structure
and/or the data from a database and is usually in the form of a list of SQL statements.
(An example file named world.sql is present in the Week2 folder)
- Collecting the dump of an existing database from terminal
mysqldump -uroot -p database > dump-file.sql - Applying the dump from mysql command prompt
source /path/to/the/dump/file - Applying the dump from the terminal
mysql -uroot -p [database] < /path/to/the/dump/file
- Group by clause is used to group rows with same values.
- It can be used in conjunction with aggregate functions (E.g. min, max).
- The queries that contain the group by clause only return a single row for every grouped item.
- Having clause restricts the query results of group by clause.
INSERT INTO Department SET dept_id=101, dept_name='fun', dept_head='unmesh';
The program is called async-create-insert.js and can be found in Week2 folder.
- async : to create asynchronous function and ensure they return promise without having to worry about building those promises
- await : to call a function returning promise without having to call .then() over that promise
- promisify() : to convert a callback based function to a promise based one.
- One to One (one user has one profile)
- One to Many (one department has many employees)
- Many to Many (book(s) and author(s))
alter table Employee add column dept_id int
update Department set dept_head = 'Lucas' where dept_id = 3;
- A comma (,) after FROM is equivalent to the CROSS join.
- Implicit inner join (when the keyword JOIN is not used), WHERE clause has conditions.
- self join use case : Employee table with (eid field and reports_to field)
- left and right join : reverse of each other
- Join manual
- OWASP on SQL Injection
- Parameter Validation on Wikipedia
- Node MySQL Escaping Query Values
- Node MySQL Preparing Queries (automatic escaping)
- MySQL SHOW GRANTS
- Falsehoods Programmers Believe About Names
- Rewatch the previously recorded session: part 1
- Rewatch the previously recorded session: part 2
- Rewatch the previously recorded session: part 3