From 5856ab651cc07caca647d33fff982a2aa43c2d96 Mon Sep 17 00:00:00 2001 From: masontmorrow Date: Tue, 4 Sep 2018 23:19:32 +0200 Subject: [PATCH 1/4] Initial commit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a6d7c7c..aa96b11 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SQL +# SQL See [the Lambda page on Relational Databases](https://github.com/LambdaSchool/Relational-Databases) for more From 36959d111a7935b4dec6c25f0e2223ded5dfaaf3 Mon Sep 17 00:00:00 2001 From: masontmorrow Date: Thu, 6 Sep 2018 01:13:15 +0200 Subject: [PATCH 2/4] Add CREATEs & SQL queries --- .gitignore | 1 + README.md | 9 +++++++-- setup.sql | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a404e24 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +mydb.db diff --git a/README.md b/README.md index aa96b11..e606b7c 100644 --- a/README.md +++ b/README.md @@ -82,17 +82,22 @@ column names in the following tables. We'll use `setup.sql` later. * 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 BETWEEN 1975 AND 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`. + `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. - + `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`. - + * Select the average year all albums were released. * Select the average year all albums by `Leia and the Ewoks` were released. diff --git a/setup.sql b/setup.sql index 94bf2cf..4b7b1ec 100644 --- a/setup.sql +++ b/setup.sql @@ -1,3 +1,30 @@ +CREATE TABLE album ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + release_year INTEGER +); + +CREATE TABLE artist ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(128) NOT NULL +); + +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 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) +); + + 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); From c3b2aa1181dc31ba86b509ff69f68fb2b18a9529 Mon Sep 17 00:00:00 2001 From: masontmorrow Date: Thu, 6 Sep 2018 23:22:30 +0200 Subject: [PATCH 3/4] Add Day 1 Exercises --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e606b7c..d4d592a 100644 --- a/README.md +++ b/README.md @@ -97,14 +97,15 @@ column names in the following tables. We'll use `setup.sql` later. the output. `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`. - + `SELECT album.title FROM album, artist, artist_album WHERE artist.name IS 'Han Solo' AND artist.id IS artist_album.artist_id AND album.id IS artist_album.album_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 AVG(release_year) FROM album, artist, artist_album WHERE artist.name IS 'Leia and the Ewoks' AND artist.id IS artist_album.artist_id AND album.id IS artist_album.album_id;` * Select the number of artists. - + `SELECT COUNT(id) FROM artist;` * Select the number of tracks on `Super Dubstep Album`. + `SELECT COUNT(track.title) FROM album, track WHERE album.id IS track.album_id AND album.title IS 'Super Dubstep Album';` ### Exercises, Day 2 From 20d213b107284d0b3ebc129b01d302077154124b Mon Sep 17 00:00:00 2001 From: masontmorrow Date: Fri, 7 Sep 2018 01:09:42 +0200 Subject: [PATCH 4/4] Add Day 2 MVP --- .gitignore | 1 + README.md | 21 +++++++++++++++++---- notes.sql | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 notes.sql diff --git a/.gitignore b/.gitignore index a404e24..8e1cf74 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ mydb.db +notes.db diff --git a/README.md b/README.md index d4d592a..b582cdb 100644 --- a/README.md +++ b/README.md @@ -112,31 +112,44 @@ column names in the following tables. We'll use `setup.sql` later. Create a database for taking notes. * What are the columns that a note table needs? + 1. note id + 2. title + 3. body + 4. author id reference + 5. timestamp * 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. * What columns are needed for the author table? + 1. author id + 2. name + Write queries that: * Insert authors to the author table. - + in: `notes.sql` * Insert notes to the note table. - + in: `notes.sql` * Select all notes by an author's name. - + `SELECT note.id, note.title FROM author, note WHERE note.author_id IS author.id AND author.name IS 'Proust';` * Select author for a particular note by note ID. - + `SELECT author.name FROM author, note WHERE note.author_id IS author.id AND note.id IS 2;` * Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) + `SELECT author.name, COUNT(note.id) FROM author, note WHERE note.author_id IS 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? + FOREIGN KEY constraint failed * How can you prevent this? + Use `ON DELETE CASCADE`, which deletes all related records with the matching foreign key. 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..62564d2 --- /dev/null +++ b/notes.sql @@ -0,0 +1,24 @@ +CREATE TABLE author ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(128) NOT NULL +); + +CREATE TABLE note ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + body TEXT NOT NULL, + created DATETIME DEFAULT CURRENT_TIMESTAMP, + author_id INT REFERENCES author(id) +); + +INSERT INTO author (name) VALUES ('Proust'); +INSERT INTO author (name) VALUES ('Dostoevsky'); +INSERT INTO author (name) VALUES ('Twain'); +INSERT INTO author (name) VALUES ('Plath'); +INSERT INTO author (name) VALUES ('Morrison'); + +INSERT INTO note (title, body, author_id) VALUES ('Le Temps Perdu', 'In search of lost time', 1); +INSERT INTO note (title, body, author_id) VALUES ('Crime and Punishment', 'Raskolnikova', 2); +INSERT INTO note (title, body, author_id) VALUES ('Toulumne County', 'Yosemite', 3); +INSERT INTO note (title, body, author_id) VALUES ('The Bell Jar', 'Fig Trees', 4); +INSERT INTO note (title, body, author_id) VALUES ('Beloved', 'Oprah', 5); \ No newline at end of file