From 273d4da25e3710bbc85e6246e59662bcdb888693 Mon Sep 17 00:00:00 2001 From: derrick-mei Date: Tue, 4 Sep 2018 15:42:58 -0500 Subject: [PATCH 1/5] initial commit --- .gitignore | 0 22.db | Bin 0 -> 20480 bytes music.sql | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 22.db create mode 100644 music.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/22.db b/22.db new file mode 100644 index 0000000000000000000000000000000000000000..ad6998edf01b6e22f0cd35c4b99bc1b0fb1ab831 GIT binary patch literal 20480 zcmeI#%WB&|6o6qVwrddNvJ=^zMJX0GA#pa@6snkMYvL%Wq6EA$vNHiuIl;05?XG^N zzF@~vOd>C=5I&KfeM3 t2q1s}0tg_000IagfB*v9FF^gj{bx*u2q1s}0tg_000IagfB*sr`~!*5mV*EQ literal 0 HcmV?d00001 diff --git a/music.sql b/music.sql new file mode 100644 index 0000000..e69de29 From 23e5e0122fcd1eaa4c6bc32d1783b1dacb44e449 Mon Sep 17 00:00:00 2001 From: derrick-mei Date: Tue, 4 Sep 2018 16:32:24 -0500 Subject: [PATCH 2/5] finished easy queries --- 22.db => 22music.db | Bin 20480 -> 24576 bytes music.sql | 84 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) rename 22.db => 22music.db (79%) diff --git a/22.db b/22music.db similarity index 79% rename from 22.db rename to 22music.db index ad6998edf01b6e22f0cd35c4b99bc1b0fb1ab831..1ef5c09db4efba082d47ce2a3bd2dfb37af12b6f 100644 GIT binary patch literal 24576 zcmeI4Pi)&%9LJykB}*Lpb%WwKVeo(!5*k*Lv?~)ALhI3rEZx!!si!J!jEI^hTjFRc z(xm1FcO--+aY9@X2S9>LB_s|*aNZ=CI4~hDoHl_t@$;W_$>cXxqNnZ`DY4^^?f3k# zQ~BH9OV%!xn~u%i?R0nQ4l9xgA_(LS#t0$1@QlE7_?UnhdH4pV!pJ;tc9)!)+S7n4 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S}h17imM@Av=N`0o$E7=WWae}lUGXnzyD-go~^F$qEQDz1`l?Gm*5DT9s1Ahg-8sBC<&CQAeTX}UG z*;&}d#l;yLb4wDFa#Bl*5|gvR1gmq9t7C|(LWrZ2kE;S$NMUjSpDDAEfx+beysCzU z%h2oEr3vyOgTQ7%g|qw 1975 AND release_year < 1990; +/* +id title release_year +---------- ----------------- ------------ +3 Super Disco Album 1978 +4 Super Hairband Al 1984 + */ + +/* Show all albums whose names start with Super D */ +SELECT * from album WHERE album.title LIKE '%Super D%'; +/* +id title release_year +---------- ----------------- ------------ +3 Super Disco Album 1978 +5 Super Dubstep Alb + */ + +/* Show all albums that have no release year */ +SELECT * from album WHERE album.release_year IS NULL; +/* +---------- ----------------- ------------ +2 Super Funky Album +5 Super Dubstep Alb + */ + +/* */ + +/* */ + +/* */ + +/* */ + + +/**/ + + + +/**/ +/**/ +/**/ +/**/ +/**/ +/**/ +/**/ +/**/ \ No newline at end of file From 3173b340c94e31a13aa21177e42eb10c85dbd331 Mon Sep 17 00:00:00 2001 From: derrick-mei Date: Tue, 4 Sep 2018 17:18:07 -0500 Subject: [PATCH 3/5] finished harder queries --- music.sql | 83 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/music.sql b/music.sql index 80df562..cbf087f 100644 --- a/music.sql +++ b/music.sql @@ -18,13 +18,13 @@ CREATE TABLE artist (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT CREATE TABLE track(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, album_id INT REFERENCES album(id)); CREATE TABLE artist_album(artist_id INT REFERENCES artist(id), album_id INT REFERENCES album(id)); -/* Load Data from setup.sql */ +/* Load Data FROM setup.sql */ .read setup.sql /* Write Queries */ /* Show all Albums */ -SELECT * from album; +SELECT * FROM album; /* id title release_year ---------- ------------------- ------------ @@ -36,7 +36,7 @@ id title release_year */ /* Show all albums made between 1975 and 1990 */ -SELECT * from album WHERE release_year > 1975 AND release_year < 1990; +SELECT * FROM album WHERE release_year > 1975 AND release_year < 1990; /* id title release_year ---------- ----------------- ------------ @@ -45,7 +45,7 @@ id title release_year */ /* Show all albums whose names start with Super D */ -SELECT * from album WHERE album.title LIKE '%Super D%'; +SELECT * FROM album WHERE album.title LIKE '%Super D%'; /* id title release_year ---------- ----------------- ------------ @@ -54,31 +54,78 @@ id title release_year */ /* Show all albums that have no release year */ -SELECT * from album WHERE album.release_year IS NULL; +SELECT * FROM album WHERE album.release_year IS NULL; /* ---------- ----------------- ------------ 2 Super Funky Album 5 Super Dubstep Alb */ -/* */ +/* HARDER QUERIES */ -/* */ +/* Show all track titles FROM Super Funky Album */ +SELECT * FROM track WHERE track.title LIKE '%Super Funky%'; /*isn't what we're looking for.*/ -/* */ +SELECT track.title FROM track, album WHERE track.album_id = album.id AND album.title = 'Super Funky Album'; +/* +title +------------------- +Super Funky Track 1 +Super Funky Track 2 +Super Funky Track 3 +Super Funky Track 4 + */ + +/* Same as above, but rename column 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*/ +/*give me album titles where the artist is Han Solo, then using name you can get artist id then go to relational table and get the albums with that artist's id */ +SELECT album.title FROM album, artist, artist_album WHERE artist.name = 'Han Solo' AND artist.id = artist_album.artist_id AND artist_album.album_id = album.id; +/* +title +----------------- +Super Disco Album +Super Hairband Al +*/ + +/*SELECT the average year all albums were released*/ +SELECT AVG(release_year) FROM album; + +/* +avg(release_year) +----------------- +1984.0 +*/ + + +/*SELECT the average year all albums by Leia and the Ewoks were released*/ +SELECT AVG(release_year) FROM album, artist, artist_album WHERE artist.id = artist_album.artist_id AND album.id = artist_album.album_id AND artist.name = 'Leia and the Ewoks'; +/*FROM lecture*/ +SELECT AVG(release_year) FROM album JOIN artist_album ON(album.id=artist_album.album_id) JOIN artist ON(artist.id = artist_album.artist_id) WHERE artist.name=“Leia and the Ewoks”; + +/* +AVG(release_year) +----------------- +1990.0 +*/ -/* */ +/*SELECT the number of Artists*/ +SELECT COUNT(id) FROM artist; -/**/ +/* +COUNT(id) +---------- +3 +*/ +/*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 +/* +COUNT(track.id) +--------------- +5 +*/ \ No newline at end of file From 0dc01b1a71ddec6dd15835a2f72f40d7eabff460 Mon Sep 17 00:00:00 2001 From: derrick-mei Date: Wed, 5 Sep 2018 04:03:06 -0500 Subject: [PATCH 4/5] sql day 2 MVP --- 22notes.db | Bin 0 -> 20480 bytes music.sql | 6 +++++- notes.sql | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 22notes.db create mode 100644 notes.sql diff --git a/22notes.db b/22notes.db new file mode 100644 index 0000000000000000000000000000000000000000..4bea97d9e189a34fc32db2ee5cede8c27c83b929 GIT binary patch literal 20480 zcmeI(%}>){90%~H>s~5xJT7D|+#VxF7C_6!B5<{qR?~}Cq?bqk|cKNKA?q%bt*>fGXH5_esTvjHx zh#-*1j1fX6_?8Mcfj=h0jX(Iq>s>S^$b+fZSw2h59ufMI=CemP-(9o_#~}a#2tWV= z5P$##AOHafoDdL}3Wb7ju&#ZFrq28po9=a4tzqjcy2aM5 zT3xYPY*lYDrD?C#8hk-rZ`iZU?YVu2JyR_8iDFI9mllhxv1YSI(==z<7~^AeIB=bT zdpt)jEu5Ux*>hhFNA2Kr%+d|r(i^J2!GZ()brgRmjhv3_bld!n%dXY+4O^+NM`vT2 zZYWLDW@^*o0d_l@60YudcHQ>&u-n_}Io(SI6!)jZOd+2a4rU@a4=H($?Ml?H1Z?wf zt^R>&J&`WV%m^(XX?Lg3?~vQO&d#1Q*mTZ36;IKfIXFG&Y&)}jW%12zAyb%~6!tTI zW^hFW`^2e)p!cWzTfGs#7QC}WS7k|-PhOP1!s!W>%JGC^mi{CedPwMZ`h|X`@98J{ zksk5}1_U4g0SG_<0uX=z1Rwwb2tWV=|4l%Wk|dW~F;+||S80Z3N}^Y(hH6R)uc{hVlfV42Tuw0*)BA2dLBA3DgYPgP009U<00Izz00bZa z0SG_<0uZ?70#ZWE3*N6i(qn=+DFi<>ki7f<^!Rf^KhQn;aQy4|TmA$C0uX=z1Rwwb z2tWV=5P$##AaDr*J)I$U@82kw6{W%NJ8kbD7D1^7f2AnN^NVxx(wtmj^5XKsqvetu z)6!BIa_62Uxgv5b4%uzVp>`sylUxg`&m?beSRq#?g-YCVd!jza5F|A9yotO#%2tWV=5P$##AOHafKmY;|fWS2s F_yq=LiE;n{ literal 0 HcmV?d00001 diff --git a/music.sql b/music.sql index cbf087f..845bf3d 100644 --- a/music.sql +++ b/music.sql @@ -128,4 +128,8 @@ SELECT COUNT(track.id) FROM track, album WHERE track.album_id = album.id AND alb COUNT(track.id) --------------- 5 -*/ \ No newline at end of file +*/ + +.quit + +/*DONE!!!*/ \ No newline at end of file diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..2de144b --- /dev/null +++ b/notes.sql @@ -0,0 +1,56 @@ +sqlite3 22notes.db +PRAGMA foreign_keys = ON; +.mode column +.header ON + +/* Create database for taking notes */ + +/*Make Author Table*/ +CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(256) NOT NULL, last_name VARCHAR(256) NOT NULL); + +/*Make Note Table*/ +CREATE TABLE note ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + content VARCHAR(1024) NOT NULL, + author_id INT REFERENCES author(id), + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + +/*Write Query to Insert Author*/ +INSERT INTO author (first_name, last_name) VALUES ("AF", "AL"); +INSERT INTO author VALUES (2, "BF", "BL"); +INSERT INTO author (first_name, last_name) VALUES ("CF", "CL"); +INSERT INTO author (first_name, last_name) VALUES ("DF", "DL"); +INSERT INTO author VALUES (6, "EF", "EL"); +INSERT INTO author (first_name, last_name) VALUES ("FF", "FL"); +INSERT INTO author VALUES (5, "5F", "5L"); +INSERT INTO author (first_name, last_name) VALUES ("GF", "GL"); + +/*Write Query to insert note*/ +INSERT INTO note (title, content, author_id) VALUES ('ANoteTitle', 'ANoteContent', 1); +INSERT INTO note (title, content, author_id) VALUES ('BNoteTitle', 'BNoteContent', 2); +INSERT INTO note (title, content, author_id) VALUES ('CNoteTitle', 'CNoteContent', 3); +INSERT INTO note (title, content, author_id) VALUES ('DNoteTitle', 'DNoteContent', 4); +INSERT INTO note (title, content, author_id) VALUES ('ENoteTitle', 'ENoteContent', 5); +INSERT INTO note (title, content, author_id) VALUES ('AANoteTitle', 'AANoteContent', 1); + +/*Select All notes by author name*/ +SELECT note.title as Title, note.content as Content from note, author WHERE author.id = note.author_id and author.id = 1; + +/*Select author by note.id*/ +SELECT author.first_name, author.last_name from author, note WHERE note.id = 1 AND author.id = note.author_id; + +/*Select the names of all the authors along with the number o fnotes they each have (hint: GROUP BY)*/ +SELECT first_name, last_name, COUNT(*) FROM author, note WHERE author.id = note.author_id GROUP BY author.id; + +/*Delete Authors from the author table*/ + DELETE FROM author WHERE id=1; + +/*What happens whtn you try to delete an author with an existing note?*/ +Error: FOREIGN KEY constraint failed + + +/*How can you prevent this?*/ +PRAGMA foreign_keys = OFF; to turn off protection From ffd7acc65c0ba34a0727191dfea7d35b83bda51d Mon Sep 17 00:00:00 2001 From: derrick-mei Date: Wed, 5 Sep 2018 11:40:16 -0500 Subject: [PATCH 5/5] added to answer to foreign key question --- notes.sql | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/notes.sql b/notes.sql index 2de144b..58393f3 100644 --- a/notes.sql +++ b/notes.sql @@ -37,7 +37,7 @@ INSERT INTO note (title, content, author_id) VALUES ('ENoteTitle', 'ENoteContent INSERT INTO note (title, content, author_id) VALUES ('AANoteTitle', 'AANoteContent', 1); /*Select All notes by author name*/ -SELECT note.title as Title, note.content as Content from note, author WHERE author.id = note.author_id and author.id = 1; +SELECT note.title as Title, note.content as Content from note, author WHERE author.id = note.author_id and author.name = 'AF'; /*Select author by note.id*/ SELECT author.first_name, author.last_name from author, note WHERE note.id = 1 AND author.id = note.author_id; @@ -46,7 +46,7 @@ SELECT author.first_name, author.last_name from author, note WHERE note.id = 1 A SELECT first_name, last_name, COUNT(*) FROM author, note WHERE author.id = note.author_id GROUP BY author.id; /*Delete Authors from the author table*/ - DELETE FROM author WHERE id=1; +DELETE FROM author WHERE id=1; /*What happens whtn you try to delete an author with an existing note?*/ Error: FOREIGN KEY constraint failed @@ -54,3 +54,11 @@ Error: FOREIGN KEY constraint failed /*How can you prevent this?*/ PRAGMA foreign_keys = OFF; to turn off protection + +You can also delete notes associated with author first and then author - You should implement it as a transaction +begin transaction; +delete from notes where author_id is 1; +delete from author where id is 1; +commit; + +You can also apply cascade to foreign key: author_id INT REFERENCES author(id) ON DELETE CASCADE \ No newline at end of file