From 89d33332ea6cbb7213b70eb60189cf568b001f1f Mon Sep 17 00:00:00 2001 From: samarv Date: Wed, 5 Sep 2018 01:13:15 +0530 Subject: [PATCH 1/2] pushing day 1 in readme --- README.md | 34 +++++++++++++++++++++++++++++++--- music.db | Bin 0 -> 24576 bytes 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 music.db diff --git a/README.md b/README.md index a6d7c7c..d5eaaab 100644 --- a/README.md +++ b/README.md @@ -65,42 +65,70 @@ 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 should be a _foreign key_. +CREATE TABLE track (id INTEGER PRIMARY KEY AUTOINCREMENT,title VARCHAR(128) NOT NULL, album_ID INT 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. + CREATE TABLE artist_album(id INTEGER PRIMARY KEY AUTOINCREMENT,album_ID INT REFERENCES album(id),artist_ID INT REFERENCES artist(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 `CREATE TABLE`. +.read setup.sql + * 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 > 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 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,album WHERE album.title ='Super Funky Album' AND album.id = album_id; * Select all album titles by `Han Solo`. +SELECT * FROM artist,artist_album WHERE artist.name = 'Han Solo' AND artist.id = artist_id; + * 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 the number of artists. +SELECT AVG(release_year) FROM artist,artist_album,album WHERE artist.name = 'Leia and the Ewoks' AND artist.id = artist_id; + * Select the number of artists. +SELECT COUNT(ID) from artist; * Select the number of tracks on `Super Dubstep Album`. + SELECT count(track.title) AS Track_Title FROM track,album WHERE album.title ='Super Dubstep Album' AND album.id = album_id; + ### Exercises, Day 2 Create a database for taking notes. diff --git a/music.db b/music.db new file mode 100644 index 0000000000000000000000000000000000000000..68d59b18522a29ae6f8dd75b6c5cd504c00dda71 GIT binary patch literal 24576 zcmeI3O>7ip7{}*pKW27j-ttj*XIFGoDqUK^{U`B8ebEQM(v(_Oq zcd)!~ya*E}tzvn&Rc+O5^SNAUtdJ`WrqbJ!W^rHHEFPSg7&a@lnZ?7?<9X*gX35%P zm8{~JHRYX$>nD@2_}8NY1N^CV?usgnR<+rhcIS1!4z5^VH@Ur4x)W<$^uaLU$wPDxnO4FWRvMY^F=#``f>0GaK9J|UcvGeRSJIQQTWFyRAoPJfmq@UMM z>nC+vFX|(@0R!nE17v^JLUgUfZEdrBwK-dFgOB^y0BB!r&*H-Tk_U`FDAFFE zJ7zcQ@YL-V34Ix5Sbe=meGRLx`qz)(^%bnX+@pRNs~_^OAH?eiu=-Mu`XW|e@UPFK z^_}N`iCyH_4R($F3@_*)17v^z;`!}sIn+V zdAHT2pa`PThN{fNX}8T_z4QF9GWh-fuk0Ikft_WqK?lGbn`FD#My3IvgA9-XGC&5% z02v?yWPl8i0Wv@a$iO{dAfk%ghC*e*oT}IA{SlE%P1x0n=`=gEUbIc?Sbe@3S43{- z#Nxbt^K`yZuggU}^=tJ5 z^-cAJI;)PWTh+CZ8<8uKPa^L`mLqeKLS#77r~IK@Q9f1PRbEzV$^m6m*`SE>HThfl zy!^KOl3bDV@+Mgc{~rD>{9*X5@bU2Tupm0f02v?yWPl8ifxFs3ydZI-{K_8Evh#Ph z=13sE7h~B#d=JJlfw+aSbReF`SSpYhleiu2wcKo5^%gTKC3d55o0Q0*a70S%LSa@) zJcGiFlz1A2X({m(3R6<-N$A?_Xmz`6z4wt3V>|sxT8us6Pf}uR$1UQu=6YAl3bF0} zBqPK|{YhGgZSyB7!5D#?BYR=!^!m2LU}Q0rH8L2=7-n+sg>V zw_*%3xdmg8$;U7TnH-Lf?OftjZ*G{reHfB((ke z5y+&|`QNV7eKp7=H2!0mgw}t5lr^C9-ydZR=>7LcX#@KI{ZY#M{r@?R{mrhk-`Fqg z2lhR?$i85ov5(k$?A%@b9}ij_86X2>fDDiUGC&5%02v?yWPl8ifqTk85VkmYUJ`?_ zp~3T_7=-N$o)?4wY*xs;5P+QtUKRqd4Z+KSAnZA~gJFlk9SqwE?qJwEa0bIZfioEP H1Yq#L`^st} literal 0 HcmV?d00001 From cee555ca13a6c77144a22ce549260a7da96113c9 Mon Sep 17 00:00:00 2001 From: samarv Date: Wed, 5 Sep 2018 02:32:31 +0530 Subject: [PATCH 2/2] completed day 2 --- README.md | 25 +++++++++++++++++++++++++ notes.db | Bin 0 -> 16384 bytes notes.sql | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 notes.db create mode 100644 notes.sql diff --git a/README.md b/README.md index d5eaaab..3f97801 100644 --- a/README.md +++ b/README.md @@ -135,31 +135,56 @@ Create a database for taking notes. * What are the columns that a note table needs? +CREATE TABLE notes( id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, body VARCHAR(256) , Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, author_ID INT REFERENCES author(id)); + * If you have a timestamp field, how do you auto-populate it with the date? +Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP * A note should have a foreign key pointing to an author in an author table. + +author_ID INT REFERENCES author(id) + * What columns are needed for the author table? +id +name + +CREATE TABLE author( id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(128) NOT NULL); Write queries that: * Insert authors to the author table. + INSERT INTO author (name) VALUES ("john green"); + * Insert notes to the note table. +INSERT INTO notes (title, body, author_ID) VALUES ("title goes here", "body here", 1); + * Select all notes by an author's name. +SELECT * FROM notes,author WHERE author.name = "john green" AND author.id = author_id; + * Select author for a particular note by note ID. +SELECT * FROM notes,author WHERE notes.id = 1 AND author.id = author_id; + * Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) +SELECT COUNT(author_id),author.name + FROM author, notes + WHERE author.id = notes.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. * What happens when you try to delete an author with an existing note? + DELETE FROM author WHERE name = "samarv"; * How can you prevent this? + cascading? + 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..49b6a71583172ede02fb4073fb16882df997b140 GIT binary patch literal 16384 zcmeI&J#W)M7zgmP9i<9t>JCU&CY}maifIB#oT4TY5+-pWB5{lBAa$zT_S(24Hg()e zjD-)t%GY5pUxJMhi46uWb_=AS3c-Mi{wMkDyXPMJ?&rxb8&A4^%xN!-MqW$?n`e^5 z?h`S_X2q5f+j*HB|Jat!1DEV)+3oqydDUjh+#;L%GPkI{zGO5)5P$##AOHafKmY;| zfB*y_@NWf9TxCYnH0jk$>}?Nu5XO9*RC3F)OxGgUe9*Nh=_-=HOC8&_9$F5qJDr~C zY|>+Elgz%m*0DuG&$8VT#eO{G^we}(k4&dnt*z)}ueoIRyWJ9Phr2Jn#cIoUbt;kT zkN7zDM$f4&F6nl97PYNav)^^8)ps0m(ydA7hHLiLOXMBJgD~3av?p^R$6B?-h?cc+ z7AX3=y1p)FwWTF#Gg;L5a3~&xx5oVNhzC3TLQ|Pq=!IZ0@J75OGIepA%n8)Xdw&QO zW@fZPK{{z9`RO9kIx{tcwC{JVC-b9YFWMP+(KNN57gJW$AyeO}Z^RA-0uX=z1Rwwb z2tWV=5P$##AOL~C6u6~i*-g!m(pMh?9`RaI)hgAMa-~tO)Tvf!R+pQGAscd*l^2X$ zI(9$gV@k#*Z$^HdRc%zIEL*zqQ_}aG=?m4Hbz}1UFRLGz`bktM5P$##AOHafKmY;| zfB*y_009VGZGoJuXj1ZrhH_0(3ewqM1?l(yQ>K1Y->awU+pE1u%pL*|fB*y_009U< z00Izz00bcLp9