From 4885b8d1c27528f45eb4baa2cb75847c37376c8b Mon Sep 17 00:00:00 2001 From: NandoTheessen Date: Tue, 4 Sep 2018 16:57:32 +0200 Subject: [PATCH 1/4] Added day1.sql including all required queries as per README.md tested using DB Browser and cli --- day1.sql | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 day1.sql diff --git a/day1.sql b/day1.sql new file mode 100644 index 0000000..0db6524 --- /dev/null +++ b/day1.sql @@ -0,0 +1,68 @@ +-- Show all albums. +SELECT * FROM album; + +-- Show all albums made between 1975 and 1990. +SELECT * FROM album +WHERE (release_year <= 1990) +AND (release_year >= 1975); + +-- 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; + + +-- Show all track titles from `Super Funky Album`. +SELECT track.title, album.title from track +JOIN album ON(album.title = "Super Funky Album"); +--OR +SELECT track.title FROM track,album +WHERE album.title = 'Super Funky Album' +AND album_id = album.id; + +-- Same query as above, but rename the column from `title` to +-- `Track_Title` in the output. +SELECT track.title AS Track_Title +from track +JOIN album ON(album.title = "Super Funky Album"); +-- OR +SELECT track.title AS Track_Title +FROM track,album +WHERE album.title = 'Super Funky Album' +AND album_id = album.id + +-- Select all album titles by `Han Solo`. + + SELECT album.title AS Album_Title +from album +JOIN artist ON(artist.name = "Han Solo"); +-- OR +SELECT * FROM artist, artist_album +WHERE artist.id = 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 artist, album, artist_album +WHERE artist.id = artist_album.artist_id +AND album.id = artist_album.album_id +AND artist.name = 'Leia and the Ewoks'; + +-- Select the number of artists + +SELECT count(id) +FROM artist; + +-- Select the number of tracks on `Super Dubstep Album`. + +SELECT count(track.id) +FROM track, album +WHERE album.title = "Super Dubstep Album"; \ No newline at end of file From e1de8226405395ee5d3bd9dbd3911bbaa0308520 Mon Sep 17 00:00:00 2001 From: NandoTheessen Date: Tue, 4 Sep 2018 16:58:25 +0200 Subject: [PATCH 2/4] Added the db file into the repo --- day1.db | Bin 0 -> 28672 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 day1.db diff --git a/day1.db b/day1.db new file mode 100644 index 0000000000000000000000000000000000000000..98dc74bc6ca7d6627bdd2a450f2454138f968c9e GIT binary patch literal 28672 zcmeI(Pi)&%90zbe|4B$X=WUGQ{1MXIq)CZdF(qj`5Enx0t`k)YY1uR`iJQKGRmW}} zM^$fY5fU7@ap693<-~aCXP@`&Pi3(s_vx=~c!gNh0Er6_3owdsVzY*~0u$8_O16x4XF; zYn$eEvppNOI$Ohj)M&c_e~0^`ae2LM-nhQTUN>)7AM8+LZFAXdn`?{a2J0T)6brm^ zg&cX&+>?VxM;m=VKHbZs(dGYOR?+y~YF8|>_RvC3*FV0Jcuq;Dd@WG3MyW&&7f03= z@1l*-$8D^t=k0BlfU;fR?pyu=+u;Yy8U~*2_I%#wZZNCo!|$BkY_%6xTJ37R@luVg zt#2}S=s0@5c9>JLhGCGyy?EI0TZtBs}D9B|7&S%R3$ z8*;{&nIX4FPRh=J?+v-zOHSm|kILBW;*;?-tcEL|6+LT*WfHCK=wfutz_)rk(UUyZ zKWaL&)t#K#EIF~6&+l;YrzaM{qO59f*TizeYxAP+Nc3m=HT{f!Ob=*>nsi27ssCB{ zzVKz?)53=Zr?6gV6sGmR^zZaz{ht1T=zt9Z5P$##AOHafKmY;|fB*zi1!>vKUciComHOS_*vXPd}Y31kBvZhv^PRklv`MI>Lu2n9o(#y$QRxI1!vfS-t zGS}3~v#H^NTDg!K&a0L4so|Vjp{Ze0trSwjhFZ~6!@62Lr%KHeR(sI%l65qd;?(4% zp%n9zle$vOJxG>^?#@ASv^lx>%;cmg7qgR-hFr`{PU><=6JJg)J{em3eBg=yk0d8U zNllC9N=jPPEXiq6qeRl8dU*Y>&>tlF2mMvNV1obzAOHafKmY;|fB*y_009U<;HeYH zDT+a&!h&L6R!Sr;+sNdIXpX7?^5OM=o=V~U|8ME%^b>lQa=J$6X-PCdu)hGfU8Ni6jdC=!57n_ z5a4QZw77U63QA!WK(hbIz5ckzSB5A9m^{8H0!STSQ~^vLU(^6h8eiQ1PZ>YF{?E|6 z68(vOLyze_`jPm64FV8=00bZa0SG_<0uX=z1Rwx`GZmQ1DAEfnmdiFg$D7s^slLi> zi-o_&2;Sw)-1l|{WmS=0Ssm{1)8-}Lv$q4!<%20@w7)#LKS}R@ri;PBAOHafKmY;| zfB*y_009U<00IzrA_e06|Mw;OH~pRdMt`9{()&;3L@_A{KmY;|fB*y_009U<00Izz k00bTu&@u`kimGYhi=wIGMV3`XAvsx04%1ED*ylh literal 0 HcmV?d00001 From 3a539a9851541e2d3dfbb05ea22430f9ef0a9377 Mon Sep 17 00:00:00 2001 From: NandoTheessen Date: Tue, 4 Sep 2018 20:05:26 +0200 Subject: [PATCH 3/4] added solutions - Added day2.db & notes.sql - included db creation queries - included data insertion queries - included search queries - included delete query --- Day2.db | Bin 0 -> 16384 bytes Day2.db-journal | Bin 0 -> 16928 bytes README.md | 93 +++++++++++++++++++++++++----------------------- day1.sql | 2 +- notes.sql | 58 ++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 45 deletions(-) create mode 100644 Day2.db create mode 100644 Day2.db-journal create mode 100644 notes.sql diff --git a/Day2.db b/Day2.db new file mode 100644 index 0000000000000000000000000000000000000000..7785aaa849be8156d401250e9807852e1d7616d8 GIT binary patch literal 16384 zcmeI#NlU{(6bJCch(f^SQi`{C5RzaKt+;>)#&*>~}Uv zRcsI9)$$*h3^OmY{gTUTT|1!^be9fdU($}Us;H`ROr$7^meu*JewFFI`W5)4)s)Tk zm!<3lZP`^;-d7H@1_c5TfB*y_009U<00Izz00jQM!0UsyST2>+w|(h%Bax)CsPi?> zY>$y=pE!*2Q8M)~poZ(QGsdaO8<#fk&;{#|-S(~;ZZ_ePxn4z=p^OCG*t~YmxVc?D zu*kjg$Zb1LMenA;^EhU6v%0mr_ahkV{z%@ZgWDhbaCXW#b8DZC#Zdb3 zBh~X$*4e4uc04izUy3|t8cF(ORLF?Zo*(5ShGpqib8ex$v7vT~s}B27mPg+XMSmoc zo|x)t697{|Gx1|l$!1!8b+Np*roJ8(OODS!e$GuSFdqA}VZQ!1(*&Vz<+*v5^rJuk z0uX=z1Rwwb2tWV=5P$##ATYxM$p2^f7a87u2VpG^@qrHz!8)T3Yt`5ZJ4v+>C++H7M7;UBdiD?W zukdEF$#etJtK%E!Bl*#^`7}NBM-1buygt4?t6JVLa_TVt6~Ngb3*<)h=2tWV=5P$##AOHafKmY;|7<_@xJb-z0 z=+~v7^Xxi}BrT|=0j6oJ6Da{pZEAf;xAfP|Zsg|^%b1zE*SQ93G%&_q##Yq<1p*L& z00bZa0SG_<0uX=z1pd9i!=*J`n3yo1mSuFBh(;#cTfWSh7cvUH4WE&2CC9GER0%@1 z%{U$K%AUt-w99JbRm1&Cpf1>BL0Gb7T_%E#Jzm~n+?ktSaVgjjDX98>$v(~EyH3pG z&iw4+(zjr0M>p~!yFU4LAI`QIXF-`AQ9Bqr_1M+-NJUFV=@o72r`%*)UeyoDi6bd= z%qcdq+hU1|Np=<`x~1s4wtJ8pDojtCwf0b3%|!i2pR`2tMl{YuU)AaX=nFcHC>14j zrQ0-z3zL)P!&ZGSe@{dbvq2yIv80p8WL%VRyjzyJ6!00=+;0uX=z U1Rwwb2tWV=5P-n10(w2b2Pqqv$N&HU literal 0 HcmV?d00001 diff --git a/README.md b/README.md index a6d7c7c..f1b940b 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,17 @@ When you get to the prompt, you can type `.help` for commands. Some helpful ones: -* `.mode column` turn on column output for `SELECT`. -* `.header on` turn on column headers for `SELECT`. -* `.read filename` execute the SQL in `filename`. -* `.open dbname` re-open a memory-only DB to a persistent file. -* `.quit` exit SQLite. (Note that if you're using a memory-only DB, all +- `.mode column` turn on column output for `SELECT`. +- `.header on` turn on column headers for `SELECT`. +- `.read filename` execute the SQL in `filename`. +- `.open dbname` re-open a memory-only DB to a persistent file. +- `.quit` exit SQLite. (Note that if you're using a memory-only DB, all data is lost at this point.) Another potentially useful third-party tool is [DB Browser for SQLite](https://sqlitebrowser.org/), a GUI-based SQLite viewer and data manipulator that can also run SQL queries. - ## Create a Music Database Make an albums table to hold album information: @@ -58,80 +57,86 @@ CREATE TABLE artist ( ); ``` - ### Exercises, Day 1 Before you begin, look at the queries in `setup.sql` to get a hint as to the column names in the following tables. We'll use `setup.sql` later. -* Create a table called `track` that holds information about a music track. It should contain: - * An autoincrementing `id` - * A title (of type `VARCHAR`, probably) - * A reference to an `id` in table `album` (the album the track is on). This +- Create a table called `track` that holds information about a music track. It should contain: + + - An autoincrementing `id` + - A title (of type `VARCHAR`, probably) + - A reference to an `id` in table `album` (the album the track is on). This should be a _foreign key_. -* Create a table called `artist_album` to connect artists to albums. (Note that +- Create a table called `artist_album` to connect artists to albums. (Note that an artist might have several albums and an album might be created by multiple artists.) - * Use foreign keys for this, as well. - -* Run the queries in the file `setup.sql`. This will populate the tables. - * 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 + + - Use foreign keys for this, as well. + +- Run the queries in the file `setup.sql`. This will populate the tables. + + - 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`. -* Write SQL `SELECT` queries that: - * Show all 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. +- Write SQL `SELECT` queries that: -* 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 + - Show all 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. + +- 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 all album titles by `Han Solo`. - * Select the average year all albums were released. + - Select the average year all albums were released. - * Select the average year all albums by `Leia and the Ewoks` were released. + - Select the average year all albums by `Leia and the Ewoks` were released. - * Select the number of artists. + - Select the number of artists. - * Select the number of tracks on `Super Dubstep Album`. + - Select the number of tracks on `Super Dubstep Album`. ### Exercises, Day 2 Create a database for taking notes. -* What are the columns that a note table needs? +- What are the columns that a note table needs? -* If you have a timestamp field, how do you auto-populate it with the date? +- If you have a timestamp field, how do you auto-populate it with the date? -* A note should have a foreign key pointing to an author in an author table. +- A note should have a foreign key pointing to an author in an author table. -* What columns are needed for the author table? +- What columns are needed for the author table? Write queries that: -* Insert authors to the author table. +- Insert authors to the author table. + +- Insert notes to the note table. -* Insert notes to the note table. +- Select all notes by an author's name. -* Select all notes by an author's name. +- Select author for a particular note by note ID. -* Select author for a particular note by note ID. +- Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) -* Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) +- Delete authors from the author table. -* 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. - - * What happens when you try to delete an author with an existing note? - * How can you prevent this? + + - What happens when you try to delete an author with an existing note? + In my case nothing, since I enabled foreign keys before trying this. + I don't think I'd want to prevent this behaviour ^^ + - How can you prevent this? Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. - diff --git a/day1.sql b/day1.sql index 0db6524..b463c50 100644 --- a/day1.sql +++ b/day1.sql @@ -36,7 +36,7 @@ AND album_id = album.id -- Select all album titles by `Han Solo`. - SELECT album.title AS Album_Title +SELECT album.title AS Album_Title from album JOIN artist ON(artist.name = "Han Solo"); -- OR diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..5be1e5e --- /dev/null +++ b/notes.sql @@ -0,0 +1,58 @@ +-- create an author table with needed data + +create table author ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +name VARCHAR(128) NOT NULL +); + +-- create a notes table including an autopopulated date field +-- and a foreign key referencing the author + +create table note ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +title VARCHER(128) NOT NULL, +body VARCHAR(2056) NOT NULL,m +author_id INTEGER REFERENCES authod(id), +timestamp DATE DEFAULT (datetime('now', 'localtime')) +); + +-- fill db with authors + +INSERT INTO author VALUES ("Peter"); +INSERT INTO author VALUES ("Klaus"); +INSERT INTO author VALUES ("Margareth"); +INSERT INTO author VALUES ("Ursula"); +INSERT INTO author VALUES ("Petra"); + +-- fill db with notes + +INSERT INTO note (title, body, author_id) VALUES("blubb1", "blalbalbab1", 1); +INSERT INTO note (title, body, author_id) VALUES("blubb2", "blalbalbab2", 2); +INSERT INTO note (title, body, author_id) VALUES("blubb3", "blalbalbab3", 3); +INSERT INTO note (title, body, author_id) VALUES("blubb4", "blalbalbab4", 4); +INSERT INTO note (title, body, author_id) VALUES("blubb5", "blalbalbab5", 5); +INSERT INTO note (title, body, author_id) VALUES("blubb6", "blalbalbab5", 1); +INSERT INTO note (title, body, author_id) VALUES("blubb7", "blalbalbab5", 2); + +-- select all notes by author "Peter" + +SELECT name, title, body, timestamp FROM note, author +WHERE author.name = "Peter" +AND author.id = author_id; + +-- select author of note nr 3 + +SELECT author.name FROM author, note +WHERE note.id = 3 and +author.id = author_id; + +-- count notes and group by author + +SELECT name, count(title) FROM note, author +WHERE author.id = author_id +GROUP BY author.name; + +-- delete author + +DELETE FROM author +WHERE name = "Peter"; \ No newline at end of file From e5eb4228125ae527abee486006f0c7b3baa8d638 Mon Sep 17 00:00:00 2001 From: NandoTheessen Date: Tue, 4 Sep 2018 20:07:35 +0200 Subject: [PATCH 4/4] Included an updated version of day2.db --- Day2.db | Bin 16384 -> 16384 bytes Day2.db-journal | Bin 16928 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Day2.db-journal diff --git a/Day2.db b/Day2.db index 7785aaa849be8156d401250e9807852e1d7616d8..9f3be5e7c170f323d4ad4dda9357926addc6542a 100644 GIT binary patch delta 662 zcmZwC&r9P#6bJB`PGV~?^AsssJUHS-w^fq1{-`~b;<{x~m*Osq7fE!q!8Da=a?pzv z&w|oF!n5pQFWyD=sIWKx0P!e$c493JONSX4WJp9rlWxPT(9{2Celt7~Ts5+6$@@p-BJlTLlsHD`}Gp3e`sU`N8KTjH1<@?&NN ze#7y01+R0@&*+-(_%3HZO4ah0Qgz16F3+-!%`N5yuA9-@w{+a-Ts2u(Yls~gTiMuJ zU0ba(!S}h~-Y##3sq%Bi*_)L`1QxO0F2EnE!aah2@B&Zp0Qa)d-%w7d1dV;;zOV)r zf>Pf_GjOdTLeS*8B@QgX{aPX>$ye73nhD&&T!b^n;2FUyJjm)DJWU_Qmkd->JtOEODxQWe4+gPc8FgES0{EHoAT{6iG{LVbL6xRUZyDj{-? zK^jH|re;Wzxm<~*B^mie@tG+Ko_-;&?yf-!L9TADL9TwzuE7dm$rO#u6ipqjlFZ!H z;*!MN0tFYx5LX2kS2xE{pAZF&l*E$Ba{N-0E%{F~F$ioHRJh4Mae^Wn$mKY>05-rr AumAu6 diff --git a/Day2.db-journal b/Day2.db-journal deleted file mode 100644 index 4de2d00f5917fc70ee9a02cabe70c09e595a47bd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16928 zcmeI&%S*#B90%}r9SQ{>7a87u2VpG^@qrHz!8)T3Yt`5ZJ4v+>C++H7M7;UBdiD?W zukdEF$#etJtK%E!Bl*#^`7}NBM-1buygt4?t6JVLa_TVt6~Ngb3*<)h=2tWV=5P$##AOHafKmY;|7<_@xJb-z0 z=+~v7^Xxi}BrT|=0j6oJ6Da{pZEAf;xAfP|Zsg|^%b1zE*SQ93G%&_q##Yq<1p*L& z00bZa0SG_<0uX=z1pd9i!=*J`n3yo1mSuFBh(;#cTfWSh7cvUH4WE&2CC9GER0%@1 z%{U$K%AUt-w99JbRm1&Cpf1>BL0Gb7T_%E#Jzm~n+?ktSaVgjjDX98>$v(~EyH3pG z&iw4+(zjr0M>p~!yFU4LAI`QIXF-`AQ9Bqr_1M+-NJUFV=@o72r`%*)UeyoDi6bd= z%qcdq+hU1|Np=<`x~1s4wtJ8pDojtCwf0b3%|!i2pR`2tMl{YuU)AaX=nFcHC>14j zrQ0-z3zL)P!&ZGSe@{dbvq2yIv80p8WL%VRyjzyJ6!00=+;0uX=z U1Rwwb2tWV=5P-n10(w2b2Pqqv$N&HU