Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,37 @@ column names in the following tables. We'll use `setup.sql` later.
* Use foreign keys for this, as well.

* Run the queries in the file `setup.sql`. This will populate the tables.
-> run command .read setup.sql to read queries from file
* 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`.

Good resource for wildcard operators: https://www.w3schools.com/sql/sql_wildcards.asp
* Write SQL `SELECT` queries that:
* Show all albums.
* Show all albums made between 1975 and 1990.

* 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.

* 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 the average year all albums were released.

* Select the average year all albums by `Leia and the Ewoks` were released.

* Select the number of artists.

* Select the number of tracks on `Super Dubstep Album`.


### Exercises, Day 2

Expand Down Expand Up @@ -135,3 +142,4 @@ Write queries that:
Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`)
and query the database as noted above.

.
49 changes: 49 additions & 0 deletions musiq.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--******************************** COMMANDS TO CREATE ALL TABLES FOR MUSIC DB ************************************
CREATE TABLE track (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128), album_id INTEGER REFERENCES album(id));
CREATE TABLE album (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INT);
CREATE TABLE artist (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL);
CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id));
PRAGMA foreign_keys=ON;
.read setup.sql
.headers on
.mode column
--*****************************************************************************************************************************

-- 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; --start & end inclusive
SELECT * FROM album WHERE release_year > 1975 AND release_year < 1990 --start & end exclusive

-- 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';
--resource: http://www1.udel.edu/evelyn/SQL-Class2/SQLclass2_2tblEg.html

-- 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';
--resource: https://www.w3schools.com/sql/sql_alias.asp

-- Select all album titles by `Han Solo`.
SELECT album.title FROM album, artist, artist_album WHERE album.id=artist_album.album_id AND artist.id=artist_album.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 album al, artist ar, artist_album aa WHERE aa.artist_id=ar.id AND aa.album_id=al.id AND ar.name='Leia and the Ewoks';

-- Select the number of artists.
SELECT COUNT(id) FROM artist;
--resource: https://www.lifewire.com/counting-values-with-sql-count-function-1019771

-- Select the number of tracks on `Super Dubstep Album`.
SELECT COUNT(track.id) FROM track , album WHERE track.album_id=album.id AND album.title='Super Dubstep Album';
54 changes: 54 additions & 0 deletions notes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--Create a database for taking notes.

-- ******************************************** COMMANDS TO CREATE ALL TABLES FOR NOTES DB ********************************************
.headers on
.mode column
PRAGMA foreign_keys=ON;
CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL);
CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), content TEXT, author_id INTEGER REFERENCES author(id) ON DELETE CASCADE, created_at DATE DEFAULT datetime('now','localtime'), last_modified DATE DEFAULT NULL);
-- command to test last_modified: UPDATE note SET title = 'Iqra''s Note', last_modified = datetime('now','localtime') WHERE id = 1;
CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), content TEXT, author_id INTEGER REFERENCES author(id) ON DELETE CASCADE ON UPDATE CASCADE, created_at DATE DEFAULT (datetime('now','localtime')), last_modified DATE DEFAULT NULL);
--*************************************************************************************************************************************


-- What are the columns that a note table needs?
id, title, content, author_id, created_at, last_modfied

-- If you have a timestamp field, how do you auto-populate it with the date?
set default

-- A note should have a foreign key pointing to an author in an author table.

-- What columns are needed for the author table?
id, name

-- Write queries that:

-- Insert authors to the author table.
see setupNotesDB.sql

-- Insert notes to the note table.
see setupNotesDB.sql
-- Select all notes by an author's name.
SELECT note.title FROM note, author WHERE note.author_id=author.id AND author.name = 'Author1';

-- Select author for a particular note by note ID.
SELECT author.name FROM author, note WHERE author.id=note.author_id AND note.id=10;

-- Select the names of all the authors along with the number of notes they each have. (Hint: `GROUP BY`.)
SELECT COUNT(note.author_id), author.name FROM note, author WHERE note.author_id=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.
DELETE FROM author WHERE author.id=3;

-- What happens when you try to delete an author with an existing note?
Error: FOREIGN KEY constraint failed
-- How can you prevent this?
By adding ON DELETE CASCADE in the foreign key definition.
--resource: https://stackoverflow.com/questions/15443913/sqlite3-foreign-key-constraint-failed


-- Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`)
-- and query the database as noted above.
31 changes: 31 additions & 0 deletions setupNotesDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
INSERT INTO author (name) VALUES ('Author1');
INSERT INTO author (name) VALUES ('Author2');
INSERT INTO author (name) VALUES ('Author3');
INSERT INTO author (name) VALUES ('Author4');
INSERT INTO author (name) VALUES ('Author5');


INSERT INTO note (title, content, author_id) VALUES ('Note1', 'First note''s content', 4);
INSERT INTO note (title, content, author_id) VALUES ('Note2', 'Second note''s content', 1);
INSERT INTO note (title, content, author_id) VALUES ('Note3', 'Third note''s content', 4);
INSERT INTO note (title, content, author_id) VALUES ('Note4', 'Fourth note''s content', 3);
INSERT INTO note (title, content, author_id) VALUES ('Note5', 'Fifth note''s content', 5);
INSERT INTO note (title, content, author_id) VALUES ('Note6', 'Sixth note''s content', 2);
INSERT INTO note (title, content, author_id) VALUES ('Note7', 'Seventh note''s content', 2);
INSERT INTO note (title, content, author_id) VALUES ('Note8', 'Eighth note''s content', 4);
INSERT INTO note (title, content, author_id) VALUES ('Note9', 'Ninth note''s content', 2);
INSERT INTO note (title, content, author_id) VALUES ('Note10', 'Tenth note''s content', 5);
INSERT INTO note (title, content, author_id) VALUES ('Note11', 'Eleventh note''s content', 1);
INSERT INTO note (title, content, author_id) VALUES ('Note12', 'Twelveth note''s content', 4);
INSERT INTO note (title, content, author_id) VALUES ('Note13', 'Thirteenth note''s content', 3);
INSERT INTO note (title, content, author_id) VALUES ('Note14', 'Fourteenth note''s content', 4);
INSERT INTO note (title, content, author_id) VALUES ('Note15', 'Fifteenth note''s content', 5);

-- HOW MANY NOTES DOES EACH AUTHOR HAVE --
/*
Author1: 2
Author2: 3
Author3: 2
Author4: 5
Author5: 3
*/