diff --git a/README.md b/README.md index a6d7c7c..9ec6019 100644 --- a/README.md +++ b/README.md @@ -135,3 +135,4 @@ Write queries that: Submit a file `notes.sql` with the queries that build (`CREATE TABLE`/`INSERT`) and query the database as noted above. +#Initial Commit diff --git a/ex1.sql b/ex1.sql new file mode 100644 index 0000000..59345bd --- /dev/null +++ b/ex1.sql @@ -0,0 +1,73 @@ +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); + +/* Exercises, Day 1 */ + +/* 1. 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 TABLE track (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(128) NOT NULL, album_id INTEGER NOT NULL REFERENCES album(id)); + +/* 2. 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. */ + + CREATE TABLE artist_album (artist_id INTEGER NOT NULL REFERENCES artist(id), album_id INTEGER NOT NULL REFERENCES album(id)); + +/* 3. 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. */ + +/* 4. 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 >= 1975 AND release_year <= 1990; + + /* - 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; + +/* 5. Write SQL SELECT queries that: */ + + /* - Show all track titles FROM Super Funky Album. */ + + SELECT title FROM track WHERE title LIKE 'Super Funky%'; + + /* - Same query as above, but rename the column FROM title to Track_Title in the output. */ + + SELECT title AS Track_Title FROM track WHERE title LIKE 'Super Funky%'; + + /* - Select all album titles by Han Solo. */ + + SELECT title FROM (SELECT album.title, artist.name FROM album, artist, artist_album WHERE album.id = artist_album.album_id AND artist.id = artist_album.artist_id) + WHERE 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 (SELECT album.title, album.release_year, artist.name FROM album, artist, artist_album WHERE album.id = artist_album.album_id AND artist.id = artist_album.artist_id) + WHERE 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(id) FROM track WHERE title LIKE 'Super Dubstep%'; \ No newline at end of file diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..6ebe3bf --- /dev/null +++ b/notes.sql @@ -0,0 +1,56 @@ +/* Create authors table */ + +CREATE TABLE authors (id INTEGER PRIMARY Key AUTOINCREMENT, first_name VARCHAR(200) NOT NULL, last_name VARCHAR(255) NOT NULL); + +/* Create notes table */ + +CREATE TABLE notes (id INTEGER PRIMARY Key AUTOINCREMENT, title VARCHAR(200) NOT NULL, content VARCHAR(255) NOT NULL, author_id INTEGER REFERENCES authors(id), created_at DATETIME DEFAULT CURRENT_TIMESTAMP, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP); + +/* Create a trigger to update the timestamp for the last_modified field in a record when changes are made to it */ + +CREATE TRIGGER UPDATE_TIMESTAMP_TRIGGER +AFTER UPDATE ON notes +BEGIN + UPDATE notes SET TIMESTAMP = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id; +END; + +/* Insert authors to the authors table. */ + +InSERT INTO authors (first_name, last_name) VALUES ('Edward', 'Gonzalez'); +InSERT INTO authors (first_name, last_name) VALUES ('Bob', 'Dillan'); +InSERT INTO authors (first_name, last_name) VALUES ('Maria', 'Stratus'); +InSERT INTO authors (first_name, last_name) VALUES ('Tom', 'Smith'); + +/* Insert notes to the note table. */ + +InSERT INTO notes (title, content, authors_id) VALUES ('Title 1', 'Content 1', 5); +InSERT INTO notes (title, content, authors_id) VALUES ('Title 2', 'Content 2', 4); +InSERT INTO notes (title, content, authors_id) VALUES ('Title 3', 'Content 3', 2); +InSERT INTO notes (title, content, authors_id) VALUES ('Title 4', 'Content 4', 1); +InSERT INTO notes (title, content, authors_id) VALUES ('Title 5', 'Content 5', 1); + +/* Select all notes by an author's name. */ + +SELECT title, content FROM notes, authors WHERE authors.id = notes.authors_id AND authors.first_name = 'Edward'; + +/* Select author for a particular note by note ID. */ + +SELECT first_name, last_name FROM (SELECT notes.id as notes_id, authors.first_name, authors.last_name FROM authors, notes WHERE notes.authors_id = authors.id) WHERE notes_id = 3; + +/* Select the names of all the authors along with the number of notes they each have. (Hint: GROUP BY.) */ + +SELECT count(notes_id) notes, first_name, last_name FROM (SELECT notes.id AS notes_id, authors.first_name, authors.last_name FROM authors, notes WHERE notes.authors_id = authors.id) group by first_name, last_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 authors WHERE id = 4; + +/* What happens when you try to delete an author with an existing note? */ + +/* The author id 4 will still be referenced in the notes table. */ + +/* How can you prevent this? */ + +PRAGMA foreign_keys = ON; + +/* Since PRAGMA foreign_keys is on you will receive a an error such as "Error: FOREIGN Key constraint failed" when trying to delete an author that is referenced in the notes table. */ diff --git a/sqldb.db b/sqldb.db new file mode 100644 index 0000000..c656f65 Binary files /dev/null and b/sqldb.db differ