diff --git a/Day2.db b/Day2.db new file mode 100644 index 0000000..9f3be5e Binary files /dev/null and b/Day2.db differ diff --git a/README.md b/README.md index a6d7c7c..f1b940b 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,17 @@ When you get to the prompt, you can type `.help` for commands. Some helpful ones: -* `.mode column` turn on column output for `SELECT`. -* `.header on` turn on column headers for `SELECT`. -* `.read filename` execute the SQL in `filename`. -* `.open dbname` re-open a memory-only DB to a persistent file. -* `.quit` exit SQLite. (Note that if you're using a memory-only DB, all +- `.mode column` turn on column output for `SELECT`. +- `.header on` turn on column headers for `SELECT`. +- `.read filename` execute the SQL in `filename`. +- `.open dbname` re-open a memory-only DB to a persistent file. +- `.quit` exit SQLite. (Note that if you're using a memory-only DB, all data is lost at this point.) Another potentially useful third-party tool is [DB Browser for SQLite](https://sqlitebrowser.org/), a GUI-based SQLite viewer and data manipulator that can also run SQL queries. - ## Create a Music Database Make an albums table to hold album information: @@ -58,80 +57,86 @@ CREATE TABLE artist ( ); ``` - ### Exercises, Day 1 Before you begin, look at the queries in `setup.sql` to get a hint as to the column names in the following tables. We'll use `setup.sql` later. -* Create a table called `track` that holds information about a music track. It should contain: - * An autoincrementing `id` - * A title (of type `VARCHAR`, probably) - * A reference to an `id` in table `album` (the album the track is on). This +- Create a table called `track` that holds information about a music track. It should contain: + + - An autoincrementing `id` + - A title (of type `VARCHAR`, probably) + - A reference to an `id` in table `album` (the album the track is on). This should be a _foreign key_. -* Create a table called `artist_album` to connect artists to albums. (Note that +- Create a table called `artist_album` to connect artists to albums. (Note that an artist might have several albums and an album might be created by multiple artists.) - * Use foreign keys for this, as well. - -* Run the queries in the file `setup.sql`. This will populate the tables. - * Fix any errors at this point by making sure your tables are correct. - * `DROP TABLE` can be used to delete a table so you can recreate it with + + - Use foreign keys for this, as well. + +- Run the queries in the file `setup.sql`. This will populate the tables. + + - Fix any errors at this point by making sure your tables are correct. + - `DROP TABLE` can be used to delete a table so you can recreate it with `CREATE TABLE`. -* Write SQL `SELECT` queries that: - * Show all albums. - * 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. +- Write SQL `SELECT` queries that: -* Write SQL `SELECT` queries that: - * Show all track titles from `Super Funky Album`. - * Same query as above, but rename the column from `title` to `Track_Title` in + - Show all albums. + - 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. + +- Write SQL `SELECT` queries that: + + - Show all track titles from `Super Funky Album`. + - Same query as above, but rename the column from `title` to `Track_Title` in the output. - * Select all album titles by `Han Solo`. + - Select all album titles by `Han Solo`. - * Select the average year all albums were released. + - Select the average year all albums were released. - * Select the average year all albums by `Leia and the Ewoks` were released. + - Select the average year all albums by `Leia and the Ewoks` were released. - * Select the number of artists. + - Select the number of artists. - * Select the number of tracks on `Super Dubstep Album`. + - Select the number of tracks on `Super Dubstep Album`. ### 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? -* 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? -* A note should have a foreign key pointing to an author in an author table. +- 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? Write queries that: -* Insert authors to the author table. +- Insert authors to the author table. + +- Insert notes to the note table. -* Insert notes to the note table. +- Select all notes by an author's name. -* Select all notes by an author's name. +- Select author for a particular note by note ID. -* Select author for a particular note by note ID. +- Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) -* Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.) +- Delete authors from the author table. -* 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? - * How can you prevent this? + + - What happens when you try to delete an author with an existing note? + In my case nothing, since I enabled foreign keys before trying this. + I don't think I'd want to prevent this behaviour ^^ + - How can you prevent this? Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. - diff --git a/day1.db b/day1.db new file mode 100644 index 0000000..98dc74b Binary files /dev/null and b/day1.db differ diff --git a/day1.sql b/day1.sql new file mode 100644 index 0000000..b463c50 --- /dev/null +++ b/day1.sql @@ -0,0 +1,68 @@ +-- Show all albums. +SELECT * FROM album; + +-- Show all albums made between 1975 and 1990. +SELECT * FROM album +WHERE (release_year <= 1990) +AND (release_year >= 1975); + +-- 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; + + +-- Show all track titles from `Super Funky Album`. +SELECT track.title, album.title from track +JOIN album ON(album.title = "Super Funky Album"); +--OR +SELECT track.title FROM track,album +WHERE album.title = 'Super Funky Album' +AND album_id = album.id; + +-- 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.title = "Super Funky Album"); +-- OR +SELECT track.title AS Track_Title +FROM track,album +WHERE album.title = 'Super Funky Album' +AND album_id = album.id + +-- Select all album titles by `Han Solo`. + +SELECT album.title AS Album_Title +from album +JOIN artist ON(artist.name = "Han Solo"); +-- OR +SELECT * FROM artist, artist_album +WHERE artist.id = artist_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 artist, album, artist_album +WHERE artist.id = artist_album.artist_id +AND album.id = artist_album.album_id +AND artist.name = 'Leia and the Ewoks'; + +-- Select the number of artists + +SELECT count(id) +FROM artist; + +-- Select the number of tracks on `Super Dubstep Album`. + +SELECT count(track.id) +FROM track, album +WHERE album.title = "Super Dubstep Album"; \ No newline at end of file diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..5be1e5e --- /dev/null +++ b/notes.sql @@ -0,0 +1,58 @@ +-- create an author table with needed data + +create table author ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +name VARCHAR(128) NOT NULL +); + +-- create a notes table including an autopopulated date field +-- and a foreign key referencing the author + +create table note ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +title VARCHER(128) NOT NULL, +body VARCHAR(2056) NOT NULL,m +author_id INTEGER REFERENCES authod(id), +timestamp DATE DEFAULT (datetime('now', 'localtime')) +); + +-- fill db with authors + +INSERT INTO author VALUES ("Peter"); +INSERT INTO author VALUES ("Klaus"); +INSERT INTO author VALUES ("Margareth"); +INSERT INTO author VALUES ("Ursula"); +INSERT INTO author VALUES ("Petra"); + +-- fill db with notes + +INSERT INTO note (title, body, author_id) VALUES("blubb1", "blalbalbab1", 1); +INSERT INTO note (title, body, author_id) VALUES("blubb2", "blalbalbab2", 2); +INSERT INTO note (title, body, author_id) VALUES("blubb3", "blalbalbab3", 3); +INSERT INTO note (title, body, author_id) VALUES("blubb4", "blalbalbab4", 4); +INSERT INTO note (title, body, author_id) VALUES("blubb5", "blalbalbab5", 5); +INSERT INTO note (title, body, author_id) VALUES("blubb6", "blalbalbab5", 1); +INSERT INTO note (title, body, author_id) VALUES("blubb7", "blalbalbab5", 2); + +-- select all notes by author "Peter" + +SELECT name, title, body, timestamp FROM note, author +WHERE author.name = "Peter" +AND author.id = author_id; + +-- select author of note nr 3 + +SELECT author.name FROM author, note +WHERE note.id = 3 and +author.id = author_id; + +-- count notes and group by author + +SELECT name, count(title) FROM note, author +WHERE author.id = author_id +GROUP BY author.name; + +-- delete author + +DELETE FROM author +WHERE name = "Peter"; \ No newline at end of file