diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/22music.db b/22music.db new file mode 100644 index 0000000..1ef5c09 Binary files /dev/null and b/22music.db differ diff --git a/22notes.db b/22notes.db new file mode 100644 index 0000000..4bea97d Binary files /dev/null and b/22notes.db differ diff --git a/music.sql b/music.sql new file mode 100644 index 0000000..845bf3d --- /dev/null +++ b/music.sql @@ -0,0 +1,135 @@ +/*Launch into SQL database with specified DB file*/ +sqlite3 22music.db + +/*Foreign Key constraint on*/ +PRAGMA foreign_keys = ON; + +/* turn on column output to make it look pretty*/ +.mode COLUMN + +/*turns on headers so you can see columm header labels*/ +.header ON + + /*Provided Tables*/ +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); + +/*Make track table and artist_album m:m table*/ +CREATE TABLE track(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, album_id INT REFERENCES album(id)); +CREATE TABLE artist_album(artist_id INT REFERENCES artist(id), album_id INT REFERENCES album(id)); + +/* Load Data FROM setup.sql */ +.read setup.sql + +/* Write 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 > 1975 AND release_year < 1990; +/* +id title release_year +---------- ----------------- ------------ +3 Super Disco Album 1978 +4 Super Hairband Al 1984 + */ + +/* Show all albums whose names start with Super D */ +SELECT * FROM album WHERE album.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 album.release_year IS NULL; +/* +---------- ----------------- ------------ +2 Super Funky Album +5 Super Dubstep Alb + */ + +/* HARDER QUERIES */ + +/* Show all track titles FROM Super Funky Album */ +SELECT * FROM track WHERE track.title LIKE '%Super Funky%'; /*isn't what we're looking for.*/ + +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 as above, but rename column 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*/ +/*give me album titles where the artist is Han Solo, then using name you can get artist id then go to relational table and get the albums with that artist's id */ +SELECT album.title FROM album, artist, artist_album WHERE artist.name = 'Han Solo' AND artist.id = artist_album.artist_id AND artist_album.album_id = album.id; +/* +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 album, artist, artist_album WHERE artist.id = artist_album.artist_id AND album.id = artist_album.album_id AND artist.name = 'Leia and the Ewoks'; +/*FROM lecture*/ +SELECT AVG(release_year) FROM album JOIN artist_album ON(album.id=artist_album.album_id) JOIN artist ON(artist.id = artist_album.artist_id) WHERE 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.id) FROM track, album WHERE track.album_id = album.id AND album.title = 'Super Dubstep Album'; + +/* +COUNT(track.id) +--------------- +5 +*/ + +.quit + +/*DONE!!!*/ \ No newline at end of file diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..58393f3 --- /dev/null +++ b/notes.sql @@ -0,0 +1,64 @@ +sqlite3 22notes.db +PRAGMA foreign_keys = ON; +.mode column +.header ON + +/* Create database for taking notes */ + +/*Make Author Table*/ +CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(256) NOT NULL, last_name VARCHAR(256) NOT NULL); + +/*Make Note Table*/ +CREATE TABLE note ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + content VARCHAR(1024) NOT NULL, + author_id INT REFERENCES author(id), + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + +/*Write Query to Insert Author*/ +INSERT INTO author (first_name, last_name) VALUES ("AF", "AL"); +INSERT INTO author VALUES (2, "BF", "BL"); +INSERT INTO author (first_name, last_name) VALUES ("CF", "CL"); +INSERT INTO author (first_name, last_name) VALUES ("DF", "DL"); +INSERT INTO author VALUES (6, "EF", "EL"); +INSERT INTO author (first_name, last_name) VALUES ("FF", "FL"); +INSERT INTO author VALUES (5, "5F", "5L"); +INSERT INTO author (first_name, last_name) VALUES ("GF", "GL"); + +/*Write Query to insert note*/ +INSERT INTO note (title, content, author_id) VALUES ('ANoteTitle', 'ANoteContent', 1); +INSERT INTO note (title, content, author_id) VALUES ('BNoteTitle', 'BNoteContent', 2); +INSERT INTO note (title, content, author_id) VALUES ('CNoteTitle', 'CNoteContent', 3); +INSERT INTO note (title, content, author_id) VALUES ('DNoteTitle', 'DNoteContent', 4); +INSERT INTO note (title, content, author_id) VALUES ('ENoteTitle', 'ENoteContent', 5); +INSERT INTO note (title, content, author_id) VALUES ('AANoteTitle', 'AANoteContent', 1); + +/*Select All notes by author name*/ +SELECT note.title as Title, note.content as Content from note, author WHERE author.id = note.author_id and author.name = 'AF'; + +/*Select author by note.id*/ +SELECT author.first_name, author.last_name from author, note WHERE note.id = 1 AND author.id = note.author_id; + +/*Select the names of all the authors along with the number o fnotes they each have (hint: GROUP BY)*/ +SELECT first_name, last_name, COUNT(*) FROM author, note WHERE author.id = note.author_id GROUP BY author.id; + +/*Delete Authors from the author table*/ +DELETE FROM author WHERE id=1; + +/*What happens whtn you try to delete an author with an existing note?*/ +Error: FOREIGN KEY constraint failed + + +/*How can you prevent this?*/ +PRAGMA foreign_keys = OFF; to turn off protection + +You can also delete notes associated with author first and then author - You should implement it as a transaction +begin transaction; +delete from notes where author_id is 1; +delete from author where id is 1; +commit; + +You can also apply cascade to foreign key: author_id INT REFERENCES author(id) ON DELETE CASCADE \ No newline at end of file