From 490382d254675ca3c6b6aefd293090221ceea947 Mon Sep 17 00:00:00 2001 From: "A.D.Faris" Date: Tue, 4 Sep 2018 09:43:54 -0400 Subject: [PATCH 1/3] ADFaris SQL --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a6d7c7c..f57088c 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,12 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. + ``` sqlite> select * from album; +1|Super Awesome Album|1990 +2|Super Funky Album| +3|Super Disco Album|1978 +4|Super Hairband Album|1984 +5|Super Dubstep Album|``` * 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. From d59e85fb9507302be9d0d17f9b9fa42018fdae07 Mon Sep 17 00:00:00 2001 From: "A.D.Faris" Date: Tue, 4 Sep 2018 16:55:14 -0400 Subject: [PATCH 2/3] ADFaris notes.sql --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- notes.sql | 23 +++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 notes.sql diff --git a/README.md b/README.md index f57088c..023ded6 100644 --- a/README.md +++ b/README.md @@ -88,44 +88,87 @@ column names in the following tables. We'll use `setup.sql` later. 3|Super Disco Album|1978 4|Super Hairband Album|1984 5|Super Dubstep Album|``` + * Show all albums made between 1975 and 1990. + ``` sqlite> select * from album where release_year between 1975 and 1990; +1|Super Awesome Album|1990 +3|Super Disco Album|1978 +4|Super Hairband Album|1984``` + * Show all albums whose names start with `Super D`. +```sqlite> select * from album where title like '%Super D%'; +3|Super Disco Album|1978 +5|Super Dubstep Album| ``` + * Show all albums that have no release year. +```sqlite> select * from album where release_year is NULL; +2|Super Funky Album| +5|Super Dubstep Album| ``` * Write SQL `SELECT` queries that: * Show all track titles from `Super Funky Album`. + + ```sqlite> select title from track where album_id = 2; +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. + ```sqlite> select title AS 'Track_Title' from track where album_id = 2; +Super Funky Track 1 +Super Funky Track 2 +Super Funky Track 3 +Super Funky Track 4 ``` + * Select all album titles by `Han Solo`. + ``` sqlite> select title from album where title = 'Han Solo';``` * Select the average year all albums were released. + ```sqlite> select avg(release_year) as 'Average year' from album; +1984.0 ``` * Select the average year all albums by `Leia and the Ewoks` were released. + ```sqlite> select AVG(release_year), A.name from album as AL + ...> JOIN artist_album as AA ON AA.album_id = AL.album_id + ...> JOIN artist as A ON A.artist_id = AA.artist_id + ...> where A.name = 'Leia and the Ewoks'; +AVG(release_year)|name +1990.0|Leia and the Ewoks ``` * Select the number of artists. + ``` sqlite> select count(artist_id) from artist; +3``` * Select the number of tracks on `Super Dubstep Album`. + ``` sqlite> select count(*) from album as A inner join track as T on A.album_id = T.album_id where T.title like + ...> '%Super Dubstep%'; +5``` ### 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? ID, title, content, user_id -* 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? timestamp DATE DEFAULT (datetime('now','localtime')) * 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? ID, username, created_at Write queries that: * Insert authors to the author table. +sqlite> INSERT INTO user (username) VALUES ("adfaris"); * Insert notes to the note table. +```sqlite> INSERT INTO note (title, content) VALUES ("Note 1", "The content of note 1 goes in here"); ``` * Select all notes by an author's name. +select * from note N join user U on U.user_id = N.user_id where U.username = 'adfaris; * Select author for a particular note by note ID. @@ -136,7 +179,9 @@ Write queries that: > 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? + ERROR: FOREIGN Key constraint failed * How can you prevent this? + CASCADE DELETE Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..d2db99a --- /dev/null +++ b/notes.sql @@ -0,0 +1,23 @@ +create table note( + id integer primary key autoincrement, + title varchar(128), + content(varchar(256), + user_id NOT NULL references user(user_id)); + + create table user( + user_id integer primary key autoincrement, + username varchar(128), + created_at timestamp DATE DEFAULT (datetime('now','localtime'))); + + INSERT INTO note (title, content) VALUES ("Note 1", "The content of note 1 goes in here", 1); + insert into note(title, content, user_id) values("Note2", "The content of note 2 goes in here", 2); + insert into note(title, content, user_id) values("Note3", "The content of note 3 goes in here", 3); + insert into note(title, content, user_id) values("Note4", "The content of note 4 goes in here", 1); + insert into note(title, content, user_id) values("Note5", "The content of note 5 goes in here", 1); + insert into note(title, content, user_id) values("Note6", "The content of note 6 goes in here", 4); + insert into note(title, content, user_id) values("Note7", "The content of note 7 goes in here", 4); + insert into note(title, content, user_id) values("Note8", "The content of note 8 goes in here", 4); + insert into note(title, content, user_id) values("Note9", "The content of note 9 goes in here", 3); + insert into note(title, content, user_id) values("Note10", "The content of note 10 goes in here", 3); + insert into note(title, content, user_id) values("Note11", "The content of note 11 goes in here", 3); + insert into note(title, content, user_id) values("Note12", "The content of note 12 goes in here", 3); \ No newline at end of file From d5707a44ed3a26238fecfdc2ab8fb6063e6ad0b1 Mon Sep 17 00:00:00 2001 From: "A.D.Faris" Date: Tue, 4 Sep 2018 16:55:41 -0400 Subject: [PATCH 3/3] MVP Done --- notes.sql | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/notes.sql b/notes.sql index d2db99a..7126e7d 100644 --- a/notes.sql +++ b/notes.sql @@ -20,4 +20,11 @@ create table note( insert into note(title, content, user_id) values("Note9", "The content of note 9 goes in here", 3); insert into note(title, content, user_id) values("Note10", "The content of note 10 goes in here", 3); insert into note(title, content, user_id) values("Note11", "The content of note 11 goes in here", 3); - insert into note(title, content, user_id) values("Note12", "The content of note 12 goes in here", 3); \ No newline at end of file + insert into note(title, content, user_id) values("Note12", "The content of note 12 goes in here", 3); + + + select * from note N join user U on U.user_id = N.user_id where U.username = 'adfaris; + + select user_id, id, title, content from note where user_id = 1; + + select username, count(id) from user u join note n ON u.user_id = n.user_id group by username; \ No newline at end of file