From 4074a12935bce6129ea1d528ac4b814f87a2fbd3 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Tue, 4 Sep 2018 07:28:05 -0700 Subject: [PATCH 1/4] set up database --- README.md | 1 + musicdatabase.db | Bin 0 -> 24576 bytes 2 files changed, 1 insertion(+) create mode 100644 musicdatabase.db diff --git a/README.md b/README.md index a6d7c7c..f6d7a0b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. + >SELECT * FROM albums * 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. diff --git a/musicdatabase.db b/musicdatabase.db new file mode 100644 index 0000000000000000000000000000000000000000..e047ece32885d736ba3d60588e13fe11fd8a1409 GIT binary patch literal 24576 zcmeI(Pi)&%90%}czvpD}u-`T)b`l{y35Jq20wrlF2q6(#k5**qNHe6KBDXcdYMQKx zqtV-PgCi0b?uaV~#D!BOBo0V$M*_qFgt%~G8sfyWopjA+qaL_a`##C>pRfPC_lYkr z`ep0wrXP6X-NEp{4Md$>B8oy@7lIISO)eR^%o>v$;WBH=Y~w%H*T@Uc-8JM1n(-0g z@AK!)FU)74RVHrW?({r&82F>0>-KiW2dB3) zt8J&zaYU!_M$-|e6H%J(`Fmoc)p6c*+M?}TciK*C)!CY!TJrbG6%pOFn{rhKDth1t zJx{#TXs@m}+NEmk<#I(oQ+M?8AgAhi##&rdwxcJG4tsLS+#Pv`W3RvKJ+w>b`tVR#3LjFDSC%K55~KR@D5CE-{7~nv zk4Vlughx?N#!%6Ro_w1|UiV18Q?nOCFGq88ewXmy`1kxv{)yZ~g8&2|009U<00Izz z00bZa0SG|ge-^MbM(Sj1eA^p}weij<@NSDn_&Y#>F|w@2!Mf`YcijG7oMewJ%E*h; zY|S6-4yM6Z3#tsJce_6B?;piMUXc%(p5M6RjRx|Uj_4uTZ^HRMSvB{yG1cAMq}C_#(e--Zy_TzcxQJKQw#h zrdcx=jK7T^jbr0e;{$mC8U!E!0SG_<0uX=z1Rwwb2&4)WY(`d;`C9Kz_-}f#tQD-Z zY)LCTotD+L!a`bB(+W?eWmT>CBqOg(`*KoV9(QJi6|ba*FR|j~)Nq*UmlFpB&Y~`v2sp8qWVS{0GAS z;eYWz`S1J}xq}7)2tWV=5P$##AOHafKmY;|fWY|>&@xm}DATlXMKvZ@s>&!;aw=2h LKLAvw$WQHGX^7$d literal 0 HcmV?d00001 From a3d29ec6e670f5a99d05e33c104daf0ec81d1f09 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Tue, 4 Sep 2018 08:21:08 -0700 Subject: [PATCH 2/4] finished first set of exercises --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f6d7a0b..d228bab 100644 --- a/README.md +++ b/README.md @@ -82,25 +82,38 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. - >SELECT * FROM albums + > SELECT * FROM albums; + * Show all albums made between 1975 and 1990. + > SELECT * FROM album WHERE release_year>1975 AND release_year<1990; + * 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`. + > * 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 From a4c8e5067be40ea35fd8507943fbb9937cf78f86 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Tue, 4 Sep 2018 12:33:54 -0700 Subject: [PATCH 3/4] finished Day 1 Exercises --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d228bab..93b1b39 100644 --- a/README.md +++ b/README.md @@ -95,25 +95,26 @@ column names in the following tables. We'll use `setup.sql` later. * 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"; + * 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"; * Select all album titles by `Han Solo`. - > + > SELECT album.title FROM album, artist, artist_album WHERE artist_album.artist_id = artist.id AND artist_album.album_id = album.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, artist, artist_album WHERE artist_album.artist_id = artist.id AND artist_album.album_id = album.id AND artist.name = "Leia and the Ewoks"; * Select the number of artists. - > + > SELECT COUNT(*) FROM artist; * Select the number of tracks on `Super Dubstep Album`. - > + > SELECT COUNT(*) FROM track, album WHERE track.album_id = album.id AND album.title = "Super Dubstep Album"; ### Exercises, Day 2 From ec7e59ed1932cf8ae000bed432445954b7459bb0 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Tue, 4 Sep 2018 14:10:41 -0700 Subject: [PATCH 4/4] finished day 2 --- README.md | 8 ++++++++ notes.sql | 25 +++++++++++++++++++++++++ notesdatabase.db | Bin 0 -> 16384 bytes 3 files changed, 33 insertions(+) create mode 100644 notes.sql create mode 100644 notesdatabase.db diff --git a/README.md b/README.md index 93b1b39..0242145 100644 --- a/README.md +++ b/README.md @@ -135,17 +135,25 @@ Write queries that: * Insert notes to the note table. * Select all notes by an author's name. + > SELECT * FROM note, author WHERE note.author_id = author.id AND author.name = "Braden"; * Select author for a particular note by note ID. + > SELECT author.name FROM author, note WHERE note.author_id = author.id AND note.id = 1; * Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) + > SELECT COUNT(*), name FROM note, author WHERE note.author_id = author.id GROUP BY author_id; * Delete authors from the author table. + >> DELETE FROM author WHERE author.name = "Braden"; + > 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. * What happens when you try to delete an author with an existing note? + > `Error: FOREIGN KEY constraint failed` + * How can you prevent this? + > Add `ON DELETE CASCADE` to the end of the author_id reference in the note table Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..8c9254d --- /dev/null +++ b/notes.sql @@ -0,0 +1,25 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE note ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128), + content TEXT, + author_id INTEGER REFERENCES author(id) ON DELETE CASCADE, + created_at TIMESTAMP +); + +CREATE TABLE author ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(32) NOT NULL +); + +INSERT INTO author (name) VALUES ("Braden"); +INSERT INTO author (name) VALUES ("Beej"); +INSERT INTO author (name) VALUES ("Maverick"); + +INSERT INTO note (title, content, author_id, created_at) VALUES ("bradenTest1", "Here's a test for a note by Braden", 1, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("beejTest1", "Here's a test for a note by Beej", 2, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("maverickTest1", "Here's a test for a note by Maverick", 3, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("beejTest2", "Here's another by Beej", 2, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("maverickTest2", "Here's another by Maverick", 3, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("bradenTest2", "Here's another by Braden", 1, CURRENT_TIMESTAMP); \ No newline at end of file diff --git a/notesdatabase.db b/notesdatabase.db new file mode 100644 index 0000000000000000000000000000000000000000..8101c69578a5c9f6cc2a04f7a90b44c1642fcff7 GIT binary patch literal 16384 zcmeI&-*3`D902fZi?9K0_d*sPjF)H}WX|wo%t6%Hb%!I+ZY$m5UR=sJGxh_r(iweo zn)vQNVfGgo-~0#sANFXXKF)WqwCFYmQ6Ni9zD){kulMcuKBY~LyXtDC;ZXdnW$#-K z&Y}s#ap)1o2%$N)2H4tN!ffO0KG!Yi^F4>|-g+g