From db0a9d113f65be4330966dc84ef0f1efa3856c24 Mon Sep 17 00:00:00 2001 From: JSNavey Date: Sun, 2 Sep 2018 21:10:05 -0400 Subject: [PATCH 1/9] Set up database and create tables --- music.sql | 13 +++++++++++++ musicdb.db | Bin 0 -> 28672 bytes 2 files changed, 13 insertions(+) create mode 100644 music.sql create mode 100644 musicdb.db diff --git a/music.sql b/music.sql new file mode 100644 index 0000000..0a48a8e --- /dev/null +++ b/music.sql @@ -0,0 +1,13 @@ +sqlite3 musicdb.db -- Specify a persistent DB file + +PRAGMA foreign_keys = ON; -- The library is compiled with foreign keys enabled. +.mode COLUMN +.header ON + +/********* CREATE TABLES **********/ +CREATE TABLE album (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INTEGER); +CREATE TABLE artist (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL); +CREATE TABLE track (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, album_id INTEGER REFERENCES album(id)); +CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id)); + + diff --git a/musicdb.db b/musicdb.db new file mode 100644 index 0000000000000000000000000000000000000000..6b9b74c3f680b3aac8b9e7d83b36a0e9eadf83ee GIT binary patch literal 28672 zcmeI)Pi)&%90%}czvqN(v)=|3JBcQ}14D@#fs(Wo#D&m$v}%@)G@Gb6L{4Lb)il{+ zM_q4(6WqD%j<|9_NQhGIw6eA{apzwWC{O{{O&ma5p zq@T0z{}fB*y_009U<00I#BR|<@IDs5i5qKuuu>GWJ@7fYg4ZpUM0D16!hYcu z!`+JG%UaqrP35g@)T3bN>>osv)Lb8;O+p@Vdez2GOEhlO>*9KI>sqb3C${Z9QM=LF z*=oomui1@ONd#WdbH&YCbA7YcER-wH6~*+hzuUh%ENy19%6NMQEGE$AIv8(z=pOc+ z!-NhNuhW#dysYd+7xr)Wqa=?kjxVT@_4p{hndq^H z6FPT%^xz))PX5w*=*pMPciYGEm76}n;T7^h#d$ zPRW=16~nQ)9Q30>00Izz00bZa0SG_<0uX=z1R(Gq76`}x8h?}UANiO3BYsydqCo%x z5P$##AOHafKmY;|fB*y_@DB@^8Y5M*JG$i##m1=P2ktFV3u_B|Z-_{edi=h%S=;EFAw+{vF}J z@}K1l4FV8=00bZa0SG_<0uX=z1Rwx`GbfOt)KsExK&*2r%_{NN7Br*C&C%xyT0H(g zA^Zpa1wT1+H;4{H00Izz00bZa0SG_<0uX=z1pXHS=P4zZH=VxN9rOlEDkbH*>p3DU z76{&Og?%(Q@N)_!FVsf|?&;=@;lS$#gTCvB<9|!=+eeN&j8{MnBQt)8Cc{pg{lv5P$##AOHafKmY;|fIy-^&SGRunXmP3 zg@4>9R<)d&l&xsFCz7(NmRm~7Dq8OGq^zvvA7kYCNncLO%j3?hvHYWn;j=7%DKT7S z`HP9+3d{4vu*&jAVpw5$JuxgZ>w>IqpRz{P@bP_AsdavFQlZwl#Yvf3nfpmxLLZ-P zMYSGToK#gSy*R0;R%&rlRegm8`7%UmR7lvi5&*R0-?< z7f0oA{GZ}q6aEMPh2P^p@$cmd8U!E!0SG_<0uX=z1Rwwb2tWV=XG1_sQAMFl)500m Vn4GC9qg2VLOqE{%s7#Sl?N1Ny27>?q literal 0 HcmV?d00001 From 4671be523a9c13e4b73442766344928d04629225 Mon Sep 17 00:00:00 2001 From: JSNavey Date: Sun, 2 Sep 2018 21:16:31 -0400 Subject: [PATCH 2/9] Populate the tables --- music.sql | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/music.sql b/music.sql index 0a48a8e..21dce6f 100644 --- a/music.sql +++ b/music.sql @@ -4,6 +4,7 @@ PRAGMA foreign_keys = ON; -- The library is compiled with foreign keys enabled .mode COLUMN .header ON + /********* CREATE TABLES **********/ CREATE TABLE album (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INTEGER); CREATE TABLE artist (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL); @@ -11,3 +12,50 @@ CREATE TABLE track (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id)); +/********* Run the queries in the file `setup.sql` to populate the tables **********/ +INSERT INTO album (title, release_year) VALUES ("Super Awesome Album", 1990); +INSERT INTO album (title) VALUES ("Super Funky Album"); +INSERT INTO album (title, release_year) VALUES ("Super Disco Album", 1978); +INSERT INTO album (title, release_year) VALUES ("Super Hairband Album", 1984); +INSERT INTO album (title) VALUES ("Super Dubstep Album"); + +INSERT INTO artist (name) VALUES ("Luke and the Droidtones"); +INSERT INTO artist (name) VALUES ("Leia and the Ewoks"); +INSERT INTO artist (name) VALUES ("Han Solo"); + +INSERT INTO artist_album (artist_id, album_id) VALUES (1, 5); +INSERT INTO artist_album (artist_id, album_id) VALUES (1, 2); +INSERT INTO artist_album (artist_id, album_id) VALUES (2, 1); +INSERT INTO artist_album (artist_id, album_id) VALUES (2, 2); +INSERT INTO artist_album (artist_id, album_id) VALUES (3, 3); +INSERT INTO artist_album (artist_id, album_id) VALUES (3, 4); + +INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 1", 1); +INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 2", 1); +INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 3", 1); +INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 4", 1); +INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 5", 1); + +INSERT INTO track (title, album_id) VALUES ("Super Funky Track 1", 2); +INSERT INTO track (title, album_id) VALUES ("Super Funky Track 2", 2); +INSERT INTO track (title, album_id) VALUES ("Super Funky Track 3", 2); +INSERT INTO track (title, album_id) VALUES ("Super Funky Track 4", 2); + +INSERT INTO track (title, album_id) VALUES ("Super Disco Track 1", 3); +INSERT INTO track (title, album_id) VALUES ("Super Disco Track 2", 3); +INSERT INTO track (title, album_id) VALUES ("Super Disco Track 3", 3); + +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 1", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 2", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 3", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 4", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 5", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 6", 4); +INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 7", 4); + +INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 1", 5); +INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 2", 5); +INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 3", 5); +INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 4", 5); +INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 5", 5); + From b8216cc27d3976c0b23ffe6a19fee14d0e5639bb Mon Sep 17 00:00:00 2001 From: JSNavey Date: Sun, 2 Sep 2018 21:54:54 -0400 Subject: [PATCH 3/9] Implement SQL SELECT part1 --- music.sql | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/music.sql b/music.sql index 21dce6f..396b9ab 100644 --- a/music.sql +++ b/music.sql @@ -59,3 +59,45 @@ INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 3", 5); INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 4", 5); INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 5", 5); + +/********* SELECT queries *********/ + +/********* Show all albums *********/ + +SELECT * FROM album; +id title release_year +---------- ------------------- ------------ +1 Super Awesome Album 1990 +2 Super Funky Album +3 Super Disco Album 1978 +4 Super Hairband Albu 1984 +5 Super Dubstep Album + + +/******** Show all albums made between 1975 and 1990 *********/ + +SELECT * FROM album WHERE release_year BETWEEN 1975 AND 1990; -- inclusive begin and end value +SELECT * FROM album WHERE release_year >= 1975 AND release_year <= 1990; -- BETWEEN is shorthand for >= AND <= +id title release_year +---------- ------------------- ------------ +1 Super Awesome Album 1990 +3 Super Disco Album 1978 +4 Super Hairband Albu 1984 + + +/********* Show all albums whose names start with Super D *********/ + +SELECT * FROM album WHERE 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 release_year IS NULL; +id title release_year +---------- ----------------- ------------ +2 Super Funky Album +5 Super Dubstep Alb + From 1317b66de00dfe7e62aa15d38f4396f916b3bb7a Mon Sep 17 00:00:00 2001 From: JSNavey Date: Sun, 2 Sep 2018 23:31:11 -0400 Subject: [PATCH 4/9] Implement SQL SELECT part2. Finish Day 1 --- music.sql | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/music.sql b/music.sql index 396b9ab..0e27c29 100644 --- a/music.sql +++ b/music.sql @@ -93,6 +93,7 @@ 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 release_year IS NULL; @@ -101,3 +102,92 @@ id title release_year 2 Super Funky Album 5 Super Dubstep Alb + +/********* 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'; + +title +------------------- +Super Funky Track 1 +Super Funky Track 2 +Super Funky Track 3 +Super Funky Track 4 + + +/********* 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'; + +Track_Title +------------------- +Super Funky Track 1 +Super Funky Track 2 +Super Funky Track 3 +Super Funky Track 4 + + +/********* Select all album titles by Han Solo *********/ + +SELECT album.title +FROM artist, album, artist_album +WHERE artist_album.album_id = album.id +AND artist_album.artist_id = artist.id +AND artist.name = 'Han Solo'; + +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 artist, album, artist_album +WHERE artist_album.album_id = album.id +AND artist_album.artist_id = artist.id +AND 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.title) +FROM track, album +WHERE track.album_id = album.id +AND album.title = 'Super Dubstep Album'; + +COUNT(track.title) +------------------ +5 + + From 37df0fc5be2d09eca2b236588b148d0b2739ecbc Mon Sep 17 00:00:00 2001 From: JSNavey Date: Mon, 3 Sep 2018 16:24:17 -0400 Subject: [PATCH 5/9] Comment the output field --- music.sql | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/music.sql b/music.sql index 0e27c29..509bc3f 100644 --- a/music.sql +++ b/music.sql @@ -1,8 +1,8 @@ -sqlite3 musicdb.db -- Specify a persistent DB file +-- sqlite3 musicdb.db -- Specify a persistent DB file -PRAGMA foreign_keys = ON; -- The library is compiled with foreign keys enabled. -.mode COLUMN -.header ON +-- PRAGMA foreign_keys = ON; -- The library is compiled with foreign keys enabled. +-- .mode COLUMN +-- .header ON /********* CREATE TABLES **********/ @@ -65,6 +65,7 @@ INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 5", 5); /********* Show all albums *********/ SELECT * FROM album; +/* id title release_year ---------- ------------------- ------------ 1 Super Awesome Album 1990 @@ -72,35 +73,42 @@ id title release_year 3 Super Disco Album 1978 4 Super Hairband Albu 1984 5 Super Dubstep Album +*/ /******** Show all albums made between 1975 and 1990 *********/ SELECT * FROM album WHERE release_year BETWEEN 1975 AND 1990; -- inclusive begin and end value SELECT * FROM album WHERE release_year >= 1975 AND release_year <= 1990; -- BETWEEN is shorthand for >= AND <= +/* id title release_year ---------- ------------------- ------------ 1 Super Awesome Album 1990 3 Super Disco Album 1978 -4 Super Hairband Albu 1984 +4 Super Hairband Albu 1984 +*/ /********* Show all albums whose names start with Super D *********/ SELECT * FROM album WHERE 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 release_year IS NULL; +/* id title release_year ---------- ----------------- ------------ 2 Super Funky Album 5 Super Dubstep Alb +*/ /********* Show all track titles from Super Funky Album *********/ @@ -109,13 +117,14 @@ 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 query as above, but rename the column from title to Track_Title in the output *********/ @@ -125,13 +134,14 @@ AS Track_Title FROM track, album WHERE track.album_id = album.id AND album.title = 'Super Funky Album'; - +/* Track_Title ------------------- Super Funky Track 1 Super Funky Track 2 Super Funky Track 3 Super Funky Track 4 +*/ /********* Select all album titles by Han Solo *********/ @@ -141,20 +151,22 @@ FROM artist, album, artist_album WHERE artist_album.album_id = album.id AND artist_album.artist_id = artist.id AND artist.name = 'Han Solo'; - +/* 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 *********/ @@ -164,19 +176,21 @@ FROM artist, album, artist_album WHERE artist_album.album_id = album.id AND artist_album.artist_id = artist.id AND 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 *********/ @@ -185,9 +199,10 @@ SELECT COUNT(track.title) FROM track, album WHERE track.album_id = album.id AND album.title = 'Super Dubstep Album'; - +/* COUNT(track.title) ------------------ 5 +*/ From 664c1fde2151d9b54650f46bb133f271a3dc38ef Mon Sep 17 00:00:00 2001 From: JSNavey Date: Mon, 3 Sep 2018 16:31:07 -0400 Subject: [PATCH 6/9] Set up database for Notes --- notedatabase.db | Bin 0 -> 16384 bytes notes.sql | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 notedatabase.db create mode 100644 notes.sql diff --git a/notedatabase.db b/notedatabase.db new file mode 100644 index 0000000000000000000000000000000000000000..1c20420eda704b87818614807da0652cabbeab80 GIT binary patch literal 16384 zcmeI&PfOci90%|xNmp&aW*}0E2+y{IE!L4Hb+tV$#;9d&?P9vErxNYsEYt+khK?o}<_Ge#q#lADp`()yZQb z2;?QDgpf2p68wmp$Zw|mLj2&KYnnW#pO$!mlr9q`oBqNtXb^w^1Rwwb2tWV=5P$## zAnNzAgZ989(2ksw+<$Oy_ zW;Qm2&S*BniveHLc8BcZl6fa=t}D&VZ|-3(`ecD8^EW+VA+xd~eApPh)AK_X9pX$n z8u|Nw8=G{eL~Lu%vaP0R9o>TE`aS-$VLu$q0!-`*@`>;9DTUOr-f^#!fVm;-b@?KG zm+L#VhBc|ByVjo3t~=Ch+x#m|H+p*H7>$;kkJjd?^i!gIQ$8z!Qc(zBqkl|y_z)Td zAOHafKmY;|fB*y_009X6R{~q9BoVS@U1~;)UDLc>cAE6~njWy=yzhHVQ;XZ1YGqT^ zsiszorK-NYwKyHyiDNtW#x#9vVLE2SG2`BtS}G@bY<*J~N0Zc}YTNhXbljhtre4*` zKHrf-qHm=uO_vZ|ADZRlO3e|3&2oQGW3g8U!E!0SG_<0uX=z1Rwwb z2tWV=4_shTlvadj!$L|Hq^vOBgdmOf<||)`@>BWFFK7^e00bZa0SG_<0uX=z1Rwwb y2>jCm%Tj`@t;Rbt_5;^@&w{j=Aj_+KE5uvo4hQU75fUVmJ#d5Iy5)x<3w{Gdk}946 literal 0 HcmV?d00001 diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..afe460e --- /dev/null +++ b/notes.sql @@ -0,0 +1,31 @@ +/* +Create a database for taking notes. + +What are the columns that a note table needs? + - An autoincrement id + - A title (of type VARCHAR(128), should be enough??) + - A content (of type TEXT, with 65,535 characters - 64 KB should be sufficiently capable of handling typical long-form text content.) + - Author_id is reference to an id in author table (foreign key). + - Created_at as timestamp when a note is created. + +If you have a timestamp field, how do you auto-populate it with the date? + - DATETIME NOT NULL DEFAULT(GETDATE()) + ---> get an error "unknown function: GETDATE()" + + - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP + ---> generate column and print date and time stamp as I wish. + `https://www.w3schools.com/sql/sql_ref_mysql.asp` + +A note should have a foreign key pointing to an author in an author table. + +What columns are needed for the author table? + - An autoincrement id + - Author's first name and last name +*/ + +/********* set up database *********/ +-- sqlite3 notedatabase.db +-- PRAGMA foreign_keys = ON; +-- .mode column +-- .header on + From d40f9909047f0762d16a231423eb05cd483cfe78 Mon Sep 17 00:00:00 2001 From: JSNavey Date: Mon, 3 Sep 2018 16:38:41 -0400 Subject: [PATCH 7/9] Create tables and insert data --- notes.sql | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/notes.sql b/notes.sql index afe460e..c3816ee 100644 --- a/notes.sql +++ b/notes.sql @@ -29,3 +29,60 @@ What columns are needed for the author table? -- .mode column -- .header on + +/********* CREATE TABLES ********/ +CREATE TABLE note ( +id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, +author_id INTEGER REFERENCES author(id), +title VARCHAR(128) NOT NULL, +content TEXT, +created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE author ( +id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, +firstname VARCHAR(128), +lastname VARCHAR(128) +); + + +/********* POPULATE TABLES ********/ +INSERT INTO author (firstname, lastname) VALUES ("Harry", "Potter"); +INSERT INTO author (firstname, lastname) VALUES ("Ron", "Weasley"); +INSERT INTO author (firstname, lastname) VALUES ("Hermione", "Granger"); +/* +SELECT * FROM author; + +id firstname lastname +---------- ---------- ---------- +1 Harry Potter +2 Ron Weasley +3 Hermione Granger +*/ + +INSERT INTO note (author_id, title, content) VALUES (1, "Note One", "Content One"); +INSERT INTO note (author_id, title, content) VALUES (1, "Note Two", "Content Two"); +INSERT INTO note (author_id, title, content) VALUES (1, "Note Three", "Content Three"); + +INSERT INTO note (author_id, title, content) VALUES (2, "Note 1 by Ron", "Content 1 by Ron"); +INSERT INTO note (author_id, title, content) VALUES (2, "Note 2 by Ron", "Content 2 by Ron"); +INSERT INTO note (author_id, title, content) VALUES (2, "Note 3 by Ron", "Content 3 by Ron"); + +INSERT INTO note (author_id, title, content) VALUES (3, "Note A", "Content A by Hermione"); +INSERT INTO note (author_id, title, content) VALUES (3, "Note B", "Content B by Hermione"); +INSERT INTO note (author_id, title, content) VALUES (3, "Note C", "Content C by Hermione"); +/* +SELECT * FROM note; + +id author_id title content created_at +---------- ---------- ---------- ----------- ------------------- +1 1 Note One Content One 2018-09-03 20:07:39 +2 1 Note Two Content Two 2018-09-03 20:09:20 +3 1 Note Three Content Thr 2018-09-03 20:09:42 +4 2 Note 1 by Content 1 b 2018-09-03 20:12:43 +5 2 Note 2 by Content 2 b 2018-09-03 20:13:06 +6 2 Note 3 by Content 3 b 2018-09-03 20:13:27 +7 3 Note A Content A b 2018-09-03 20:14:04 +8 3 Note B Content B b 2018-09-03 20:14:23 +9 3 Note C Content C b 2018-09-03 20:14:38 +*/ \ No newline at end of file From 4f190fabcd5c2a3195da8a683d08fc5c0af89e3b Mon Sep 17 00:00:00 2001 From: JSNavey Date: Mon, 3 Sep 2018 22:45:26 -0400 Subject: [PATCH 8/9] Implement SQL SELECT. Finish Day 2 --- notes.sql | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/notes.sql b/notes.sql index c3816ee..c480b58 100644 --- a/notes.sql +++ b/notes.sql @@ -34,6 +34,7 @@ What columns are needed for the author table? CREATE TABLE note ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, author_id INTEGER REFERENCES author(id), +-- can write FOREIGN KEY(author_id) REFERENCES author(id), title VARCHAR(128) NOT NULL, content TEXT, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP @@ -85,4 +86,61 @@ id author_id title content created_at 7 3 Note A Content A b 2018-09-03 20:14:04 8 3 Note B Content B b 2018-09-03 20:14:23 9 3 Note C Content C b 2018-09-03 20:14:38 -*/ \ No newline at end of file +*/ + + +/********* Select all notes by an author's name *********/ +SELECT title, content FROM note, author +WHERE note.author_id = author.id +AND author.firstname = "Harry" +AND author.lastname = "Potter"; +/* +title content +---------- ----------- +Note One Content One +Note Two Content Two +Note Three Content Thr +*/ + + +/********* Select author for a particular note by note ID *********/ +SELECT firstname, lastname FROM note, author +WHERE note.id = 9 +AND note.author_id = author.id; +/* +firstname lastname +---------- ---------- +Hermione Granger +*/ + + +/********* Select the names of all the authors along with the number of notes they each have ********/ +SELECT firstname, lastname, COUNT(note.id) +FROM note, author +WHERE author.id = note.author_id +GROUP BY author.id; +/* +firstname lastname COUNT(note.id) +---------- ---------- -------------- +Harry Potter 3 +Ron Weasley 3 +Hermione Granger 3 +*/ + + +/********* Delete authors from the author table *********/ +DELETE FROM author +WHERE author.id = 3; +/* +Error: FOREIGN KEY constraint failed + +What happens when you try to delete an author with an existing note? + Foreign key constraint don't allow the data that have the relationship with other tables delete or edit. + +How can you prevent this? + Add `ON DELETE CASCADE` clause to the foreign key for example + `FOREIGN KEY(author_id) REFERENCES author(id) ON DELETE CASCADE`. + + refer to https://www.sqlite.org/foreignkeys.html + A "CASCADE" action propagates the delete or update operation on the parent key to each dependent child key. For an "ON DELETE CASCADE" action, this means that each row in the child table that was associated with the deleted parent row is also deleted. For an "ON UPDATE CASCADE" action, it means that the values stored in each dependent child key are modified to match the new parent key values. +*/ From 4936370ca2d1a916193255d0c3b88459a47fdbed Mon Sep 17 00:00:00 2001 From: JSNavey Date: Tue, 4 Sep 2018 13:58:28 -0400 Subject: [PATCH 9/9] Minor change at COUNT(note.id) --- notes.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notes.sql b/notes.sql index c480b58..f95aa38 100644 --- a/notes.sql +++ b/notes.sql @@ -115,12 +115,12 @@ Hermione Granger /********* Select the names of all the authors along with the number of notes they each have ********/ -SELECT firstname, lastname, COUNT(note.id) +SELECT firstname, lastname, COUNT(*) -- COUNT(note.id) returns the number that NOT NULL. FROM note, author WHERE author.id = note.author_id GROUP BY author.id; /* -firstname lastname COUNT(note.id) +firstname lastname COUNT(*) ---------- ---------- -------------- Harry Potter 3 Ron Weasley 3