Young Men’s Leadership Academy Chadd Johnson YMLA: Expectations.
Introduction to MySQL /MariaDB and SQL Basics Read Chapter...
Transcript of Introduction to MySQL /MariaDB and SQL Basics Read Chapter...
![Page 1: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/1.jpg)
09/16/19CS445
Pacific University 1
Introduction to MySQL /MariaDBand SQL Basics
Read Chapter 3!
http://dev.mysql.com/doc/refman/
https://mariadb.com/kb/en/the-mariadb-library/documentation/
MySQL / MariaDB
![Page 2: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/2.jpg)
09/16/19CS445
Pacific University 2
College Database E-R Diagram
![Page 3: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/3.jpg)
09/16/19CS445
Pacific University 3
Database tasks
● start MariaDB– setup user passwords
● shutdown MariaDB
● create database
● create table– primary key
– index
– foreign key
● insert data– source a file
● delete data– drop
● query data– where
– join
– group
– order
– subquery
https://mariadb.com/kb/en/the-mariadb-library/documentation
*Coding standards on the class web page!
![Page 4: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/4.jpg)
09/16/19CS445
Pacific University 4
MariaDB
● ssh to db.cs.pacificu.edu (64.59.233.245)
ssh -X db.cs.pacificu.edu
[you@db ~]# $ mysql -u PUNetID -p
mysql> set password = PASSWORD('NEWPASSWORD');
mysql> show databases;
mysql> use PUNetID_test;
![Page 5: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/5.jpg)
09/16/19CS445
Pacific University 5
Data types
● TINYINT/SMALLINT/INT/BIGINT SIGNED/UNSIGNED
● BIT
● FLOAT/DOUBLE
● BOOLEAN
● CHAR / BINARY
● VARCHAR(###) / VARBINARY(###)
● DATE / TIME / DATETIME / TIMESTAMP
● [TINY|MEDIUM|LONG]TEXT
● [TINY|MEDIUM|LONG]BLOB
● ENUM
● SET
● JSON
https://mariadb.com/kb/en/the-mariadb-library/data-types/
![Page 6: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/6.jpg)
09/16/19CS445
Pacific University 6
M● “M indicates the maximum display width for integer types. The maximum display width is 255. Display width is unrelated to the range of values a type can contain, … .
● For floating-point and fixed-point types, M is the total number of digits that can be stored.“
● https://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html
![Page 7: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/7.jpg)
09/16/19CS445
Pacific University 7
Strings● Character Set
– set of characters (ASCII, UTF8, ...
● Collation– the order of the characters within the set
https://mariadb.com/kb/en/mariadb/character-set-and-collation-overview/https://mariadb.com/kb/en/mariadb/supported-character-sets-and-collations/
MariaDB [will4614_test]> show variables like 'character_set_server';+----------------------+-------+| Variable_name | Value |+----------------------+-------+| character_set_server | utf8 |+----------------------+-------+1 row in set (0.00 sec)
MariaDB [will4614_test]> show variables like 'collation_server';+------------------+-----------------+| Variable_name | Value |+------------------+-----------------+| collation_server | utf8_general_ci |+------------------+-----------------+1 row in set (0.01 sec)
![Page 8: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/8.jpg)
09/16/19CS445
Pacific University 8
Create TableCREATE TABLE People (
PersonID INT NOT NULL AUTO_INCREMENT,
FName VARCHAR(50),
LName VARCHAR(50),
Login VARCHAR(20) NOT NULL,
CONSTRAINT People_PersonID_PK PRIMARY KEY (PersonID),
CONSTRAINT People_Login_U UNIQUE (Login)
) Engine=InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin;
![Page 9: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/9.jpg)
09/16/19CS445
Pacific University 9
Create a Table
mysql> show tables;
mysql> show create table People;
![Page 10: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/10.jpg)
09/16/19CS445
Pacific University 10
Insert One Row
INSERT INTO People ( FName, LName, Login)
VALUES ( "Chadd", "Williams","chadd");
INSERT INTO tablename ( ColumnName, …) VALUES ( data, …);
![Page 11: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/11.jpg)
09/16/19CS445
Pacific University 11
Insert
INSERT INTO People ( FName, LName, Login) VALUES ( "Chadd", "Williams","chadd");
INSERT INTO People ( FName, LName, Login) VALUES ( "Doug", "Ryan","ryand");
INSERT INTO People ( FName, LName, Login) VALUES ("Shereen", "Khoja","shereen");
INSERT INTO People ( FName, LName, Login) VALUES ("Chris", "Lane", "lanec");
mysql> SELECT * FROM People;
![Page 12: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/12.jpg)
09/16/19CS445
Pacific University 12
Retrieve Data
SELECT * FROM People WHERE PersonID > 2;
SELECT column, …
FROM table
WHERE condition ;
![Page 13: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/13.jpg)
09/16/19CS445
Pacific University 13
MySQL
mysql> SELECT * FROM People WHERE PersonID > 2;
mysql> SELECT * FROM People WHERE LName = “Ryan”;
mysql> SELECT * FROM People WHERE FName like “C%”;
mysql> SELECT * FROM People WHERE FName like “c%”;
mysql> SELECT FName, LName FROM People WHERE PersonID > 1;
![Page 14: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/14.jpg)
09/16/19CS445
Pacific University 14
Create a table● Create another table
CREATE TABLE Professors ( ProfID INT NOT NULL,
Rank ENUM ('Assistant', 'Associate','Full', 'Emeritus') NOT NULL,
CONSTRAINT Professors_ProfID_PK PRIMARY KEY (ProfID)
) Engine=InnoDB;
![Page 15: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/15.jpg)
09/16/19CS445
Pacific University 15
Constraints
mysql> ALTER TABLE Professors ADD CONSTRAINT Professors_ProfID_FK FOREIGN KEY (ProfID) REFERENCES People(PersonID) ON DELETE CASCADE;
![Page 16: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/16.jpg)
09/16/19CS445
Pacific University 16
MySQL● Insert some data
INSERT INTO Professors (ProfID, Rank) VALUES (1, 'Associate'); -- chadd
INSERT INTO Professors (ProfID, Rank) VALUES (2, 'Full'); -- doug
INSERT INTO Professors (ProfID, Rank) VALUES (3, 'Associate'); -- shereen
INSERT INTO Professors (ProfID, Rank) VALUES (4, 'The Boss'); -- chris
![Page 17: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/17.jpg)
09/16/19CS445
Pacific University 17
Let's make this go faster● Load data from a SQL scriptThis file is full of INSERT and CREATE statements.
mysql> source /tmp/CreateDatabase.sql;
Let's look at that file.
db$ cat /tmp/CreateDatabase.sql | less
![Page 18: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/18.jpg)
09/16/19CS445
Pacific University 18
Deleting Data● Let's delete some data
mysql> SELECT * FROM People;
mysql> SELECT * FROM CurrentlyTeaching;
mysql> DELETE FROM People WHERE PersonID=1;
mysql> SELECT * FROM People;
mysql> SELECT * FROM CurrentlyTeaching;
mysql> SHOW TABLES;
mysql> DROP TABLE People;
mysql> source /tmp/CreateDatabase.sql;
![Page 19: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/19.jpg)
09/16/19CS445
Pacific University 19
dbeaver● Let’s use a GUI to make this easier
./dbeaver
● alias
![Page 20: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/20.jpg)
09/16/19CS445
Pacific University 20
Queries● What Courses have a MaxSize of greater than 5?
mysql> SELECT *FROM Courses WHERE MaxSize > 5;
![Page 21: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/21.jpg)
09/16/19CS445
Pacific University 21
Order By● Let's sort the output
mysql> SELECT * FROM Courses ORDER BY MaxSize;
mysql> SELECT * FROM Courses ORDER BY MaxSize DESC ;
mysql> SELECT * FROM People ORDER BY LName, FName;
![Page 22: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/22.jpg)
09/16/19CS445
Pacific University 22
Group By● Aggregate selected rows
mysql> SELECT LNameFROM People ;
mysql> SELECT LName, COUNT(*) FROM People GROUP BY LName;
mysql> SELECT AVG(MaxSize) FROM Courses;
mysql> SELECT AVG(Grade) FROM CurrentlyEnrolledGROUP BY CourseID;
● Other useful functions: AVG(), STDDEV(), MAX(), SUM()
![Page 23: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/23.jpg)
09/16/19CS445
Pacific University 23
Let's Query the Data● List all the Full professors in our database (ProfID).
● List every student with a GPA less than 1.0
(StudentID)
![Page 24: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/24.jpg)
09/16/19CS445
Pacific University 24
Joins● List all the Full professors in our database (FName, LName).
mysql> SELECT FName, LName FROM People, ProfessorsWHERE People.PersonID=Professors.ProfID
ANDRank=”Full”;
● List every student with a GPA less than 1.0
(StudentID, FName, LName)
![Page 25: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/25.jpg)
09/16/19CS445
Pacific University 25
Joins● Inner Join
– matching records in each table
SELECT * FROM People, Students WHERE (People.PersonID=Students.StudentID);
● Outer Join– all records in each table (maybe not matching)
– may produce NULL values for some columns
SELECT * FROM People LEFT JOIN Students ON (People.PersonID=Students.StudentID);
● Produce all rows from the LEFT table
– RIGHT JOIN
● Produce all rows from the RIGHT table;
![Page 26: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/26.jpg)
09/16/19CS445
Pacific University 26
Let's Query the Data● List all the courses in our database taught by an Associate professor. (CourseID) (CourseID, Title)
● How many courses are taught by each rank of professor?
● How many professors teach zero courses?
![Page 27: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/27.jpg)
09/16/19CS445
Pacific University 27
Joins● Four table joins, show all courses taught by Associate Profs (Title, FName, LName)
mysql> SELECT * FROM Courses, CurrentlyTeaching, Professors, PeopleWHERE
and Rank = “Associate”;
![Page 28: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/28.jpg)
09/16/19CS445
Pacific University 28
Joins● A join looks a one row at a time
● Some queries need more information
● Who is in a class with Bart Simpson?
![Page 29: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/29.jpg)
09/16/19CS445
Pacific University 29
Subquery● Who was in class with Bart Simpson?
● Let’s find the each course each person is in
● Let’s find all the courses Bart Simpson is in
● Let’s combine these two queries
● The goal of a subquery is to send data into or out of a subquery and filter using that data.
![Page 30: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/30.jpg)
09/16/19CS445
Pacific University 30
Subquery● Who was in class with Bart Simpson?
● Let’s find the each course each person is in
– exclude Bart Simpson
SELECT People.*, CurrentlyEnrolled.*
FROM Students, CurrentlyEnrolled, People
WHERE(Students.StudentID=CurrentlyEnrolled.StudentID)
AND People.PersonID = Students.StudentID
AND (FName != "Bart" or LName != "Simpson")
order by PersonID;
● Which courses was Bart enrolled in?
![Page 31: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/31.jpg)
09/16/19CS445
Pacific University 31
Subquery● Which courses was Bart enrolled in?
SELECT *FROM CurrentlyEnrolled AS BSClassWHERE BSClass.StudentID=5 -- Bart Simpson;
● Let’s only return rows from the previous query where the CourseID matches a row in the above query.
![Page 32: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/32.jpg)
09/16/19CS445
Pacific University 32
Subquery
SELECT People.*, CurrentlyEnrolled.*FROM Students, CurrentlyEnrolled, PeopleWHERE(Students.StudentID=CurrentlyEnrolled.StudentID) AND People.PersonID = Students.StudentIDAND (FName != "Bart" or LName != "Simpson")AND EXISTS( SELECT * FROM CurrentlyEnrolled AS BSClass WHERE (CurrentlyEnrolled.CourseID=BSClass.CourseID) AND BSClass.StudentID=5 -- Bart Simpson;)order by PersonID;
![Page 33: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/33.jpg)
09/16/19CS445
Pacific University 33
Subquries● Who has the maximum grade in each class? (Fname, Lname, grade, class name)
– Does this require a subquery?
![Page 34: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/34.jpg)
09/16/19CS445
Pacific University 34
Views
● A View is a logical table backed up by a query– Changes automatically when the results of the query change
mysql> CREATE VIEW CS150_VW AS SELECT LName, FName, Grade, StudentIDFROM Courses, CurrentlyEnrolled, PeopleWHERE Courses.CourseID=CurrentlyEnrolled.CourseID and People.PersonID=StudentID and Title like “CS150%”;
![Page 35: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/35.jpg)
09/16/19CS445
Pacific University 35
Views
mysql> SELECT * FROM CS150_VW;
mysql> DELETE FROM People WHERE PersonID=5;
mysql> SELECT * FROM CS150_VW Order by Grade;
mysql> DROP VIEW CS150_VW;
![Page 36: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/36.jpg)
09/16/19CS445
Pacific University 36
Exercise● Rebuild CS150_VW
● Determine how closely a student's grade in CS150 matches their GPA. (1.0 = perfect match, 0.5 = Grade is half the GPA, 1.5 Grade is 50% better than GPA)
● GPA goes from 0.0 to 4.0, Grade goes from 0.0 to 100.0
![Page 37: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/37.jpg)
09/16/19CS445
Pacific University 37
Control FlowIF( condition, trueValue, falseValue)
SELECT Title, IF( MaxSize > 50, 1, 0)
FROM Courses;
IFNULL(Value, returnIfValueIsNULL)
SELECT IFNULL(Title, “ITISNULL”)
FROM Courses;
There is also a case (switch) statement
![Page 38: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/38.jpg)
09/16/19CS445
Pacific University 38
GROUP BY and HAVING● Allows SQL to filter on calculated/aggregate values
● Similar to WHERE
● must be last
SELECT StudentID, avg(Grade) as AvgGrade,count(*) as NumberRows
FROM CurrentlyEnrolled
WHERE Grade > 20
GROUP BY StudentID
HAVING AvgGrade > 80 and NumberRows > 1;
![Page 39: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/39.jpg)
09/16/19CS445
Pacific University 39
Limit – only show some results
SELECT StudentID, count(*) as TotalFROM WasIn GROUP BY StudentIDHAVING Total > 1LIMIT 2; -- show only first two rows
LIMIT 2,4; -- skip the first two rows, then show the next 4
LIMIT 3; is equivalent to LIMIT 0, 3
![Page 40: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/40.jpg)
09/16/19CS445
Pacific University 40
Backup Your Database!
@db:~> mysqldump PUNetID_test -u PUNetID -p > backup_test.sql
Database NameOutput file
To see what this file looks like:
@db:~> cat backup_test.sql | less
Copy to Zeus for safe keeping!
@db:~> scp backup_test.sql PUNetID@zeus:Don't forget the colon!
![Page 41: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/41.jpg)
09/16/19CS445
Pacific University 41
Practice● List all Course titles and CourseID. For each Course, display the CourseID if Chadd teaches it and “Not A Chadd Course” otherwise.
● Find all courses whose maximum and minimum grade is at least 50 points different.
● Display each student name, course title, and student's grade in that course and the string “passing” or “not passing” if the student is not passing the course.
● Find each course that does not contain a Simpson.
● Display all students whose grade for a class is above the average grade for that class.
![Page 42: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/42.jpg)
09/16/19CS445
Pacific University 42
Explainmysql> SHOW CREATE TABLE People;
mysql> SHOW CREATE TABLE CurrentlyTeaching;
mysql> EXPLAIN SELECT * FROM People, CurrentlyTeaching WHERE (PersonID=ProfID);
mysql> EXPLAIN SELECT * FROM People, CurrentlyTeaching WHERE (PersonID=ProfID)AND FName like '%a%';
EXPLAIN
TYPE: system, const, eq_ref, ref, index, all
ROWS: number of rows scanned
![Page 43: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/43.jpg)
09/16/19CS445
Pacific University 43
Indexesmysql> USE chadd_test;
mysql> SHOW TABLE STATUS LIKE 'EnronVocab';
mysql> SHOW TABLE STATUS LIKE 'EnronWordCount';
mysql> SHOW CREATE TABLE EnronVocab;
mysql> SHOW CREATE TABLE EnronWordCount;
mysql> SHOW PROCESSLIST;
mysql> EXPLAIN SELECT WordCount FROM EnronWordCount WHERE DocID = ??;
mysql> EXPLAIN SELECT WordCount FROM EnronWordCount WHERE WordID = ??;
● 1 to 39861 DocID
● 1 to 28102 WordIDhttp://archive.ics.uci.edu/ml/datasets/Bag+of+Words
![Page 44: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/44.jpg)
09/16/19CS445
Pacific University 44
Practice● How many students are in each class?
● For each class, what was the min, max, average grade ?– do this with and without using the AVG() function.
● Who took a class with Bart Simpson and received a higer grade than Bart? Lower Grade?
![Page 45: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/45.jpg)
09/16/19CS445
Pacific University 45
INTO OUTFILE● Save a query to a text file
SELECT StudenID, count(*) as TotalFROM CurrentlyEnrolledGROUP BY StudentIDHAVING Total > 1INTO OUTFILE '/tmp/PUNETID.txt';
-- writes data on the server
gray> scp /tmp/PUNETID.txt c@zeus:
mysql -u user -p -D database -e “select ... “ > outfile
![Page 46: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/46.jpg)
09/16/19CS445
Pacific University 46
LOAD DATA INFILE
mysql> source /tmp/createTest.sql;
mysql> ALTER TABLE test DISABLE KEYS;mysql> SET FOREIGN_KEY_CHECKS=0;
mysql> LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test COLUMNS TERMINATED BY ',';
mysql> SET FOREIGN_KEY_CHECKS=1;mysql> ALTER TABLE test ENABLE KEYS;
Query OK, 69679427 rows affected (21 min 34.26 sec)
● with a well tuned MySQL (innodb_buffer_pool_size, innodb_log_*)
![Page 47: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/47.jpg)
09/16/19CS445
Pacific University 47
Triggers
CREATE TRIGGER name BEFORE INSERT ON table FOR EACH ROW BEGIN -- SQL Statements or control flow (IF)
INSERT INTO test2 SET a2 = NEW.a1; END;
BEFORE | AFTER
INSERT | DELETE | UPDATE
Cannot stop an insert!
The row being inserted
![Page 48: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/48.jpg)
09/16/19CS445
Pacific University 48
Trigger
CREATE TRIGGER name BEFORE INSERT ON table FOR EACH ROW BEGIN
SIGNAL SQLSTATE '99991' SET MESSAGE_TEXT = 'ERROR MESSAGE';
END;
DROP TRIGGER name;
![Page 49: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/49.jpg)
09/16/19CS445
Pacific University 49
Stored Procedures
![Page 50: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/50.jpg)
09/16/19CS445
Pacific University 50
Control Flow● CASE
● IF()
● IFNULL()
● NULLIF()
![Page 51: Introduction to MySQL /MariaDB and SQL Basics Read Chapter 3!zeus.cs.pacificu.edu/chadd/cs445f19/Lectures/cs445_MySQL.pdf · 09/16/19 CS445 Pacific University 25 Joins Inner Join](https://reader033.fdocuments.in/reader033/viewer/2022050506/5f979e8e4b34e0529f46a440/html5/thumbnails/51.jpg)
09/16/19CS445
Pacific University 51
Advanced SQL● Control Flow Functions
– http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html
● Trigger– http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
● http://dev.mysql.com/doc/refman/5.5/en/select.html
– Having
– Limit
– into outfile
● load data– http://dev.mysql.com/doc/refman/5.5/en/load-data.html
● Binary Data
● Stored Procedures