diff --git a/.RData b/.RData new file mode 100644 index 0000000..96f04ea Binary files /dev/null and b/.RData differ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 0000000..b778c40 --- /dev/null +++ b/.Rhistory @@ -0,0 +1,424 @@ +install.packages("DBI") +install.packages("Rtools") +updateR() +library(installr) +updateR() +install.packages("RMySQL") +#install.packages("DBI", "RMySQL") +library(DBI) +library(RMySQL) +db_user <- 'admin' +db_password <- 'HUDK4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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) +#install.packages("DBI", "RMySQL") +library(DBI) +library(RMySQL) +db_user <- 'admin' +db_password <- 'HUDK4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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) +db_user <- 'admin' +db_password <- 'HUDK4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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) +#install.packages("DBI", "RMySQL") +library(DBI) +library(RMySQL) +db_user <- 'admin' +db_password <-'Hudk4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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) +View(studentInfo) +#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. +library(tidyverse) +#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. +#loading the libraries needed to create toy data sets +library(tidyverse) +EDB1 <- studentInfo %>% select (id_student,gender,region) +EDB2 <- studentInfo %>% select(id_student,highest_education,num_of_prev_attempts) +dbWriteTable(mydb,"EDB1", EDB1) +dbWriteTable(mydb,"EDB2", EDB2) +dbReadTable(mydb, 'EDB1') +dbReadTable(mydb, 'EDB2') +#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;") +#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 +#install.packages("DBI", "RMySQL") +library(DBI) +library(RMySQL) +db_user <- 'admin' +db_password <-'Hudk4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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) +View(EDB1) +View(EDB1) +View(EDB1) +View(EDB1) +View(EDB1) +#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 id_student AS studentID FROM EDB1 ORDER BY id_student DESC LIMIT 20") +#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 id_student AS studentID,gender,region FROM EDB1 ORDER BY id_student DESC LIMIT 20") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb,"SELECT FROM EDB2 WHERE num_of_prev_attempts>1") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb,"SELECT COUNT FROM EDB2 WHERE num_of_prev_attempts>1") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb,"SELECT COUNT(*) FROM EDB2 WHERE num_of_prev_attempts>1") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb,"SELECT * FROM EDB2 WHERE num_of_prev_attempts>1") +#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');") +#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 EDB1 (id_student,gender,region) VALUES ('01000','','East');") +#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 id_student AS studentID,gender,region FROM EDB1 ORDER BY id_student DESC LIMIT 20") +#EXERCISE 3 +#Insert a new row in one of your toy data tables leaving one variable empty. +dbGetQuery(mydb,"INSERT INTO EDB1 (id_student,gender,region) VALUES ('01000','','East');") +#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;") +dbGetQuery(mydb, "SELECT * FROM EDB1 ORDER BY id_student LIMIT 10") +#Add a row +dbGetQuery(mydb, "INSERT INTO studentAssessment (id_assessment, id_student, date_submitted, is_banked, score) VALUES ('00001', '1', '20', '0', '50');") +#View inserted row +dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;") +dbGetQuery(mydb, "SELECT * FROM EDB2 ORDER BY id_student LIMIT 5" ) +#Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. +dbGetQuery(mydb, "UPDATE EDB2 SET num_of_prev_attepts='5' WHERE id_student=11391;") +#Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. +dbGetQuery(mydb, "UPDATE EDB2 SET num_of_prev_attempts='5' WHERE id_student=11391;") +dbGetQuery(mydb, "SELECT * FROM EDB2 ORDER BY id_student LIMIT 5" ) +dbGetQuery(mydb, "DELETE FROM EDB1 WHERE id_student=1000;") +dbGetQuery(mydb, "DELETE FROM EDB2 WHERE id_student=11391;") +#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;") +dbListTables(mydb) +#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. +dbGetQuery(mydb,"CREATE TABLE toy1 ( +id_student INTEGER, +gender TEXT, +regiond TEXT +);") +dbListTables(mydb) +#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. +dbGetQuery(mydb,"CREATE TABLE toy1 ( +id_student INTEGER, +gender TEXT, +region TEXT +);") +dbGetQuery(mydb, "DROP TABLE toy;") +dbGetQuery(mydb, "DROP TABLE toy1;") +#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. +dbGetQuery(mydb,"CREATE TABLE toy1 ( +id_student INTEGER, +gender TEXT, +region TEXT +);") +dbListTables(mydb) +View(EDB1) +View(EDB1) +View(EDB1) +View(EDB1) +dbGetQuery(mydb, "INSERT INTO toy1 VALUES (11391,'M','Anglican Region');") +dbGetQuery(mydb, "INSERT INTO toy1 VALUES (28400,'F','Scotland');") +dbGetQuery(mydb, "INSERT INTO toy1 VALUES (30268,'F','North Western Region');") +dbGetQuery(mydb, "SELECT * FROM toy1") +bGetQuery(mydb, "DROP TABLE toy1") +dbGetQuery(mydb, "DROP TABLE EDB1") +#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. +#Recreated EDB1 and named it toy1 +dbGetQuery(mydb,"CREATE TABLE toy1 ( +id_student INTEGER, +gender TEXT, +region TEXT +);") +dbGetQuery(mydb, "DROP TABLE IF EXISTS toy1;") +#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. +#Recreated EDB1 and named it toy1 +dbGetQuery(mydb,"CREATE TABLE toy1 ( +id_student INTEGER, +gender TEXT, +region TEXT +);") +#Added data to table +dbGetQuery(mydb, "INSERT INTO toy1 (id_student, gender, region) SELECT id_student, gender, region FROM EDB1;") +dbListTables(mydb) +dbWriteTable(mydb,"EDB1", EDB1) +dbReadTable(mydb, 'EDB1') +dbListTables(mydb) +#Added data to table +dbGetQuery(mydb, "INSERT INTO toy1 (id_student, gender, region) SELECT id_student, gender, region FROM EDB1;") +#Displayed the table +dbGetQuery(mydb, "SELECT * FROM toy1") +#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;") +View(EDB2) +View(EDB2) +#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. +#Creating Toy table 2 from EDB2 table +dbGetQuery(mydb,"CREATE TABLE toy2 ( +id_student INTEGER, +highest_education TEXT +num_of_prev_attempts INTEGER DEFAULT 0 +);") +#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. +#Creating Toy table 2 from EDB2 table +dbGetQuery(mydb,"CREATE TABLE toy2 ( +id_student INTEGER, +highest_education TEXT, +num_of_prev_attempts INTEGER DEFAULT 0 +);") +dbListTables(mydb) +#Add Values +dbGetQuery(mydb,"INSERT INTO toy2 (id_student,highest_education,num_of_prev_attempts) SELECT id_student,highest_education,num_of_prev_attempts FROM toy2;") +#display table +dbGetQuery(mydb,"SELECT *, FROM toy2") +#display table +dbGetQuery(mydb,"SELECT * FROM toy2") +#display table +dbGetQuery(mydb,"SELECT * FROM toy2") +#Add Values +dbGetQuery(mydb,"INSERT INTO toy2 (id_student,highest_education,num_of_prev_attempts) SELECT id_student,highest_education,num_of_prev_attempts FROM EDB2;") +#display table +dbGetQuery(mydb,"SELECT * FROM toy2") +dbGetQuery(mydb, "DROP TABLE toy2" ) +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;") +dbGetQuery(mydb, "INSERT INTO toy3 (id,score,student,Gender) VALUES (1,80,'A',M);") +#EXERCISE 7 +#Create a new table with four variables and a primary key that is a sequential id value. +dbGetQuery(mydb,"CREATE TABLE toy3 ( +id INTEGER AUTO_INCREMENT PRIMARY KEY, +score INTEGER, +student TEXT +Gender TEXT +);") +#EXERCISE 7 +#Create a new table with four variables and a primary key that is a sequential id value. +dbGetQuery(mydb,"CREATE TABLE toy3 ( +id INTEGER AUTO_INCREMENT PRIMARY KEY, +score INTEGER, +student TEXT, +Gender TEXT +);") +dbGetQuery(mydb, "INSERT INTO toy3 (id,score,student,Gender) VALUES (1,80,'A',M);") +dbGetQuery(mydb, "INSERT INTO toy3 (id,score,student,Gender) VALUES (1,80,'A','M');") +dbGetQuery(mydb, "INSERT INTO toy3 (id,score,student,Gender) VALUES (4,50,'D','F');") +dbGetQuery(mydb, "SELECT * FROM toy3") +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 toy3;") +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 toy3 ( +id INTEGER AUTO_INCREMENT PRIMARY KEY, +score INTEGER, +student TEXT, +Gender TEXT +);") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (80,'A','M');") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (50,'D','F');") +#Display +dbGetQuery(mydb, "SELECT * FROM toy3") +#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;") +dbGetQuery(mydb, "ALTER TABLE toy1 ADD test INTEGER DEFAULT 3 ") +dbGetQuery(mydb, "SELECT * FROM toy1 LIMIT 10;") +#Delete a column +dbGetQuery(mydb, "ALTER TABLE toy1 DROP COLUMN test;") +dbGetQuery(mydb, "SELECT id_student, date_submitted FROM studentAssessment WHERE date_submitted > 550 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%';") +#Begin with 'Region' +dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE region LIKE 'Region%';") +#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 id_student,gender, region FROM toy1 WHERE gender is F AND region IN ('East Anglian Region');") +#EXERCISE 8 +#Query one of your original toy data tables, for two different conditions. +dbGetQuery(mydb, "SELECT id_student,gender, region FROM toy1 WHERE gender= F AND region IN ('East Anglian Region');") +#EXERCISE 8 +#Query one of your original toy data tables, for two different conditions. +dbGetQuery(mydb, "SELECT id_student,gender, region FROM toy1 WHERE gender= 'F' AND region IN ('East Anglian Region');") +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 toy1,(id_student,gender,region) VALUES (11391,'M','East Anglian Region');") +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb, "INSERT INTO toy1 (id_student,gender,region) VALUES (11391,'M','East Anglian Region');") +deGetQuery (mydb, "SELECT * FROM toy1") +dbGetQuery (mydb, "SELECT * FROM toy1") +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb, "INSERT INTO toy1 (id_student,gender,region) VALUES (11391,'M','USA');") +dbGetQuery (mydb, "SELECT * FROM toy1") +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb, "INSERT INTO toy1 (id_student,gender,region) VALUES (11391 ,'M','USA');") +dbGetQuery (mydb, "SELECT * FROM toy1") +#EXERCISE 9 +#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates. +dbGetQuery(mydb, "INSERT INTO toy1 (id_student,gender,region) VALUES ('11391' ,'M','USA');") +dbGetQuery (mydb, "SELECT * FROM toy1") +dbGetQuery (mydb, "SELECT * FROM toy1 SORT BY id_student DESC") +dbGetQuery (mydb, "SELECT * FROM toy1 SORT BY id_student DESC LIMIT 10") +dbGetQuery (mydb, "SELECT id_student,gender,region FROM toy1 SORT BY id_student DESC LIMIT 10") +dbGetQuery (mydb, "SELECT * FROM toy1 SORT BY id_student ") +dbGetQuery (mydb, "SELECT * FROM toy1 ORDER BY id_student DESC ") +dbGetQuery (mydb, "SELECT * FROM toy1 ORDER BY id_student") +dbGetQuery(mydb, "SELECT DISTINCT id_student,region FROM toy1") +#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, "SELECT FROM EDB1 JOIN EDB2 ON EDB1.id_studen=EDB2.id_student") +#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, "SELECT * FROM EDB1 JOIN EDB2 ON EDB1.id_studen=EDB2.id_student") +#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, "SELECT * FROM toy1 JOIN toy2 ON toy1.id_student=toy2.id_student") +#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, "SELECT * FROM EDB1 JOIN EDB2 ON EDB1.id_student=EDB2.id_student") +#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, "SELECT * FROM EDB1 INNER JOIN EDB2 ON EDB1.id_student=EDB2.id_student") +#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, "SELECT * FROM EDB1 LEFT JOIN EDB2 USING id_student") +#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, "SELECT * FROM EDB1 LEFT JOIN EDB2 USING (id_student)") diff --git a/.Rproj.user/4F6DDDC0/pcs/files-pane.pper b/.Rproj.user/4F6DDDC0/pcs/files-pane.pper new file mode 100644 index 0000000..07a3f44 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/pcs/files-pane.pper @@ -0,0 +1,9 @@ +{ + "sortOrder": [ + { + "columnIndex": 2, + "ascending": true + } + ], + "path": "~/GitHub/sql-db-setup" +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/pcs/packages-pane.pper b/.Rproj.user/4F6DDDC0/pcs/packages-pane.pper new file mode 100644 index 0000000..4218d6e --- /dev/null +++ b/.Rproj.user/4F6DDDC0/pcs/packages-pane.pper @@ -0,0 +1,7 @@ +{ + "installOptions": { + "installFromRepository": true, + "libraryPath": "C:/Users/svasqu00/Documents/R/win-library/4.0", + "installDependencies": true + } +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/pcs/source-pane.pper b/.Rproj.user/4F6DDDC0/pcs/source-pane.pper new file mode 100644 index 0000000..902cc6f --- /dev/null +++ b/.Rproj.user/4F6DDDC0/pcs/source-pane.pper @@ -0,0 +1,3 @@ +{ + "activeTab": 0 +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/pcs/windowlayoutstate.pper b/.Rproj.user/4F6DDDC0/pcs/windowlayoutstate.pper new file mode 100644 index 0000000..3fb33d6 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/pcs/windowlayoutstate.pper @@ -0,0 +1,14 @@ +{ + "left": { + "splitterpos": 198, + "topwindowstate": "MAXIMIZE", + "panelheight": 766, + "windowheight": 804 + }, + "right": { + "splitterpos": 457, + "topwindowstate": "NORMAL", + "panelheight": 767, + "windowheight": 804 + } +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/pcs/workbench-pane.pper b/.Rproj.user/4F6DDDC0/pcs/workbench-pane.pper new file mode 100644 index 0000000..75e70e9 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/pcs/workbench-pane.pper @@ -0,0 +1,5 @@ +{ + "TabSet1": 0, + "TabSet2": 0, + "TabZoom": {} +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/rmd-outputs b/.Rproj.user/4F6DDDC0/rmd-outputs new file mode 100644 index 0000000..3f2ff2d --- /dev/null +++ b/.Rproj.user/4F6DDDC0/rmd-outputs @@ -0,0 +1,5 @@ + + + + + diff --git a/.Rproj.user/4F6DDDC0/saved_source_markers b/.Rproj.user/4F6DDDC0/saved_source_markers new file mode 100644 index 0000000..2b1bef1 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/saved_source_markers @@ -0,0 +1 @@ +{"active_set":"","sets":[]} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA b/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA new file mode 100644 index 0000000..b5841b5 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA @@ -0,0 +1,24 @@ +{ + "id": "30BB14DA", + "path": "~/GitHub/sql-db-setup/sql-project.Rmd", + "project_path": "sql-project.Rmd", + "type": "r_markdown", + "hash": "2978340199", + "contents": "", + "dirty": false, + "created": 1612227021359.0, + "source_on_save": false, + "relative_order": 1, + "properties": { + "cursorPosition": "436,0", + "scrollLine": "421" + }, + "folds": "", + "lastKnownWriteTime": 1612488447, + "encoding": "UTF-8", + "collab_server": "", + "source_window": "", + "last_content_update": 1612488447250, + "read_only": false, + "read_only_alternatives": [] +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA-contents b/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA-contents new file mode 100644 index 0000000..330ec7b --- /dev/null +++ b/.Rproj.user/4F6DDDC0/sources/per/t/30BB14DA-contents @@ -0,0 +1,455 @@ +--- +title: "sql-workshop" +author: "Sara Vasquez" +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 <-'Hudk4051' +db_name <- 'oudb' +db_host <- 'database-1.crtjgff03ugl.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. +#loading the libraries needed to create toy data sets +library(tidyverse) +EDB1 <- studentInfo %>% select (id_student,gender,region) +EDB2 <- studentInfo %>% select(id_student,highest_education,num_of_prev_attempts) + +dbWriteTable(mydb,"EDB1", EDB1) +dbWriteTable(mydb,"EDB2", EDB2) + +dbReadTable(mydb, 'EDB1') +dbReadTable(mydb, 'EDB2') + +``` + +## 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 id_student AS studentID,gender,region FROM EDB1 ORDER BY id_student DESC LIMIT 20") +#Read the other table according to a condition of one of the variables. +dbGetQuery(mydb,"SELECT * FROM EDB2 WHERE num_of_prev_attempts>1") +``` + +## 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. +dbGetQuery(mydb,"INSERT INTO EDB1 (id_student,gender,region) VALUES ('01000','','East');") + +dbGetQuery(mydb, "SELECT * FROM EDB1 ORDER BY id_student LIMIT 10") + +dbGetQuery(mydb, "DELETE FROM EDB1 WHERE id_student=1000;") +#Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. +dbGetQuery(mydb, "UPDATE EDB2 SET num_of_prev_attempts='5' WHERE id_student=11391;") + +dbGetQuery(mydb, "SELECT * FROM EDB2 ORDER BY id_student LIMIT 5" ) + +dbGetQuery(mydb, "DELETE FROM EDB2 WHERE id_student=11391;") + + +``` + +## 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. +#Recreated EDB1 and named it toy1 +dbGetQuery(mydb,"CREATE TABLE toy1 ( + id_student INTEGER, + gender TEXT, + region TEXT + );") +dbListTables(mydb) +#Added data to table +dbGetQuery(mydb, "INSERT INTO toy1 (id_student, gender, region) SELECT id_student, gender, region FROM EDB1;") +#Displayed the table +dbGetQuery(mydb, "SELECT * FROM toy1 ORDER BY id_student DE") +#Delete original toy table named EDB1 in # because I have to use it later +#dbGetQuery(mydb, "DROP TABLE EDB1") + + +``` + +# 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. +#Creating Toy table 2 from EDB2 table +dbGetQuery(mydb,"CREATE TABLE toy2 ( + id_student INTEGER, + highest_education TEXT, + num_of_prev_attempts INTEGER DEFAULT 0 + );") +dbListTables(mydb) + +#Add Values +dbGetQuery(mydb,"INSERT INTO toy2 (id_student,highest_education,num_of_prev_attempts) SELECT id_student,highest_education,num_of_prev_attempts FROM EDB2;") + +#display table +dbGetQuery(mydb,"SELECT * FROM toy2") +dbGetQuery(mydb, "DROP TABLE toy2" ) + +``` + + +# 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 toy1 ADD test INTEGER DEFAULT 3 ") + +dbGetQuery(mydb, "SELECT * FROM toy1 LIMIT 10;") + +#Delete a column +dbGetQuery(mydb, "ALTER TABLE toy1 DROP COLUMN test;") +``` + + +# 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 toy3 ( + id INTEGER AUTO_INCREMENT PRIMARY KEY, + score INTEGER, + student TEXT, + Gender TEXT + );") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (80,'A','M');") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (50,'D','F');") +#Display +dbGetQuery(mydb, "SELECT * FROM toy3") +``` + +## 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 id_student,gender, region FROM toy1 WHERE gender= 'F' AND region IN ('East Anglian Region');") + + + + +``` + +## 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 toy1 (id_student,gender,region) VALUES ('11391' ,'M','USA');") +dbGetQuery (mydb, "SELECT * FROM toy1 ORDER BY id_student") + +dbGetQuery(mydb, "SELECT DISTINCT id_student,region FROM toy1") + +``` + +## 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. +dbGetQuery(mydb, "SELECT * FROM EDB1 LEFT JOIN EDB2 USING (id_student)") + +``` +```{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/.Rproj.user/4F6DDDC0/sources/prop/882B6BAC b/.Rproj.user/4F6DDDC0/sources/prop/882B6BAC new file mode 100644 index 0000000..33c39b7 --- /dev/null +++ b/.Rproj.user/4F6DDDC0/sources/prop/882B6BAC @@ -0,0 +1,4 @@ +{ + "cursorPosition": "436,0", + "scrollLine": "421" +} \ No newline at end of file diff --git a/.Rproj.user/4F6DDDC0/sources/prop/INDEX b/.Rproj.user/4F6DDDC0/sources/prop/INDEX new file mode 100644 index 0000000..71d725b --- /dev/null +++ b/.Rproj.user/4F6DDDC0/sources/prop/INDEX @@ -0,0 +1 @@ +~%2FGitHub%2Fsql-db-setup%2Fsql-project.Rmd="882B6BAC" diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC02506D4E4/chunks.json b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC02506D4E4/chunks.json new file mode 100644 index 0000000..3b730b6 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC02506D4E4/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":24,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-9"},"document_id":"30BB14DA","chunk_id":"c08hrqbwy0qs8","chunk_label":"unnamed-chunk-1"},{"row":35,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-10"},"document_id":"30BB14DA","chunk_id":"cxsnmmcubhg94","chunk_label":"unnamed-chunk-2"},{"row":67,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-26"},"document_id":"30BB14DA","chunk_id":"czufp48smc2g4","chunk_label":"unnamed-chunk-3"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-27"},"document_id":"30BB14DA","chunk_id":"cgi7uhmvtt4s9","chunk_label":"unnamed-chunk-4"}],"doc_write_time":1612228851} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC0EB6F275C/chunks.json b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC0EB6F275C/chunks.json new file mode 100644 index 0000000..9c275de --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/4F6DDDC0EB6F275C/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":24,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-1"},"document_id":"30BB14DA","chunk_id":"c08hrqbwy0qs8","chunk_label":"unnamed-chunk-1"},{"row":35,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-2"},"document_id":"30BB14DA","chunk_id":"cxsnmmcubhg94","chunk_label":"unnamed-chunk-2"},{"row":67,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"30BB14DA","chunk_id":"czufp48smc2g4","chunk_label":"unnamed-chunk-3"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-23"},"document_id":"30BB14DA","chunk_id":"cjpnpu7yiodfl","chunk_label":"unnamed-chunk-4"},{"row":147,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-35"},"document_id":"30BB14DA","chunk_id":"cvq8xv7vdpls1","chunk_label":"unnamed-chunk-5"},{"row":196,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-62"},"document_id":"30BB14DA","chunk_id":"cj3wa61qmz7ie","chunk_label":"unnamed-chunk-6"},{"row":263,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-79"},"document_id":"30BB14DA","chunk_id":"ckhhnl2fbcm0n","chunk_label":"unnamed-chunk-8"},{"row":285,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-106"},"document_id":"30BB14DA","chunk_id":"cj4139k1eaof8","chunk_label":"unnamed-chunk-9"},{"row":315,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-100"},"document_id":"30BB14DA","chunk_id":"cfxn5beks64q9","chunk_label":"unnamed-chunk-10"},{"row":349,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-116"},"document_id":"30BB14DA","chunk_id":"cbe3n0ptmmur0","chunk_label":"unnamed-chunk-11"},{"row":364,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-135"},"document_id":"30BB14DA","chunk_id":"cuyphrx7upk1j","chunk_label":"unnamed-chunk-12"},{"row":436,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-142"},"document_id":"30BB14DA","chunk_id":"c1gmthglt4hgm","chunk_label":"unnamed-chunk-14"}],"doc_write_time":1612487647} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c08hrqbwy0qs8/000008.csv b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c08hrqbwy0qs8/000008.csv new file mode 100644 index 0000000..2a73dae --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c08hrqbwy0qs8/000008.csv @@ -0,0 +1,56 @@ +"0","#install.packages(""DBI"", ""RMySQL"")" +"2","Warning messages: +" +"2","1: package ‘RMySQL’ was built under R version 4.0.3 +" +"2","2: package ‘DBI’ was built under R version 4.0.3 +" +"0","library(DBI)" +"0","library(RMySQL)" +"0","" +"0","db_user <- 'admin'" +"0","db_password <-'Hudk4051'" +"0","db_name <- 'oudb'" +"0","db_host <- 'database-1.crtjgff03ugl.us-east-2.rds.amazonaws.com'" +"0","db_port <- 3306" +"0","" +"0","mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port)" +"0","" +"0","summary(mydb)" +"1","<" +"1","" +"1","" +"1","" +"1","MySQLConnection:" +"1","" +"1","0,0" +"1","" +"1","> +" +"1"," User: " +"1"," " +"1","admin" +"1"," " +"1"," +" +"1"," Host: " +"1"," " +"1","database-1.crtjgff03ugl.us-east-2.rds.amazonaws.com" +"1"," " +"1"," +" +"1"," Dbname:" +"1"," " +"1","oudb" +"1"," " +"1"," +" +"1"," Connection type:" +"1"," " +"1","database-1.crtjgff03ugl.us-east-2.rds.amazonaws.com via TCP/IP" +"1"," " +"1"," +" +"1"," +Results: +" diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.metadata new file mode 100644 index 0000000..feb0c1a --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":40801,"ncol":7} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.rdf new file mode 100644 index 0000000..a835f54 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/c1gmthglt4hgm/00000c.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.metadata new file mode 100644 index 0000000..dd7d7ac --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":1623,"ncol":3} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.rdf new file mode 100644 index 0000000..2b09c02 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cbe3n0ptmmur0/00000d.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.metadata new file mode 100644 index 0000000..87dd080 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":2,"ncol":4} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.rdf new file mode 100644 index 0000000..7fa8925 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cfxn5beks64q9/000019.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.metadata new file mode 100644 index 0000000..393da16 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":10,"ncol":2} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.rdf new file mode 100644 index 0000000..50c9c87 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cgi7uhmvtt4s9/000006.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/chunks.json b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/chunks.json new file mode 100644 index 0000000..9c275de --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":24,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-1"},"document_id":"30BB14DA","chunk_id":"c08hrqbwy0qs8","chunk_label":"unnamed-chunk-1"},{"row":35,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-2"},"document_id":"30BB14DA","chunk_id":"cxsnmmcubhg94","chunk_label":"unnamed-chunk-2"},{"row":67,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"30BB14DA","chunk_id":"czufp48smc2g4","chunk_label":"unnamed-chunk-3"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-23"},"document_id":"30BB14DA","chunk_id":"cjpnpu7yiodfl","chunk_label":"unnamed-chunk-4"},{"row":147,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-35"},"document_id":"30BB14DA","chunk_id":"cvq8xv7vdpls1","chunk_label":"unnamed-chunk-5"},{"row":196,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-62"},"document_id":"30BB14DA","chunk_id":"cj3wa61qmz7ie","chunk_label":"unnamed-chunk-6"},{"row":263,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-79"},"document_id":"30BB14DA","chunk_id":"ckhhnl2fbcm0n","chunk_label":"unnamed-chunk-8"},{"row":285,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-106"},"document_id":"30BB14DA","chunk_id":"cj4139k1eaof8","chunk_label":"unnamed-chunk-9"},{"row":315,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-100"},"document_id":"30BB14DA","chunk_id":"cfxn5beks64q9","chunk_label":"unnamed-chunk-10"},{"row":349,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-116"},"document_id":"30BB14DA","chunk_id":"cbe3n0ptmmur0","chunk_label":"unnamed-chunk-11"},{"row":364,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-135"},"document_id":"30BB14DA","chunk_id":"cuyphrx7upk1j","chunk_label":"unnamed-chunk-12"},{"row":436,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-142"},"document_id":"30BB14DA","chunk_id":"c1gmthglt4hgm","chunk_label":"unnamed-chunk-14"}],"doc_write_time":1612487647} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.metadata new file mode 100644 index 0000000..41b7564 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":32593,"ncol":3} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.rdf new file mode 100644 index 0000000..253c8ef Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj3wa61qmz7ie/00001c.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.metadata new file mode 100644 index 0000000..4582e1e --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":0,"ncol":0} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.rdf new file mode 100644 index 0000000..cfdd0e8 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cj4139k1eaof8/000007.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.metadata new file mode 100644 index 0000000..69cbf0c --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":20,"ncol":3} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.rdf new file mode 100644 index 0000000..9a8e9c6 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cjpnpu7yiodfl/00000a.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.metadata new file mode 100644 index 0000000..4582e1e --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":0,"ncol":0} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.rdf new file mode 100644 index 0000000..15d45f7 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/ckhhnl2fbcm0n/000014.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.metadata new file mode 100644 index 0000000..0a5eef2 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":28786,"ncol":2} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.rdf new file mode 100644 index 0000000..42310de Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cuyphrx7upk1j/00001a.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.metadata new file mode 100644 index 0000000..4582e1e --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":0,"ncol":0} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.rdf new file mode 100644 index 0000000..aebe425 Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cvq8xv7vdpls1/00001b.rdf differ diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cxsnmmcubhg94/000002.csv b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cxsnmmcubhg94/000002.csv new file mode 100644 index 0000000..d81b81c --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/cxsnmmcubhg94/000002.csv @@ -0,0 +1,7 @@ +"0","#Student demographic data" +"0","studentInfo <- read.csv(""studentInfo.csv"", header = TRUE)" +"0","#Student assessment data" +"0","studentAssessment <- read.csv(""studentAssessment.csv"", header = TRUE)" +"0","#Course data" +"0","courses <- read.csv(""courses.csv"", header = TRUE)" +"0","studentRegistration <- read.csv(""studentRegistration.csv"", header = TRUE)" diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.metadata b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.metadata new file mode 100644 index 0000000..41b7564 --- /dev/null +++ b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":32593,"ncol":3} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.rdf b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.rdf new file mode 100644 index 0000000..9a9a3ea Binary files /dev/null and b/.Rproj.user/shared/notebooks/C3363FAF-sql-project/1/s/czufp48smc2g4/00000c.rdf differ diff --git a/.Rproj.user/shared/notebooks/patch-chunk-names b/.Rproj.user/shared/notebooks/patch-chunk-names new file mode 100644 index 0000000..e69de29 diff --git a/.Rproj.user/shared/notebooks/paths b/.Rproj.user/shared/notebooks/paths new file mode 100644 index 0000000..336d68e --- /dev/null +++ b/.Rproj.user/shared/notebooks/paths @@ -0,0 +1 @@ +C:/Users/svasqu00/Documents/GitHub/sql-db-setup/sql-project.Rmd="C3363FAF" diff --git a/sql-db-setup.Rproj b/sql-db-setup.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/sql-db-setup.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/sql-project.Rmd b/sql-project.Rmd index 99a7974..330ec7b 100644 --- a/sql-project.Rmd +++ b/sql-project.Rmd @@ -1,6 +1,6 @@ --- title: "sql-workshop" -author: "Charles Lang" +author: "Sara Vasquez" output: html_document --- @@ -14,9 +14,9 @@ library(DBI) library(RMySQL) db_user <- 'admin' -db_password <- 'testsql!' +db_password <-'Hudk4051' db_name <- 'oudb' -db_host <- 'PASTE YOUR ENDPOINT HERE' +db_host <- 'database-1.crtjgff03ugl.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) @@ -54,6 +54,16 @@ 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. +#loading the libraries needed to create toy data sets +library(tidyverse) +EDB1 <- studentInfo %>% select (id_student,gender,region) +EDB2 <- studentInfo %>% select(id_student,highest_education,num_of_prev_attempts) + +dbWriteTable(mydb,"EDB1", EDB1) +dbWriteTable(mydb,"EDB2", EDB2) + +dbReadTable(mydb, 'EDB1') +dbReadTable(mydb, 'EDB2') ``` @@ -82,9 +92,9 @@ dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id #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 id_student AS studentID,gender,region FROM EDB1 ORDER BY id_student DESC LIMIT 20") #Read the other table according to a condition of one of the variables. - +dbGetQuery(mydb,"SELECT * FROM EDB2 WHERE num_of_prev_attempts>1") ``` ## Getting into SQL - UPDATING @@ -121,7 +131,18 @@ 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. +#Insert a new row in one of your toy data tables leaving one variable empty. +dbGetQuery(mydb,"INSERT INTO EDB1 (id_student,gender,region) VALUES ('01000','','East');") + +dbGetQuery(mydb, "SELECT * FROM EDB1 ORDER BY id_student LIMIT 10") + +dbGetQuery(mydb, "DELETE FROM EDB1 WHERE id_student=1000;") +#Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted. +dbGetQuery(mydb, "UPDATE EDB2 SET num_of_prev_attempts='5' WHERE id_student=11391;") + +dbGetQuery(mydb, "SELECT * FROM EDB2 ORDER BY id_student LIMIT 5" ) + +dbGetQuery(mydb, "DELETE FROM EDB2 WHERE id_student=11391;") ``` @@ -157,7 +178,21 @@ dbGetQuery(mydb, "SELECT * FROM test;") #This should produce an error since your 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. +#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. +#Recreated EDB1 and named it toy1 +dbGetQuery(mydb,"CREATE TABLE toy1 ( + id_student INTEGER, + gender TEXT, + region TEXT + );") +dbListTables(mydb) +#Added data to table +dbGetQuery(mydb, "INSERT INTO toy1 (id_student, gender, region) SELECT id_student, gender, region FROM EDB1;") +#Displayed the table +dbGetQuery(mydb, "SELECT * FROM toy1 ORDER BY id_student DE") +#Delete original toy table named EDB1 in # because I have to use it later +#dbGetQuery(mydb, "DROP TABLE EDB1") + ``` @@ -166,10 +201,10 @@ dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") #No error since it is only if it #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 = NULL; #Instead use -SELECT * FROM test WHERE score is NULL; +#SELECT * FROM test WHERE score is NULL; ``` @@ -210,7 +245,21 @@ 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. +#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. +#Creating Toy table 2 from EDB2 table +dbGetQuery(mydb,"CREATE TABLE toy2 ( + id_student INTEGER, + highest_education TEXT, + num_of_prev_attempts INTEGER DEFAULT 0 + );") +dbListTables(mydb) + +#Add Values +dbGetQuery(mydb,"INSERT INTO toy2 (id_student,highest_education,num_of_prev_attempts) SELECT id_student,highest_education,num_of_prev_attempts FROM EDB2;") + +#display table +dbGetQuery(mydb,"SELECT * FROM toy2") +dbGetQuery(mydb, "DROP TABLE toy2" ) ``` @@ -227,6 +276,13 @@ 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 toy1 ADD test INTEGER DEFAULT 3 ") + +dbGetQuery(mydb, "SELECT * FROM toy1 LIMIT 10;") + +#Delete a column +dbGetQuery(mydb, "ALTER TABLE toy1 DROP COLUMN test;") ``` @@ -247,7 +303,16 @@ 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 toy3 ( + id INTEGER AUTO_INCREMENT PRIMARY KEY, + score INTEGER, + student TEXT, + Gender TEXT + );") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (80,'A','M');") +dbGetQuery(mydb, "INSERT INTO toy3 (score,student,Gender) VALUES (50,'D','F');") +#Display +dbGetQuery(mydb, "SELECT * FROM toy3") ``` ## Filtering (WHERE) @@ -277,6 +342,10 @@ dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE regio #EXERCISE 8 #Query one of your original toy data tables, for two different conditions. +dbGetQuery(mydb, "SELECT id_student,gender, region FROM toy1 WHERE gender= 'F' AND region IN ('East Anglian Region');") + + + ``` @@ -288,6 +357,10 @@ 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 toy1 (id_student,gender,region) VALUES ('11391' ,'M','USA');") +dbGetQuery (mydb, "SELECT * FROM toy1 ORDER BY id_student") + +dbGetQuery(mydb, "SELECT DISTINCT id_student,region FROM toy1") ``` @@ -358,7 +431,8 @@ dbGetQuery(mydb, "SELECT * FROM left_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. +# 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, "SELECT * FROM EDB1 LEFT JOIN EDB2 USING (id_student)") ``` ```{r}