diff --git a/mydatabase.db b/mydatabase.db new file mode 100644 index 0000000..0da5699 Binary files /dev/null and b/mydatabase.db differ diff --git a/notes.db b/notes.db new file mode 100644 index 0000000..291b46e Binary files /dev/null and b/notes.db differ diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..1cd24a3 --- /dev/null +++ b/notes.sql @@ -0,0 +1,52 @@ + +/* Create database for notes */ +CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(200) NOT NULL, last_name VARCHAR(255) NOT NULL); +CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(200) NOT NULL, content VARCHAR(255) NOT NULL, author_id INT REFERENCES author(id), created_at datetime default current_timestamp, last_modified datetime default current_timestamp); + + +/* add authors to database */ +INSERT INTO author (first_name, last_name) VALUES ("Anne", "Courtney"); +INSERT INTO author (first_name, last_name) VALUES ("Ryan", "Courtney"); +INSERT INTO author (first_name, last_name) VALUES ("Coraline", "Courtney"); + +/* Add notes to database */ +INSERT into note (title, content, author_id) values ("Test 1", "testing testing", 1); +INSERT into note (title, content, author_id) values ("Test 2", "testing again", 1); +INSERT into note (title, content, author_id) values ("Test 3", "final test i promise", 2); +INSERT into note (title, content, author_id) values ("Test 3", "final test i promise", 3); + + +/* Select all notes by an author's name */ +SELECT note.title, note.content FROM note, author WHERE note.author_id = author.id AND author.first_name = "Anne"; +SELECT note.title, note.content FROM note, author WHERE note.author_id = author.id AND author.first_name = "Ryan"; +SELECT note.title, note.content FROM note, author WHERE note.author_id = author.id AND author.first_name = "Coraline"; + + +/* Select author for a particular note by note ID */ +SELECT author.first_name, author.last_name FROM note, author WHERE note.author_id = author.id AND note.id = 1; +SELECT author.first_name, author.last_name FROM note, author WHERE note.author_id = author.id AND note.id = 2; +SELECT author.first_name, author.last_name FROM note, author WHERE note.author_id = author.id AND note.id = 3; + + +/* Select names of all the authors along with the number of notes they have */ +SELECT author.first_name, author.last_name, COUNT(author_id) FROM author, note WHERE note.author_id = author.id GROUP BY author.last_name; +SELECT author.first_name, author.last_name, COUNT(author_id) FROM author, note WHERE note.author_id = author.id GROUP BY author.first_name; + +/* Delete authors from the author table */ +DELETE FROM author WHERE id=3; + +/* What happens when you try to delete an author with an existing note? */ +/* Foreign key constraint error - it does not allow you to delete. */ +/* Solution is changing the delete cascade */ + +/* Step one for me was opening notes.db so that my foreign_key constraints were turned off - .open notes.db */ +begin transaction; +alter table note rename to _note_old_; +CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(200) NOT NULL, content VARCHAR(255) NOT NULL, author_id INT REFERENCES author(id) ON DELETE CASCADE, created_at datetime default current_timestamp, last_modified datetime default current_timestamp); +INSERT INTO note SELECT * from _note_old_; +commit; +.quit + +/* sqlite3 notes.db */ +drop table _note_old_; +DELETE FROM author where author.id = 4; \ No newline at end of file diff --git a/track.sql b/track.sql new file mode 100644 index 0000000..cd0a6ea --- /dev/null +++ b/track.sql @@ -0,0 +1,103 @@ +/*specify persistent DB file*/ +-- sqlite3 mydatabase.db + +-- PRAGMA foreign_keys = ON +-- .mode COLUMNS +-- .header ON + +/*CREATE 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, title VARCHAR(128) NOT NULL); +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)); + +/*Populate tables*/ +.read setup.sql + +/* 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 + +/* Showing all albums made between 1975 and 1990 */ +SELECT * from album WHERE release_year < 1990 AND release_year > 1975; +-- id title release_year +-- ---------- ----------------- ------------ +-- 3 Super Disco Album 1978 +-- 4 Super Hairband Al 1984 +-- 8 Super Disco Album 1978 +-- 9 Super Hairband Al 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 +-- 8 Super Disco Album 1978 + +/* 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 +-- 7 Super Funky Album + +/* 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 + +/* Show all track titles from Super Funky Album and change title to Track_Title in output */ +SELECT track.title AS Track_Title FROM track, album WHERE track.album_id = album.id AND album.title = 'Super Funky Album'; +/* output: + Track_Title + ------------------- + Super Funky Track 1 + Super Funky Track 2 + Super Funky Track 3 + +/* Select all album titles by Han Solo */ +SELECT album.title FROM album, artist, artist_album WHERE artist.id = artist_album.artist_id AND album.id = artist_album.album_id AND artist.name = 'Han Solo'; +/* output: + title + ----------------- + Super Disco Album + Super Hairband Al */ + +/* Select the average year all albums were released */ +SELECT AVG(release_year) from album; +/* output: + avg(release_year) + ----------------- + 1984.0 */ + +/* Select 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'; +/* output: + AVG(release_year) + ----------------- + 1990.0 */ + +/* Select the number of artists */ +SELECT COUNT(id) FROM artist; +/* output: + count(id) + ---------- + 3 */ + +/* Select the number of tracks on Super Dubstep Album */ +SELECT COUNT(track.id) FROM track, album WHERE album.id = track.album_id and album.title = 'Super Dubstep Album'; +/* output: + COUNT(track.id) + --------------- + 5 */ \ No newline at end of file