From 1f648569e2ce42ee951eda3b147297d289ca9c0b Mon Sep 17 00:00:00 2001 From: Edward Gonzalez Date: Tue, 4 Sep 2018 15:18:32 -0400 Subject: [PATCH 1/4] Initial Commit --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From 760cbfca9fc5a23919cee3651b0297732be98175 Mon Sep 17 00:00:00 2001 From: eddygonzalez9708 Date: Wed, 5 Sep 2018 17:17:20 -0400 Subject: [PATCH 2/4] Completed Exercises, Day 1 --- ex1.sql | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ sqldb.db | Bin 0 -> 24576 bytes 2 files changed, 73 insertions(+) create mode 100644 ex1.sql create mode 100644 sqldb.db 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/sqldb.db b/sqldb.db new file mode 100644 index 0000000000000000000000000000000000000000..f09b397cb478c4985e55405066d5876cc7ff770d GIT binary patch literal 24576 zcmeI)Pl)4G90%~1yq9*nYxDMxW0IDIC%DiugG<{P$Hj}IQ`y6Se`YS8N~ZJH4Na5T zBz5VYwtLdM7xye)J&E9HK=2^wT~P6$h!;nk!U0X?Xg%K6L58C|sCV9yxxg^c6 z`!DZ#i4-@&XzV7UN6r&XBhLy!2)VC574=!Ph8kgu2{qJ~#>+zK@#18VZDVqUodaFi$26N@r(b)M~d z@i3eRAFk;tm~ZWcNidq`fupHT=F9KgmT{;ab7Y-VKThxeDgTP_pZND`fCd2wKmY;| zfB*y_009U<00Izz!2eF5MyaJ`FKO6kb?RvO>kzc2soB|!1x9-RU*Vq<{wx2H|G>ZF zU#kf;2tWV=5P$##AOHafKmY;|fB*zeRp1}{{okliO{2^((g8J?8t6KsRIBMsS5E+R zrm64j{-5#_!oT64ssS1VAOHafKmY;|fB*y_009U<00RF(fpe6S%h%mN?1z51rc=_{ zlb$QmVu9qQ6#d)bC~jz!JhL|$$p^D{qtH7_!a&C9@Ben)PwW3b;qURc_>>QLpRe-^ z=FjGr=11nc=9{K(zG!yMwfZmhFX|`tcj|Ab70@660SG_<0uX=z1Rwwb2tc4zpkXtz zt*zF2AEbXyD7K7-RhDfUjmOHep3zt<%eqG6(Xy;#G%qo7ZN4uL%FFYe*=Eg)rQuVo zd7(7iV$JiV;U;VH(y+&xW@*@E&3b9rVfG`cx_!Z#Rm11U(WCac;-pLMv&Bh=+O^-4 zyo5gA+NN%wDNcI2T`f+!x?L$wI=Z6@|F>)T#jvQ!pI;1)D*Z2xHXT*@UmW!uRr_BY Lb<_I);;8c*iIi5l literal 0 HcmV?d00001 From 0c11592ba350a363f6cf90b500949041da48cf75 Mon Sep 17 00:00:00 2001 From: eddygonzalez9708 Date: Thu, 6 Sep 2018 01:48:36 -0400 Subject: [PATCH 3/4] Completed Exercises, Day 2 --- notes.sql | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ sqldb.db | Bin 24576 -> 32768 bytes 2 files changed, 57 insertions(+) create mode 100644 notes.sql diff --git a/notes.sql b/notes.sql new file mode 100644 index 0000000..69ed96b --- /dev/null +++ b/notes.sql @@ -0,0 +1,57 @@ +/* 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 index f09b397cb478c4985e55405066d5876cc7ff770d..c656f6538c3b2373fb8b7fd97be8e8dcdd58e298 100644 GIT binary patch delta 1249 zcma)4L2uGv7;d2i=my_5lQo1a`!o_DfmtXFVZjWxI+w^)23b6jZ|es%DeY1|T(X0( z#UG&aXDl8JF`kx8FFSbiXyWcBUUu+qX(<~z7n`()=Y5~&dEe)4@09NK9Iu1Z#}q|* zjMskV-^7HuV`|}U&u2g$_JRTtuSa-#V0~DInhH%ayNw&N%6hA& zgN1cr8xyFYHmS8VNv_LSf_c~`dVMfJBpiAk54GYU35Fi!LZw`&9*)8A#*?jZ6z(;# zj-fp0-w?MJWN8VqSd|WEJ#LQU=vNa2Z7bMxHa{MF_(JiqXt~44TZb>zP4awn06?{) zC^5LBsRXM8zCij;6X}s;I(@Z_I;5%THN#jo`i#V&j_bxK>JDy0s~C}`sG*}{BT1=- z2wXK%tE`tqxGFv@R3BI1O0`@TH!8IO*x0NTN>5{dq;Mm}WJC{U-EJ-rpby>Vs+PC) zb&oA;VMo45p)Tu2)GBJ>%k{>$H*YcCaCQH_?mL?CyTBO*et{q09DD_5{b~3Ca9)>_ zqs^pUEaME&mMPOezjue98vKI{IHAB#@Ev>sCkEvms7^8rb&IQLt<9E9no~}O;({fl z%V?A6h;-UMhfxm{we3||QII-AGgM$sl=hG=t!e5$Qt-YHEK@Fjj`HU~&;Jt8{?Gl} zQxSi+NV}=vJS{9$WTIf0SQ#EQ;Y{+0`*A)S=hHALWhTY+68E{H1 q4{{jVU+&<#S-|E$a99}(`AOS9$=t|ax3kR{hrc?&e;#6#$>MMHfLQDR delta 118 zcmZo@U}`wPI6+!)76StV8xX^Q)I=R)@mUOdDh<3qAy(dE27V3xD!%P};=IM16$RpW zr1-0tLd3 Date: Thu, 6 Sep 2018 01:49:33 -0400 Subject: [PATCH 4/4] Completed Exercises, Day 2 --- notes.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/notes.sql b/notes.sql index 69ed96b..6ebe3bf 100644 --- a/notes.sql +++ b/notes.sql @@ -33,7 +33,6 @@ InSERT INTO notes (title, content, authors_id) VALUES ('Title 5', 'Content 5', 1 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;