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
208 changes: 208 additions & 0 deletions music.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
-- sqlite3 musicdb.db -- Specify a persistent DB file

-- PRAGMA foreign_keys = ON; -- The library is compiled with foreign keys enabled.
-- .mode COLUMN
-- .header ON


/********* CREATE TABLES **********/
CREATE TABLE album (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, release_year INTEGER);
CREATE TABLE artist (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL);
CREATE TABLE track (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, album_id INTEGER REFERENCES album(id));
CREATE TABLE artist_album (artist_id INTEGER REFERENCES artist(id), album_id INTEGER REFERENCES album(id));


/********* Run the queries in the file `setup.sql` to populate the tables **********/
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);
INSERT INTO album (title, release_year) VALUES ("Super Hairband Album", 1984);
INSERT INTO album (title) VALUES ("Super Dubstep Album");

INSERT INTO artist (name) VALUES ("Luke and the Droidtones");
INSERT INTO artist (name) VALUES ("Leia and the Ewoks");
INSERT INTO artist (name) VALUES ("Han Solo");

INSERT INTO artist_album (artist_id, album_id) VALUES (1, 5);
INSERT INTO artist_album (artist_id, album_id) VALUES (1, 2);
INSERT INTO artist_album (artist_id, album_id) VALUES (2, 1);
INSERT INTO artist_album (artist_id, album_id) VALUES (2, 2);
INSERT INTO artist_album (artist_id, album_id) VALUES (3, 3);
INSERT INTO artist_album (artist_id, album_id) VALUES (3, 4);

INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 1", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 2", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 3", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 4", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 5", 1);

INSERT INTO track (title, album_id) VALUES ("Super Funky Track 1", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 2", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 3", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 4", 2);

INSERT INTO track (title, album_id) VALUES ("Super Disco Track 1", 3);
INSERT INTO track (title, album_id) VALUES ("Super Disco Track 2", 3);
INSERT INTO track (title, album_id) VALUES ("Super Disco Track 3", 3);

INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 1", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 2", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 3", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 4", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 5", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 6", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 7", 4);

INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 1", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 2", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 3", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 4", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 5", 5);


/********* SELECT queries *********/

/********* Show all albums *********/

SELECT * FROM album;
/*
id title release_year
---------- ------------------- ------------
1 Super Awesome Album 1990
2 Super Funky Album
3 Super Disco Album 1978
4 Super Hairband Albu 1984
5 Super Dubstep Album
*/


/******** Show all albums made between 1975 and 1990 *********/

SELECT * FROM album WHERE release_year BETWEEN 1975 AND 1990; -- inclusive begin and end value
SELECT * FROM album WHERE release_year >= 1975 AND release_year <= 1990; -- BETWEEN is shorthand for >= AND <=
/*
id title release_year
---------- ------------------- ------------
1 Super Awesome Album 1990
3 Super Disco Album 1978
4 Super Hairband Albu 1984
*/


/********* Show all albums whose names start with Super D *********/

SELECT * FROM album WHERE title LIKE 'Super D%';
/*
id title release_year
---------- ----------------- ------------
3 Super Disco Album 1978
5 Super Dubstep Alb
*/


/********* Show all albums that have no release year *********/

SELECT * FROM album WHERE release_year IS NULL;
/*
id title release_year
---------- ----------------- ------------
2 Super Funky Album
5 Super Dubstep Alb
*/


/********* 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';
/*
title
-------------------
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 *********/

SELECT track.title
AS Track_Title
FROM track, album
WHERE track.album_id = album.id
AND album.title = 'Super Funky Album';
/*
Track_Title
-------------------
Super Funky Track 1
Super Funky Track 2
Super Funky Track 3
Super Funky Track 4
*/


/********* Select all album titles by Han Solo *********/

SELECT album.title
FROM artist, album, artist_album
WHERE artist_album.album_id = album.id
AND artist_album.artist_id = artist.id
AND artist.name = 'Han Solo';
/*
title
-----------------
Super Disco Album
Super Hairband Al
*/


/********* Select the average year all albums were released *********/

SELECT AVG(release_year) FROM album;
/*
AVG(release_year)
-----------------
1984.0
*/


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

SELECT AVG(release_year)
FROM artist, album, artist_album
WHERE artist_album.album_id = album.id
AND artist_album.artist_id = artist.id
AND artist.name = 'Leia and the Ewoks';
/*
AVG(release_year)
-----------------
1990.0
*/


/********* Select the number of artists *********/

SELECT COUNT(id) FROM artist;
/*
COUNT(id)
----------
3
*/


/********* Select the number of tracks on Super Dubstep Album *********/

SELECT COUNT(track.title)
FROM track, album
WHERE track.album_id = album.id
AND album.title = 'Super Dubstep Album';
/*
COUNT(track.title)
------------------
5
*/


Binary file added musicdb.db
Binary file not shown.
Binary file added notedatabase.db
Binary file not shown.
146 changes: 146 additions & 0 deletions notes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Create a database for taking notes.

What are the columns that a note table needs?
- An autoincrement id
- A title (of type VARCHAR(128), should be enough??)
- A content (of type TEXT, with 65,535 characters - 64 KB should be sufficiently capable of handling typical long-form text content.)
- Author_id is reference to an id in author table (foreign key).
- Created_at as timestamp when a note is created.

If you have a timestamp field, how do you auto-populate it with the date?
- DATETIME NOT NULL DEFAULT(GETDATE())
---> get an error "unknown function: GETDATE()"

- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
---> generate column and print date and time stamp as I wish.
`https://www.w3schools.com/sql/sql_ref_mysql.asp`

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

What columns are needed for the author table?
- An autoincrement id
- Author's first name and last name
*/

/********* set up database *********/
-- sqlite3 notedatabase.db
-- PRAGMA foreign_keys = ON;
-- .mode column
-- .header on


/********* CREATE TABLES ********/
CREATE TABLE note (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
author_id INTEGER REFERENCES author(id),
-- can write FOREIGN KEY(author_id) REFERENCES author(id),
title VARCHAR(128) NOT NULL,
content TEXT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE author (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
firstname VARCHAR(128),
lastname VARCHAR(128)
);


/********* POPULATE TABLES ********/
INSERT INTO author (firstname, lastname) VALUES ("Harry", "Potter");
INSERT INTO author (firstname, lastname) VALUES ("Ron", "Weasley");
INSERT INTO author (firstname, lastname) VALUES ("Hermione", "Granger");
/*
SELECT * FROM author;

id firstname lastname
---------- ---------- ----------
1 Harry Potter
2 Ron Weasley
3 Hermione Granger
*/

INSERT INTO note (author_id, title, content) VALUES (1, "Note One", "Content One");
INSERT INTO note (author_id, title, content) VALUES (1, "Note Two", "Content Two");
INSERT INTO note (author_id, title, content) VALUES (1, "Note Three", "Content Three");

INSERT INTO note (author_id, title, content) VALUES (2, "Note 1 by Ron", "Content 1 by Ron");
INSERT INTO note (author_id, title, content) VALUES (2, "Note 2 by Ron", "Content 2 by Ron");
INSERT INTO note (author_id, title, content) VALUES (2, "Note 3 by Ron", "Content 3 by Ron");

INSERT INTO note (author_id, title, content) VALUES (3, "Note A", "Content A by Hermione");
INSERT INTO note (author_id, title, content) VALUES (3, "Note B", "Content B by Hermione");
INSERT INTO note (author_id, title, content) VALUES (3, "Note C", "Content C by Hermione");
/*
SELECT * FROM note;

id author_id title content created_at
---------- ---------- ---------- ----------- -------------------
1 1 Note One Content One 2018-09-03 20:07:39
2 1 Note Two Content Two 2018-09-03 20:09:20
3 1 Note Three Content Thr 2018-09-03 20:09:42
4 2 Note 1 by Content 1 b 2018-09-03 20:12:43
5 2 Note 2 by Content 2 b 2018-09-03 20:13:06
6 2 Note 3 by Content 3 b 2018-09-03 20:13:27
7 3 Note A Content A b 2018-09-03 20:14:04
8 3 Note B Content B b 2018-09-03 20:14:23
9 3 Note C Content C b 2018-09-03 20:14:38
*/


/********* Select all notes by an author's name *********/
SELECT title, content FROM note, author
WHERE note.author_id = author.id
AND author.firstname = "Harry"
AND author.lastname = "Potter";
/*
title content
---------- -----------
Note One Content One
Note Two Content Two
Note Three Content Thr
*/


/********* Select author for a particular note by note ID *********/
SELECT firstname, lastname FROM note, author
WHERE note.id = 9
AND note.author_id = author.id;
/*
firstname lastname
---------- ----------
Hermione Granger
*/


/********* Select the names of all the authors along with the number of notes they each have ********/
SELECT firstname, lastname, COUNT(*) -- COUNT(note.id) returns the number that NOT NULL.
FROM note, author
WHERE author.id = note.author_id
GROUP BY author.id;
/*
firstname lastname COUNT(*)
---------- ---------- --------------
Harry Potter 3
Ron Weasley 3
Hermione Granger 3
*/


/********* Delete authors from the author table *********/
DELETE FROM author
WHERE author.id = 3;
/*
Error: FOREIGN KEY constraint failed

What happens when you try to delete an author with an existing note?
Foreign key constraint don't allow the data that have the relationship with other tables delete or edit.

How can you prevent this?
Add `ON DELETE CASCADE` clause to the foreign key for example
`FOREIGN KEY(author_id) REFERENCES author(id) ON DELETE CASCADE`.

refer to https://www.sqlite.org/foreignkeys.html
A "CASCADE" action propagates the delete or update operation on the parent key to each dependent child key. For an "ON DELETE CASCADE" action, this means that each row in the child table that was associated with the deleted parent row is also deleted. For an "ON UPDATE CASCADE" action, it means that the values stored in each dependent child key are modified to match the new parent key values.
*/