From 03504e00af00ce084ccb505f4e55e74c91654e6a Mon Sep 17 00:00:00 2001 From: Mister-Corn Date: Sat, 1 Sep 2018 22:00:26 -0700 Subject: [PATCH 1/2] 22:00 01-Sep: Day 1 Exercises complete. --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- music.db | Bin 0 -> 24576 bytes 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 music.db diff --git a/README.md b/README.md index a6d7c7c..60c0f10 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,30 @@ column names in the following tables. We'll use `setup.sql` later. * A reference to an `id` in table `album` (the album the track is on). This should be a _foreign key_. +```sql +CREATE TABLE track( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + album_id INTEGER NOT NULL, + FOREIGN KEY(album_id) REFERENCES album(id) +); +``` + * 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. + ```sql + CREATE TABLE artist_album( + id INTEGER PRIMARY KEY AUTOINCREMENT, + artist_id INTEGER NOT NULL, + album_id INTEGER NOT NULL, + FOREIGN KEY(artist_id) REFERENCES artist(id), + FOREIGN KEY(album_id) REFERENCES album(id) + ); + ``` + * 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 @@ -82,24 +101,83 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. + ```sql + SELECT * FROM album; + ``` * Show all albums made between 1975 and 1990. + ```sql + SELECT * FROM album WHERE release_year BETWEEN 1975 AND 1990; + ``` * Show all albums whose names start with `Super D`. + ```sql + SELECT * FROM album WHERE title LIKE 'Super D%'; + ``` * Show all albums that have no release year. + ```sql + SELECT * FROM album WHERE release_year IS NULL; + ``` + +--- + +Great resources: +* [sqlite-select](http://www.sqlitetutorial.net/sqlite-select/) +* [sqlite-where](http://www.sqlitetutorial.net/sqlite-where/) +* [where-isnull](https://www.dofactory.com/sql/where-isnull) +* [sqlite-avg](http://www.sqlitetutorial.net/sqlite-avg/) +* [formatting sqlite shell output](https://dba.stackexchange.com/a/40672) + +--- * Write SQL `SELECT` queries that: * Show all track titles from `Super Funky Album`. + ```sql + SELECT track.title + FROM track + JOIN album ON album.id = track.album_id + WHERE album.title IS 'Super Funky Album'; + ``` * Same query as above, but rename the column from `title` to `Track_Title` in the output. - + ```sql + SELECT track.title AS Track_Title + FROM track + JOIN album ON album.id = track.album_id + WHERE album.title IS 'Super Funky Album'; + ``` * Select all album titles by `Han Solo`. - + ```sql + SELECT artist.name AS Artist, album.title AS Album_Title + FROM album + JOIN artist_album ON artist_album.album_id = album.id + JOIN artist ON artist.id = artist_album.artist_id + WHERE artist.name IS 'Han Solo'; + ``` * Select the average year all albums were released. + ```sql + SELECT avg(release_year) FROM album; + ``` * Select the average year all albums by `Leia and the Ewoks` were released. + ```sql + SELECT AVG(release_year) + FROM album + JOIN artist_album ON artist_album.album_id = album.id + JOIN artist ON artist.id = artist_album.artist_id + WHERE artist.name IS 'Leia and the Ewoks'; + ``` * Select the number of artists. + ```sql + SELECT COUNT(*) AS Number_of_Artists FROM artist; + ``` * Select the number of tracks on `Super Dubstep Album`. + ```sql + SELECT COUNT(*) AS Number_of_Tracks + FROM track + JOIN album ON album.id = track.album_id + WHERE album.title IS 'Super Dubstep Album'; + ``` ### Exercises, Day 2 diff --git a/music.db b/music.db new file mode 100644 index 0000000000000000000000000000000000000000..8a36140fd21edce625d4be949f0ba0a663884dc3 GIT binary patch literal 24576 zcmeI(KWyVv90&0C>}QAME`BXg>?9z)rBZS|fs(WogpdfWd#y;*J2!{aDRNsQtft9b z;^@?k9t-R&Oe}~M78a&TNDK&A5Mltt01Su?2>~0=e^QcDd~nhY9llRW;`sag=Y8x9 ze)?tiM$->G@mhay>;|Gv<_M+aB_RkQSL92VuW@5?A$*M+9WUJH^9p(T$&d1~gLe5F z!n?d^yqEt`E}}sI0uX=z1Rwwb2tWV=5O_EOcQabXTwJ7Y-U!_NuICN{e;9P!?*8a_ za#>$<>_*!b?Z(SZTTCY9oDjl46kDyfea&{nb!Th4;p~Z5>^;%A+1}Y|$xXNIR{Lt$ zG1+pWe`}{LS~r``I1Z27NlQ0&9DD0pE8KPYd`A`G*c-NEx7O_4WcOwNuoCTVYJ+6g z)BVGhVfUPQeL3q99VGKv`25fK7li-8f92oG1vCgi00Izz00bZa0SG_< z0uX=z1TM5dmZ>IIZ0Gcpyw2|kgQR&RCbJ3YvJ?1%FhV@nSUui&>#Q- z2tWV=5P$##AOHafKmY;|xPSteRFyon>Gs5KzuR9>R8nnvzAM6Ff#7vd*mwFz!y;A5 z3(e7ycfNXk(Dx66e$N|*&;M4w8`l4S&OhYu@DtwQHecfN#-GLy#;3*y#@j~M*fDCx zLjJG(_xZE@`}w!z251m~00bZa0SG_<0uX=z1R#(qP_!6XrPHu Date: Tue, 4 Sep 2018 10:23:03 -0700 Subject: [PATCH 2/2] 10:22 PDT 4-Sep: Day 2 tasks complete. --- README.md | 24 ++++++++++++++++++-- notes.db | Bin 0 -> 16384 bytes notes.sql | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 notes.db create mode 100644 notes.sql diff --git a/README.md b/README.md index 60c0f10..51abc93 100644 --- a/README.md +++ b/README.md @@ -185,12 +185,28 @@ Create a database for taking notes. * What are the columns that a note table needs? +|Columns |Relation | +|:---: |:---: | +|id |Primary | +|title | | +|note_text | | +|date_created | | +|author_id |Foreign | + * If you have a timestamp field, how do you auto-populate it with the date? +Per this [StackOverflow answer](https://stackoverflow.com/a/14462319), use `CURRENT_TIMESTAMP` +as the default value. + * A note should have a foreign key pointing to an author in an author table. * What columns are needed for the author table? +|Columns |Relation | +|:---: |:---: | +|id |Primary | +|author_name | | + Write queries that: * Insert authors to the author table. @@ -207,9 +223,13 @@ Write queries that: > 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?
+ `FOREIGN KEY constraint failed`
+ This happens because we are trying to authors, but will end up leaving notes with foreign keys pointing to nowhere if we do so, so the deletion is stopped. + * How can you prevent this? + We can set `ON DELETE CASCADE`, which will delete all related rows with a foreign key matching the author being deleted. + Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. diff --git a/notes.db b/notes.db new file mode 100644 index 0000000000000000000000000000000000000000..669d760ed986e656a9ad14aa28caad411280e248 GIT binary patch literal 16384 zcmeI&O^ee&7zgl~q_|Sp)JrXe1)tkC>>~Jrcr>QdwU{RBG!=U)aXYewzOdaaE8Y}8 zfqoIclHkd+AHZieM!S1h@YbdO36p8&E%W@6%h2>+MU&jnyTx*r<@8A05mJh0L_!FU zbA@x69R9I-Ez5>$N>AM1`L@j+T<@;%et2KFK!N}SAOHafKmY;|fB*y_0D-j;_~33e znoapJ$+Ndplbz+qi)C@HgjxluqBM9ODJtq(Rieq5x^b!wl%_-7?FD*BFV&EOL7H@9 z9@A5Cy3hTJ5uy`PEv{oQB?I0om-8)JvqF%_36o!x2L01b8=?p zN9M|05U(7jQ