From b448272a3a20d304429903d98d67a28572a51362 Mon Sep 17 00:00:00 2001 From: Iqra Javed Date: Fri, 31 Aug 2018 17:10:32 -0500 Subject: [PATCH 1/3] initial commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a6d7c7c..d94191c 100644 --- a/README.md +++ b/README.md @@ -135,3 +135,4 @@ Write queries that: Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. +. \ No newline at end of file From 76c13fef2d5b2579e2442f491a86a08cc429f33b Mon Sep 17 00:00:00 2001 From: Iqra Javed Date: Sat, 1 Sep 2018 14:33:45 -0500 Subject: [PATCH 2/3] complete Day 1 exercises --- README.md | 15 +++++++++++---- musiq.sql | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 musiq.sql diff --git a/README.md b/README.md index d94191c..204b373 100644 --- a/README.md +++ b/README.md @@ -76,30 +76,37 @@ column names in the following tables. We'll use `setup.sql` later. * Use foreign keys for this, as well. * Run the queries in the file `setup.sql`. This will populate the tables. + -> run command .read setup.sql to read queries from file * Fix any errors at this point by making sure your tables are correct. * `DROP TABLE` can be used to delete a table so you can recreate it with `CREATE TABLE`. +Good resource for wildcard operators: https://www.w3schools.com/sql/sql_wildcards.asp * Write SQL `SELECT` queries that: * Show all albums. - * Show all albums made between 1975 and 1990. + + * Show all albums made between 1975 and 1990. + * Show all albums whose names start with `Super D`. - * Show all albums that have no release year. + + * Show all albums that have no release year. * Write SQL `SELECT` queries that: * Show all track titles from `Super Funky Album`. + * Same query as above, but rename the column from `title` to `Track_Title` in the output. * Select all album titles by `Han Solo`. - + * Select the average year all albums were released. * Select the average year all albums by `Leia and the Ewoks` were released. - + * Select the number of artists. * Select the number of tracks on `Super Dubstep Album`. + ### Exercises, Day 2 diff --git a/musiq.sql b/musiq.sql new file mode 100644 index 0000000..b4907d6 --- /dev/null +++ b/musiq.sql @@ -0,0 +1,49 @@ +--******************************** COMMANDS TO CREATE ALL TABLES FOR MUSIC DB ************************************ +CREATE TABLE track (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128), album_id INTEGER REFERENCES album(id)); +CREATE TABLE album(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INT); +CREATE TABLE artist (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL); +CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id)); +PRAGMA foreign_keys=ON; +.read setup.sql +.headers on +.mode column +--***************************************************************************************************************************** + +-- Write SQL `SELECT` queries that: + -- Show all albums. + SELECT * FROM album; + + -- Show all albums made between 1975 and 1990. + SELECT * FROM album WHERE release_year BETWEEN 1975 AND 1990; --start & end inclusive + SELECT * FROM album WHERE release_year > 1975 AND release_year < 1990 --start & end exclusive + + -- Show all albums whose names start with `Super D`. + SELECT * FROM album WHERE title LIKE 'SUPER D%'; + + -- Show all albums that have no release year. + SELECT * FROM album WHERE release_year IS NULL; + +-- Write SQL `SELECT` queries that: + -- Show all track titles from `Super Funky Album`. + SELECT track.title FROM track, album WHERE track.album_id=album.id AND album.title='Super Funky Album'; + --resource: http://www1.udel.edu/evelyn/SQL-Class2/SQLclass2_2tblEg.html + + -- Same query as above, but rename the column from `title` to `Track_Title` in the output. + SELECT track.title AS 'Track_Title' FROM track, album WHERE track.album_id=album.id AND album.title='Super Funky Album'; + --resource: https://www.w3schools.com/sql/sql_alias.asp + + -- Select all album titles by `Han Solo`. + SELECT album.title FROM album, artist, artist_album WHERE album.id=artist_album.album_id AND artist.id=artist_album.artist_id AND artist.name='Han Solo'; + + -- Select the average year all albums were released. + SELECT AVG(release_year) FROM album; + + -- Select the average year all albums by `Leia and the Ewoks` were released. + SELECT AVG(release_year) FROM album al, artist ar, artist_album aa WHERE aa.artist_id=ar.id AND aa.album_id=al.id AND ar.name='Leia and the Ewoks'; + + -- Select the number of artists. + SELECT COUNT(id) FROM artist; + --resource: https://www.lifewire.com/counting-values-with-sql-count-function-1019771 + + -- Select the number of tracks on `Super Dubstep Album`. + SELECT COUNT(track.id) FROM track , album WHERE track.album_id=album.id AND album.title='Super Dubstep Album'; \ No newline at end of file From e094899cfe143420f0c42474aeb4df46cf5b5d34 Mon Sep 17 00:00:00 2001 From: Iqra Javed Date: Sat, 1 Sep 2018 22:30:11 -0500 Subject: [PATCH 3/3] complete Day 2 exercises --- musiq.sql | 2 +- notes.sql | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ setupNotesDB.sql | 31 +++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 notes.sql create mode 100644 setupNotesDB.sql diff --git a/musiq.sql b/musiq.sql index b4907d6..59354b1 100644 --- a/musiq.sql +++ b/musiq.sql @@ -1,6 +1,6 @@ --******************************** COMMANDS TO CREATE ALL TABLES FOR MUSIC DB ************************************ CREATE TABLE track (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128), album_id INTEGER REFERENCES album(id)); -CREATE TABLE album(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INT); +CREATE TABLE album (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INT); CREATE TABLE artist (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL); CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id)); PRAGMA foreign_keys=ON; diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..3560eeb --- /dev/null +++ b/notes.sql @@ -0,0 +1,54 @@ +--Create a database for taking notes. + +-- ******************************************** COMMANDS TO CREATE ALL TABLES FOR NOTES DB ******************************************** +.headers on +.mode column +PRAGMA foreign_keys=ON; +CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL); +CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), content TEXT, author_id INTEGER REFERENCES author(id) ON DELETE CASCADE, created_at DATE DEFAULT datetime('now','localtime'), last_modified DATE DEFAULT NULL); +-- command to test last_modified: UPDATE note SET title = 'Iqra''s Note', last_modified = datetime('now','localtime') WHERE id = 1; +CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), content TEXT, author_id INTEGER REFERENCES author(id) ON DELETE CASCADE ON UPDATE CASCADE, created_at DATE DEFAULT (datetime('now','localtime')), last_modified DATE DEFAULT NULL); +--************************************************************************************************************************************* + + +-- What are the columns that a note table needs? + id, title, content, author_id, created_at, last_modfied + +-- If you have a timestamp field, how do you auto-populate it with the date? + set default + +-- A note should have a foreign key pointing to an author in an author table. + +-- What columns are needed for the author table? + id, name + +-- Write queries that: + +-- Insert authors to the author table. + see setupNotesDB.sql + +-- Insert notes to the note table. + see setupNotesDB.sql +-- Select all notes by an author's name. + SELECT note.title FROM note, author WHERE note.author_id=author.id AND author.name = 'Author1'; + +-- Select author for a particular note by note ID. + SELECT author.name FROM author, note WHERE author.id=note.author_id AND note.id=10; + +-- Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) + SELECT COUNT(note.author_id), author.name FROM note, author WHERE note.author_id=author.id GROUP BY author.name; + +-- Delete authors from the author table. + -- Note that SQLite doesn't enforce foreign key constrains by default. You have + -- to enable them by running `PRAGMA foreign_keys = ON;` before your queries. + DELETE FROM author WHERE author.id=3; + + -- What happens when you try to delete an author with an existing note? + Error: FOREIGN KEY constraint failed + -- How can you prevent this? + By adding ON DELETE CASCADE in the foreign key definition. + --resource: https://stackoverflow.com/questions/15443913/sqlite3-foreign-key-constraint-failed + + +-- Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) +-- and query the database as noted above. \ No newline at end of file diff --git a/setupNotesDB.sql b/setupNotesDB.sql new file mode 100644 index 0000000..b36a3e5 --- /dev/null +++ b/setupNotesDB.sql @@ -0,0 +1,31 @@ +INSERT INTO author (name) VALUES ('Author1'); +INSERT INTO author (name) VALUES ('Author2'); +INSERT INTO author (name) VALUES ('Author3'); +INSERT INTO author (name) VALUES ('Author4'); +INSERT INTO author (name) VALUES ('Author5'); + + +INSERT INTO note (title, content, author_id) VALUES ('Note1', 'First note''s content', 4); +INSERT INTO note (title, content, author_id) VALUES ('Note2', 'Second note''s content', 1); +INSERT INTO note (title, content, author_id) VALUES ('Note3', 'Third note''s content', 4); +INSERT INTO note (title, content, author_id) VALUES ('Note4', 'Fourth note''s content', 3); +INSERT INTO note (title, content, author_id) VALUES ('Note5', 'Fifth note''s content', 5); +INSERT INTO note (title, content, author_id) VALUES ('Note6', 'Sixth note''s content', 2); +INSERT INTO note (title, content, author_id) VALUES ('Note7', 'Seventh note''s content', 2); +INSERT INTO note (title, content, author_id) VALUES ('Note8', 'Eighth note''s content', 4); +INSERT INTO note (title, content, author_id) VALUES ('Note9', 'Ninth note''s content', 2); +INSERT INTO note (title, content, author_id) VALUES ('Note10', 'Tenth note''s content', 5); +INSERT INTO note (title, content, author_id) VALUES ('Note11', 'Eleventh note''s content', 1); +INSERT INTO note (title, content, author_id) VALUES ('Note12', 'Twelveth note''s content', 4); +INSERT INTO note (title, content, author_id) VALUES ('Note13', 'Thirteenth note''s content', 3); +INSERT INTO note (title, content, author_id) VALUES ('Note14', 'Fourteenth note''s content', 4); +INSERT INTO note (title, content, author_id) VALUES ('Note15', 'Fifteenth note''s content', 5); + +-- HOW MANY NOTES DOES EACH AUTHOR HAVE -- +/* +Author1: 2 +Author2: 3 +Author3: 2 +Author4: 5 +Author5: 3 +*/