From 8fbb19c8c6c5c637465bf13a5e20fedc73b9c4d5 Mon Sep 17 00:00:00 2001 From: ckopecky Date: Fri, 31 Aug 2018 16:01:17 -0700 Subject: [PATCH 1/6] create music.db --- README.md | 2 +- music.db | Bin 0 -> 24576 bytes 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 music.db diff --git a/README.md b/README.md index a6d7c7c..e6d136b 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ column names in the following tables. We'll use `setup.sql` later. * Write SQL `SELECT` queries that: * Show all albums. * Show all albums made between 1975 and 1990. - * Show all albums whose names start with `Super D`. + * Show all albums whose titles start with `Super D`. * Show all albums that have no release year. * Write SQL `SELECT` queries that: diff --git a/music.db b/music.db new file mode 100644 index 0000000000000000000000000000000000000000..c7abae7c5ec4f75d7dbd40668fad1eec67ada88f GIT binary patch literal 24576 zcmeI(KWyVv90&0C{GJn%!+tGL>?8uc<)r3%0wrlFh=tJlXhoVHX%4AV@bYm?|MLAi<6VhyjFHm_VRILOk2a(VT2fC$SX1Pjc+|>pwq#?8VV9 zd#|+o(37tXM<;G58)Sh{N?wqX5OPILIW^4&R}0ZJ8+5kt5A!SJsf9lbwSjKDK}27a z&G(Hz)glH2AOHafKmY;|fB*y_0D=Foz+_R+TbD1>i5I#Dea{_*eh~KD{=xX<{Bmxi z<21XD>^5I)Ir4lWmy1%3=_>z7ZnwL#<6Lt(PJ6@IlkuUJ{iEvDWV`6dy_C)36Gz(> ztCJ^r-m)zERwb?}9JzNBJ5`Tf^yoyluvm@X&BdEU{2;y*AB*?YA_fE?009U< z00Izz00bZa0SG_<0{^mrrE}6Cd*hqlNN$b~g3!Auo6&CqWzNZ(mIPa_KRR#+M@f=D zvlu5&PqR%wI2=xcPnI+lOpkVLJUBj0f)Z6HnQq^_?FB>it4Dm2{AbboKNH^)@vHbr z%`hMU0SG_<0uX=z1Rwwb2tWV=5V&vx1;#8IcW>B5jg@H9Ccz3+t&ZCa^yK~jjEL{W z7vk)~T_6gE00bZa0SG_<0uX=z1Rwwb2>dSuE-^-)*m4JQZ`dC$X^hlbp6|-2Ss;Af zlg{noaZsj=Jl`50dv{lFj)wkGI2?FE^!{%f{iy%{bMcXQSDcETaKwsOG=DR{F+Vjw zG~Y4%=B`;cmyF+yuZ=U~1LJMA0|o>j009U<00Izz00bZa0SIIYlx3pwuH~P!H zT+_=|R<^2_AJ57fdU+`;tLx>*va*_9d6bjqrnTH{FHeeD=aok?!)JJ9F*97_m4(c3 zl~+V&*x(g2GpzH9kr~#w{jln8pRvZ>@X2*Fn0+ZdsWZEnp46CK_&Z5j=#!(ZYW73v zNkg;q=}BF)bLmMK_0ar|MJx3uyu7JOBUy literal 0 HcmV?d00001 From 0bda2bd1d0f64678edc6a156b744273cfa6bda5f Mon Sep 17 00:00:00 2001 From: ckopecky Date: Tue, 4 Sep 2018 00:08:19 -0700 Subject: [PATCH 2/6] Completion of all but two exercises on Day 1 --- create.sql | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ music.db | Bin 24576 -> 28672 bytes setup.sql | 48 ++++++++++++++-------------- 3 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 create.sql diff --git a/create.sql b/create.sql new file mode 100644 index 0000000..8637340 --- /dev/null +++ b/create.sql @@ -0,0 +1,91 @@ +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 +); + +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) +) + +SELECT * from album; +SELECT * from album WHERE release_year >= 1975 AND release_year <= 1990; +SELECT * from album WHERE title LIKE 'Super D%'; +SELECT * from album WHERE release_year IS NULL; + +-- updated for new query request with new column heading + +SELECT album.id, track.Track_Title +FROM album +INNER JOIN track ON album.id = track.album_id WHERE album.title LIKE 'Super Funky Album%'; + +PRAGMA foreign_keys=off; + +BEGIN TRANSACTION; + +ALTER TABLE track RENAME TO temp_table; + +CREATE TABLE track +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + Track_Title VARCHAR(128), + album_id INT REFERENCES album(id)); + + +INSERT INTO track (id) + SELECT id + FROM temp_table; + +INSERT INTO track (Track_Title) + SELECT title + FROM temp_table; + +INSERT INTO track (album_id) + SELECT album_id + FROM temp_table; + +DROP TABLE temp_table; + +COMMIT; + +PRAGMA foreign_keys=on; + +SELECT artist_album.artist_id, artist.name, artist.id, album.title, album.id +FROM + artist_album + INNER JOIN artist + ON artist_album.artist_id = artist.id + INNER JOIN album + ON artist_album.album_id = album.id + WHERE artist.name LIKE 'Han Solo%'; + + +SELECT AVG(release_year) +FROM album; + +-- SELECT artist_album.artist_id, artist.name, artist.id, album.release_year, album.id +-- FROM +-- artist_album +-- INNER JOIN artist +-- ON artist_album.artist_id = artist.id +-- INNER JOIN album +-- ON artist_album.album_id = album.id +-- WHERE artist.name LIKE 'Leia and the Ewoks%' +-- AND SELECT AVG(release_year) + +-- will figure this one out later...has to do with subqueries. Need to investigate more; + + +SELECT COUNT(name) +FROM artist; \ No newline at end of file diff --git a/music.db b/music.db index c7abae7c5ec4f75d7dbd40668fad1eec67ada88f..1585dba0a07fc051f74d0f51ff4828746178a0fe 100644 GIT binary patch literal 28672 zcmeHPdu$xV9lrNtcV{Lh3G3bPI6|N{F@fDX+q_zE66XQ76CCph&^UH{G)^2l*k^)y z1dbMjTH2z35FlDgg(_5@f`STtD5?-3T2e$`Nkv;)pcJT3Um!#y+HdFQOKfY#q5rjX zPP*S`-#2?R_w9Tut-qgd-MJlum2%mMpt%GFfr@heAwzQ+Yu<+oJg@p!d zaG6QG{pEJ{zJC(t53wTVvG|N;?G)}i7IdQZks}Y-}x*(f%T{nLH ze9|rYsFkRsr!7RjWHl7zTYo&&qQ&ZzJH!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^bLq~T}2dK1nw##bdkZUh`>WEUppH!Hz2ZlHC6Hu T;H#;khg4rpg&v}QH5L3D&bQ8^ delta 491 zcmXxg!An#@90%~3_vWqc>dbf7T;IGmQR*epP~Y3#I(8|jz0`joTM)Fq5^Ozm2;O?i zn=)Ry=tVp#xOnsMz*9ldb)m#V5nY79C<>x^BMl7m`F-Ix!~D`ynr3ueRi+6c1+)Jz zwsF1YPm|g5A8sO#w+Rx2Vw?Zv?Os$qAJ%QYS?XS41HSLmRi%f-q%%IH=f#|Ug9-go zOwdwqOFX8+LrU-k?{SFNNPCs>XVB@@Zho(e6M`T3h7%JXVPk!CzUaC>?JTz*Ey|MP z2DG)@dGfsD7O9C_$-|YW-bG_Z@EPxrnZ^U??AGnNJN7%Yj~k=QTM_z14XO0jJkw~%`sMU<&Z_}%924qi#j!V-I0S9HAfB@ z1hinw$O$}s)|74|VEL){mJ0fN)2EJ2 Date: Tue, 4 Sep 2018 08:56:13 -0700 Subject: [PATCH 3/6] Finish last two exercises on Day 1 --- create.sql | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/create.sql b/create.sql index 8637340..367d680 100644 --- a/create.sql +++ b/create.sql @@ -74,18 +74,11 @@ FROM SELECT AVG(release_year) FROM album; --- SELECT artist_album.artist_id, artist.name, artist.id, album.release_year, album.id --- FROM --- artist_album --- INNER JOIN artist --- ON artist_album.artist_id = artist.id --- INNER JOIN album --- ON artist_album.album_id = album.id --- WHERE artist.name LIKE 'Leia and the Ewoks%' --- AND SELECT AVG(release_year) - --- will figure this one out later...has to do with subqueries. Need to investigate more; - +SELECT AVG(release_year) FROM album, artist, artist_album + WHERE artist_album.album_id = album.id AND artist_album.artist_id = artist.id AND artist.name = "Leia and the Ewoks"; SELECT COUNT(name) -FROM artist; \ No newline at end of file +FROM artist; + +SELECT COUNT(Track_Title) FROM album, track +WHERE album.id = track.album_id; \ No newline at end of file From a593e0316c65bf65ae110665c4f39e18705b3fb5 Mon Sep 17 00:00:00 2001 From: ckopecky Date: Tue, 4 Sep 2018 08:58:10 -0700 Subject: [PATCH 4/6] Add query for album on last exercise --- create.sql | 3 ++- notes.md | 0 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 notes.md diff --git a/create.sql b/create.sql index 367d680..da8dd72 100644 --- a/create.sql +++ b/create.sql @@ -81,4 +81,5 @@ SELECT COUNT(name) FROM artist; SELECT COUNT(Track_Title) FROM album, track -WHERE album.id = track.album_id; \ No newline at end of file +WHERE album.id = track.album_id AND +album.title = "Super Dubstep Album"; \ No newline at end of file diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..e69de29 From 0313358d6a25cc0920537a0a7ee4116e2dd857c5 Mon Sep 17 00:00:00 2001 From: ckopecky Date: Tue, 4 Sep 2018 10:54:48 -0700 Subject: [PATCH 5/6] exercises and notes --- earthquake.db | Bin 0 -> 16384 bytes employee.db | Bin 0 -> 8192 bytes example.db | Bin 0 -> 12288 bytes notes.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 earthquake.db create mode 100644 employee.db create mode 100644 example.db diff --git a/earthquake.db b/earthquake.db new file mode 100644 index 0000000000000000000000000000000000000000..c6c829168b8edcbdb4c7b62cd4b98bc1afa4bdd2 GIT binary patch literal 16384 zcmeI(F>ljA6bJCTIEjgI{45n@iRn@)B2gnmlctP~i7bdnLhA-ZrpSN`6-^+m^8w1p z%mNDo5}$#Ats8=ojg5f~vBSXGbx7@jN{sM-%E`WWXW!+Q$#m?jXu@{I2 zGa2WsB?Mz^Nw%zPifmlALfn^!D8k%nvBWN!KZ^8KZsqg^igE>mE;9N?pXoilr8o4Po>D+#8qpTDsVN5$5P$##AOHaf zKmY;|fB*y_0D=EQKsV%@aJ6+|Cknk7_BOsWqR{ndOpSYIqu#j>VSLoJXy(u3UalZ} zT-DD+Q}~N8UYcr_m6`Lq#vrbk`BY}z%%w6-Gn>k+nMx|tFf*x4-Q=mv>ip-Uuk?xD z(QA4}Q@S(H;s^ltXy)S z*@`O)RyH}X){2V@mXaK3Y{Yd2GozIGiN))x#gkK4TXFRveE-ksJCh#-1Rwwb2tWV= a5P$##AOHafKmY>&h=9SB5?}mX!T1Fy^zI4( literal 0 HcmV?d00001 diff --git a/employee.db b/employee.db new file mode 100644 index 0000000000000000000000000000000000000000..57955bdfc9b0de9837366dcfbfefb8deb1abc6d8 GIT binary patch literal 8192 zcmeIu!Aiq07zgmAW;hQcb09@VF|f-9LquP|+GRK>18eafN2G<;j5Y^7`V!vu3OE$6}gSwVkU{i-$0YZo_1Fc`+J`SpDvfI{cF`z9M|X z*ZhO8+z|l*2tWV=5P$##AOHafKmY;|fWUtW@D`&dr!Be2j9i%7Jogx*e!wQWuxgiF s&wZ%ZKRbvrRTa737T;sEKk%mJ#a71tl-jz{W6F;FAS{-d+9lWe2B~pL6#xJL literal 0 HcmV?d00001 diff --git a/example.db b/example.db new file mode 100644 index 0000000000000000000000000000000000000000..8dccf8b6ab1b8cc657027d297a69bd0a730d0bf2 GIT binary patch literal 12288 zcmeI#&ubGw6bJB`{SnQ!>01mXNC+M1rG=tZ{{gq#H4Uz*ZeyXRFtua5cC)LSNi-e= z5B?4P8~iW4`4@Qf)Po3uC!x@byTW?$&})m|f%j(KJcb!Qx7m3T##)KzSw50l++!}| zoZT0KF;*|aETS5`SXfn2%w>JisIyz{d4tZFL0v{&`h3v=^dSHN2tWV=5P$##AOHaf zKmY=lSl}JM*6MWl`)w`v6BUeJCfQ8=oee(|95-!woz;O9nRwPK)o5iN{T=I zP)>CAryf=}4)0W}@?U?w^lZ>KX446yAM}Mj(MLL=50}^(PlEsiAOHafKmY;|fB*y_ z009VGT>-ad@lKP%NqYD?)^CHUa&3#Z7Z?0&lEfs%BoQkFs>; zlpQ){bWXqMCw-?=`g(OC@p1@200Izz00bZa0SG_<0uX?}e<)y^oKvmL3wD_sR+;C` LGS3;7$?MK<{gZBe literal 0 HcmV?d00001 diff --git a/notes.md b/notes.md index e69de29..7aa630c 100644 --- a/notes.md +++ b/notes.md @@ -0,0 +1,95 @@ +Inner Join, The Most Common Join + + + +```sql + SELECT Employee.ID, Employee.Name, Department.Name + FROM Employee, Department + WHERE Employee.DepartmentID = Department.ID; +``` + + +Alternative syntax: + +```sql +SELECT Employee.ID, Employee.Name, Department.Name + FROM Employee INNER JOIN Department + ON Employee.DepartmentID = Department.ID; +``` + +Left Outer Join (includes rows that have null values in right table (table after LEFT JOIN clause)) + +```sql + SELECT Employee.ID, Employee.Name, Department.Name + FROM Employee LEFT JOIN Department + ON Employee.DepartmentID = Department.ID; +``` + +Right Outer Join (includes rows that have null values in left table(table after FROM clause)) + +```sql +SELECT Employee.ID, Employee.Name, Department.Name + FROM Employee RIGHT JOIN Department + ON Employee.DepartmentID = Department.ID; +``` + +Full Outer Join + +Blend of left and right outer join. All information from both tables is selected, with NULL filling the gaps where necessary. + +```sql +SELECT Employee.ID, Employee.Name, Department.Name + FROM Employee + FULL JOIN Department + ON Employee.DepartmentID = Department.ID; + +``` + +Parameterized Queries +-------- +Will save you from desanitized inputs from users! +Sanitize your inputs! + +https://node-postgres.com/features/queries#parameterized-query + + +Indexes +-------- +```sql +CREATE INDEX ON Employee (LastName); +``` + +Make sure if you are doing a search or a join on a table that your columns are indexed. This is a way to create an index that doesn't have a primary key. + + +Transactions +---------- +You can bundle a series of statements into a transaction. The transaction all at once or not at all. + +This makes transactions atomic. Transactions will either be done all at once when you commit the change or not at all. + +ROLLBACK will rollback your commands in case you make a mistake. COMMIT will commit your changes or not at all if there are errors. + +Normalization +------------ +Process of designing or refactoring your tables for maximum consistency and minimum redundancy. We normalize databases to avoid anomalies. + + * Insert anomaly -- When we cannot insert a row into the table because some of the dependent information is not yet known. + + * Update anomaly -- When info is duplicated in the database and some of the rows are updated but not others.. + + * Delete anomaly -- The opposite of insert anomaly. When we delete some information and some other related information must also be deleted against our will. + +There are many normal forms: + +First Normal Form +--------------- +When a database is in first normal form, there is a primary key for each row and there are no repeating sets of columns that should be in their own table. + +Second Normal Form +--------------- +To Be in Second Normal Form, a table must already be in First Normal Form. Additionally, all non-key data must fully relate to the key-data in the table. + +Third Normal Form +-------------- +A table in Third Normal Form, a table must already be in Second Normal form. Additonally, columns that relate to each other AND to the key need to be moved into their own tables. This is know as removing transitive dependencies. \ No newline at end of file From 2165a4f98f25814dc4058880a893ec27e1f68ff9 Mon Sep 17 00:00:00 2001 From: ckopecky Date: Tue, 4 Sep 2018 14:21:33 -0700 Subject: [PATCH 6/6] complete Day 2 exercises --- note.db | Bin 0 -> 28672 bytes notedatabase.sql | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 note.db create mode 100644 notedatabase.sql diff --git a/note.db b/note.db new file mode 100644 index 0000000000000000000000000000000000000000..16c7b0d202b656065c83aabcaef9f77980b16452 GIT binary patch literal 28672 zcmeHQ--{d971peFwY7tz&`=f!0!I(QUZPbUheAmow(?qbHn!KUR;lrmxth7s+-PR5 za%W~`l~Us3f=mB_wtqk$`*-xUZ+!@~gi{)3*?6fDMUC$gH5!dq@V$-i^{a`4&Gj4nj-ELFqQh4j zy_Y^dhqIgK{?cgQc=2B^{s;%*6%Ysr1Ox&C0fB%(Kp-Fx5C{ka1Ofuj5`l*=ZEo+p z_FD8XuGBcws;*M&?iaSw<&(!-2g5;sG@w!c%FO{id4?{YCA?NcCZWTl5e)~|2E)P8 z!Qgmx;-X1*yE0Kk-~a498Ru!c%jWjZ`Sa1kTTf`@#czI&7OVBWz`^ywknRi*Z}o@w z=*Hk4_1_)cK0Lx@w+2U}l>#c)^!@(u;O+kK;v1L0y-P>8M|AY=&6{U;?`*VoUVlBh zCo7llXSfT#U+VX3UBvpSx6M!K^3=(T1s3Vz%H4;TqwSrYo#@90B2%oC9~+;#UWQ=G zmouX>RrUoobkD}NsB}@Cn)t0ZE`MTT_w1K&)b17{E^2?wakt(6xb^eRA8vg8Sq5Q{ z{Z&Wc(X`cS?EdtPs9#Lok_JmXc6L!7Tj!QtI=qn6)S6;Sm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B+L0~+Jlrn( zH5zwD8L*ioGZV}}%`&SS2v(gVMiTkd8D;GBB7xrA0cTwC*m|jtl6@tdUsPOUVKB-} z4ST9mB6a}-aCe3P#EodH#s46ARrJB2nYlO0s;YnfIvVXAP^7; z2m}P4Zv@V4GHqMV0Iy=#=&1U0eeCFd!6GMZH zKUUS+z|cq++YzQ8;Uj7M@Bgj#?;7oYxBuS$Yx_^_-{Vbq1q1>D0fB%(Kp-Fx5C{ka z1Ofs9fq+0jARzEuAh6YHM$uMlYYV@cTU(p>wXwO`Y@Wfp4ZLY>Y(~xK%%bTC5)aS+ zhv)yp^Z)-Z&;Ns%r6c$M(YX&B_zJIpKtLcM5D*9m1Ox&C0fB%(Kp-Fx5O_`zc+}Z$ zHTsXwMK?@R=p_@st|=~OtC53DIptZsna)s{{>FCcX-h$p2Nq^<%z;K1Z1o`IV`@E_ zlY>7F)UM33%%sYmBQLF|%rfekO4mfyOq6$eVof{9T-MqNMfN@z35DU#Q)n}pxB^kM z0$U)HnWmuM+^0ME?+QS;$<&zqw@9JLESYN?8i4f?Dy#%3k;~~)f zZ$#a!WUj!=3z|aEpn42Twg}{ko|V#*YkN*pW<3M}HK6~5>v(k@r$9n!K#vKg@(Bnb pncLeiGbF}9{j5M4DQu%)@&w-?7`Z{czGCiXR%|V7fQ$XD{{WGsL4^PS literal 0 HcmV?d00001 diff --git a/notedatabase.sql b/notedatabase.sql new file mode 100644 index 0000000..7832606 --- /dev/null +++ b/notedatabase.sql @@ -0,0 +1,75 @@ +CREATE TABLE notes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(128) NOT NULL, + content VARCHAR(512) NOT NULL, + author_id INT REFERENCES author(id) +); + +CREATE TABLE author ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(128) NOT NULL +); + +CREATE TABLE author_notes ( + author_id INT REFERENCES author(id), + notes_id INT REFERENCES notes(id)); + +INSERT INTO author VALUES (1, 'Nine'); +INSERT INTO author VALUES (2, 'Ten'); +INSERT INTO author VALUES (3, 'Eleven'); +INSERT INTO author VALUES (4, 'War'); +INSERT INTO author VALUES (5, 'Twelve'); +INSERT INTO author VALUES (6, 'Four'); + +INSERT INTO notes VALUES (1, "The War Doctor", "Are you capable of speaking without flapping your hands about? And in that battle there was a man with more blood on his hands than any other. A man who would commit a crime that would silence the universe. And that man was me. Great men are forged in fire. It is the privilege of lesser men to light the flame. Whatever the cost. Shall we ask for a better quality of door so we ca", 4); + +INSERT INTO notes VALUES (2, "Jelly Babies!", "Well, of course I'm being childish! There's nopoint being grown-up if you can't be childish sometimes. Come on! The trouble with computers, of course, is that they're very sophisticated idiots. They do exactly what you tell them at amazing speed. Even if you order them to kill you. So if you do happen to change your mind, it's very difficult to stopthem from obeying the original order. But not impossible. You're a clumsy, ham fisted idiot! Are you listening to me? Have a jelly baby. It may be irrational of me, but human beings are my favorite species.", 6); + +INSERT INTO notes VALUES (3, "Fantastic!", "It's called the TARDIS, this thing. T-A-R-D-I-S. That's Time And Relative Dimension In Space. That’s okay. Culture shock. Happens to the best of us. Anold friend of mine. Well, enemy. The stuff of nightmares reduced to an exhibit. I'm getting old. We're fallin' through space, you and me, clinging to the skin of this tiny little world, and if we let go... Ricky, if I was to tell you what I was doing to the controls of my frankly magnificent timeship, would you even begin to understand? The thing is, Adam, time travel is like visiting Paris. You can't just read the guidebook, you've got to throw yourself in! Eat the food, use the wrong verbs, get chargeddouble and end up kissing complete strangers! Or is that just me?", 1); + +INSERT INTO notes VALUES (4, "Allons-y, Alonso!", "There's something else I've always wanted to say: Allons-y, Alonso! I'd call you a genius, except I'm in the room. Aw, I wanted to be ginger! I've never been ginger! And you, Rose Tyler! Fat lot of good you were! You gave up on me! Ooh, that's rude. Is that the sort of man I am now? Am I rude? Rude and not ginger. It is! It's the city of New New York! Strictly speaking, it's the fifteenth New York since the original, so that makes it New-New-New-New-New-New-New-New-New-New-New-New-New-New-New New York.", 2); + +INSERT INTO notes VALUES (5, "Bows ties are cool.", "I need...I need...I need... fish fingersand custard! Look at me. No plans, no backup, no weapons worth a damn. Oh, and something else I don'thave: anything to lose. So, if you're sitting up there with your silly little spaceships and your silly little guns and you've any plans on taking the Pandorica tonight; just remember who's standing in your way. Remember every black day I ever stopped you and then, and then, do the smart thing. Let somebody else try first. Frightened people. Give me a Dalek any day.", 3); + +INSERT INTO author_notes VALUES (4, 1); +INSERT INTO author_notes VALUES (6, 2); +INSERT INTO author_notes VALUES (1, 3); +INSERT INTO author_notes VALUES (2, 4); +INSERT INTO author_notes VALUES (3, 5); + +INSERT INTO notes VALUES (6, "Angry Eyebrows", "Sorry, I'm going to have to relieve you of your pet. Shut up, I was talking to the horse. Don't be lasagna. I've lived for over 2000 years and not all of them have been good. Shut up! Just shut up, shut up, shut up, shuttity up up up! My carer. She cares so I don't have to. Why do you have three mirrors? Why don't you just turn your head? That's a relief. I hate babysitters. I am totally against bantering. I just have one question… do you know how to fly this thing? No sir. Thirteen! You realise one of us is lying about our basic programming. And I think we both know which one that is.", 5); + +SELECT author.id, author.name, notes.title + FROM author, notes + WHERE notes.author_id = author.id; + +SELECT author.id, author.name, notes.title + FROM author, notes + WHERE notes.author_id = author.id + AND author.name = "Twelve" + + SELECT author.id, author.name, notes.id + FROM author, notes + WHERE notes.author_id = author.id + AND notes.id = 2; + +SELECT COUNT(notes.title), author.name + FROM notes, author + WHERE notes.author_id = author.id + GROUP BY author.name; + +PRAGMA foreign_keys = ON; +DELETE FROM author WHERE ID = 1; + +-- It just deleted the author without its associated note!!! + +-- We need to turn foreign key constraints on to prevent that from happening. + +-- first normal form +SELECT author.name, notes.id FROM author, notes WHERE author.id = notes.author_id; + +--second normal form +SELECT author.name, notes.id + FROM author, author_notes, notes + WHERE notes.id = author_notes.notes_id + AND author.id = author_notes.author_id ; \ No newline at end of file