diff --git a/README.md b/README.md index a6d7c7c..0242145 100644 --- a/README.md +++ b/README.md @@ -82,24 +82,39 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. + > SELECT * FROM albums; + * 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`. + > SELECT track.title FROM track, album WHERE track.album_id = album.id AND album.title = "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, album WHERE track.album_id = album.id AND album.title = "Super Funky Album"; * Select all album titles by `Han Solo`. + > SELECT album.title FROM album, artist, artist_album WHERE artist_album.artist_id = artist.id AND artist_album.album_id = album.id AND artist.name = "Han Solo"; * 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_album.artist_id = artist.id AND artist_album.album_id = album.id AND artist.name = "Leia and the Ewoks"; * Select the number of artists. + > SELECT COUNT(*) FROM artist; * Select the number of tracks on `Super Dubstep Album`. + > SELECT COUNT(*) FROM track, album WHERE track.album_id = album.id AND album.title = "Super Dubstep Album"; ### Exercises, Day 2 @@ -120,17 +135,25 @@ Write queries that: * Insert notes to the note table. * Select all notes by an author's name. + > SELECT * FROM note, author WHERE note.author_id = author.id AND author.name = "Braden"; * Select author for a particular note by note ID. + > SELECT author.name FROM author, note WHERE note.author_id = author.id AND note.id = 1; * Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) + > SELECT COUNT(*), name FROM note, author WHERE note.author_id = author.id GROUP BY author_id; * Delete authors from the author table. + >> DELETE FROM author WHERE author.name = "Braden"; + > 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? + > `Error: FOREIGN KEY constraint failed` + * How can you prevent this? + > Add `ON DELETE CASCADE` to the end of the author_id reference in the note table Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. diff --git a/musicdatabase.db b/musicdatabase.db new file mode 100644 index 0000000..e047ece Binary files /dev/null and b/musicdatabase.db differ diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..8c9254d --- /dev/null +++ b/notes.sql @@ -0,0 +1,25 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE note ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128), + content TEXT, + author_id INTEGER REFERENCES author(id) ON DELETE CASCADE, + created_at TIMESTAMP +); + +CREATE TABLE author ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(32) NOT NULL +); + +INSERT INTO author (name) VALUES ("Braden"); +INSERT INTO author (name) VALUES ("Beej"); +INSERT INTO author (name) VALUES ("Maverick"); + +INSERT INTO note (title, content, author_id, created_at) VALUES ("bradenTest1", "Here's a test for a note by Braden", 1, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("beejTest1", "Here's a test for a note by Beej", 2, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("maverickTest1", "Here's a test for a note by Maverick", 3, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("beejTest2", "Here's another by Beej", 2, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("maverickTest2", "Here's another by Maverick", 3, CURRENT_TIMESTAMP); +INSERT INTO note (title, content, author_id, created_at) VALUES ("bradenTest2", "Here's another by Braden", 1, CURRENT_TIMESTAMP); \ No newline at end of file diff --git a/notesdatabase.db b/notesdatabase.db new file mode 100644 index 0000000..8101c69 Binary files /dev/null and b/notesdatabase.db differ