diff --git a/.RData b/.RData new file mode 100644 index 0000000..13bd739 Binary files /dev/null and b/.RData differ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 0000000..6693f6f --- /dev/null +++ b/.Rhistory @@ -0,0 +1,437 @@ +#install.packages("DBI", "RMySQL") +library(DBI) +library(RMySQL) +db_user <- 'admin' +db_password <- 'testsql!' +db_name <- 'oudb' +db_host <- 'database-1.cvnrn4rmwkcz.us-east-2.rds.amazonaws.com' +db_port <- 3306 +mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) +summary(mydb) +#Student demographic data +studentInfo <- read.csv("studentInfo.csv", header = TRUE) +#Student assessment data +studentAssessment <- read.csv("studentAssessment.csv", header = TRUE) +#Course data +courses <- read.csv("courses.csv", header = TRUE) +studentRegistration <- read.csv("studentRegistration.csv", header = TRUE) +#List the tables in the DB - should be zero +dbListTables(mydb) +#Write a new table to the DB +dbWriteTable(mydb, "studentInfo", studentInfo) +dbWriteTable(mydb, "studentAssessment", studentAssessment) +dbWriteTable(mydb, "courses", courses) +dbWriteTable(mydb, "studentRegistration", studentRegistration) +#List tables to see that table was added +dbListTables(mydb) +#Read a particular table +dbReadTable(mydb, 'studentInfo') +#EXERCISE 1 +#Make two toy data sets with at least three variables and at least 30 rows each in them. Have a mix of numeric and character variables. Transfer these dataframes to your SQL database using the DBI commands. Name the tables whatever you like. +Toy_assessment <- read.csv("Toy_assessment.csv", header = TRUE, StringAsFactor=TRUE) +Toy_assessment <- read.csv("Toy_assessment.csv", header = TRUE) +Toy_info <- read.csv("Toy_info.csv", header = TRUE) +dbListTables(mydb) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +dbWriteTable(mydb, "Toy_info", Toy_info) +dbListTables(mydb) +#Query a portion of the database (always returns dataframe) +dbGetQuery(mydb, "SELECT * FROM studentInfo LIMIT 10;") +dbGetQuery(mydb, "SELECT * FROM studentInfo ORDER BY id_student LIMIT 10;") +dbGetQuery(mydb, "SELECT id_student, gender FROM studentInfo ORDER BY id_student DESC LIMIT 10;") #Order listed will be reflected in order in table +dbGetQuery(mydb, "SELECT id_student AS 'Student ID', gender FROM studentInfo LIMIT 10;") #SQL Standard says quotes for literal strings and double quotes for everything else but that conflicts with R +#Count the number of rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") +#Using a WHERE statement on all columns +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50;") +#Using a WHERE statement on a single column (will not include missing data) +dbGetQuery(mydb, "SELECT COUNT(score) FROM studentAssessment WHERE score > 50;") +#Using an AND statement +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id_assessment = '1752';") +#EXERCISE 2 +#Read one of your toy data tables, make sure the output is ordered in descending order, you rename one of the variables and the output is limited to the first 20 rows. +dbGetQuery(mydb, "SELECT price AS 'Price', category_number, brand FROM Toy_info ORDER BY price DESC LIMIT 20;") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb, "SELECT COUNT(safety_test) FROM Toy_assessment WHERE safety_test = 'Pass';") +dbGetQuery(mydb, "SELECT COUNT(quality_level) FROM Toy_assessment WHERE quality_level <3 ;") +#Count rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") +#Add a row +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted, is_banked, score) VALUES ('00001', '1', '20', '0', '50');") +#Count rows again +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") +#Add a row with missing values +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted) VALUES ('00001', '1', '20');") +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") +#Update a row +dbGetQuery(mydb, "UPDATE studentAssessment SET score = '20' WHERE id_student = 1;") +dbGetQuery(mydb, "SELECT id_student, score FROM studentAssessment ORDER BY id_student LIMIT 10;") +#Update a row with NULL +dbGetQuery(mydb, "UPDATE studentAssessment SET score = 'NULL' WHERE id_student = 6516;") +#Delete a row (destructive) +dbGetQuery(mydb, "DELETE FROM studentAssessment WHERE id_student = 1;") +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") +#EXERCISE 3 +#Insert a new row in one of your toy data tables leaving one variable empty. Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. +dbGetQuery(mydb, "INSERT INTO Toy_info (price, category_number, brand) VALUES ('1', '0', 'Inserted');") +dbGetQuery(mydb, "UPDATE Toy_info SET ToyInfo = 'NULL' WHERE category_number = 0 ;") +dbReadTable(mydb,'Toy_info') +dbGetQuery(mydb, "DELETE FROM Toy_info WHERE category_number = 0;") +dbGetQuery(mydb, "UPDATE Toy_assessment SET quality_level = '-1' WHERE factory_number = 13098;") +dbReadTable(mydb,'Toy_assessment') +dbGetQuery(mydb, "DELETE FROM Toy_assessment WHERE quality_level = -1;") +dbGetQuery(mydb, "SELECT COUNT(*) FROM Toy_assessment;") +#Creating a new table in SQL +dbGetQuery(mydb,"CREATE TABLE test ( +score INTEGER, +student TEXT +);") +dbListTables(mydb) +#Inserting data into the table +dbGetQuery(mydb, "INSERT INTO test VALUES ( 10, 'Amy' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 11, 'Jen' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 9, 'Frank' );") +dbGetQuery(mydb, "SELECT * FROM test;") +#Inserting a NULL row +dbGetQuery(mydb, "INSERT INTO test DEFAULT VALUES;") #Will not work use instead: +dbGetQuery(mydb,"INSERT INTO test (score, student) SELECT score, id_student FROM studentAssessment;") +dbGetQuery(mydb, "DROP TABLE test;") +dbGetQuery(mydb, "SELECT * FROM test;") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") +dbGetQuery(mydb,"CREATE TABLE Toy_infonew ( +ToyInfo TEXT, price INTEGER, category_number INTEGER, +brand TEXT);") +dbGetQuery(mydb,"INSERT INTO Toy_infonew (ToyInfo, price, category_number,brand) SELECT ToyInfo, price, category_number,brand FROM Toy_info;") +dbReadTable(mydb,'Toy_infonew') +dbGetQuery(mydb, "DROP TABLE Toy_info;") +#NULL is a state (similar to R), represents the lack of a value. But is not compatible with R backend so this code doesn't work as part of dbGetQuery() +#This doesn't work because NULL is not a value +SELECT * FROM test WHERE score = NULL; +SELECT * FROM test WHERE score is NULL; +#Create table where student column *cannot* be NULL +dbGetQuery(mydb,"CREATE TABLE test2 ( +score INTEGER, +student TEXT NOT NULL +);") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") +dbGetQuery(mydb,"CREATE TABLE test2 ( +score INTEGER DEFAULT 0, +student TEXT +);") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (student) VALUES ('B');") +dbGetQuery(mydb, "SELECT * FROM test2;") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") +dbGetQuery(mydb,"CREATE TABLE test2 ( +score INTEGER UNIQUE, +student TEXT +);") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") +#Error because of unique +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") +#EXERCISE 5 +#Recreate one of your toy data tables with the constraint that for one of the integer variables the default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table. +dbGetQuery(mydb,"CREATE TABLE test2 ( ToyInfo TEXT, price INTEGER, category_number INTEGER, brand TEXT );") +dbGetQuery(mydb, "DROP TABLE test2;") +#EXERCISE 5 +#Recreate one of your toy data tables with the constraint that for one of the integer variables the default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table. +dbGetQuery(mydb,"CREATE TABLE test2 ( ToyInfo TEXT, price INTEGER, category_number INTEGER, brand TEXT );") +dbGetQuery(mydb,"CREATE TABLE test3 ( ToyInfo TEXT, price INTEGER DEFAULT 0, category_number INTEGER, brand TEXT );") +dbGetQuery(mydb,"INSERT INTO test3 (ToyInfo, price, category_number,brand) VALUES ('NA','1', '1','A');") +dbGetQuery(mydb,"INSERT INTO test3 (ToyInfo, brand) VALUES (NULL,'A');") +dbGetQuery(mydb, "SELECT * FROM test3;") +dbGetQuery(mydb, "DROP TABLE test3;") +#Add a column with default value 1 +dbGetQuery(mydb, "ALTER TABLE studentAssessment ADD email INTEGER DEFAULT 1 ") +dbGetQuery(mydb, "SELECT * FROM studentAssessment LIMIT 10;") +#Delete a column +dbGetQuery(mydb, "ALTER TABLE studentAssessment DROP COLUMN email;") +#EXERCISE 6 +#Add a column to one of your toy data tables with a default value of 3. Display your new table. Delete this column. +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD address INTEGER DEFAULT 3 ") +dbGetQuery(mydb, "SELECT * FROM Toy_assessment LIMIT 10;") +dbGetQuery(mydb, "ALTER TABLE Toy_assessment DROP COLUMN address;") +dbGetQuery(mydb,"CREATE TABLE test3 ( +id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax +score INTEGER, +student TEXT +);") +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (1, 'A');") +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (5, 'B');") +dbGetQuery(mydb, "SELECT * FROM test3;") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test3;") +#EXERCISE 7 +#Create a new table with four variables and a primary key that is a sequential id value. +dbGetQuery(mydb,"CREATE TABLE test4 ( +id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax +score INTEGER, +student TEXT +);") +dbGetQuery(mydb,"INSERT INTO test4 (score, student) VALUES (1, 'A');") +dbGetQuery(mydb,"INSERT INTO test4(score, student) VALUES (5, 'B');") +dbGetQuery(mydb, "SELECT * FROM test4;") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test4;") +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 ORDER BY date_submitted DESC;") +#OR Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 OR date_submitted < 2 ORDER BY date_submitted DESC;") +#AND Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 AND id_student = 325750 ORDER BY date_submitted DESC;") +#LIKE +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region%';") +#Begin with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE 'Region%';") +#End with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region';") +#'c' is the second letter +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '_c%';") +#IN +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region IN ('Wales','Ireland');") +#EXERCISE 8 +#Query one of your original toy data tables, for two different conditions. +dbGetQuery(mydb, "SELECT quality_level, factory_number FROM Toy_assessment WHERE quality_level > 2 AND factory_number < 50000 ORDER BY factory_number DESC;") +dbGetQuery(mydb, "SELECT price, category_number, brand FROM Toy_infonew WHERE brand LIKE 'A%';") +dbGetQuery(mydb, "SELECT DISTINCT region FROM studentInfo;") +dbGetQuery(mydb, "SELECT DISTINCT region, gender FROM studentInfo;") +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb,"INSERT INTO Toy_infonew (ToyInfo, price, category_number,brand) VALUES ('NA','357', '3','Morgan');") +dbGetQuery(mydb, "SELECT DISTINCT brand FROM Toy_infonew;") +dbGetQuery(mydb, "CREATE TABLE booltest (a INTEGER, b INTEGER);") +dbGetQuery(mydb, "INSERT INTO booltest VALUES (1, 0);") +dbGetQuery(mydb, "SELECT * FROM booltest;") +dbGetQuery(mydb,"SELECT +CASE WHEN a THEN 'true' ELSE 'false' END as boolA, +CASE WHEN b THEN 'true' ELSE 'false' END as boolB +FROM booltest") +dbGetQuery(mydb,"SELECT +CASE a WHEN 1 THEN 'true' ELSE 'false' END as boolA, +CASE b WHEN 1 THEN 'true' ELSE 'false' END as boolB +FROM booltest") +View(Toy_info) +View(Toy_assessment) +View(Toy_info) +#Create two tables with matches and join them +dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "CREATE TABLE right_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 1, 'left 01');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 2, 'left 02');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 3, 'left 03');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 4, 'left 04');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 5, 'left 05');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 10, 'left 10');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 11, 'left 11');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 12, 'left 12');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 13, 'left 13');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 14, 'left 14');") +dbGetQuery(mydb, "SELECT * FROM left_table;") +dbGetQuery(mydb, "SELECT * FROM right_table;") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +JOIN right_table AS r ON l.id = r.id") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +RIGHT JOIN right_table AS r ON l.id = r.id") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +LEFT JOIN right_table AS r ON l.id = r.id") +#Union +dbGetQuery(mydb, "SELECT * FROM left_table +UNION +SELECT * FROM right_table;") +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbGetQuery(mydb, "ALTER TABLE Toy_Assessment ADD id INTEGER") +dbReadTable(mydb,'Toy_assessment') +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD id INTEGER") +dbGetQuery(mydb,"INSERT INTO test3 (id) VALUES (1);") +dbGetQuery(mydb,"INSERT INTO Toy_assessment(id) VALUES (1);") +dbReadTable(mydb,'Toy_assessment') +dbReadTable(mydb,'Toy_assessment') +dbGetQuery(mydb, "ALTER TABLE Toy_assessment DROP COLUMN id;") +dbReadTable(mydb,'Toy_assessment') +dbWriteTable(mydb, "Toy_Info", Toy_info) +dbWriteTable(mydb, "Toy_Assessment", Toy_assessment) +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD id INTEGER VALUES (1:34) ") +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD id INTEGER") +dbReadTable(mydb,'Toy_assessment') +dbWriteTable(mydb, "Toy_Assessment", Toy_assessment) +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +dbWriteTable(mydb, "Toy_Assessment", Toy_assessment) +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +dbGetQuery(mydb, "DROP TABLE Toy_Assessment;") +dbGetQuery(mydb, "DROP TABLE Toy_Info;") +dbWriteTable(mydb, "Toy_info", Toy_info) +dbReadTable(mydb, 'Toy_info') +dbReadTable(mydb, 'Toy_assessment') +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD id INTEGER VALUES (1:34) ") +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD id INTEGER") +dbReadTable(mydb, 'Toy_assessment') +dbGetQuery(mydb,"INSERT INTO Toy_assessment (id) VALUES (1);") +dbReadTable(mydb, 'Toy_assessment') +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbWriteTable(mydb, "Toy_info", Toy_info) +dbGetQuery(mydb, "ALTER TABLE Toy_assessment DROP COLUMN id;") +dbWriteTable(mydb, "Toy_info", Toy_info) +dbReadTable(mydb, 'Toy_assessment') +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +dbGetQuery(mydb, "DROP TABLE Toy_info;") +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbWriteTable(mydb, "Toy_info", Toy_info) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +dbGetQuery(mydb, "INSERT INTO left_table VALUES (1);") +left_id <- read.csv("left_id.csv", header = TRUE) +right_id <- read.csv("right_id.csv", header = TRUE) +dbWriteTable(mydb, "left_id",left_id ) +dbWriteTable(mydb, "right_id", right_id) +dbGetQuery(mydb, "SELECT * FROM Toy_assessment +UNION +SELECT * FROM left_id") +dbGetQuery(mydb, "SELECT * FROM Toy_info +UNION +SELECT * FROM right_id") +mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) +summary(mydb) +dbGetQuery(mydb, "DROP TABLE Toy_info;") +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbWriteTable(mydb, "Toy_info", Toy_info) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +left_id <- read.csv("left_id.csv", header = TRUE) +right_id <- read.csv("right_id.csv", header = TRUE) +dbWriteTable(mydb, "left_id",left_id ) +dbWriteTable(mydb, "right_id", right_id) +dbGetQuery(mydb, "SELECT * FROM Toy_assessment +UNION +SELECT * FROM left_id") +dbGetQuery(mydb,"SELECT l.* AS Toy_assessment, r.* AS Toy_info +FROM Toy_assessment AS l +JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb, "SELECT * FROM Toy_assessment +UNION +SELECT * FROM Toy_info;") +dbGetQuery(mydb,"SELECT Toy_assessment.quality_level, Toy_assessment.safety_test, Toy_assessment.factory_number, Toy_assessment.Common_id, Toy_info.price, Toy_info.category_number, Toy_info.brand, +FROM Toy_assessment +JOIN Toy_info ON Toy_assessment.Common_id = Toy_info.Common_id") +mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) +summary(mydb) +dbGetQuery(mydb,"SELECT Toy_assessment.quality_level, Toy_assessment.safety_test, Toy_assessment.factory_number, Toy_assessment.Common_id, Toy_info.price, Toy_info.category_number, Toy_info.brand, +FROM Toy_assessment +JOIN Toy_info ON Toy_assessment.Common_id = Toy_info.Common_id") +dbGetQuery(mydb,"SELECT Toy_assessment.quality_level, Toy_assessment.safety_test, Toy_assessment.factory_number, Toy_assessment.Common_id, Toy_info.price, Toy_info.category_number, Toy_info.brand, +FROM Toy_assessment +JOIN Toy_info ON Toy_assessment.Common_id = Toy_info.Common_id") +dbGetQuery(mydb,"SELECT a_assessment.quality_level, a.safety_test, a.factory_number, a.Common_id, b.price, b.category_number, b.brand, +FROM Toy_assessment AS a +JOIN Toy_info AS b ON a.Common_id = b.Common_id") +dbGetQuery(mydb,"SELECT a_assessment.quality_level, a.safety_test, a.factory_number, a.Common_id, b.price, b.category_number, b.brand, +FROM Toy_assessment AS a JOIN Toy_info AS b ON a.Common_id = b.Common_id") +dbReadTable(mydb, 'Toy_info') +dbReadTable(mydb, 'Toy_assessment') +dbGetQuery(mydb,"SELECT a.quality_level, a.safety_test, a.factory_number, a.Common_id, b.price, b.category_number, b.brand, +FROM Toy_assessment AS a JOIN Toy_info AS b ON a.Common_id = b.Common_id") +dbGetQuery(mydb,"SELECT a.quality_level, a.safety_test, a.factory_number, a.Common_id AS Toy_assessment, b.price, b.category_number, b.brand AS Toy_info, +FROM Toy_assessment AS a JOIN Toy_info AS b ON a.Common_id = b.Common_id") +#Create two tables with matches and join them +dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "DROP TABLE left_table;") +dbGetQuery(mydb, "DROP TABLE right_table;") +#Create two tables with matches and join them +dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "CREATE TABLE right_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 1, 'left 01');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 2, 'left 02');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 3, 'left 03');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 4, 'left 04');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 5, 'left 05');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 10, 'left 10');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 11, 'left 11');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 12, 'left 12');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 13, 'left 13');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 14, 'left 14');") +dbGetQuery(mydb, "SELECT * FROM left_table;") +dbGetQuery(mydb, "SELECT * FROM right_table;") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +JOIN right_table AS r ON l.id = r.id") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +RIGHT JOIN right_table AS r ON l.id = r.id") +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table +FROM left_table AS l +LEFT JOIN right_table AS r ON l.id = r.id") +#Union +dbGetQuery(mydb, "SELECT * FROM left_table +UNION +SELECT * FROM right_table;") +dbGetQuery(mydb,"SELECT l.* AS Toy_assessment, r.* AS Toy_info, ++ FROM Toy_assessment AS l JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, r.price, r.category_number,r.brand ++ FROM Toy_assessment AS l JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, r.price, r.category_number, r.brand ++ FROM Toy_assessment AS l JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, r.price, r.category_number, r.brand ++ FROM Toy_assessment AS l JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, r.price, r.category_number, r.brand ++ FROM Toy_assessment AS l +JOIN Toy_info AS r ON l.Common_id = r.Common_id") +dbGetQuery(mydb, "SELECT * FROM Toy_assessment +UNION +SELECT * FROM Toy_info;") +dbGetQuery(mydb, "DROP TABLE Toy_info;") +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbWriteTable(mydb, "Toy_info", Toy_info) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +#As I already added a column with random id as the common variable into the two tables, we do not need to add ids by repeating inserting. +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, l.id, r.price, r.category_number, r.brand +FROM Toy_assessment AS l +JOIN Toy_info AS r ON l.id = r.id") +View(Toy_assessment) +Toy_assessment <- read.csv("Toy_assessment.csv", header = TRUE) +Toy_info <- read.csv("Toy_info.csv", header = TRUE) +dbGetQuery(mydb, "DROP TABLE Toy_assessment;") +dbGetQuery(mydb, "DROP TABLE Toy_info;") +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. +dbWriteTable(mydb, "Toy_info", Toy_info) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +#As I already added a column with random id as the common variable into the two tables, we do not need to add ids by repeating inserting. +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, l.id, r.price, r.category_number, r.brand +FROM Toy_assessment AS l +JOIN Toy_info AS r ON l.id = r.id") +#Now disconnect from your database +dbDisconnect(mydb) +#Then retunr to your AWS console and: +#1. Click on "Actions" and then "Stop" +#2. Do NOT make a snapshot +#3 Click on "Actions" again and click "Delete" +#4. Unclick "Make a final snapshot" +#5. Clicl "I acknowledge that upon instance deletion, automated backups, including system snapshots and point-in-time recovery, will no longer be available." +#6. Type "delete me" into the field +#Failure to follow these steps could result in charges to your credit card. diff --git a/Project 1.docx b/Project 1.docx new file mode 100644 index 0000000..ac4ab93 Binary files /dev/null and b/Project 1.docx differ diff --git a/Project-sql-Siyuan.Rmd b/Project-sql-Siyuan.Rmd new file mode 100644 index 0000000..8182b18 --- /dev/null +++ b/Project-sql-Siyuan.Rmd @@ -0,0 +1,446 @@ +--- +title: "Project-sql" +author: "Siyuan Gu" +date: "2021/4/28" +output: html_document +--- + +title: "sql-workshop" +author: "Charles Lang" +output: html_document +--- + +Before you follow the directions below, please take a screenshot of your AWS console showing the running database and upload it to your repo. + +## Connect to AWS MySQL Database +```{r} +#install.packages("DBI", "RMySQL") + +library(DBI) +library(RMySQL) + +db_user <- 'admin' +db_password <- 'testsql!' +db_name <- 'oudb' +db_host <- 'database-1.cvnrn4rmwkcz.us-east-2.rds.amazonaws.com' +db_port <- 3306 + +mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) +summary(mydb) +``` + +## Load OU Data +```{r} +#Student demographic data +studentInfo <- read.csv("studentInfo.csv", header = TRUE) +#Student assessment data +studentAssessment <- read.csv("studentAssessment.csv", header = TRUE) +#Course data +courses <- read.csv("courses.csv", header = TRUE) +studentRegistration <- read.csv("studentRegistration.csv", header = TRUE) +``` + +## Write data to the DB using the DBI package +```{r} +#List the tables in the DB - should be zero +dbListTables(mydb) + +#Write a new table to the DB +dbWriteTable(mydb, "studentInfo", studentInfo) +dbWriteTable(mydb, "studentAssessment", studentAssessment) +dbWriteTable(mydb, "courses", courses) +dbWriteTable(mydb, "studentRegistration", studentRegistration) + +#List tables to see that table was added +dbListTables(mydb) + +#Read a particular table +dbReadTable(mydb, 'studentInfo') + +#EXERCISE 1 +#Make two toy data sets with at least three variables and at least 30 rows each in them. Have a mix of numeric and character variables. Transfer these dataframes to your SQL database using the DBI commands. Name the tables whatever you like. +Toy_assessment <- read.csv("Toy_assessment.csv", header = TRUE) +Toy_info <- read.csv("Toy_info.csv", header = TRUE) +dbListTables(mydb) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) +dbWriteTable(mydb, "Toy_info", Toy_info) +dbListTables(mydb) +``` + +## Getting into SQL - READING +```{r} +#Query a portion of the database (always returns dataframe) +dbGetQuery(mydb, "SELECT * FROM studentInfo LIMIT 10;") + +dbGetQuery(mydb, "SELECT * FROM studentInfo ORDER BY id_student LIMIT 10;") + +dbGetQuery(mydb, "SELECT id_student, gender FROM studentInfo ORDER BY id_student DESC LIMIT 10;") #Order listed will be reflected in order in table + +dbGetQuery(mydb, "SELECT id_student AS 'Student ID', gender FROM studentInfo LIMIT 10;") #SQL Standard says quotes for literal strings and double quotes for everything else but that conflicts with R + +#Count the number of rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#Using a WHERE statement on all columns +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50;") + +#Using a WHERE statement on a single column (will not include missing data) +dbGetQuery(mydb, "SELECT COUNT(score) FROM studentAssessment WHERE score > 50;") + +#Using an AND statement +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id_assessment = '1752';") + +#EXERCISE 2 +#Read one of your toy data tables, make sure the output is ordered in descending order, you rename one of the variables and the output is limited to the first 20 rows. + +dbGetQuery(mydb, "SELECT price AS 'Price', category_number, brand FROM Toy_info ORDER BY price DESC LIMIT 20;") + +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb, "SELECT COUNT(safety_test) FROM Toy_assessment WHERE safety_test = 'Pass';") +dbGetQuery(mydb, "SELECT COUNT(quality_level) FROM Toy_assessment WHERE quality_level <3 ;") +``` + +## Getting into SQL - UPDATING +```{r} +#Count rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#Add a row +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted, is_banked, score) VALUES ('00001', '1', '20', '0', '50');") + +#Count rows again +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Add a row with missing values +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted) VALUES ('00001', '1', '20');") + +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Update a row +dbGetQuery(mydb, "UPDATE studentAssessment SET score = '20' WHERE id_student = 1;") + +dbGetQuery(mydb, "SELECT id_student, score FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Update a row with NULL +dbGetQuery(mydb, "UPDATE studentAssessment SET score = 'NULL' WHERE id_student = 6516;") + +#Delete a row (destructive) +dbGetQuery(mydb, "DELETE FROM studentAssessment WHERE id_student = 1;") + +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#EXERCISE 3 +#Insert a new row in one of your toy data tables leaving one variable empty. Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. + +dbGetQuery(mydb, "INSERT INTO Toy_info (price, category_number, brand) VALUES ('1', '0', 'Inserted');") +dbGetQuery(mydb, "UPDATE Toy_info SET ToyInfo = 'NULL' WHERE category_number = 0 ;") +dbReadTable(mydb,'Toy_info') +dbGetQuery(mydb, "DELETE FROM Toy_info WHERE category_number = 0;") +dbGetQuery(mydb, "UPDATE Toy_assessment SET quality_level = '-1' WHERE factory_number = 13098;") +dbReadTable(mydb,'Toy_assessment') +dbGetQuery(mydb, "DELETE FROM Toy_assessment WHERE quality_level = -1;") +dbGetQuery(mydb, "SELECT COUNT(*) FROM Toy_assessment;") +``` + +## Add/Deleting Table +```{r} +#Creating a new table in SQL +dbGetQuery(mydb,"CREATE TABLE test ( + score INTEGER, + student TEXT + );") + +dbListTables(mydb) + +#Inserting data into the table +dbGetQuery(mydb, "INSERT INTO test VALUES ( 10, 'Amy' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 11, 'Jen' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 9, 'Frank' );") + +dbGetQuery(mydb, "SELECT * FROM test;") + +#Inserting a NULL row +dbGetQuery(mydb, "INSERT INTO test DEFAULT VALUES;") #Will not work use instead: + +dbGetQuery(mydb,"INSERT INTO test (score, student) SELECT score, id_student FROM studentAssessment;") + +#Delete a table +dbGetQuery(mydb, "DROP TABLE test;") + +dbGetQuery(mydb, "SELECT * FROM test;") #This should produce an error since your table no longer exists + +#Delete a table if it exists +dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") #No error since it is only if it exists +``` + +#EXERCISE 4 +#Create a table that is exactly the same as your first toy data table but this time use SQL commands. Display your new table. Then delete the original table. +```{r} +dbGetQuery(mydb,"CREATE TABLE Toy_infonew ( + ToyInfo TEXT, price INTEGER, category_number INTEGER, + brand TEXT);") + +dbGetQuery(mydb,"INSERT INTO Toy_infonew (ToyInfo, price, category_number,brand) SELECT ToyInfo, price, category_number,brand FROM Toy_info;") + +dbReadTable(mydb,'Toy_infonew') + +dbGetQuery(mydb, "DROP TABLE Toy_info;") +``` + +# NULL Value +```{r} +#NULL is a state (similar to R), represents the lack of a value. But is not compatible with R backend so this code doesn't work as part of dbGetQuery() + +#This doesn't work because NULL is not a value +SELECT * FROM test WHERE score = NULL; + +#Instead use +SELECT * FROM test WHERE score is NULL; + +``` + +# Constraints +```{r} +#Create table where student column *cannot* be NULL +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER, + student TEXT NOT NULL + );") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") + +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER DEFAULT 0, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (student) VALUES ('B');") + +dbGetQuery(mydb, "SELECT * FROM test2;") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") + +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER UNIQUE, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") + +#Error because of unique +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") + +#NULL is exempt +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") + +dbGetQuery(mydb, "DROP TABLE test2;") +``` + +```{r} +#EXERCISE 5 +#Recreate one of your toy data tables with the constraint that for one of the integer variables the default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table. +dbGetQuery(mydb,"CREATE TABLE test2 ( ToyInfo TEXT, price INTEGER, category_number INTEGER, brand TEXT );") +dbGetQuery(mydb,"CREATE TABLE test3 ( ToyInfo TEXT, price INTEGER DEFAULT 0, category_number INTEGER, brand TEXT );") +dbGetQuery(mydb,"INSERT INTO test3 (ToyInfo, price, category_number,brand) VALUES ('NA','1', '1','A');") +dbGetQuery(mydb,"INSERT INTO test3 (ToyInfo, brand) VALUES (NULL,'A');") +dbGetQuery(mydb, "SELECT * FROM test3;") +dbGetQuery(mydb, "DROP TABLE test3;") +``` + + +# Adding a column with a default value +```{r} +#Add a column with default value 1 +dbGetQuery(mydb, "ALTER TABLE studentAssessment ADD email INTEGER DEFAULT 1 ") + +dbGetQuery(mydb, "SELECT * FROM studentAssessment LIMIT 10;") + +#Delete a column +dbGetQuery(mydb, "ALTER TABLE studentAssessment DROP COLUMN email;") + +#EXERCISE 6 +#Add a column to one of your toy data tables with a default value of 3. Display your new table. Delete this column. +dbGetQuery(mydb, "ALTER TABLE Toy_assessment ADD address INTEGER DEFAULT 3 ") +dbGetQuery(mydb, "SELECT * FROM Toy_assessment LIMIT 10;") +dbGetQuery(mydb, "ALTER TABLE Toy_assessment DROP COLUMN address;") +``` + + +# ID Columns +```{r} +dbGetQuery(mydb,"CREATE TABLE test3 ( + id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax + score INTEGER, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (1, 'A');") +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (5, 'B');") + +dbGetQuery(mydb, "SELECT * FROM test3;") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test3;") + +#EXERCISE 7 +#Create a new table with four variables and a primary key that is a sequential id value. +dbGetQuery(mydb,"CREATE TABLE test4 ( + id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax + score INTEGER, + student TEXT + );") +dbGetQuery(mydb,"INSERT INTO test4 (score, student) VALUES (1, 'A');") +dbGetQuery(mydb,"INSERT INTO test4(score, student) VALUES (5, 'B');") +dbGetQuery(mydb, "SELECT * FROM test4;") +dbGetQuery(mydb, "DROP TABLE IF EXISTS test4;") +``` + +## Filtering (WHERE) +```{r} +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 ORDER BY date_submitted DESC;") + +#OR Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 OR date_submitted < 2 ORDER BY date_submitted DESC;") + +#AND Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 AND id_student = 325750 ORDER BY date_submitted DESC;") + +#LIKE +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region%';") + +#Begin with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE 'Region%';") + +#End with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region';") + +#'c' is the second letter +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '_c%';") + +#IN +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region IN ('Wales','Ireland');") + +#EXERCISE 8 +#Query one of your original toy data tables, for two different conditions. +dbGetQuery(mydb, "SELECT quality_level, factory_number FROM Toy_assessment WHERE quality_level > 2 AND factory_number < 50000 ORDER BY factory_number DESC;") +dbGetQuery(mydb, "SELECT price, category_number, brand FROM Toy_infonew WHERE brand LIKE 'A%';") +``` + +## Removing Duplicates +```{r} +dbGetQuery(mydb, "SELECT DISTINCT region FROM studentInfo;") + +dbGetQuery(mydb, "SELECT DISTINCT region, gender FROM studentInfo;") + +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb,"INSERT INTO Toy_infonew (ToyInfo, price, category_number,brand) VALUES ('NA','357', '3','Morgan');") +dbGetQuery(mydb, "SELECT DISTINCT brand FROM Toy_infonew;") +``` + +## Conditional Expressions (non-standard) +```{r} +dbGetQuery(mydb, "CREATE TABLE booltest (a INTEGER, b INTEGER);") +dbGetQuery(mydb, "INSERT INTO booltest VALUES (1, 0);") +dbGetQuery(mydb, "SELECT * FROM booltest;") + +dbGetQuery(mydb,"SELECT + CASE WHEN a THEN 'true' ELSE 'false' END as boolA, + CASE WHEN b THEN 'true' ELSE 'false' END as boolB + FROM booltest") + +dbGetQuery(mydb,"SELECT + CASE a WHEN 1 THEN 'true' ELSE 'false' END as boolA, + CASE b WHEN 1 THEN 'true' ELSE 'false' END as boolB + FROM booltest") +``` + +#Relationships (JOIN) - *Slide* +```{r} + +#Create two tables with matches and join them + +dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "CREATE TABLE right_table (id INTEGER, description TEXT);") + +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 1, 'left 01');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 2, 'left 02');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 3, 'left 03');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 4, 'left 04');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 5, 'left 05');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 9, 'left 09');") + +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 10, 'left 10');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 11, 'left 11');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 12, 'left 12');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 13, 'left 13');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 14, 'left 14');") + +dbGetQuery(mydb, "SELECT * FROM left_table;") +dbGetQuery(mydb, "SELECT * FROM right_table;") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + JOIN right_table AS r ON l.id = r.id") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + RIGHT JOIN right_table AS r ON l.id = r.id") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + LEFT JOIN right_table AS r ON l.id = r.id") + +#Union +dbGetQuery(mydb, "SELECT * FROM left_table + UNION + SELECT * FROM right_table;") +``` + +```{r} +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. + +dbWriteTable(mydb, "Toy_info", Toy_info) +dbWriteTable(mydb, "Toy_assessment", Toy_assessment) + +#As I already added a column with random id as the common variable into the two tables, we do not need to add ids by repeating inserting. + +dbGetQuery(mydb,"SELECT l.quality_level, l.safety_test, l.factory_number, l.id, r.price, r.category_number, r.brand + FROM Toy_assessment AS l + JOIN Toy_info AS r ON l.id = r.id") + + +``` + +```{r} +#Now disconnect from your database +dbDisconnect(mydb) + +#Then retunr to your AWS console and: + +#1. Click on "Actions" and then "Stop" +#2. Do NOT make a snapshot +#3 Click on "Actions" again and click "Delete" +#4. Unclick "Make a final snapshot" +#5. Clicl "I acknowledge that upon instance deletion, automated backups, including system snapshots and point-in-time recovery, will no longer be available." +#6. Type "delete me" into the field + +#Failure to follow these steps could result in charges to your credit card. + + +``` + + diff --git a/Toy_assessment.csv b/Toy_assessment.csv new file mode 100644 index 0000000..e4dfd4e --- /dev/null +++ b/Toy_assessment.csv @@ -0,0 +1,35 @@ +ToyAssessment,quality_level,safety_test,factory_number +,0,Pass,66513 +,1,Fail,38591 +,5,Fail,67197 +,4,Fail,14950 +,3,Fail,25906 +,3,Pass,33895 +,4,Pass,64103 +,0,Fail,85999 +,0,Fail,29256 +,2,Pass,61846 +,0,Pass,68218 +,0,Fail,13098 +,4,Pass,16636 +,2,Fail,36013 +,4,Pass,16077 +,5,Pass,52267 +,1,Fail,95938 +,4,Pass,13155 +,3,Fail,42944 +,2,Pass,49368 +,0,Pass,45379 +,1,Pass,96408 +,4,Fail,73629 +,5,Pass,33057 +,3,Pass,66114 +,4,Fail,88771 +,2,Pass,75831 +,5,Fail,39638 +,2,Fail,18825 +,4,Fail,53516 +,3,Pass,62111 +,4,Pass,48139 +,1,Fail,58120 +,5,Fail,71953 diff --git a/Toy_info.csv b/Toy_info.csv new file mode 100644 index 0000000..41bf272 --- /dev/null +++ b/Toy_info.csv @@ -0,0 +1,35 @@ +ToyInfo,price,category_number,brand +,139,1,Cody +,267,5,Bear +,193,2,Pony +,193,1,Grant +,210,4,Kel +,65,3,Knight +,474,1,Smith +,141,5,Black +,431,2,Edwards +,86,5,Ponds +,427,3,Fear +,356,3,Jackson +,157,5,T +,223,4,Kkoma +,339,2,Milytech +,225,2,Phil +,153,6,Ohio +,197,1,Tompson +,214,3,Gamma +,246,1,Alpha +,500,6,Zeta +,207,3,Auto +,393,6,Union +,329,3,Western +,253,2,Star +,437,5,Childhood +,210,1,Mimic +,65,1,Greatwall +,422,4,Confidence +,355,6,Nike +,310,2,Rstar +,229,5,Lego +,368,6,Alibaba +,357,3,Morgan diff --git a/database-web1.PNG b/database-web1.PNG new file mode 100644 index 0000000..83bf59c Binary files /dev/null and b/database-web1.PNG differ diff --git a/database-web2.PNG b/database-web2.PNG new file mode 100644 index 0000000..059460c Binary files /dev/null and b/database-web2.PNG differ diff --git a/sql-project.Rmd b/sql-project.Rmd index 99a7974..27ca3ee 100644 --- a/sql-project.Rmd +++ b/sql-project.Rmd @@ -1,381 +1,381 @@ ---- -title: "sql-workshop" -author: "Charles Lang" -output: html_document ---- - -Before you follow the directions below, please take a screenshot of your AWS console showing the running database and upload it to your repo. - -## Connect to AWS MySQL Database -```{r} -#install.packages("DBI", "RMySQL") - -library(DBI) -library(RMySQL) - -db_user <- 'admin' -db_password <- 'testsql!' -db_name <- 'oudb' -db_host <- 'PASTE YOUR ENDPOINT HERE' -db_port <- 3306 - -mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) - -summary(mydb) -``` - -## Load OU Data -```{r} -#Student demographic data -studentInfo <- read.csv("studentInfo.csv", header = TRUE) -#Student assessment data -studentAssessment <- read.csv("studentAssessment.csv", header = TRUE) -#Course data -courses <- read.csv("courses.csv", header = TRUE) -studentRegistration <- read.csv("studentRegistration.csv", header = TRUE) -``` - -## Write data to the DB using the DBI package -```{r} -#List the tables in the DB - should be zero -dbListTables(mydb) - -#Write a new table to the DB -dbWriteTable(mydb, "studentInfo", studentInfo) -dbWriteTable(mydb, "studentAssessment", studentAssessment) -dbWriteTable(mydb, "courses", courses) -dbWriteTable(mydb, "studentRegistration", studentRegistration) - -#List tables to see that table was added -dbListTables(mydb) - -#Read a particular table -dbReadTable(mydb, 'studentInfo') - -#EXERCISE 1 -#Make two toy data sets with at least three variables and at least 30 rows each in them. Have a mix of numeric and character variables. Transfer these dataframes to your SQL database using the DBI commands. Name the tables whatever you like. - -``` - -## Getting into SQL - READING -```{r} -#Query a portion of the database (always returns dataframe) -dbGetQuery(mydb, "SELECT * FROM studentInfo LIMIT 10;") - -dbGetQuery(mydb, "SELECT * FROM studentInfo ORDER BY id_student LIMIT 10;") - -dbGetQuery(mydb, "SELECT id_student, gender FROM studentInfo ORDER BY id_student DESC LIMIT 10;") #Order listed will be reflected in order in table - -dbGetQuery(mydb, "SELECT id_student AS 'Student ID', gender FROM studentInfo LIMIT 10;") #SQL Standard says quotes for literal strings and double quotes for everything else but that conflicts with R - -#Count the number of rows -dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") - -#Using a WHERE statement on all columns -dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50;") - -#Using a WHERE statement on a single column (will not include missing data) -dbGetQuery(mydb, "SELECT COUNT(score) FROM studentAssessment WHERE score > 50;") - -#Using an AND statement -dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id_assessment = '1752';") - -#EXERCISE 2 -#Read one of your toy data tables, make sure the output is ordered in descending order, you rename one of the variables and the output is limited to the first 20 rows. - -#Read the other table according to a condition of one of the variables. - -``` - -## Getting into SQL - UPDATING -```{r} -#Count rows -dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") - -#Add a row -dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted, is_banked, score) VALUES ('00001', '1', '20', '0', '50');") - -#Count rows again -dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") - -#View inserted row -dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") - -#Add a row with missing values -dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted) VALUES ('00001', '1', '20');") - -#View inserted row -dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") - -#Update a row -dbGetQuery(mydb, "UPDATE studentAssessment SET score = '20' WHERE id_student = 1;") - -dbGetQuery(mydb, "SELECT id_student, score FROM studentAssessment ORDER BY id_student LIMIT 10;") - -#Update a row with NULL -dbGetQuery(mydb, "UPDATE studentAssessment SET score = 'NULL' WHERE id_student = 6516;") - -#Delete a row (destructive) -dbGetQuery(mydb, "DELETE FROM studentAssessment WHERE id_student = 1;") - -dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") - -#EXERCISE 3 -#Insert a new row in one of your toy data tables leaving one variable empty. Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. - - -``` - -## Add/Deleting Table -```{r} -#Creating a new table in SQL -dbGetQuery(mydb,"CREATE TABLE test ( - score INTEGER, - student TEXT - );") - -dbListTables(mydb) - -#Inserting data into the table -dbGetQuery(mydb, "INSERT INTO test VALUES ( 10, 'Amy' );") -dbGetQuery(mydb, "INSERT INTO test VALUES ( 11, 'Jen' );") -dbGetQuery(mydb, "INSERT INTO test VALUES ( 9, 'Frank' );") - -dbGetQuery(mydb, "SELECT * FROM test;") - -#Inserting a NULL row -dbGetQuery(mydb, "INSERT INTO test DEFAULT VALUES;") #Will not work use instead: - -dbGetQuery(mydb,"INSERT INTO test (score, student) SELECT score, id_student FROM studentAssessment;") - -#Delete a table -dbGetQuery(mydb, "DROP TABLE test;") - -dbGetQuery(mydb, "SELECT * FROM test;") #This should produce an error since your table no longer exists - -#Delete a table if it exists -dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") #No error since it is only if it exists - -#EXERCISE 4 -#Create a table that is exactly the same as your first toy data table but this time use SQL commands. Display your new table. Then delete the original table. - -``` - -# NULL Value -```{r} -#NULL is a state (similar to R), represents the lack of a value. But is not compatible with R backend so this code doesn't work as part of dbGetQuery() - -#This doesn't work because NULL is not a value -SELECT * FROM test WHERE score = NULL; - -#Instead use -SELECT * FROM test WHERE score is NULL; - -``` - -# Constraints -```{r} -#Create table where student column *cannot* be NULL -dbGetQuery(mydb,"CREATE TABLE test2 ( - score INTEGER, - student TEXT NOT NULL - );") - -dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") - -dbGetQuery(mydb,"CREATE TABLE test2 ( - score INTEGER DEFAULT 0, - student TEXT - );") - -dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") -dbGetQuery(mydb,"INSERT INTO test2 (student) VALUES ('B');") - -dbGetQuery(mydb, "SELECT * FROM test2;") - -dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") - -dbGetQuery(mydb,"CREATE TABLE test2 ( - score INTEGER UNIQUE, - student TEXT - );") - -dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") - -#Error because of unique -dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") - -#NULL is exempt -dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") -dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") - -#EXERCISE 5 -#Recreate one of your toy data tables with the constraint that for one of the integer variablesthe default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table. - -``` - - -# Adding a column with a default value -```{r} -#Add a column with default value 1 -dbGetQuery(mydb, "ALTER TABLE studentAssessment ADD email INTEGER DEFAULT 1 ") - -dbGetQuery(mydb, "SELECT * FROM studentAssessment LIMIT 10;") - -#Delete a column -dbGetQuery(mydb, "ALTER TABLE studentAssessment DROP COLUMN email;") - -#EXERCISE 6 -#Add a column to one of your toy data tables with a default value of 3. Display your new table. Delete this column. -``` - - -# ID Columns -```{r} -dbGetQuery(mydb,"CREATE TABLE test3 ( - id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax - score INTEGER, - student TEXT - );") - -dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (1, 'A');") -dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (5, 'B');") - -dbGetQuery(mydb, "SELECT * FROM test3;") - -dbGetQuery(mydb, "DROP TABLE IF EXISTS test3;") - -#EXERCISE 7 -#Create a new table with four variables and a primary key that is a sequential id value. - -``` - -## Filtering (WHERE) -```{r} -dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 ORDER BY date_submitted DESC;") - -#OR Statement -dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 OR date_submitted < 2 ORDER BY date_submitted DESC;") - -#AND Statement -dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 AND id_student = 325750 ORDER BY date_submitted DESC;") - -#LIKE -dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region%';") - -#Begin with 'Region' -dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE 'Region%';") - -#End with 'Region' -dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region';") - -#'c' is the second letter -dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '_c%';") - -#IN -dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region IN ('Wales','Ireland');") - -#EXERCISE 8 -#Query one of your original toy data tables, for two different conditions. - -``` - -## Removing Duplicates -```{r} -dbGetQuery(mydb, "SELECT DISTINCT region FROM studentInfo;") - -dbGetQuery(mydb, "SELECT DISTINCT region, gender FROM studentInfo;") - -#EXERCISE 9 -#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. - -``` - -## Conditional Expressions (non-standard) -```{r} -dbGetQuery(mydb, "CREATE TABLE booltest (a INTEGER, b INTEGER);") -dbGetQuery(mydb, "INSERT INTO booltest VALUES (1, 0);") -dbGetQuery(mydb, "SELECT * FROM booltest;") - -dbGetQuery(mydb,"SELECT - CASE WHEN a THEN 'true' ELSE 'false' END as boolA, - CASE WHEN b THEN 'true' ELSE 'false' END as boolB - FROM booltest") - -dbGetQuery(mydb,"SELECT - CASE a WHEN 1 THEN 'true' ELSE 'false' END as boolA, - CASE b WHEN 1 THEN 'true' ELSE 'false' END as boolB - FROM booltest") -``` - -#Relationships (JOIN) - *Slide* -```{r} - -#Create two tables with matches and join them - -dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") -dbGetQuery(mydb, "CREATE TABLE right_table (id INTEGER, description TEXT);") - -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 1, 'left 01');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 2, 'left 02');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 3, 'left 03');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 4, 'left 04');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 5, 'left 05');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 6, 'left 06');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 7, 'left 07');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 8, 'left 08');") -dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 9, 'left 09');") - -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 6, 'left 06');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 7, 'left 07');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 8, 'left 08');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 9, 'left 09');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 10, 'left 10');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 11, 'left 11');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 12, 'left 12');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 13, 'left 13');") -dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 14, 'left 14');") - -dbGetQuery(mydb, "SELECT * FROM left_table;") -dbGetQuery(mydb, "SELECT * FROM right_table;") - -dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table - FROM left_table AS l - JOIN right_table AS r ON l.id = r.id") - -dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table - FROM left_table AS l - RIGHT JOIN right_table AS r ON l.id = r.id") - -dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table - FROM left_table AS l - LEFT JOIN right_table AS r ON l.id = r.id") - -#Union -dbGetQuery(mydb, "SELECT * FROM left_table - UNION - SELECT * FROM right_table;") - - -#EXERCISE 10 -# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. - -``` -```{r} -#Now disconnect from your database -dbDisconnect(mydb) - -#Then retunr to your AWS console and: - -#1. Click on "Actions" and then "Stop" -#2. Do NOT make a snapshot -#3 Click on "Actions" again and click "Delete" -#4. Unclick "Make a final snapshot" -#5. Clicl "I acknowledge that upon instance deletion, automated backups, including system snapshots and point-in-time recovery, will no longer be available." -#6. Type "delete me" into the field - -#Failure to follow these steps could result in charges to your credit card. - - -``` - +--- +title: "sql-workshop" +author: "Charles Lang" +output: html_document +--- + +Before you follow the directions below, please take a screenshot of your AWS console showing the running database and upload it to your repo. + +## Connect to AWS MySQL Database +```{r} +#install.packages("DBI", "RMySQL") + +library(DBI) +library(RMySQL) + +db_user <- 'admin' +db_password <- 'testsql!' +db_name <- 'oudb' +db_host <- 'database-1.cvnrn4rmwkcz.us-east-2.rds.amazonaws.com' +db_port <- 3306 + +mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port) + +summary(mydb) +``` + +## Load OU Data +```{r} +#Student demographic data +studentInfo <- read.csv("studentInfo.csv", header = TRUE) +#Student assessment data +studentAssessment <- read.csv("studentAssessment.csv", header = TRUE) +#Course data +courses <- read.csv("courses.csv", header = TRUE) +studentRegistration <- read.csv("studentRegistration.csv", header = TRUE) +``` + +## Write data to the DB using the DBI package +```{r} +#List the tables in the DB - should be zero +dbListTables(mydb) + +#Write a new table to the DB +dbWriteTable(mydb, "studentInfo", studentInfo) +dbWriteTable(mydb, "studentAssessment", studentAssessment) +dbWriteTable(mydb, "courses", courses) +dbWriteTable(mydb, "studentRegistration", studentRegistration) + +#List tables to see that table was added +dbListTables(mydb) + +#Read a particular table +dbReadTable(mydb, 'studentInfo') + +#EXERCISE 1 +#Make two toy data sets with at least three variables and at least 30 rows each in them. Have a mix of numeric and character variables. Transfer these dataframes to your SQL database using the DBI commands. Name the tables whatever you like. + +``` + +## Getting into SQL - READING +```{r} +#Query a portion of the database (always returns dataframe) +dbGetQuery(mydb, "SELECT * FROM studentInfo LIMIT 10;") + +dbGetQuery(mydb, "SELECT * FROM studentInfo ORDER BY id_student LIMIT 10;") + +dbGetQuery(mydb, "SELECT id_student, gender FROM studentInfo ORDER BY id_student DESC LIMIT 10;") #Order listed will be reflected in order in table + +dbGetQuery(mydb, "SELECT id_student AS 'Student ID', gender FROM studentInfo LIMIT 10;") #SQL Standard says quotes for literal strings and double quotes for everything else but that conflicts with R + +#Count the number of rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#Using a WHERE statement on all columns +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50;") + +#Using a WHERE statement on a single column (will not include missing data) +dbGetQuery(mydb, "SELECT COUNT(score) FROM studentAssessment WHERE score > 50;") + +#Using an AND statement +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id_assessment = '1752';") + +#EXERCISE 2 +#Read one of your toy data tables, make sure the output is ordered in descending order, you rename one of the variables and the output is limited to the first 20 rows. + +#Read the other table according to a condition of one of the variables. + +``` + +## Getting into SQL - UPDATING +```{r} +#Count rows +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#Add a row +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted, is_banked, score) VALUES ('00001', '1', '20', '0', '50');") + +#Count rows again +dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment;") + +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Add a row with missing values +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted) VALUES ('00001', '1', '20');") + +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Update a row +dbGetQuery(mydb, "UPDATE studentAssessment SET score = '20' WHERE id_student = 1;") + +dbGetQuery(mydb, "SELECT id_student, score FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#Update a row with NULL +dbGetQuery(mydb, "UPDATE studentAssessment SET score = 'NULL' WHERE id_student = 6516;") + +#Delete a row (destructive) +dbGetQuery(mydb, "DELETE FROM studentAssessment WHERE id_student = 1;") + +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") + +#EXERCISE 3 +#Insert a new row in one of your toy data tables leaving one variable empty. Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. + + +``` + +## Add/Deleting Table +```{r} +#Creating a new table in SQL +dbGetQuery(mydb,"CREATE TABLE test ( + score INTEGER, + student TEXT + );") + +dbListTables(mydb) + +#Inserting data into the table +dbGetQuery(mydb, "INSERT INTO test VALUES ( 10, 'Amy' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 11, 'Jen' );") +dbGetQuery(mydb, "INSERT INTO test VALUES ( 9, 'Frank' );") + +dbGetQuery(mydb, "SELECT * FROM test;") + +#Inserting a NULL row +dbGetQuery(mydb, "INSERT INTO test DEFAULT VALUES;") #Will not work use instead: + +dbGetQuery(mydb,"INSERT INTO test (score, student) SELECT score, id_student FROM studentAssessment;") + +#Delete a table +dbGetQuery(mydb, "DROP TABLE test;") + +dbGetQuery(mydb, "SELECT * FROM test;") #This should produce an error since your table no longer exists + +#Delete a table if it exists +dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") #No error since it is only if it exists + +#EXERCISE 4 +#Create a table that is exactly the same as your first toy data table but this time use SQL commands. Display your new table. Then delete the original table. + +``` + +# NULL Value +```{r} +#NULL is a state (similar to R), represents the lack of a value. But is not compatible with R backend so this code doesn't work as part of dbGetQuery() + +#This doesn't work because NULL is not a value +SELECT * FROM test WHERE score = NULL; + +#Instead use +SELECT * FROM test WHERE score is NULL; + +``` + +# Constraints +```{r} +#Create table where student column *cannot* be NULL +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER, + student TEXT NOT NULL + );") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") + +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER DEFAULT 0, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (student) VALUES ('B');") + +dbGetQuery(mydb, "SELECT * FROM test2;") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test2;") + +dbGetQuery(mydb,"CREATE TABLE test2 ( + score INTEGER UNIQUE, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") + +#Error because of unique +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES ('1', 'A');") + +#NULL is exempt +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") +dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');") + +#EXERCISE 5 +#Recreate one of your toy data tables with the constraint that for one of the integer variables the default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table. + +``` + + +# Adding a column with a default value +```{r} +#Add a column with default value 1 +dbGetQuery(mydb, "ALTER TABLE studentAssessment ADD email INTEGER DEFAULT 1 ") + +dbGetQuery(mydb, "SELECT * FROM studentAssessment LIMIT 10;") + +#Delete a column +dbGetQuery(mydb, "ALTER TABLE studentAssessment DROP COLUMN email;") + +#EXERCISE 6 +#Add a column to one of your toy data tables with a default value of 3. Display your new table. Delete this column. +``` + + +# ID Columns +```{r} +dbGetQuery(mydb,"CREATE TABLE test3 ( + id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax + score INTEGER, + student TEXT + );") + +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (1, 'A');") +dbGetQuery(mydb,"INSERT INTO test3 (score, student) VALUES (5, 'B');") + +dbGetQuery(mydb, "SELECT * FROM test3;") + +dbGetQuery(mydb, "DROP TABLE IF EXISTS test3;") + +#EXERCISE 7 +#Create a new table with four variables and a primary key that is a sequential id value. + +``` + +## Filtering (WHERE) +```{r} +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 ORDER BY date_submitted DESC;") + +#OR Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 OR date_submitted < 2 ORDER BY date_submitted DESC;") + +#AND Statement +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 AND id_student = 325750 ORDER BY date_submitted DESC;") + +#LIKE +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region%';") + +#Begin with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE 'Region%';") + +#End with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '%Region';") + +#'c' is the second letter +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE '_c%';") + +#IN +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region IN ('Wales','Ireland');") + +#EXERCISE 8 +#Query one of your original toy data tables, for two different conditions. + +``` + +## Removing Duplicates +```{r} +dbGetQuery(mydb, "SELECT DISTINCT region FROM studentInfo;") + +dbGetQuery(mydb, "SELECT DISTINCT region, gender FROM studentInfo;") + +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. + +``` + +## Conditional Expressions (non-standard) +```{r} +dbGetQuery(mydb, "CREATE TABLE booltest (a INTEGER, b INTEGER);") +dbGetQuery(mydb, "INSERT INTO booltest VALUES (1, 0);") +dbGetQuery(mydb, "SELECT * FROM booltest;") + +dbGetQuery(mydb,"SELECT + CASE WHEN a THEN 'true' ELSE 'false' END as boolA, + CASE WHEN b THEN 'true' ELSE 'false' END as boolB + FROM booltest") + +dbGetQuery(mydb,"SELECT + CASE a WHEN 1 THEN 'true' ELSE 'false' END as boolA, + CASE b WHEN 1 THEN 'true' ELSE 'false' END as boolB + FROM booltest") +``` + +#Relationships (JOIN) - *Slide* +```{r} + +#Create two tables with matches and join them + +dbGetQuery(mydb, "CREATE TABLE left_table (id INTEGER, description TEXT);") +dbGetQuery(mydb, "CREATE TABLE right_table (id INTEGER, description TEXT);") + +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 1, 'left 01');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 2, 'left 02');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 3, 'left 03');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 4, 'left 04');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 5, 'left 05');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO left_table VALUES ( 9, 'left 09');") + +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 6, 'left 06');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 7, 'left 07');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 8, 'left 08');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 9, 'left 09');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 10, 'left 10');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 11, 'left 11');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 12, 'left 12');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 13, 'left 13');") +dbGetQuery(mydb, "INSERT INTO right_table VALUES ( 14, 'left 14');") + +dbGetQuery(mydb, "SELECT * FROM left_table;") +dbGetQuery(mydb, "SELECT * FROM right_table;") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + JOIN right_table AS r ON l.id = r.id") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + RIGHT JOIN right_table AS r ON l.id = r.id") + +dbGetQuery(mydb,"SELECT l.description AS left_table, r.description AS right_table + FROM left_table AS l + LEFT JOIN right_table AS r ON l.id = r.id") + +#Union +dbGetQuery(mydb, "SELECT * FROM left_table + UNION + SELECT * FROM right_table;") + + +#EXERCISE 10 +# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other. + +``` +```{r} +#Now disconnect from your database +dbDisconnect(mydb) + +#Then retunr to your AWS console and: + +#1. Click on "Actions" and then "Stop" +#2. Do NOT make a snapshot +#3 Click on "Actions" again and click "Delete" +#4. Unclick "Make a final snapshot" +#5. Clicl "I acknowledge that upon instance deletion, automated backups, including system snapshots and point-in-time recovery, will no longer be available." +#6. Type "delete me" into the field + +#Failure to follow these steps could result in charges to your credit card. + + +``` +