[ { "text": "[AUDIO LOGO]", "start": 0.0, "duration": 3.437 }, { "text": "[? CARTER ZENKE: ?] Well, hello, one\nand all, and welcome back to CS50's", "start": 16.72, "duration": 3.36 }, { "text": "Introduction to Databases with SQL.", "start": 20.08, "duration": 2.4 }, { "text": "My name is Carter [? Zenke, ?]\nand last we left off,", "start": 22.48, "duration": 3.24 }, { "text": "we learned how to create our\nvery own database schemas--", "start": 25.72, "duration": 2.73 }, { "text": "that is, a way to organize\ndata in our database.", "start": 28.45, "duration": 3.15 }, { "text": "Today, we'll learn how to actually\nadd data to our databases--", "start": 31.6, "duration": 2.85 }, { "text": "to insert, to update, and delete data.", "start": 34.45, "duration": 3.7 }, { "text": "We'll do all of this in the context of\nBoston's very own Museum of Fine Arts,", "start": 38.15, "duration": 5.18 }, { "text": "or the MFA for short.", "start": 43.33, "duration": 1.84 }, { "text": "So the MFA here in Boston is a century--", "start": 45.17, "duration": 2.447 }, { "text": "well, maybe, about a\ncentury-old museum that", "start": 47.617, "duration": 1.833 }, { "text": "has many artifacts and artwork inside\nof it, both historical and contemporary.", "start": 49.45, "duration": 5.19 }, { "text": "And it's worth asking, how do they\nkeep track of the thousands of items", "start": 54.64, "duration": 4.08 }, { "text": "that are in their collections?", "start": 58.72, "duration": 1.69 }, { "text": "What could they possibly use?", "start": 60.41, "duration": 1.88 }, { "text": "Well, chances are, they're likely\nusing some kind of database.", "start": 62.29, "duration": 4.29 }, { "text": "And on that database, they\nwant to do four actions", "start": 66.58, "duration": 4.05 }, { "text": "we've learned about back in week zero.", "start": 70.63, "duration": 2.55 }, { "text": "They could, perhaps, create data-- to\nadd data or insert data to the database", "start": 73.18, "duration": 4.44 }, { "text": "when they get some new piece\nof artwork, for instance.", "start": 77.62, "duration": 2.65 }, { "text": "They might want to query the\ndatabase, to read from it.", "start": 80.27, "duration": 2.7 }, { "text": "They could also update data, change the\nartist, change the artwork in some way.", "start": 82.97, "duration": 4.28 }, { "text": "And they could also just delete\ndata to remove it all together.", "start": 87.25, "duration": 4.09 }, { "text": "But if we think about these--", "start": 91.34, "duration": 1.58 }, { "text": "creating, reading, updating,\nand deleting-- we'll", "start": 92.92, "duration": 3.45 }, { "text": "notice that reading,\nupdating, and deleting-- we", "start": 96.37, "duration": 2.67 }, { "text": "can't do those if we don't\nactually have data in our database.", "start": 99.04, "duration": 3.87 }, { "text": "And so today, we'll see\nhow to create data--", "start": 102.91, "duration": 3.3 }, { "text": "how to insert data into\nour very own database.", "start": 106.21, "duration": 4.65 }, { "text": "Now, let's think about the MFA's\n[? collections, ?] [? their ?]", "start": 110.86, "duration": 3.54 }, { "text": "[? collection ?] of art and artifacts.", "start": 114.4, "duration": 1.583 }, { "text": "And let me propose that they\nhave a database [? that ?]", "start": 115.983, "duration": 2.292 }, { "text": "looks a bit like this.", "start": 118.275, "duration": 1.265 }, { "text": "It's a single table, and it has\na title column and accession", "start": 119.54, "duration": 4.16 }, { "text": "number, which is a fancy way of saying\na unique ID internal to the museum,", "start": 123.7, "duration": 4.68 }, { "text": "and also, a date it was acquired.", "start": 128.38, "duration": 2.58 }, { "text": "We have, of course, a primary\nkey on this table called ID.", "start": 130.96, "duration": 4.65 }, { "text": "And let's think.", "start": 135.61, "duration": 1.02 }, { "text": "Well, the museum might want\nto acquire this piece here--", "start": 136.63, "duration": 3.03 }, { "text": "this one called Profusion of Flowers.", "start": 139.66, "duration": 2.73 }, { "text": "Well, how could they log that this\nartwork is inside of their database?", "start": 142.39, "duration": 5.55 }, { "text": "They could maybe just add a new row.", "start": 147.94, "duration": 2.22 }, { "text": "They could say, let's\nput Profusion of Flowers", "start": 150.16, "duration": 2.13 }, { "text": "as the first item in\nour collections here.", "start": 152.29, "duration": 2.61 }, { "text": "We'll give it a title and\naccession number-- which, again,", "start": 154.9, "duration": 2.53 }, { "text": "is just a unique ID\ninternal to the museum--", "start": 157.43, "duration": 2.67 }, { "text": "and the date it was acquired.", "start": 160.1, "duration": 2.15 }, { "text": "And that row then has\nits own primary key", "start": 162.25, "duration": 3.09 }, { "text": "to identify this row\nuniquely in our database.", "start": 165.34, "duration": 3.66 }, { "text": "Now let's say they get\nanother piece of artwork.", "start": 169.0, "duration": 2.04 }, { "text": "They get this one, called\nFarmers Working at Dawn,", "start": 171.04, "duration": 3.09 }, { "text": "and they want to add this\none to their table, too.", "start": 174.13, "duration": 2.323 }, { "text": "Well, they could do the very same thing.", "start": 176.453, "duration": 1.667 }, { "text": "They could just add a new row.", "start": 178.12, "duration": 1.26 }, { "text": "They could say, let's make a\ntitle, an accession number,", "start": 179.38, "duration": 2.55 }, { "text": "and a date it was acquired,\nand this brand-new row", "start": 181.93, "duration": 2.31 }, { "text": "here for that piece of artwork.", "start": 184.24, "duration": 2.43 }, { "text": "And maybe they get another one, too.", "start": 186.67, "duration": 2.16 }, { "text": "Same thing.", "start": 188.83, "duration": 0.78 }, { "text": "Maybe they'll get back\nSpring Outing, and they", "start": 189.61, "duration": 2.16 }, { "text": "want to add this to their collection.", "start": 191.77, "duration": 1.95 }, { "text": "They could simply add\nanother row like this.", "start": 193.72, "duration": 4.09 }, { "text": "Now, it turns out that the database\nadministrator behind the MFA", "start": 197.81, "duration": 4.25 }, { "text": "might be running a SQL statement\nthat looks a bit like this--", "start": 202.06, "duration": 4.56 }, { "text": "INSERT INTO.", "start": 206.62, "duration": 2.25 }, { "text": "We can use INSERT INTO to add\na new row to any given table.", "start": 208.87, "duration": 5.31 }, { "text": "And notice how insert into needs to\nknow a few pieces of information.", "start": 214.18, "duration": 4.72 }, { "text": "The first is what table to insert into.", "start": 218.9, "duration": 3.65 }, { "text": "What is the name of that table?", "start": 222.55, "duration": 2.46 }, { "text": "Second, it needs to\nknow what columns are", "start": 225.01, "duration": 2.7 }, { "text": "we adding data to inside of this table?", "start": 227.71, "duration": 3.42 }, { "text": "We give it a list of those columns here.", "start": 231.13, "duration": 2.25 }, { "text": "Then, of course, it needs to\nknow what new values should", "start": 233.38, "duration": 3.75 }, { "text": "go into this new row for\neach of these columns.", "start": 237.13, "duration": 3.66 }, { "text": "Like we saw before, is\nit Profusion of Flowers?", "start": 240.79, "duration": 3.03 }, { "text": "Is it Spring Outing?", "start": 243.82, "duration": 1.38 }, { "text": "Et cetera.", "start": 245.2, "duration": 1.11 }, { "text": "Here, we can see that we\nhave this list of values.", "start": 246.31, "duration": 3.07 }, { "text": "And notice how value 0, the\nfirst value in this list,", "start": 249.38, "duration": 3.23 }, { "text": "corresponds to the new value that will\nbe inserted [? to ?] this first column.", "start": 252.61, "duration": 4.95 }, { "text": "And we can keep having value 1,\nor column 1, value 2 and column 2,", "start": 257.56, "duration": 3.899 }, { "text": "each one aligning with that\nparticular column there.", "start": 261.459, "duration": 4.451 }, { "text": "So let's see an example\nof this actually in code,", "start": 265.91, "duration": 3.39 }, { "text": "understand it a bit more concretely.", "start": 269.3, "duration": 2.24 }, { "text": "I'll go back to my computer here.", "start": 271.54, "duration": 2.19 }, { "text": "Let's actually create\nour very own database", "start": 273.73, "duration": 2.34 }, { "text": "that involves this schema of having our\nvery own table which can keep artists", "start": 276.07, "duration": 4.92 }, { "text": "and artwork and artifacts as well.", "start": 280.99, "duration": 3.64 }, { "text": "So I'll type", "start": 284.63, "duration": 0.77 }, { "text": "1: [? TRANSCRIPT BREAK ?]\nSo I'll type sqlite3 mfa.db", "start": 285.4, "duration": 4.11 }, { "text": "to create a database for the Museum\nof Fine Arts, abbreviated as MFA.", "start": 289.51, "duration": 5.94 }, { "text": "I'll hit Enter here.", "start": 295.45, "duration": 1.24 }, { "text": "And notice how I can now have access\nto my very own mfa.db inside my SQLite", "start": 296.69, "duration": 6.62 }, { "text": "environment.", "start": 303.31, "duration": 1.08 }, { "text": "Well, now, I can type .schema to\nsee the schema of this database.", "start": 304.39, "duration": 5.12 }, { "text": "So if I hit Enter here,\nwell, nothing's there.", "start": 309.51, "duration": 2.973 }, { "text": "Because I just made this database.", "start": 312.483, "duration": 1.417 }, { "text": "There's nothing in it yet.", "start": 313.9, "duration": 2.1 }, { "text": "Well, it turns out I do actually\nhave a schema file prepared for me", "start": 316.0, "duration": 3.84 }, { "text": "already in schema.sql.", "start": 319.84, "duration": 2.49 }, { "text": "Here, I propose we make a table called\n\"collections,\" and like our table", "start": 322.33, "duration": 4.2 }, { "text": "visually, it has let's say four columns.", "start": 326.53, "duration": 2.79 }, { "text": "One as an \"id,\" the primary key\nof this table, one for a \"title,\"", "start": 329.32, "duration": 6.195 }, { "text": "one for the \"accession_number,\" the\nunique ID internal to the museum,", "start": 335.515, "duration": 4.095 }, { "text": "and finally, the date it was acquired.", "start": 339.61, "duration": 3.06 }, { "text": "And notice here, that \"title\" has to\nbe actually added to our database.", "start": 342.67, "duration": 5.05 }, { "text": "It can't be [? NULL. ?] It\ncan't have an empty value.", "start": 347.72, "duration": 2.78 }, { "text": "Same thing for \"accession_number.\"", "start": 350.5, "duration": 2.19 }, { "text": "And further, \"accession_number\"\nhas to be unique.", "start": 352.69, "duration": 3.445 }, { "text": "I can't have two items [? with ?]\n[? a ?] [? different ?]", "start": 356.135, "duration": 2.375 }, { "text": "\"accession_number.\"", "start": 358.51, "duration": 1.08 }, { "text": "Otherwise, I might get them\nconfused in my database", "start": 359.59, "duration": 2.79 }, { "text": "or inside my museum archives as well.", "start": 362.38, "duration": 2.95 }, { "text": "So let me then add this\nschema to my database.", "start": 365.33, "duration": 4.22 }, { "text": "And I can do so with\nthis command right here.", "start": 369.55, "duration": 2.73 }, { "text": "This is a SQLite command called .read,\nthat we saw a little bit last week", "start": 372.28, "duration": 4.5 }, { "text": "as well.", "start": 376.78, "duration": 0.91 }, { "text": "I could type .read and then the\nname of this file I want to read.", "start": 377.69, "duration": 3.75 }, { "text": "So I'll say .read schema.sql.", "start": 381.44, "duration": 4.18 }, { "text": "Hit Enter.", "start": 385.62, "duration": 2.4 }, { "text": "And now if I type, let's say .schema,\nI can see that same [? sql ?]", "start": 388.02, "duration": 5.43 }, { "text": "[? schema, ?] same database\nschema. now inside of my terminal.", "start": 393.45, "duration": 5.83 }, { "text": "OK, so now as promised, let's try\nadding some rows to this table.", "start": 399.28, "duration": 5.14 }, { "text": "Because right now, if I do SELECT\n[? star ?] from [? collections ?]", "start": 404.42, "duration": 3.62 }, { "text": "[? semicolon, ?] I don't see anything\nbecause nothing is inside yet.", "start": 408.04, "duration": 4.89 }, { "text": "But I could add something\nusing INSERT INTO.", "start": 412.93, "duration": 3.07 }, { "text": "So let's try that.", "start": 416.0, "duration": 1.39 }, { "text": "I'll say INSERT INTO\nthe \"collections table.\"", "start": 417.39, "duration": 4.24 }, { "text": "And now, I have to ask, what\ncolumns inside of \"collections\"", "start": 421.63, "duration": 3.75 }, { "text": "[? do I ?] [? want ?] to add data to.", "start": 425.38, "duration": 2.7 }, { "text": "Well, I probably want to add\nto the first the \"id\" column,", "start": 428.08, "duration": 3.54 }, { "text": "that primary key column.", "start": 431.62, "duration": 1.86 }, { "text": "Then maybe, I want to add a\n\"title\" to this row, and also", "start": 433.48, "duration": 3.99 }, { "text": "an \"accession_number,\" and also,\nof course, the date this piece", "start": 437.47, "duration": 3.84 }, { "text": "was \"acquired.\"", "start": 441.31, "duration": 1.36 }, { "text": "So now, I have the table\nI'm inserting into along", "start": 442.67, "duration": 3.08 }, { "text": "with the columns I'm adding values for.", "start": 445.75, "duration": 2.49 }, { "text": "So for style's sake,\nI'll hit Enter here.", "start": 448.24, "duration": 2.82 }, { "text": "And now, I could type a list of\nvalues to insert into this new row.", "start": 451.06, "duration": 4.65 }, { "text": "I could say VALUES, and then\ninside parentheses, the values", "start": 455.71, "duration": 4.35 }, { "text": "I want to insert.", "start": 460.06, "duration": 1.59 }, { "text": "Maybe the first primary key\nthat I give to this item", "start": 461.65, "duration": 3.36 }, { "text": "is going to be just [? 1, ?] start\nat 1, and add up as we add new items.", "start": 465.01, "duration": 4.41 }, { "text": "For the title, I'll say let's call\nthis one \"Profusion of flowers.\"", "start": 469.42, "duration": 4.11 }, { "text": "This is the piece we recently\nacquired into our collection.", "start": 473.53, "duration": 3.3 }, { "text": "The accession number that\nwe gave it was 56.257.", "start": 476.83, "duration": 3.57 }, { "text": "And the date it was acquired\nwas back in 1956-04-12.", "start": 480.4, "duration": 5.72 }, { "text": "Now, I have all these values here.", "start": 486.12, "duration": 1.56 }, { "text": "I'll type [? semicolon ?] hit\nEnter, and nothing seems to happen.", "start": 487.68, "duration": 4.35 }, { "text": "But if I type, let's say, select\n[? star ?] from collections", "start": 492.03, "duration": 5.34 }, { "text": "[? semicolon, ?] what do I\nsee, but this new row inside.", "start": 497.37, "duration": 5.46 }, { "text": "Let's do it again to get a hang of this.", "start": 502.83, "duration": 1.71 }, { "text": "I want to now add \"Farmers\nworking at dawn\" to my collection.", "start": 504.54, "duration": 3.06 }, { "text": "So I'll do the same thing,\nI'll say let's INSERT INTO", "start": 507.6, "duration": 3.48 }, { "text": "the \"collections\" table.", "start": 511.08, "duration": 1.589 }, { "text": "And let's add values for\nthe \"id\" column, the \"title\"", "start": 512.669, "duration": 4.26 }, { "text": "column, the \"accession_number,\" and\nalso the \"acquired\" column like this.", "start": 516.929, "duration": 5.73 }, { "text": "Again for style's sake, I'll hit Enter.", "start": 522.659, "duration": 1.981 }, { "text": "Now, I give some list of values\nto add into this new row.", "start": 524.64, "duration": 4.14 }, { "text": "I'll say VALUES here, then this list\nof values to add for each column.", "start": 528.78, "duration": 4.5 }, { "text": "Well, the first column, I have\nto give a value for is \"id.\"", "start": 533.28, "duration": 4.08 }, { "text": "If you remember, our last id was\n1, so what should this id be?", "start": 537.36, "duration": 5.1 }, { "text": "Maybe 2.", "start": 542.46, "duration": 1.08 }, { "text": "So I'll type 2 here as the next\nincrement of my primary key,", "start": 543.54, "duration": 4.65 }, { "text": "then I'll give it a title.", "start": 548.19, "duration": 1.14 }, { "text": "And this title was\n\"Farmers working at dawn.\"", "start": 549.33, "duration": 4.14 }, { "text": "We gave it an accession\nnumber to keep track of it", "start": 553.47, "duration": 2.34 }, { "text": "in our own internal museum\nrecords, our own archives here.", "start": 555.81, "duration": 3.1 }, { "text": "So I'll say 11.6152 is the\n[? accession ?] [? number ?] for this", "start": 558.91, "duration": 6.2 }, { "text": "particular item, then I'll say we\nacquired it back in 1911-08-03.", "start": 565.11, "duration": 7.63 }, { "text": "Now, I'll hit a semicolon\nhere, hit Enter.", "start": 572.74, "duration": 3.27 }, { "text": "And now, I should be able to see if\nI type [? SELECT ?] [? star ?] FROM", "start": 576.01, "duration": 3.51 }, { "text": "\"collections,\" SELECT\n[? star ?] FROM \"collections\"", "start": 579.52, "duration": 3.0 }, { "text": "[? semicolon, ?] I now have these\ntwo items inside of my collection.", "start": 582.52, "duration": 6.22 }, { "text": "Now, let's do one more here.", "start": 588.74, "duration": 2.7 }, { "text": "But let's focus in particular\non this primary key.", "start": 591.44, "duration": 4.2 }, { "text": "Notice here, how we've been actually\ninserting our very own primary key", "start": 595.64, "duration": 3.93 }, { "text": "1 and then 2.", "start": 599.57, "duration": 1.83 }, { "text": "But maybe that's not the best design.", "start": 601.4, "duration": 3.823 }, { "text": "Let me try this again.", "start": 605.223, "duration": 0.917 }, { "text": "I'll say INSERT INTO, let's go for,\n\"collections\" [? is ?] our table name.", "start": 606.14, "duration": 5.94 }, { "text": "And maybe I'll try again to add\nto the \"id\" column, the \"title\"", "start": 612.08, "duration": 4.71 }, { "text": "column, the \"accession_number,\"\nand the \"acquired\" column here.", "start": 616.79, "duration": 7.14 }, { "text": "What could go wrong do you think if I\ntry to specify the primary key myself?", "start": 623.93, "duration": 6.03 }, { "text": "Let me ask this as an\naudience question here.", "start": 629.96, "duration": 1.92 }, { "text": "Feel free raise your hand.", "start": 631.88, "duration": 1.26 }, { "text": "What might go wrong if I try\nto make the primary key myself?", "start": 633.14, "duration": 4.56 }, { "text": "Insert a value myself?", "start": 637.7, "duration": 1.485 }, { "text": "Let's go to [INAUDIBLE].", "start": 643.01, "duration": 1.89 }, { "text": "SPEAKER 2: Yeah, what we could do when\nwe have to [INAUDIBLE] a lot of values,", "start": 644.9, "duration": 5.04 }, { "text": "like maybe like big data or something,\nis there a way to add it to a CSV file", "start": 649.94, "duration": 6.87 }, { "text": "or something rather than typing\n[? insert ?] [? into ?] tables?", "start": 656.81, "duration": 2.915 }, { "text": "SPEAKER 1: A good question.", "start": 659.725, "duration": 1.125 }, { "text": "I think you're onto\nsomething here, which", "start": 660.85, "duration": 1.708 }, { "text": "is we're inserting one row at a time,\nwhich could get easily repetitive.", "start": 662.558, "duration": 3.042 }, { "text": "So let me hold that thought\nfor just a minute here", "start": 665.6, "duration": 2.297 }, { "text": "and focus on the primary keys before\nwe see some more efficient ways", "start": 667.897, "duration": 2.833 }, { "text": "to actually add data to our table.", "start": 670.73, "duration": 2.23 }, { "text": "So I'm with you on this\nidea of maybe we don't want", "start": 672.96, "duration": 2.84 }, { "text": "to duplicate this primary key, right?", "start": 675.8, "duration": 1.897 }, { "text": "Maybe we could do a\nlittle better than that.", "start": 677.697, "duration": 1.833 }, { "text": "So I'll come back to my computer here.", "start": 679.53, "duration": 2.25 }, { "text": "And if I specify the primary\nkey, I might actually", "start": 681.78, "duration": 3.89 }, { "text": "add a value that's already in there.", "start": 685.67, "duration": 2.17 }, { "text": "So I could, thankfully, leave it\nup to SQLite to actually increment", "start": 687.84, "duration": 4.34 }, { "text": "this value for me.", "start": 692.18, "duration": 1.44 }, { "text": "Let me try leaving\noff \"id\" in this case.", "start": 693.62, "duration": 2.82 }, { "text": "I'll omit the \"id\"\naltogether, which seems", "start": 696.44, "duration": 2.91 }, { "text": "at first like a bit of a bad\nthought, like shouldn't every row", "start": 699.35, "duration": 2.82 }, { "text": "have its own id?", "start": 702.17, "duration": 1.11 }, { "text": "Well, let's just try and\nfigure out what happened.", "start": 703.28, "duration": 2.55 }, { "text": "I'll hit Enter here.", "start": 705.83, "duration": 1.23 }, { "text": "And now I'll say, let\nme give the VALUES.", "start": 707.06, "duration": 2.5 }, { "text": "Well, now the first column\nis the title column.", "start": 709.56, "duration": 3.21 }, { "text": "So I'll go ahead and say that this\none will be called \"Spring outing.\"", "start": 712.77, "duration": 3.2 }, { "text": "We're going to add this\none to our collection here.", "start": 715.97, "duration": 2.55 }, { "text": "The accession number,\nin this case, is 14.76.", "start": 718.52, "duration": 3.09 }, { "text": "And we acquired this one all the way\nback in 1914-01-08 [? semicolon. ?]", "start": 721.61, "duration": 6.93 }, { "text": "So I'll hit Enter here.", "start": 728.54, "duration": 1.63 }, { "text": "And notice how there's no\n[? id ?] that I have specified.", "start": 730.17, "duration": 3.17 }, { "text": "But now if I hit Enter, seems to work.", "start": 733.34, "duration": 3.51 }, { "text": "I'll type SELECT [? star ?]\n[? FROM ?] \"collections,\"", "start": 736.85, "duration": 3.27 }, { "text": "and what do I see but\nthe new primary key of 3.", "start": 740.12, "duration": 4.59 }, { "text": "So it seems like SQLite actually\nincrements the primary key for me.", "start": 744.71, "duration": 5.58 }, { "text": "If I add some new row, it looks, what\nis the current highest primary key", "start": 750.29, "duration": 4.89 }, { "text": "and adds 1 to that automatically for me.", "start": 755.18, "duration": 2.94 }, { "text": "And I've gotten that by\nspecifying in my schema", "start": 758.12, "duration": 3.27 }, { "text": "this primary key\nconstraint down on line 6.", "start": 761.39, "duration": 3.66 }, { "text": "So pretty handy for me here.", "start": 765.05, "duration": 2.79 }, { "text": "I see a few other questions too.", "start": 767.84, "duration": 2.19 }, { "text": "So let's take those\nbefore we move on as well.", "start": 770.03, "duration": 4.54 }, { "text": "Let's go to [? Andre. ?]", "start": 774.57, "duration": 1.19 }, { "text": "SPEAKER 3: I just want to ask\nif I delete the, let's say,", "start": 775.76, "duration": 3.27 }, { "text": "the [? first ?] record and type again,\nwill it number it [? on ?] the fourth", "start": 779.03, "duration": 7.38 }, { "text": "number or the first one.", "start": 786.41, "duration": 1.093 }, { "text": "SPEAKER 1: A great question.", "start": 787.503, "duration": 1.167 }, { "text": "We'll see this a little\nbit later on too.", "start": 788.67, "duration": 1.708 }, { "text": "But if I delete some row, let's say, I\ndeleted the row with primary key of 1,", "start": 790.378, "duration": 4.612 }, { "text": "well, what SQLite will\ndo, at least by default,", "start": 794.99, "duration": 3.0 }, { "text": "is actually take the highest value.", "start": 797.99, "duration": 2.07 }, { "text": "Let's say the highest value\nis still three in my database.", "start": 800.06, "duration": 3.28 }, { "text": "It will add 1 to that, and I\nwant to insert that new row it'll", "start": 803.34, "duration": 3.17 }, { "text": "have the [? id ?] of 4 in this case.", "start": 806.51, "duration": 3.163 }, { "text": "OK, so let's explore a bit more\nsome of these constraints on tables.", "start": 809.673, "duration": 4.757 }, { "text": "Here, we're talking about the primary\nkey constraint, but we also have,", "start": 814.43, "duration": 3.42 }, { "text": "as we saw before, the\n[? NOT ?] [? NULL ?] constraint", "start": 817.85, "duration": 2.46 }, { "text": "and the [? UNIQUE ?] constraint.", "start": 820.31, "duration": 1.69 }, { "text": "So let's try inserting given\nthose constraints here.", "start": 822.0, "duration": 3.23 }, { "text": "[? I'll go ?] back to my computer.", "start": 825.23, "duration": 2.05 }, { "text": "And again, notice in my schema,\nI specified that this \"title\"", "start": 827.28, "duration": 5.06 }, { "text": "column should always be NOT NULL.", "start": 832.34, "duration": 3.172 }, { "text": "It should never have a\n[? null ?] value inside of it.", "start": 835.512, "duration": 2.208 }, { "text": "And similarly, \"accession_number\"\nshould also be NOT NULL.", "start": 837.72, "duration": 3.56 }, { "text": "It should also be [? unique. ?]\nI should have no two rows", "start": 841.28, "duration": 3.99 }, { "text": "that have the same accession number.", "start": 845.27, "duration": 2.28 }, { "text": "And maybe kind of a\nplayfully, subversively here,", "start": 847.55, "duration": 3.75 }, { "text": "let's try to run\nagainst this constraint.", "start": 851.3, "duration": 2.158 }, { "text": "Let's actually try to add the same\naccession number and see what happens.", "start": 853.458, "duration": 3.042 }, { "text": "So I'll come back to my\n[? TERMINAL. ?] Let me just reinsert,", "start": 856.5, "duration": 4.4 }, { "text": "let's say \"Spring outing.\"", "start": 860.9, "duration": 1.59 }, { "text": "Notice how if I SELECT [? star ?] FROM\n\"collections,\" it's already in here.", "start": 862.49, "duration": 4.81 }, { "text": "But I'll try to add it yet again with\nthe same [? accession ?] [? number. ?]", "start": 867.3, "duration": 3.45 }, { "text": "So I'll say INSERT INTO \"collections.\"", "start": 870.75, "duration": 1.658 }, { "text": "And by now this is [? hopefully ?]\n[? becoming ?] a little more familiar.", "start": 872.408, "duration": 3.042 }, { "text": "I now want to say the columns\nI want to add data to.", "start": 875.45, "duration": 3.42 }, { "text": "So I'll say the \"title\" column,\nthe \"accession_number\" column,", "start": 878.87, "duration": 7.65 }, { "text": "and also the \"acquired\"\ndate I'll hit Enter.", "start": 886.52, "duration": 3.03 }, { "text": "And now I'll say the VALUES again.", "start": 889.55, "duration": 2.07 }, { "text": "Let's reinsert [? \"Spring ?]\n[? outing.\" ?] I'll say \"Spring outing\"", "start": 891.62, "duration": 3.03 }, { "text": "as a title, the accession\nnumber is 14.76,", "start": 894.65, "duration": 3.99 }, { "text": "and the date it was acquired\nwas again 1914-01-08 semicolon.", "start": 898.64, "duration": 6.03 }, { "text": "Now if I hit Enter, what do we see?", "start": 904.67, "duration": 4.43 }, { "text": "Runtime error-- UNIQUE\nconstraint failed.", "start": 909.1, "duration": 3.39 }, { "text": "So it seems we ran\nagainst this constraint", "start": 912.49, "duration": 2.4 }, { "text": "here that we specified on line 4 here.", "start": 914.89, "duration": 3.57 }, { "text": "[? \"Accession number\" ?]\nshould always be unique.", "start": 918.46, "duration": 2.37 }, { "text": "But by trying to add a new row that\nhad that same accession number,", "start": 920.83, "duration": 3.99 }, { "text": "we ran into this runtime error and\nour operation was not completed.", "start": 924.82, "duration": 4.96 }, { "text": "If I say in this case, SELECT\n[? star ?] FROM \"collections,\"", "start": 929.78, "duration": 3.41 }, { "text": "notice how I guarded myself from\nadding Spring outing more than once", "start": 933.19, "duration": 4.86 }, { "text": "with the same accession number.", "start": 938.05, "duration": 2.02 }, { "text": "So some usefulness here to\nthese kinds of constraints.", "start": 940.07, "duration": 3.56 }, { "text": "Let's try violating NOT NULL too.", "start": 943.63, "duration": 2.55 }, { "text": "I'll try adding a title that is\nactually [? null, ?] non-existent.", "start": 946.18, "duration": 3.072 }, { "text": "It's trying to add a painting\nwithout a title itself.", "start": 949.252, "duration": 2.208 }, { "text": "Let's try this.", "start": 951.46, "duration": 0.96 }, { "text": "I might try INSERT INTO\nthe \"collections\" table,", "start": 952.42, "duration": 3.6 }, { "text": "and I'll add to the \"title\" columns,\nthe \"accession_number,\" and also", "start": 956.02, "duration": 5.64 }, { "text": "the \"acquired\" column.", "start": 961.66, "duration": 2.28 }, { "text": "Now, I'll say the VALUES here.", "start": 963.94, "duration": 2.08 }, { "text": "But remember, this [? null ?] value.", "start": 966.02, "duration": 1.85 }, { "text": "Null meaning nothing.", "start": 967.87, "duration": 1.41 }, { "text": "This value doesn't exist.", "start": 969.28, "duration": 1.35 }, { "text": "I could insert that into my\ntable, or at least try to.", "start": 970.63, "duration": 3.63 }, { "text": "I'll say NULL is a title.", "start": 974.26, "duration": 2.43 }, { "text": "And similarly, NULL is\nthe accession number.", "start": 976.69, "duration": 2.92 }, { "text": "Now let's say, just playfully, we\ngot this painting back in 1900-01-10", "start": 979.61, "duration": 5.84 }, { "text": "like this.", "start": 985.45, "duration": 1.32 }, { "text": "OK now, semicolon.", "start": 986.77, "duration": 2.19 }, { "text": "I'll hit Enter, and we see the same\nruntime error-- constraint failed,", "start": 988.96, "duration": 5.01 }, { "text": "in this case, the NOT NULL constraint.", "start": 993.97, "duration": 2.23 }, { "text": "So notice again how in my schema, I\nspecified \"title\" should be NOT NULL.", "start": 996.2, "duration": 5.75 }, { "text": "But here when I try to insert a\n[? null ?] [? value, ?] I run into that", "start": 1001.95, "duration": 4.14 }, { "text": "constraint and I can't\ninsert that value.", "start": 1006.09, "duration": 2.58 }, { "text": "If I try to SELECT [? star ?]\nFROM \"collections,\"", "start": 1008.67, "duration": 2.73 }, { "text": "I should see in this case\nthat insert didn't work.", "start": 1011.4, "duration": 6.01 }, { "text": "So these constraints are\nreally guardrails against me,", "start": 1017.41, "duration": 3.11 }, { "text": "against other users to\nadding values that we really", "start": 1020.52, "duration": 2.46 }, { "text": "shouldn't add to our table.", "start": 1022.98, "duration": 3.5 }, { "text": "OK, so presumably here, we're onto\nsomething by adding some rows.", "start": 1026.48, "duration": 5.92 }, { "text": "But if the museum acquires more\nthan one item, maybe 100 at a time,", "start": 1032.4, "duration": 5.33 }, { "text": "I don't want to be the programmer who's\nsitting there writing 100 INSERT INTO.", "start": 1037.73, "duration": 3.389 }, { "text": "That's like not what I want to do.", "start": 1041.119, "duration": 1.621 }, { "text": "There's probably a\nbetter way to do this.", "start": 1042.74, "duration": 1.89 }, { "text": "[? And ?] [? we ?] show\nyou one way to do this.", "start": 1044.63, "duration": 2.41 }, { "text": "One way is to instead of inserting\none row, like this, to instead insert", "start": 1047.04, "duration": 5.15 }, { "text": "multiple separated by commas.", "start": 1052.19, "duration": 2.71 }, { "text": "So here, I might say this is my\nfirst row for each of these columns.", "start": 1054.9, "duration": 4.97 }, { "text": "This is my second row, again,\nfor each of these columns,", "start": 1059.87, "duration": 3.21 }, { "text": "and so on and so forth.", "start": 1063.08, "duration": 2.29 }, { "text": "And it's worth noting, this is\nnot just a manner of convenience.", "start": 1065.37, "duration": 4.16 }, { "text": "Like if I tried to insert 100 rows,\nthis is certainly convenient for me.", "start": 1069.53, "duration": 4.6 }, { "text": "But it's also most often\nin most cases going", "start": 1074.13, "duration": 3.08 }, { "text": "to be faster for me to\ninsert 100 rows in one go,", "start": 1077.21, "duration": 3.99 }, { "text": "than to insert one row 100 times.", "start": 1081.2, "duration": 3.19 }, { "text": "So this is both a convenience\nthing for programmers", "start": 1084.39, "duration": 2.54 }, { "text": "and also an efficiency thing to\nactually optimize your database as well.", "start": 1086.93, "duration": 4.81 }, { "text": "So let's try this syntax now to avoid\nme sitting there for hours and hours", "start": 1091.74, "duration": 4.61 }, { "text": "writing many insert interviews.", "start": 1096.35, "duration": 1.89 }, { "text": "I'll go back to my computer.", "start": 1098.24, "duration": 2.22 }, { "text": "And this time, let's say we\ngot two paintings at once.", "start": 1100.46, "duration": 3.48 }, { "text": "We got two.", "start": 1103.94, "duration": 0.81 }, { "text": "So I'll try to add both of them.", "start": 1104.75, "duration": 1.92 }, { "text": "I'll say INSERT INTO\nthe \"collections\" table.", "start": 1106.67, "duration": 3.84 }, { "text": "And again, I want to keep adding to my\n\"title\" column, my \"accession_number\"", "start": 1110.51, "duration": 5.22 }, { "text": "column, and even my\n\"acquired\" column, like this.", "start": 1115.73, "duration": 4.47 }, { "text": "Now, I want to add in some values.", "start": 1120.2, "duration": 2.44 }, { "text": "So I'll type VALUES here.", "start": 1122.64, "duration": 1.73 }, { "text": "And as a matter of style, let me\njust say I'll make a new line.", "start": 1124.37, "duration": 4.02 }, { "text": "I'll Enter here.", "start": 1128.39, "duration": 1.26 }, { "text": "And now I can type each of my\nnew rows on their very own line.", "start": 1129.65, "duration": 4.71 }, { "text": "So again, I'll type all of the\nvalues that I want in my first row.", "start": 1134.36, "duration": 4.86 }, { "text": "I might call this one\n\"Imaginative landscape.\"", "start": 1139.22, "duration": 3.0 }, { "text": "We got this one back in--", "start": 1142.22, "duration": 1.565 }, { "text": "actually, we don't quite\nknow when we got this one.", "start": 1143.785, "duration": 2.125 }, { "text": "If I type 56.496 is the accession\nnumber, like, yeah, 56.496.", "start": 1145.91, "duration": 6.3 }, { "text": "The MFA actually doesn't know\nwhen they got this painting.", "start": 1152.21, "duration": 3.22 }, { "text": "So if they don't know,\nlet's just insert NULL.", "start": 1155.43, "duration": 3.38 }, { "text": "This is intentionally left blank.", "start": 1158.81, "duration": 3.99 }, { "text": "This is my first row.", "start": 1162.8, "duration": 1.35 }, { "text": "Now, I want to add a\nsecond one in one go.", "start": 1164.15, "duration": 2.82 }, { "text": "I'll follow this up with a comma.", "start": 1166.97, "duration": 1.72 }, { "text": "And now, I'll type out\nmy next set of values.", "start": 1168.69, "duration": 3.56 }, { "text": "This next one we acquired is\ncalled \"Peonies and butterfly.\"", "start": 1172.25, "duration": 3.75 }, { "text": "Now, I'll say the [? accession ?]\n[? number ?] is 06.1899.", "start": 1176.0, "duration": 4.71 }, { "text": "And we got this one-- it was back in\n1906-01-01 now I'll hit semicolon here.", "start": 1180.71, "duration": 7.86 }, { "text": "And now, if I hit\nEnter, nothing happens.", "start": 1188.57, "duration": 3.73 }, { "text": "But if I type Select [? star ?]\nFROM \"collections\" semicolon,", "start": 1192.3, "duration": 5.03 }, { "text": "what do we see, but now two\nnew rows being inserted.", "start": 1197.33, "duration": 4.63 }, { "text": "So a handy way to insert\nmore than one value.", "start": 1201.96, "duration": 3.54 }, { "text": "And also, if you have a lot of\nvalues, a more efficient way as well.", "start": 1205.5, "duration": 5.01 }, { "text": "So we pause here.", "start": 1210.51, "duration": 1.52 }, { "text": "And ask what questions we have on\nINSERT INTO, Whether adding one row", "start": 1212.03, "duration": 5.9 }, { "text": "or adding multiple.", "start": 1217.93, "duration": 1.62 }, { "text": "Let's go to [INAUDIBLE].", "start": 1219.55, "duration": 1.77 }, { "text": "SPEAKER 4: [? So ?] [? imagine ?]\nyou are [? writing ?] [? code ?]", "start": 1221.32, "duration": 2.75 }, { "text": "or inserting a row, so by mistake\nyou have entered the wrong spelling", "start": 1224.07, "duration": 4.63 }, { "text": "of the title, so how we will rename it?", "start": 1228.7, "duration": 3.017 }, { "text": "SPEAKER 1: A great question too.", "start": 1231.717, "duration": 1.333 }, { "text": "And often as is the case with\nme, I make typos all the time.", "start": 1233.05, "duration": 2.85 }, { "text": "I might add the artwork title\nand misspell it for instance.", "start": 1235.9, "duration": 4.14 }, { "text": "Well, in that case, I can't\nuse INSERT INTO to correct it,", "start": 1240.04, "duration": 4.56 }, { "text": "but I can use a new statement we'll\nsee a little later called [? update. ?]", "start": 1244.6, "duration": 3.81 }, { "text": "And so with [? update, ?]\nyou can actually", "start": 1248.41, "duration": 1.8 }, { "text": "change the spellings of things.", "start": 1250.21, "duration": 1.62 }, { "text": "And we'll see that a little later today.", "start": 1251.83, "duration": 2.1 }, { "text": "But great question to\npeek ahead as well.", "start": 1253.93, "duration": 3.84 }, { "text": "OK, so let's keep going here.", "start": 1257.77, "duration": 2.5 }, { "text": "Let's think about how we can keep\nadding values to our data set.", "start": 1260.27, "duration": 4.23 }, { "text": "And so far we've seen INSERT INTO\nwith one row and with multiple.", "start": 1264.5, "duration": 5.12 }, { "text": "But one more way to keep in\nmind is you might already", "start": 1269.62, "duration": 4.02 }, { "text": "have your data, perhaps\nin some other format.", "start": 1273.64, "duration": 3.66 }, { "text": "And one common format looks a bit\nlike this on our screen over here.", "start": 1277.3, "duration": 5.64 }, { "text": "This file is called CSV\nfor Comma Separated Values.", "start": 1282.94, "duration": 6.81 }, { "text": "Now, why Comma Separated Values?", "start": 1289.75, "duration": 2.01 }, { "text": "Well, let's just look at it.", "start": 1291.76, "duration": 1.59 }, { "text": "Here we see some presumably\ncolumn or column names,", "start": 1293.35, "duration": 3.36 }, { "text": "like [? id, ?] [? title, ?]\n[? accession number, ?]", "start": 1296.71, "duration": 2.82 }, { "text": "and [? acquired, ?] but what\nseparates these column names?", "start": 1299.53, "duration": 3.45 }, { "text": "Well, it looks like commas--", "start": 1302.98, "duration": 1.53 }, { "text": "\"id\" comma \"title,\" comma\n\"accession_number,\" and so on.", "start": 1304.51, "duration": 3.96 }, { "text": "Every row is still its own row.", "start": 1308.47, "duration": 2.88 }, { "text": "That makes sense here.", "start": 1311.35, "duration": 1.32 }, { "text": "But now, those row values\nare also separated by commas.", "start": 1312.67, "duration": 4.74 }, { "text": "Notice here, the first\nvalue before the first comma", "start": 1317.41, "duration": 3.69 }, { "text": "corresponds to the first\ncolumn, this id is 1.", "start": 1321.1, "duration": 4.11 }, { "text": "Similarly, the next Comma Separated\nValue Profusion of flowers", "start": 1325.21, "duration": 3.84 }, { "text": "belongs to that next column\nhere-- \"title\" as well,", "start": 1329.05, "duration": 3.4 }, { "text": "and so on and so forth.", "start": 1332.45, "duration": 1.62 }, { "text": "You could if you wanted\nto draw a snaky line", "start": 1334.07, "duration": 2.75 }, { "text": "to see how these columns\ncorrespond down our file here.", "start": 1336.82, "duration": 4.96 }, { "text": "Now, you might often have\ndata already in this format.", "start": 1341.78, "duration": 3.94 }, { "text": "And it's actually\npretty convenient to try", "start": 1345.72, "duration": 1.75 }, { "text": "to import this data into\nSQLite into your very own table", "start": 1347.47, "duration": 4.29 }, { "text": "so you can use [? SQL ?] on it.", "start": 1351.76, "duration": 2.41 }, { "text": "So let's try doing that\nhere, taking a CSV like this", "start": 1354.17, "duration": 3.56 }, { "text": "and adding it to our database.", "start": 1357.73, "duration": 2.88 }, { "text": "I'll come back to my computer.", "start": 1360.61, "duration": 1.99 }, { "text": "And at this point, I want to start over.", "start": 1362.6, "duration": 3.27 }, { "text": "Let's say, I didn't use\n[? insert ?] at all.", "start": 1365.87, "duration": 2.72 }, { "text": "I actually got a CSV from the MFA\nof all the items in our collection.", "start": 1368.59, "duration": 3.97 }, { "text": "So I'll type .quit to\nleave this database.", "start": 1372.56, "duration": 2.78 }, { "text": "And now, I'll type rm mfa.db to\nremove this file all together.", "start": 1375.34, "duration": 5.7 }, { "text": "Now, let me show you mfa.csv, the\nCSV file I have that looks exactly", "start": 1381.04, "duration": 6.03 }, { "text": "like what we just saw.", "start": 1387.07, "duration": 1.41 }, { "text": "I'll type code mfa.csv.", "start": 1388.48, "duration": 2.82 }, { "text": "And now I can indeed see, I\nhave an iD column, a title", "start": 1391.3, "duration": 4.14 }, { "text": "column, and accession_number,\nand the date these pieces were", "start": 1395.44, "duration": 3.39 }, { "text": "acquired all separated by commas.", "start": 1398.83, "duration": 3.76 }, { "text": "So let's say I want to quickly import\nthis data into its very own database", "start": 1402.59, "duration": 5.69 }, { "text": "to run [? SQL ?] [? queries ?] on it.", "start": 1408.28, "duration": 1.77 }, { "text": "As it stands, I can't use [? SQL ?]\n[? queries ?] on this table", "start": 1410.05, "duration": 3.15 }, { "text": "because it's not inside\nof a SQLite database.", "start": 1413.2, "duration": 2.91 }, { "text": "But let me add one.", "start": 1416.11, "duration": 1.26 }, { "text": "I'll type SQLite3 mfa.db to\nremake [? MFA ?] down below.", "start": 1417.37, "duration": 6.1 }, { "text": "Let me now introduce this new\ncommand that is not a SQL keyword,", "start": 1423.47, "duration": 4.55 }, { "text": "but is actually a SQLite command.", "start": 1428.02, "duration": 2.38 }, { "text": "And this command is called .import.", "start": 1430.4, "duration": 3.77 }, { "text": ".import lets you take\na file, like a CSV,", "start": 1434.17, "duration": 3.39 }, { "text": "and automatically insert it row by row\ninto a table of your own making or one", "start": 1437.56, "duration": 6.45 }, { "text": "you can let SQLite create for you.", "start": 1444.01, "duration": 2.58 }, { "text": "Let's actually create\nthe table ourselves,", "start": 1446.59, "duration": 2.07 }, { "text": "and then insert this CSV using .import.", "start": 1448.66, "duration": 3.42 }, { "text": "So I'll go back to SQLite.", "start": 1452.08, "duration": 1.91 }, { "text": "Let me recreate the schema.", "start": 1453.99, "duration": 1.84 }, { "text": "Right now, there's no tables inside\nthis database, but let me create one.", "start": 1455.83, "duration": 4.66 }, { "text": "I'll instead type .read schema.sql\nto read in that old schema.sql file,", "start": 1460.49, "duration": 5.75 }, { "text": "so I can have my own table here.", "start": 1466.24, "duration": 2.22 }, { "text": "I'll type that schema again.", "start": 1468.46, "duration": 1.54 }, { "text": "And now I see, I have that table back.", "start": 1470.0, "duration": 3.15 }, { "text": "But if I type SELECT [? star ?]\nFROM \"collections\" semicolon,", "start": 1473.15, "duration": 4.94 }, { "text": "nothing is inside.", "start": 1478.09, "duration": 1.89 }, { "text": "Well, I could fix this.", "start": 1479.98, "duration": 1.09 }, { "text": "I could to import this CSV into\ncollections to insert all at once from", "start": 1481.07, "duration": 5.15 }, { "text": "[? its ?] [? own, ?] from the\nCSV file we have right here.", "start": 1486.22, "duration": 3.13 }, { "text": "So to do this, let me type .import.", "start": 1489.35, "duration": 3.56 }, { "text": "Now before I finish this off, there are\na few arguments or options I can give", "start": 1492.91, "duration": 6.36 }, { "text": ".import to make sure it works properly.", "start": 1499.27, "duration": 3.39 }, { "text": "The ones for this look as follows.", "start": 1502.66, "duration": 2.77 }, { "text": "I should type .import, and then\n[? dash ?] [? dash ?] CSV to say", "start": 1505.43, "duration": 5.3 }, { "text": "I'm importing a CSV.", "start": 1510.73, "duration": 1.83 }, { "text": "If I don't type\n[? dash ?] [? dash ?] CSV,", "start": 1512.56, "duration": 3.06 }, { "text": "SQLite I might assume something\nabout this file that just isn't true.", "start": 1515.62, "duration": 3.42 }, { "text": "It should know that numbers\nare here separated by commas,", "start": 1519.04, "duration": 3.45 }, { "text": "[? numbers, ?] or [? values, ?]\nwhatever's inside my table are", "start": 1522.49, "duration": 2.64 }, { "text": "separated by commas.", "start": 1525.13, "duration": 2.17 }, { "text": "And then [? dash ?] [? dash ?] skip 1--", "start": 1527.3, "duration": 2.54 }, { "text": "well, let's take a look.", "start": 1529.84, "duration": 1.23 }, { "text": "If I go back to this\nfile here, let's see.", "start": 1531.07, "duration": 3.21 }, { "text": "I have the first row, the second\nrow, the third row, the fourth row.", "start": 1534.28, "duration": 4.62 }, { "text": "Are there any rows I probably\nshouldn't insert into my table here?", "start": 1538.9, "duration": 5.015 }, { "text": "Let me ask the audience.", "start": 1543.915, "duration": 1.0 }, { "text": "You're free to raise your hand.", "start": 1544.915, "duration": 1.545 }, { "text": "Are there any rows I shouldn't\ninsert into this table?", "start": 1546.46, "duration": 3.315 }, { "text": "Let's go to [? Sukanya. ?]", "start": 1552.97, "duration": 1.35 }, { "text": "SPEAKER 5: Yes, I think they\nshouldn't include the first one,", "start": 1554.32, "duration": 3.09 }, { "text": "because it doesn't give\nus any value that we need.", "start": 1557.41, "duration": 2.5 }, { "text": "SPEAKER 1: Yeah, you're right.", "start": 1559.91, "duration": 0.54 }, { "text": "So if I look at this\nheader row, as we call it,", "start": 1560.45, "duration": 2.18 }, { "text": "I see id, title, accession_number, and\nacquired-- the names for my columns.", "start": 1562.63, "duration": 5.67 }, { "text": "But notice in my schema, I already\nhave those column names existing.", "start": 1568.3, "duration": 4.62 }, { "text": "So I shouldn't insert the\nvalue id into my \"id\" column.", "start": 1572.92, "duration": 4.92 }, { "text": "I want to just skip that row\nand only do the next one.", "start": 1577.84, "duration": 3.22 }, { "text": "So here by typing [? dash ?] [? dash ?]\nskip 1, I'll skip that header row.", "start": 1581.06, "duration": 4.82 }, { "text": "So let's try this now.", "start": 1585.88, "duration": 1.02 }, { "text": "I'll say .import\n[? dash ?] [? dash ?] CSV.", "start": 1586.9, "duration": 2.52 }, { "text": "I'm going to import a CSV.", "start": 1589.42, "duration": 1.874 }, { "text": "Now, [? dash ?] [? dash ?] skip,\nhow many rows should I skip?", "start": 1591.294, "duration": 3.346 }, { "text": "Well, just one, the header row there.", "start": 1594.64, "duration": 2.4 }, { "text": "Now, I say the file to import.", "start": 1597.04, "duration": 2.46 }, { "text": "So I'll type mfa.csv, the CSV\nfile to import into my database.", "start": 1599.5, "duration": 5.46 }, { "text": "Now, I type the name of the\ntable I'm importing into,", "start": 1604.96, "duration": 4.08 }, { "text": "collections, in this case.", "start": 1609.04, "duration": 1.99 }, { "text": "And notice how I no longer need to\n[? quote ?] collections or [? quote ?]", "start": 1611.03, "duration": 5.03 }, { "text": "CSV, because this is not a SQL\nstatement this is a SQLite statement,", "start": 1616.06, "duration": 5.448 }, { "text": "so I can get away with not\n[? quoting ?] anything here.", "start": 1621.508, "duration": 2.292 }, { "text": "Now, I'll hit Enter.", "start": 1623.8, "duration": 2.01 }, { "text": "And again, nothing seems to happen,\nbut it's probably a good sign.", "start": 1625.81, "duration": 2.76 }, { "text": "Let me try selecting [? star ?]\n[? from ?] \"collections\" semicolon,", "start": 1628.57, "duration": 5.67 }, { "text": "Enter.", "start": 1634.24, "duration": 0.9 }, { "text": "And now all my data is\njust magically in there.", "start": 1635.14, "duration": 2.82 }, { "text": "It's went from my CSV\ninto my very own table.", "start": 1637.96, "duration": 5.943 }, { "text": "OK, so that's a pretty\nnice step forward.", "start": 1643.903, "duration": 3.687 }, { "text": "No longer do we have to use\njust single line inserts or even", "start": 1647.59, "duration": 3.51 }, { "text": "bulk inserts, those multi-line inserts.", "start": 1651.1, "duration": 2.43 }, { "text": "We can now just import an\nentire table from a CSV.", "start": 1653.53, "duration": 4.26 }, { "text": "But I think I've been showing you a\nbit of an inaccuracy here, at least", "start": 1657.79, "duration": 4.29 }, { "text": "that's not often going to happen to you.", "start": 1662.08, "duration": 2.4 }, { "text": "If I go back to this\nfile here, this CSV.", "start": 1664.48, "duration": 4.8 }, { "text": "What you might notice is that\nI specified the primary key.", "start": 1669.28, "duration": 4.2 }, { "text": "I said [? id ?] of 1,\n[? id ?] of 2, [? id ?] of 3.", "start": 1673.48, "duration": 3.99 }, { "text": "But if we'd heard before, this\nmight not be the best design.", "start": 1677.47, "duration": 3.72 }, { "text": "Because what if I import\nthis CSV, and there's already", "start": 1681.19, "duration": 3.42 }, { "text": "an item that has the id\nof [? 1 ?] or the id of 2.", "start": 1684.61, "duration": 3.21 }, { "text": "Ideally, I could let SQLite\ncreate its very own primary keys", "start": 1687.82, "duration": 4.2 }, { "text": "for each of these rows.", "start": 1692.02, "duration": 1.68 }, { "text": "And more often than\nnot, you'll likely have", "start": 1693.7, "duration": 2.25 }, { "text": "a database or CSV that looks a bit\nmore like this without the primary key.", "start": 1695.95, "duration": 4.92 }, { "text": "But you'll still want to have a primary\nkey when you import into your database.", "start": 1700.87, "duration": 4.84 }, { "text": "So let's try working\nwith this kind of CSV now", "start": 1705.71, "duration": 3.29 }, { "text": "and having SQLite generate some\nof the primary keys for us.", "start": 1709.0, "duration": 3.87 }, { "text": "Come back to my computer.", "start": 1712.87, "duration": 1.86 }, { "text": "And let's update our mfa.csv just to\nremove those primary key column here.", "start": 1714.73, "duration": 5.71 }, { "text": "So I will open up again mfa.csv.", "start": 1720.44, "duration": 4.46 }, { "text": "And let me try to just delete\nthis column all together.", "start": 1724.9, "duration": 2.7 }, { "text": "I'll move [? id, ?] remove 1,\nand 2, and 3, and 4, and 5.", "start": 1727.6, "duration": 7.08 }, { "text": "Let me save it, and now\nthat id column is gone.", "start": 1734.68, "duration": 4.57 }, { "text": "But now there's a problem.", "start": 1739.25, "duration": 1.77 }, { "text": "Like if I go to my schema,\nand I type .schema,", "start": 1741.02, "duration": 4.53 }, { "text": "notice how there are four\ncolumns in this table--", "start": 1745.55, "duration": 3.75 }, { "text": "\"id,\" \"title,\" \"accession_number,\"\nand \"acquired.\"", "start": 1749.3, "duration": 4.05 }, { "text": "Well, now my CSV only has three columns.", "start": 1753.35, "duration": 3.64 }, { "text": "So if I try to import\nthis CSV into this table,", "start": 1756.99, "duration": 3.53 }, { "text": "I'm going to run into trouble, because\nI have different numbers of columns", "start": 1760.52, "duration": 3.21 }, { "text": "[? between ?] my CSV and my table.", "start": 1763.73, "duration": 3.025 }, { "text": "Well, what can I do instead?", "start": 1766.755, "duration": 2.375 }, { "text": "It turns out that I could\nactually import this data", "start": 1769.13, "duration": 3.78 }, { "text": "into a temporary table,\nand then take that data", "start": 1772.91, "duration": 3.57 }, { "text": "and insert that data in that\ntemporary table into my real one", "start": 1776.48, "duration": 3.57 }, { "text": "called \"collections\" here.", "start": 1780.05, "duration": 1.62 }, { "text": "I can use both .import and INSERT\nINTO to accomplish that task for me.", "start": 1781.67, "duration": 6.0 }, { "text": "So let's try this.", "start": 1787.67, "duration": 1.14 }, { "text": "I'll say .import.", "start": 1788.81, "duration": 1.95 }, { "text": "I want to import a CSV now-- [? dash ?]\n[? dash ?] [? CSV. ?] Which one?", "start": 1790.76, "duration": 4.23 }, { "text": "Well, mfa.csv in this case.", "start": 1794.99, "duration": 3.09 }, { "text": "Now, though, I want to create a brand\nnew table that doesn't yet exist.", "start": 1798.08, "duration": 3.822 }, { "text": "So I'll type the name of the table.", "start": 1801.902, "duration": 1.458 }, { "text": "And I just call it temp for\nnow to temporarily import", "start": 1803.36, "duration": 3.54 }, { "text": "this data into a brand new table.", "start": 1806.9, "duration": 2.49 }, { "text": "And notice how here I'm not\nusing [? dash ?] [? dash ?]", "start": 1809.39, "duration": 2.87 }, { "text": "skip 1, because now I want to take\nadvantage of those header rows.", "start": 1812.26, "duration": 4.09 }, { "text": "SQLite, if I import into a new table,\nwill notice I have a header row", "start": 1816.35, "duration": 6.46 }, { "text": "and make my columns the very same\nnames that are in my header row--", "start": 1822.81, "duration": 4.79 }, { "text": "title, accession number, and acquired.", "start": 1827.6, "duration": 2.61 }, { "text": "I don't want to skip them.", "start": 1830.21, "duration": 1.17 }, { "text": "I want SQLite to see them and create\nthis table using those header rows.", "start": 1831.38, "duration": 5.23 }, { "text": "So now, I'll hit Enter, and\nnothing seems to happen.", "start": 1836.61, "duration": 3.38 }, { "text": "But if I type .schema, what do I see,\nbut a brand new table called \"temp\"", "start": 1839.99, "duration": 5.01 }, { "text": "that SQLite has just made automatically\nand notice how it used that header row.", "start": 1845.0, "duration": 4.05 }, { "text": "I have \"title,\" \"accession_number,\"\nand \"acquired\" as my column names.", "start": 1849.05, "duration": 5.745 }, { "text": "OK, let's now look\ninside the temp table,", "start": 1854.795, "duration": 2.585 }, { "text": "I'll say SELECT [? star ?]\nFROM \"temp\" semicolon.", "start": 1857.38, "duration": 5.14 }, { "text": "And now, I see all that data in there.", "start": 1862.52, "duration": 1.83 }, { "text": "But I don't have primary keys yet.", "start": 1864.35, "duration": 1.85 }, { "text": "So my goal [? is ?] [? really ?]\nto take all this data and insert it", "start": 1866.2, "duration": 3.87 }, { "text": "into my collections table to give it\nthat primary key I've been wanting", "start": 1870.07, "duration": 4.56 }, { "text": "here.", "start": 1874.63, "duration": 0.72 }, { "text": "Again, if I type SELECT [? star ?]\nFROM \"collections,\" well, nothing-- oh,", "start": 1875.35, "duration": 5.648 }, { "text": "something is still there.", "start": 1880.998, "duration": 1.042 }, { "text": "Let me actually just\ndelete this [INAUDIBLE]", "start": 1882.04, "duration": 2.177 }, { "text": "I'll say DELETE FROM \"collections\"\nto remove it all together-- we'll", "start": 1884.217, "duration": 2.833 }, { "text": "[? get ?] [INAUDIBLE] just a minute.", "start": 1887.05, "duration": 1.5 }, { "text": "And now let me try, SELECT\n[? star ?] FROM \"collections,\"", "start": 1888.55, "duration": 4.47 }, { "text": "and I should see nothing\ninside of collections.", "start": 1893.02, "duration": 3.73 }, { "text": "So what could I do?", "start": 1896.75, "duration": 1.29 }, { "text": "I could [? INSERT ?] [? INTO ?]\n[? collections ?] using the data from", "start": 1898.04, "duration": 3.59 }, { "text": "my temp table.", "start": 1901.63, "duration": 1.5 }, { "text": "So I'll try that.", "start": 1903.13, "duration": 0.85 }, { "text": "I'll say INSERT INTO \"collections\"\nand choose those columns yet again.", "start": 1903.98, "duration": 5.48 }, { "text": "I'll say the \"title,\" and\nthe \"accession_number,\"", "start": 1909.46, "duration": 4.99 }, { "text": "and now let's go for the\n\"acquired\" column as well.", "start": 1914.45, "duration": 4.45 }, { "text": "I'll hit Enter.", "start": 1918.9, "duration": 1.47 }, { "text": "But instead of typing\nmany new lines of values,", "start": 1920.37, "duration": 3.96 }, { "text": "I actually have a way to dynamically\nselect all the values I want to insert,", "start": 1924.33, "duration": 5.91 }, { "text": "and it looks a bit like this.", "start": 1930.24, "duration": 1.93 }, { "text": "I could say INSERT INTO [? some ?]\ntable and some columns of that table.", "start": 1932.17, "duration": 6.72 }, { "text": "But I want to insert the results\nof this SELECT down below.", "start": 1938.89, "duration": 4.44 }, { "text": "So I'll [? SELECT ?] some columns\n[? FROM ?] some separate table.", "start": 1943.33, "duration": 3.39 }, { "text": "And so long as these columns align, I'll\nbe able to actually take the results", "start": 1946.72, "duration": 5.13 }, { "text": "of that [? SELECT ?] and [? insert ?]\nall of them [? into ?] this new table", "start": 1951.85, "duration": 3.33 }, { "text": "using INSERT INTO.", "start": 1955.18, "duration": 1.89 }, { "text": "So let me finish my statement\nhere, we can see the results.", "start": 1957.07, "duration": 2.85 }, { "text": "Come back to my computer.", "start": 1959.92, "duration": 2.13 }, { "text": "And here, I'll type a SELECT to get\nback all the items from my temp table.", "start": 1962.05, "duration": 5.64 }, { "text": "I'll say SELECT-- in\nthis case, SELECT, let's", "start": 1967.69, "duration": 4.38 }, { "text": "go for the \"title\" column,\nthe \"accession_number\"", "start": 1972.07, "duration": 4.38 }, { "text": "column, and the \"acquired\"\ncolumn from my \"temp\" table.", "start": 1976.45, "duration": 4.65 }, { "text": "Now, if I hit semicolon, I should see--", "start": 1981.1, "duration": 4.17 }, { "text": "well, nothing at first.", "start": 1985.27, "duration": 1.2 }, { "text": "But if I type SELECT\n[? star ?] FROM \"collections,\"", "start": 1986.47, "duration": 4.53 }, { "text": "I see all my data now in there\nselected from my temporary table.", "start": 1991.0, "duration": 5.61 }, { "text": "And now if I type\n.schema, I still see temp.", "start": 1996.61, "duration": 3.013 }, { "text": "But what can I do now?", "start": 1999.623, "duration": 0.917 }, { "text": "I could just delete\nthat table altogether.", "start": 2000.54, "duration": 2.17 }, { "text": "I could say DROP TABLE \"temp,\" as we\nsaw last week, semicolon, hit Enter,", "start": 2002.71, "duration": 5.66 }, { "text": ".schema again.", "start": 2008.37, "duration": 1.72 }, { "text": "And now, we're back where we\nwant to be with a single table,", "start": 2010.09, "duration": 3.59 }, { "text": "and now we've import our data\nfrom our CSV with primary keys.", "start": 2013.68, "duration": 5.35 }, { "text": "If I type \"collections\" here,\nwe see it all in this table.", "start": 2019.03, "duration": 5.62 }, { "text": "OK, so we've seen several\nversions of import, one", "start": 2024.65, "duration": 3.54 }, { "text": "to import into an existing table and\none to import into a brand new table.", "start": 2028.19, "duration": 4.27 }, { "text": "Let me ask then what questions we have\non how to use .import or how to insert", "start": 2032.46, "duration": 6.08 }, { "text": "more data into our database.", "start": 2038.54, "duration": 4.7 }, { "text": "Let's go [? Louise. ?]", "start": 2043.24, "duration": 1.19 }, { "text": "SPEAKER 6: Can we ask the\nINSERT INTO command to place", "start": 2044.43, "duration": 3.9 }, { "text": "a column in a specific position?", "start": 2048.33, "duration": 2.057 }, { "text": "SPEAKER 1: Good question.", "start": 2050.387, "duration": 1.042 }, { "text": "Can we ask INSERT INTO the place\na column in a certain position?", "start": 2051.429, "duration": 4.599 }, { "text": "There's a few ways of getting [? it-- ?]\nwhat I think you're asking here,", "start": 2056.028, "duration": 3.042 }, { "text": "so let me show you a few of those here.", "start": 2059.07, "duration": 1.625 }, { "text": "I'll come back to my computer, and let\nme pull up the syntax for INSERT INTO", "start": 2060.695, "duration": 5.694 }, { "text": "again.", "start": 2066.389, "duration": 0.821 }, { "text": "I will choose this, and I'll\nshow it on the screen over here.", "start": 2067.21, "duration": 4.59 }, { "text": "So I'll say INSERT INTO some\ntable given some columns.", "start": 2071.8, "duration": 4.609 }, { "text": "And notice how down below,\nI actually have [? values ?]", "start": 2076.409, "duration": 2.73 }, { "text": "to insert into those columns.", "start": 2079.139, "duration": 2.461 }, { "text": "I could, to your question,\nrearrange these values.", "start": 2081.6, "duration": 3.52 }, { "text": "So I might have this first value here is\nsome value that goes into this column.", "start": 2085.12, "duration": 4.55 }, { "text": "The second column here is\nattached to that column.", "start": 2089.67, "duration": 2.489 }, { "text": "And that would rearrange the\nvalues I insert into those columns.", "start": 2092.159, "duration": 3.841 }, { "text": "If you're asking, though, if\nI can reorder the columns,", "start": 2096.0, "duration": 3.27 }, { "text": "that is up to [? CREATE ?] [? TABLE ?]\nand [? CREATE ?] [? TABLE ?] only.", "start": 2099.27, "duration": 3.463 }, { "text": "And in general, I encourage you not so\nmuch out of ordering of your columns,", "start": 2102.733, "duration": 3.167 }, { "text": "[? because ?] [? it ?] could\nbe in any order whatsoever.", "start": 2105.9, "duration": 2.333 }, { "text": "But you could just rearrange\nyour selection of columns", "start": 2108.233, "duration": 2.647 }, { "text": "here to insert the data\nyou want to insert.", "start": 2110.88, "duration": 3.9 }, { "text": "OK, let's take one more question here.", "start": 2114.78, "duration": 2.83 }, { "text": "SPEAKER 7: What happens if one of\nthe rows you're trying to insert", "start": 2117.61, "duration": 2.78 }, { "text": "violates a constraint on the table?", "start": 2120.39, "duration": 1.95 }, { "text": "SPEAKER 1: Yeah, so here\nwe're inserting multiple rows.", "start": 2122.34, "duration": 2.8 }, { "text": "And if one actually\nviolates some constraint,", "start": 2125.14, "duration": 3.29 }, { "text": "then we won't actually\ninsert any of those rows.", "start": 2128.43, "duration": 3.18 }, { "text": "And this is because this\ninsert is bundled inside", "start": 2131.61, "duration": 2.4 }, { "text": "of a transaction, which we'll learn\nmore about later on in the course.", "start": 2134.01, "duration": 4.63 }, { "text": "Let's take another one.", "start": 2138.64, "duration": 1.593 }, { "text": "SPEAKER 8: I noticed that when you did\nSELECT [? star ?] FROM \"collections,\"", "start": 2140.233, "duration": 3.167 }, { "text": "that one of the acquired\ndates was just blank.", "start": 2143.4, "duration": 2.34 }, { "text": "It didn't say NULL.", "start": 2145.74, "duration": 1.29 }, { "text": "Is that because the CSV itself just\nhad an empty value next to the comma?", "start": 2147.03, "duration": 4.26 }, { "text": "SPEAKER 1: Yeah, great observations.", "start": 2151.29, "duration": 1.5 }, { "text": "So let me try this again.", "start": 2152.79, "duration": 1.06 }, { "text": "So we can see the results\nof this [? SELECT. ?]", "start": 2153.85, "duration": 1.958 }, { "text": "I'll come back to my computer\nhere, and let me show you again.", "start": 2155.808, "duration": 4.212 }, { "text": "If I say SELECT [? star ?]\nFROM \"collections\"", "start": 2160.02, "duration": 3.12 }, { "text": "semicolon, notice how this acquired\ncolumn-- this acquired cell here, if I", "start": 2163.14, "duration": 4.83 }, { "text": "go back to my screen, it's just blank.", "start": 2167.97, "duration": 3.36 }, { "text": "But we saw before that if I\n[? selected ?] [? star, ?] I would have", "start": 2171.33, "duration": 3.39 }, { "text": "seen [? NULL ?] there if this\nvalue was truly [? NULL. ?] Well,", "start": 2174.72, "duration": 4.74 }, { "text": "it turns out one downside of importing\nfrom a CSV is that all of your data is", "start": 2179.46, "duration": 5.43 }, { "text": "imported initially as text.", "start": 2184.89, "duration": 2.34 }, { "text": "And if I have just a\nblank cell in my CSV,", "start": 2187.23, "duration": 3.69 }, { "text": "it won't be converted to\n[? NULL ?] automatically.", "start": 2190.92, "duration": 3.21 }, { "text": "I need to do that myself\nperhaps using an [? update ?]", "start": 2194.13, "duration": 2.7 }, { "text": "statement that we'll see in just a bit.", "start": 2196.83, "duration": 1.96 }, { "text": "So be wary of this if you do want to\nkeep track of [? null ?] [? values ?]", "start": 2198.79, "duration": 3.35 }, { "text": "and so on.", "start": 2202.14, "duration": 0.79 }, { "text": "If you don't actually manually\nmake this value a [? null, ?]", "start": 2202.93, "duration": 3.14 }, { "text": "it'll appear as just a\nblank value not a null,", "start": 2206.07, "duration": 3.16 }, { "text": "which is different in this respect.", "start": 2209.23, "duration": 3.54 }, { "text": "OK so here, we've seen how to insert\nnot just one row, but multiple,", "start": 2212.77, "duration": 5.61 }, { "text": "and also how to import data from a CSV.", "start": 2218.38, "duration": 3.46 }, { "text": "When we come back, we'll see how to\nactually update our data altogether,", "start": 2221.84, "duration": 4.14 }, { "text": "and even delete it too.", "start": 2225.98, "duration": 1.295 }, { "text": "So come back in just a\nfew and talk about how", "start": 2227.275, "duration": 1.875 }, { "text": "to delete data from our tables.", "start": 2229.15, "duration": 1.8 }, { "text": "And we're back.", "start": 2230.95, "duration": 1.74 }, { "text": "So we just saw how to insert\nsome rows into our database,", "start": 2232.69, "duration": 3.85 }, { "text": "and also to import some data.", "start": 2236.54, "duration": 1.94 }, { "text": "But presumably, we also want to be\nable to delete some data from our table", "start": 2238.48, "duration": 3.84 }, { "text": "as well.", "start": 2242.32, "duration": 0.97 }, { "text": "You can imagine the MFA,\nthe Museum of Fine Arts,", "start": 2243.29, "duration": 2.84 }, { "text": "maybe they're selling a piece of\nartwork, or maybe they've lost one,", "start": 2246.13, "duration": 3.33 }, { "text": "or maybe it was stolen.", "start": 2249.46, "duration": 1.29 }, { "text": "But either way, they want to\nremove the row from their table.", "start": 2250.75, "duration": 4.23 }, { "text": "Well, let's see here.", "start": 2254.98, "duration": 1.41 }, { "text": "We can go back to our schema with\na table of artifacts and artwork", "start": 2256.39, "duration": 4.71 }, { "text": "that is inside of the MFA.", "start": 2261.1, "duration": 2.52 }, { "text": "Now if I want to delete\na particular piece,", "start": 2263.62, "duration": 2.25 }, { "text": "you could visually think\nof it a bit like this.", "start": 2265.87, "duration": 2.08 }, { "text": "I could first identify\nthat row I want to delete.", "start": 2267.95, "duration": 3.32 }, { "text": "Let's say it's Spring outing here.", "start": 2271.27, "duration": 1.71 }, { "text": "We've sold this piece.", "start": 2272.98, "duration": 1.83 }, { "text": "Well, I could visually\njust remove this row.", "start": 2274.81, "duration": 3.4 }, { "text": "So it's no longer there and shift\nthe remaining ones up metaphorically.", "start": 2278.21, "duration": 4.25 }, { "text": "And it turns out that\nto do this in SQL, we", "start": 2282.46, "duration": 3.15 }, { "text": "have our very own statement we can use.", "start": 2285.61, "duration": 2.61 }, { "text": "This one reads DELETE FROM some\ntable WHERE a condition is true.", "start": 2288.22, "duration": 6.61 }, { "text": "So we see our old\nfriend WHERE back again.", "start": 2294.83, "duration": 2.72 }, { "text": "And WHERE is vitally\nimportant to this DELETE FROM.", "start": 2297.55, "duration": 3.06 }, { "text": "If I say DELETE FROM\ntable with no WHERE,", "start": 2300.61, "duration": 3.3 }, { "text": "what do you think is going to happen?", "start": 2303.91, "duration": 2.88 }, { "text": "We might drop everything\nfrom our table, right?", "start": 2306.79, "duration": 3.06 }, { "text": "But if I instead say WHERE\nsome condition is true,", "start": 2309.85, "duration": 3.06 }, { "text": "I can select the rows I want to\ndelete and only delete those rows.", "start": 2312.91, "duration": 5.26 }, { "text": "So let's try this here.", "start": 2318.17, "duration": 1.73 }, { "text": "I'll try to delete some artwork\nfrom our collections table,", "start": 2319.9, "duration": 3.18 }, { "text": "for example, maybe we sold\nit and we want get rid of it.", "start": 2323.08, "duration": 2.53 }, { "text": "I'll come back to my computer over here,\nand I will open up our SQLite database.", "start": 2325.61, "duration": 6.35 }, { "text": "I'll say sqlite3 mfa.db.", "start": 2331.96, "duration": 2.58 }, { "text": "And now, I can type SELECT\n[? star ?] [? FROM ?] \"collections,\"", "start": 2334.54, "duration": 3.75 }, { "text": "and I see a few more items than last\ntime now all inside of our table.", "start": 2338.29, "duration": 5.7 }, { "text": "But per the visual, I want\nto delete Spring outing,", "start": 2343.99, "duration": 3.66 }, { "text": "to remove it from this table.", "start": 2347.65, "duration": 2.17 }, { "text": "So what could I do?", "start": 2349.82, "duration": 1.08 }, { "text": "I could try DELETE FROM and then\nthe table I want to delete from.", "start": 2350.9, "duration": 4.37 }, { "text": "In this case, I'll use the let's say\n[? collections' ?] [? name, ?] DELETE", "start": 2355.27, "duration": 5.13 }, { "text": "FROM \"collections.\"", "start": 2360.4, "duration": 1.92 }, { "text": "But I don't want to do this.", "start": 2362.32, "duration": 1.3 }, { "text": "I don't want to say\nDELETE FROM \"collections\"", "start": 2363.62, "duration": 1.97 }, { "text": "semicolon end of statement,\nbecause then I'll delete every row.", "start": 2365.59, "duration": 5.19 }, { "text": "What I should instead do is this.", "start": 2370.78, "duration": 2.76 }, { "text": "DELETE FROM \"collections\"\nWHERE some condition is true.", "start": 2373.54, "duration": 4.05 }, { "text": "Maybe the title in this case is\na title I'm trying to remove.", "start": 2377.59, "duration": 3.78 }, { "text": "So I'll say \"title\" equals \"Spring\nouting,\" just like this, semicolon,", "start": 2381.37, "duration": 6.06 }, { "text": "Enter.", "start": 2387.43, "duration": 1.08 }, { "text": "Now, nothing seems to happen.", "start": 2388.51, "duration": 1.23 }, { "text": "But if I do SELECT [? star ?]\nFROM \"collections\" semicolon,", "start": 2389.74, "duration": 5.4 }, { "text": "we no longer see Spring outing.", "start": 2395.14, "duration": 2.28 }, { "text": "And notice here, how the id\ncolumn, the id of 3 is now gone.", "start": 2397.42, "duration": 5.16 }, { "text": "If I were to insert some new\nrow here, I would start over", "start": 2402.58, "duration": 3.3 }, { "text": "with the highest number, which is\n7 in this case, going from 7 to 8.", "start": 2405.88, "duration": 4.23 }, { "text": "But 3 is no longer part of our database.", "start": 2410.11, "duration": 4.04 }, { "text": "Let me try this again, notice how\nthis one called Imaginative Landscape,", "start": 2414.15, "duration": 4.02 }, { "text": "the acquired date is NULL.", "start": 2418.17, "duration": 1.77 }, { "text": "We don't know when we got it.", "start": 2419.94, "duration": 1.873 }, { "text": "Well, let's say that we eventually sell.", "start": 2421.813, "duration": 1.667 }, { "text": "It's no longer part of our database.", "start": 2423.48, "duration": 1.53 }, { "text": "We could use a condition based on NULL\nto remove this particular artwork.", "start": 2425.01, "duration": 4.8 }, { "text": "So I'll say this.", "start": 2429.81, "duration": 1.0 }, { "text": "Let me try to DELETE FROM\n\"collections\" WHERE \"title\" equals--", "start": 2430.81, "duration": 5.96 }, { "text": "not Spring outing, but WHERE\nthe acquired date-- sorry,", "start": 2436.77, "duration": 3.15 }, { "text": "the \"acquired\" date IS NULL semicolon.", "start": 2439.92, "duration": 4.56 }, { "text": "I could also use is NOT NULL--", "start": 2444.48, "duration": 3.01 }, { "text": "WHERE \"acquired\" IS NOT NULL, but that\nwould delete all the pieces of artwork", "start": 2447.49, "duration": 3.86 }, { "text": "that actually have an acquired date.", "start": 2451.35, "duration": 2.16 }, { "text": "Here, I want to leave only those\nthat do not have an acquired date.", "start": 2453.51, "duration": 3.75 }, { "text": "So I'll say where \"acquired\" IS NULL.", "start": 2457.26, "duration": 3.51 }, { "text": "Now, I'll hit Enter,\nnothing seems to happen,", "start": 2460.77, "duration": 2.55 }, { "text": "but I'll say Select [? star ?]\nFROM \"collections\" semicolon.", "start": 2463.32, "duration": 4.53 }, { "text": "And I'll see that piece\nof artwork is now gone.", "start": 2467.85, "duration": 4.17 }, { "text": "I only have those that\nhave an acquired date.", "start": 2472.02, "duration": 4.8 }, { "text": "OK, so similar to [? INSERT, ?] we've\nbeen able to [? delete ?] one row", "start": 2476.82, "duration": 6.06 }, { "text": "at a time.", "start": 2482.88, "duration": 1.11 }, { "text": "But it would probably be helpful for\nus to delete multiple rows at a time", "start": 2483.99, "duration": 4.38 }, { "text": "as well.", "start": 2488.37, "duration": 1.18 }, { "text": "Let's look again at our table.", "start": 2489.55, "duration": 1.91 }, { "text": "Here we have our artwork, as\nit currently stands, and maybe", "start": 2491.46, "duration": 4.74 }, { "text": "let's say we want to delete those\npieces that we acquired before 1909.", "start": 2496.2, "duration": 5.82 }, { "text": "Here in Boston, the MFA actually\nmoved locations in 1909 to a new place", "start": 2502.02, "duration": 4.95 }, { "text": "still in Boston, but a brand\nnew building altogether.", "start": 2506.97, "duration": 2.8 }, { "text": "So let's say that perhaps they left\nsome items at their old location,", "start": 2509.77, "duration": 3.32 }, { "text": "they're no longer part\nof their collections.", "start": 2513.09, "duration": 2.67 }, { "text": "Via what condition do you think\ncould I select these three rows from", "start": 2515.76, "duration": 7.16 }, { "text": "my [? DELETE ?] [? FROM? ?] What could\nI put in my condition to delete these", "start": 2522.92, "duration": 5.75 }, { "text": "three rows?", "start": 2528.67, "duration": 1.23 }, { "text": "Let me ask the audience here.", "start": 2529.9, "duration": 1.77 }, { "text": "What might you propose for [? delete ?]\n[? from ?] here to delete those three", "start": 2531.67, "duration": 4.11 }, { "text": "particular rows?", "start": 2535.78, "duration": 0.795 }, { "text": "Let's go to [? Shiv. ?]", "start": 2541.54, "duration": 1.11 }, { "text": "SPEAKER 9: Greater than [? 5. ?]\n[? id ?] equals greater than 5.", "start": 2542.65, "duration": 3.818 }, { "text": "SPEAKER 1: That's a good observation.", "start": 2546.468, "duration": 1.542 }, { "text": "So we could look at our table here.", "start": 2548.01, "duration": 1.458 }, { "text": "We see the id could be greater\nthan or equal to 5, that would", "start": 2549.468, "duration": 4.022 }, { "text": "remove these three rows certainly.", "start": 2553.49, "duration": 2.73 }, { "text": "There's probably another\nway to do this too.", "start": 2556.22, "duration": 2.16 }, { "text": "Any other ideas besides the\nid, what else can we use?", "start": 2558.38, "duration": 3.81 }, { "text": "Let's go to [? Yasavi. ?]", "start": 2569.86, "duration": 1.85 }, { "text": "SPEAKER 10: So we could compare the\ndates and use greater than or less", "start": 2571.71, "duration": 3.36 }, { "text": "than sign with the acquired dates.", "start": 2575.07, "duration": 3.27 }, { "text": "SPEAKER 1: So you're proposing\nto use the date column too.", "start": 2578.34, "duration": 2.46 }, { "text": "That makes sense to me as well.", "start": 2580.8, "duration": 1.32 }, { "text": "We could see acquired\nhere, perhaps we could see,", "start": 2582.12, "duration": 3.18 }, { "text": "is the acquired value less than 1909,\nthat year that we move locations,", "start": 2585.3, "duration": 5.8 }, { "text": "we could also probably use the title,\nthe [? accession ?] [? number, ?]", "start": 2591.1, "duration": 3.41 }, { "text": "and so on.", "start": 2594.51, "duration": 0.88 }, { "text": "And each has their own [? trade-offs. ?]", "start": 2595.39, "duration": 2.21 }, { "text": "Remember, though, for this\nparticular query, which", "start": 2597.6, "duration": 2.43 }, { "text": "is deleting those items\nthat were part of the museum", "start": 2600.03, "duration": 3.45 }, { "text": "acquired before 1909, probably\nbest to use the acquired", "start": 2603.48, "duration": 5.73 }, { "text": "date to actually delete those rows.", "start": 2609.21, "duration": 3.03 }, { "text": "What if, for instance, our ids\nare actually not like this.", "start": 2612.24, "duration": 3.69 }, { "text": "But they're kind of interspersed around.", "start": 2615.93, "duration": 2.28 }, { "text": "I couldn't really do a query like if\nthe id is greater than or equal to 5,", "start": 2618.21, "duration": 3.66 }, { "text": "because I could also include those\nthat were maybe acquired 1911,", "start": 2621.87, "duration": 3.24 }, { "text": "[? 1956, ?] and so on. [? It's ?]\n[? probably ?] ideal for me to use this", "start": 2625.11, "duration": 3.57 }, { "text": "date if it's the date that\nI ultimately care about.", "start": 2628.68, "duration": 3.46 }, { "text": "So let's try this.", "start": 2632.14, "duration": 1.07 }, { "text": "I'll go back to my environment.", "start": 2633.21, "duration": 2.37 }, { "text": "And I'll try to delete\nthese three particular rows.", "start": 2635.58, "duration": 3.51 }, { "text": "I'll try DELETE FROM\n\"collections,\" as we saw before.", "start": 2639.09, "duration": 4.66 }, { "text": "But now here comes my condition.", "start": 2643.75, "duration": 1.88 }, { "text": "I'll say WHERE the \"acquired\" column\nis less than some date that I'll give.", "start": 2645.63, "duration": 6.6 }, { "text": "And here, I'll give 1909-01-01.", "start": 2652.23, "duration": 4.95 }, { "text": "It turns out that SQLite has\na few ways to represent dates.", "start": 2657.18, "duration": 4.08 }, { "text": "One of which is this format that\nfollows the YYYY or the year,", "start": 2661.26, "duration": 5.04 }, { "text": "four digits, and then dash, and then\nMM or the month with two digits,", "start": 2666.3, "duration": 4.98 }, { "text": "and then dash again, DD, in this\ncase, the day in two digits.", "start": 2671.28, "duration": 4.06 }, { "text": "So 1909-01-01 means January 1st, 1909.", "start": 2675.34, "duration": 7.13 }, { "text": "And I can use these same\noperators with dates.", "start": 2682.47, "duration": 3.96 }, { "text": "I could say greater than, or less\nthan, or equal to this particular date.", "start": 2686.43, "duration": 4.8 }, { "text": "And SQLite will be able to parse this\nfor me to understand what I mean,", "start": 2691.23, "duration": 3.15 }, { "text": "which is earlier than 1909.", "start": 2694.38, "duration": 3.63 }, { "text": "So let's try this then.", "start": 2698.01, "duration": 1.32 }, { "text": "I'll hit Enter, and\nnothing seems to happen.", "start": 2699.33, "duration": 3.12 }, { "text": "But if I do SELECT [? star ?]\nFROM \"collections,\"", "start": 2702.45, "duration": 3.42 }, { "text": "I now see I'm down to two paintings,\ntwo pieces of artwork that", "start": 2705.87, "duration": 4.95 }, { "text": "were acquired only after 1909.", "start": 2710.82, "duration": 4.9 }, { "text": "OK, so given this part on DELETE, we're\nable to delete one row or even multiple", "start": 2715.72, "duration": 7.47 }, { "text": "rows at a time.", "start": 2723.19, "duration": 1.125 }, { "text": "But there are still\ninstances where you might", "start": 2724.315, "duration": 1.875 }, { "text": "want to think about, should we delete\nsomething or can we delete something.", "start": 2726.19, "duration": 4.89 }, { "text": "We'll talk about those in just a\nminute in terms of constraints.", "start": 2731.08, "duration": 3.93 }, { "text": "We back in a few.", "start": 2735.01, "duration": 1.17 }, { "text": "And we're back.", "start": 2736.18, "duration": 1.68 }, { "text": "So we just saw how to delete\none row, even multiple rows.", "start": 2737.86, "duration": 5.4 }, { "text": "We haven't yet talked about\nwhether we should delete some data", "start": 2743.26, "duration": 3.36 }, { "text": "or whether we should delete\nsome data and particularly", "start": 2746.62, "duration": 2.4 }, { "text": "in the context of these\nconstraints, where", "start": 2749.02, "duration": 2.16 }, { "text": "you might have maybe a\npiece of data you actually", "start": 2751.18, "duration": 2.16 }, { "text": "shouldn't delete to maintain\nthe integrity of your table.", "start": 2753.34, "duration": 4.12 }, { "text": "Now, one example of this is\na [? Foreign ?] [? Key ?]", "start": 2757.46, "duration": 3.41 }, { "text": "[? Constraint, ?] which means that you\nhave some table with a primary key that", "start": 2760.87, "duration": 5.01 }, { "text": "is referenced by some other table.", "start": 2765.88, "duration": 2.283 }, { "text": "And we'll get concrete\nabout this in a minute.", "start": 2768.163, "duration": 1.917 }, { "text": "But it might mean that if you\nwere to delete that primary key,", "start": 2770.08, "duration": 3.3 }, { "text": "that other table would\nhave nothing to reference.", "start": 2773.38, "duration": 2.83 }, { "text": "So let's update our schema here for\nthe MFA, the Museum of Fine Arts.", "start": 2776.21, "duration": 4.4 }, { "text": "And let's say that they have not just\na collection now, but also artists", "start": 2780.61, "duration": 4.53 }, { "text": "involved.", "start": 2785.14, "duration": 0.75 }, { "text": "And there exists a [? many ?] [? to ?]\n[? many ?] relationship among artists", "start": 2785.89, "duration": 4.08 }, { "text": "and items in the collection.", "start": 2789.97, "duration": 2.19 }, { "text": "We could say that an artist might\nmake more than one piece of art", "start": 2792.16, "duration": 4.32 }, { "text": "in the collection.", "start": 2796.48, "duration": 1.23 }, { "text": "And a piece of art might be\ncreated by more than one artist.", "start": 2797.71, "duration": 4.77 }, { "text": "Now concretely in our database,\nthis might look as follows.", "start": 2802.48, "duration": 4.57 }, { "text": "I could have a table for artists\nand a table for collections.", "start": 2807.05, "duration": 4.43 }, { "text": "And notice how each has\ntheir own primary key.", "start": 2811.48, "duration": 3.3 }, { "text": "Artists has an id column,\ncalled our primary key column.", "start": 2814.78, "duration": 3.33 }, { "text": "Collections too has an id called\nour primary key column here.", "start": 2818.11, "duration": 4.29 }, { "text": "Now, in the middle is this\ntable created that symbolizes,", "start": 2822.4, "duration": 4.02 }, { "text": "that represents the relationship among\nartists and items in the collection.", "start": 2826.42, "duration": 4.86 }, { "text": "Here on the first row, we see the\nartist id of 1 created the piece of art", "start": 2831.28, "duration": 5.1 }, { "text": "with the collection id of 2.", "start": 2836.38, "duration": 2.37 }, { "text": "So who did what here?", "start": 2838.75, "duration": 2.08 }, { "text": "Well, we can see that the artist\nwith the ID of 1 is Li Yin.", "start": 2840.83, "duration": 4.37 }, { "text": "Li Yin created this piece of art, the\nid of 2, which is Imaginative landscape.", "start": 2845.2, "duration": 6.54 }, { "text": "So we can kind of relate in this\ncase artists with collections", "start": 2851.74, "duration": 3.6 }, { "text": "as we saw just a few weeks ago.", "start": 2855.34, "duration": 2.55 }, { "text": "But let's say we decide to\ndelete a particular artist,", "start": 2857.89, "duration": 3.99 }, { "text": "maybe we delete Unidentified\nartists down here.", "start": 2861.88, "duration": 3.63 }, { "text": "Well, we could just delete\nfrom the artist table.", "start": 2865.51, "duration": 2.59 }, { "text": "Maybe we find a condition to select\nthis row and we delete from artists.", "start": 2868.1, "duration": 5.84 }, { "text": "We'll do that here remove that row.", "start": 2873.94, "duration": 2.67 }, { "text": "But what have we done wrong?", "start": 2876.61, "duration": 2.91 }, { "text": "If we look at our created\ntable in particular,", "start": 2879.52, "duration": 4.04 }, { "text": "what have we now done wrong?", "start": 2883.56, "duration": 2.79 }, { "text": "What kind of problem might we [? run ?]\n[? into? ?] Let's go to [? Karim. ?]", "start": 2886.35, "duration": 7.51 }, { "text": "SPEAKER 11: [? Yeah ?] [INAUDIBLE]\n[? was ?] [? only ?] [? deleted ?]", "start": 2893.86, "duration": 3.455 }, { "text": "[? from ?] [? the ?] [? artist ?]\n[INAUDIBLE] without the other", "start": 2897.315, "duration": 2.625 }, { "text": "[INAUDIBLE].", "start": 2899.94, "duration": 0.813 }, { "text": "SPEAKER 1: Yeah, good point.", "start": 2900.753, "duration": 1.167 }, { "text": "We only deleted the artist with\nthe id of 3 from the artists table.", "start": 2901.92, "duration": 5.73 }, { "text": "Now if I look at the created table and\nI look for the artist with id of 3,", "start": 2907.65, "duration": 6.693 }, { "text": "do you see them?", "start": 2914.343, "duration": 0.667 }, { "text": "They don't exist anymore.", "start": 2915.01, "duration": 1.18 }, { "text": "So I can't understand this relationship.", "start": 2916.19, "duration": 2.0 }, { "text": "I don't know the artist\nwith the id of 3.", "start": 2918.19, "duration": 3.93 }, { "text": "So let's try this now with a new\nkind of schema now in our database.", "start": 2922.12, "duration": 5.67 }, { "text": "I'll go back to my computer.", "start": 2927.79, "duration": 2.49 }, { "text": "And let me open up our new\nversion of our database.", "start": 2930.28, "duration": 3.57 }, { "text": "I'll type sqlite3, in this case, mfa.db,\nsame name, but now a different schema.", "start": 2933.85, "duration": 7.59 }, { "text": "If I type .schema,\nnotice a few things here.", "start": 2941.44, "duration": 4.2 }, { "text": "I have those same\ntables we saw visually.", "start": 2945.64, "duration": 3.45 }, { "text": "I have collections, artists,\nand a created table.", "start": 2949.09, "duration": 4.65 }, { "text": "And [? let me ?] focus in\nparticular on this created table.", "start": 2953.74, "duration": 2.94 }, { "text": "If I say .schema, in this\ncase, created, Enter,", "start": 2956.68, "duration": 4.41 }, { "text": "I'll see I have a FOREIGN KEY\nconstraint on my \"artist_id\"", "start": 2961.09, "duration": 5.52 }, { "text": "column and my \"collection_id\" column.", "start": 2966.61, "duration": 3.43 }, { "text": "So if I tried to delete an artist that\nis referenced in my created table,", "start": 2970.04, "duration": 6.26 }, { "text": "I would probably raise\na constraint error--", "start": 2976.3, "duration": 3.69 }, { "text": "a FOREIGN KEY [? constraint ?] error.", "start": 2979.99, "duration": 2.38 }, { "text": "So let's just try this\nand see what happens.", "start": 2982.37, "duration": 3.3 }, { "text": "Let me try DELETE FROM the\n[? artist's ?] [? table, ?] DELETE FROM", "start": 2985.67, "duration": 4.88 }, { "text": "\"artists\" WHERE, let's say the \"name\"\nof the artist is literally \"Unidentified", "start": 2990.55, "duration": 8.51 }, { "text": "artist.\"", "start": 2999.06, "duration": 0.88 }, { "text": "This is the name they have in the\nMuseum of Fine Arts collections--", "start": 2999.94, "duration": 3.62 }, { "text": "Unidentified artist.", "start": 3003.56, "duration": 1.71 }, { "text": "So now I want to delete\nthem from that table.", "start": 3005.27, "duration": 3.0 }, { "text": "Well, if I hit Enter, I do get\nthat FOREIGN KEY constraint error.", "start": 3008.27, "duration": 5.86 }, { "text": "So does this mean I just can't delete\nthis artist or is there a workaround?", "start": 3014.13, "duration": 5.48 }, { "text": "Let's look at it visually again.", "start": 3019.61, "duration": 1.68 }, { "text": "I'll go back to what we\nhad before as our table,", "start": 3021.29, "duration": 3.45 }, { "text": "and let me ask the audience here.", "start": 3024.74, "duration": 2.88 }, { "text": "What's the solution?", "start": 3027.62, "duration": 1.59 }, { "text": "If I can't delete Unidentified\nartist, because they", "start": 3029.21, "duration": 3.93 }, { "text": "have an id referenced by this\ntable, what should I maybe do first?", "start": 3033.14, "duration": 5.16 }, { "text": "What should I do instead?", "start": 3038.3, "duration": 1.395 }, { "text": "SPEAKER 12: Yeah, I want\nto say that first we", "start": 3039.695, "duration": 1.875 }, { "text": "can delete the id that is being\nreferenced by this [? id ?]", "start": 3041.57, "duration": 4.59 }, { "text": "that we want to delete,\nthen we can delete it.", "start": 3046.16, "duration": 2.58 }, { "text": "In fact, if we want to\ndelete it from the artists,", "start": 3048.74, "duration": 2.43 }, { "text": "first we have to delete from the\ncreated table [INAUDIBLE] table, which", "start": 3051.17, "duration": 3.78 }, { "text": "it is referencing, then we can\ndelete from the artist [INAUDIBLE]", "start": 3054.95, "duration": 3.12 }, { "text": "SPEAKER 1: Yeah, I like\nyour thinking here.", "start": 3058.07, "duration": 1.99 }, { "text": "So if we look at the\ncreated table, you notice", "start": 3060.06, "duration": 2.42 }, { "text": "that we can't delete\nUnidentified artist, because it's", "start": 3062.48, "duration": 3.03 }, { "text": "referenced in the created table.", "start": 3065.51, "duration": 1.92 }, { "text": "What we should maybe first do is delete\nthis row delete their affiliation", "start": 3067.43, "duration": 4.8 }, { "text": "with their work, and then\ndelete the artist so we don't", "start": 3072.23, "duration": 3.87 }, { "text": "run into this FOREIGN KEY constraint.", "start": 3076.1, "duration": 2.8 }, { "text": "So let's try this in\nour SQLite environment.", "start": 3078.9, "duration": 3.62 }, { "text": "Come back to my computer.", "start": 3082.52, "duration": 2.26 }, { "text": "And again, our plan was to first delete\nthe artists affiliation with their work", "start": 3084.78, "duration": 5.54 }, { "text": "from the created table.", "start": 3090.32, "duration": 1.89 }, { "text": "So let me show you\nfirst the created table.", "start": 3092.21, "duration": 2.25 }, { "text": "I'll say SELECT\n[? star ?] from \"created,\"", "start": 3094.46, "duration": 3.78 }, { "text": "and see how it's exactly like\nthe table we just visualized.", "start": 3098.24, "duration": 3.84 }, { "text": "Now, let me try delete this artist\naffiliation with their work.", "start": 3102.08, "duration": 4.74 }, { "text": "I'm not deleting the artist.", "start": 3106.82, "duration": 2.05 }, { "text": "I'm deleting their affiliation\nwith their authorship,", "start": 3108.87, "duration": 3.41 }, { "text": "their [? artist ?] [? ship, ?] whatever\nyou want to call it for their work.", "start": 3112.28, "duration": 3.43 }, { "text": "Now, I'll say DELETE FROM--", "start": 3115.71, "duration": 4.1 }, { "text": "let's say, DELETE FROM \"artists\"\nWHERE the \"artist_id\" is equal to", "start": 3119.81, "duration": 7.5 }, { "text": "or equals 3.", "start": 3127.31, "duration": 3.29 }, { "text": "Well, I could do this.", "start": 3130.6, "duration": 2.17 }, { "text": "But there's probably a better way,\nlike I know the id, and that's fine.", "start": 3132.77, "duration": 4.07 }, { "text": "But we could also use, as\nwe saw a little bit ago,", "start": 3136.84, "duration": 2.53 }, { "text": "which were [? subqueries, ?]\na way to write a query that", "start": 3139.37, "duration": 3.14 }, { "text": "returns me the result. And that\nresult gets included in another query", "start": 3142.51, "duration": 3.45 }, { "text": "altogether.", "start": 3145.96, "duration": 0.88 }, { "text": "So let's try that instead.", "start": 3146.84, "duration": 1.43 }, { "text": "Let's say I don't know the artist_id.", "start": 3148.27, "duration": 2.76 }, { "text": "What I could do is\nmake a [? subquery. ?]", "start": 3151.03, "duration": 2.1 }, { "text": "I could say inside of this, let me\nselect the \"id\" FROM the \"artists\"", "start": 3153.13, "duration": 6.3 }, { "text": "table WHERE, in this case, the \"name\"\nequals \"Unidentified artist,\" and then,", "start": 3159.43, "duration": 9.12 }, { "text": "let me close out this\nquery, hit a semicolon.", "start": 3168.55, "duration": 2.58 }, { "text": "And I already see a typo in this,\nbut I'm just going to try it anyway.", "start": 3171.13, "duration": 3.21 }, { "text": "I will go ahead, and hit Enter.", "start": 3174.34, "duration": 2.7 }, { "text": "And let's see if I type SELECT\n[? star ?] FROM \"artists,\" Enter,", "start": 3177.04, "duration": 6.66 }, { "text": "I still see Unidentified artist.", "start": 3183.7, "duration": 2.85 }, { "text": "And I think my typo here was as follows.", "start": 3186.55, "duration": 1.93 }, { "text": "If I up my query using the\nup arrow on my keyboard,", "start": 3188.48, "duration": 2.57 }, { "text": "I see DELETE from \"artists,\" where the\n\"artist_id\" equals some value here.", "start": 3191.05, "duration": 5.25 }, { "text": "But do I want to delete from artists?", "start": 3196.3, "duration": 2.97 }, { "text": "I don't.", "start": 3199.27, "duration": 0.72 }, { "text": "I actually can't delete from artists.", "start": 3199.99, "duration": 1.62 }, { "text": "What I should do instead is\ndelete from the created table.", "start": 3201.61, "duration": 3.73 }, { "text": "So let's try this.", "start": 3205.34, "duration": 1.01 }, { "text": "I'll say DELETE FROM\n\"created,\" making sure I only", "start": 3206.35, "duration": 4.29 }, { "text": "have double quotes\naround created, then I'll", "start": 3210.64, "duration": 2.73 }, { "text": "say WHERE the artist_id\nequals, let's say, either 3--", "start": 3213.37, "duration": 5.1 }, { "text": "but it could also be the\nresult of this [? subquery. ?]", "start": 3218.47, "duration": 3.06 }, { "text": "I'll say 1, 2, 3, 4,\nand then SELECT the \"id\"", "start": 3221.53, "duration": 5.88 }, { "text": "FROM the \"artist\" table WHERE the\n\"name\" equals \"Unidentified artist.\"", "start": 3227.41, "duration": 8.47 }, { "text": "Then I'll close this [? subquery, ?]\nhitting semicolon, hit Enter.", "start": 3235.88, "duration": 4.35 }, { "text": "And now, if I say SELECT [? star ?]\nFROM \"created\" semicolon,", "start": 3240.23, "duration": 5.52 }, { "text": "I should see that this artist's\naffiliation with their work", "start": 3245.75, "duration": 3.75 }, { "text": "no longer exists.", "start": 3249.5, "duration": 2.1 }, { "text": "And because it no longer exists.", "start": 3251.6, "duration": 1.51 }, { "text": "I can now delete them\nfrom the artists table.", "start": 3253.11, "duration": 2.88 }, { "text": "I don't have this FOREIGN\nKEY artist_id referencing", "start": 3255.99, "duration": 4.22 }, { "text": "the primary key of this artist.", "start": 3260.21, "duration": 2.31 }, { "text": "So let's now try that.", "start": 3262.52, "duration": 1.38 }, { "text": "I could say SELECT\n[? star ?] FROM \"artists,\"", "start": 3263.9, "duration": 3.06 }, { "text": "I see that Unidentified\nartist is still in here.", "start": 3266.96, "duration": 3.69 }, { "text": "But let's delete them now.", "start": 3270.65, "duration": 1.86 }, { "text": "I'll say delete from,\nlet's say, \"artists\" WHERE", "start": 3272.51, "duration": 3.78 }, { "text": "the \"name\" equals \"Unidentified artist,\"\nclosing my quotes, semicolon, hit", "start": 3276.29, "duration": 6.33 }, { "text": "Enter.", "start": 3282.62, "duration": 0.66 }, { "text": "And now if I Select [? star ?]\nFROM \"artists\" semicolon,", "start": 3283.28, "duration": 4.5 }, { "text": "I should see they're no longer in\nthis table to our earlier example.", "start": 3287.78, "duration": 4.74 }, { "text": "This becomes a 2-step process.", "start": 3292.52, "duration": 2.23 }, { "text": "First delete their affiliation,\nthen delete their name.", "start": 3294.75, "duration": 4.73 }, { "text": "OK, So let's try this yet again,\nbut now using some additional tools", "start": 3299.48, "duration": 8.43 }, { "text": "that we have at our disposal.", "start": 3307.91, "duration": 2.23 }, { "text": "So let's look back at this FOREIGN KEY\nconstraint that existed in this table.", "start": 3310.14, "duration": 6.72 }, { "text": "If you remember, it\nlooked a bit like this.", "start": 3316.86, "duration": 2.36 }, { "text": "In my created table, I had this line.", "start": 3319.22, "duration": 2.76 }, { "text": "FOREIGN KEY, artist_id, REFERENCES\nthe \"id\" column in \"artists.\"", "start": 3321.98, "duration": 6.24 }, { "text": "Well, if that is the case, I can't\ndelete the artist with the id", "start": 3328.22, "duration": 6.21 }, { "text": "referenced by this [? foreign ?]\n[? key. ?] But it turns out that I", "start": 3334.43, "duration": 4.74 }, { "text": "could specify some alternative action\nthat happens when I try to delete", "start": 3339.17, "duration": 5.22 }, { "text": "the id that is referenced by this\n[? foreign ?] [? key. ?] I can specify", "start": 3344.39, "duration": 4.2 }, { "text": "that using a keyword called ON DELETE.", "start": 3348.59, "duration": 3.45 }, { "text": "So after FOREIGN KEY REFERENCES\n[? \"artist id,\" ?] I could say", "start": 3352.04, "duration": 2.82 }, { "text": "afterwards ON DELETE, and then specify\nsome action I want to actually happen", "start": 3354.86, "duration": 4.74 }, { "text": "when I tried to delete the primary\nkey referenced by this [? foreign ?]", "start": 3359.6, "duration": 3.9 }, { "text": "[? key. ?]", "start": 3363.5, "duration": 0.93 }, { "text": "One thing I could use\nwould be RESTRICT, which", "start": 3364.43, "duration": 3.57 }, { "text": "is kind of like what we saw before.", "start": 3368.0, "duration": 1.98 }, { "text": "If I try to delete a primary key is\n[? referenced ?] by this [? foreign ?]", "start": 3369.98, "duration": 4.62 }, { "text": "[? key, ?] I will not\nbe allowed to do it.", "start": 3374.6, "duration": 2.49 }, { "text": "That action is restricted.", "start": 3377.09, "duration": 2.63 }, { "text": "I could also decide to take NO ACTION.", "start": 3379.72, "duration": 3.69 }, { "text": "In this case, I could delete the\nprimary key [? referenced ?] by this", "start": 3383.41, "duration": 4.74 }, { "text": "[? foreign ?] [? key, ?]\nand nothing would happen.", "start": 3388.15, "duration": 2.41 }, { "text": "I would actually be allowed to do that,\nwhich may be unwise in some cases,", "start": 3390.56, "duration": 4.73 }, { "text": "but I could give myself that power.", "start": 3395.29, "duration": 2.76 }, { "text": "I could also decide to SET NULL.", "start": 3398.05, "duration": 3.54 }, { "text": "That is if I delete the primary key\nthat is [? referenced ?] by this", "start": 3401.59, "duration": 4.74 }, { "text": "[? foreign ?] [? key, ?] what I'll do\nis set the [? foreign ?] [? key ?] to be", "start": 3406.33, "duration": 3.36 }, { "text": "NULL, meaning that\nvalue no longer exists.", "start": 3409.69, "duration": 3.967 }, { "text": "I can alternatively [? set ?] it to a\n[? default ?] value for that column.", "start": 3413.657, "duration": 3.083 }, { "text": "Or perhaps, most compellingly, I\ncould try to CASCADE the deletion.", "start": 3416.74, "duration": 6.03 }, { "text": "Where CASCADE means if I delete\nthe artist, for instance,", "start": 3422.77, "duration": 3.82 }, { "text": "let's also delete their affiliation\nwith their artwork all in one go.", "start": 3426.59, "duration": 5.0 }, { "text": "This converts our 2-step\nprocess into a 1-step process.", "start": 3431.59, "duration": 4.84 }, { "text": "So let's visualize this.", "start": 3436.43, "duration": 2.28 }, { "text": "Let's say we have now applied\nthis constraint ON DELETE CASCADE.", "start": 3438.71, "duration": 5.33 }, { "text": "So if I delete the artist, I'll\nalso delete their affiliation", "start": 3444.04, "duration": 3.81 }, { "text": "with their work.", "start": 3447.85, "duration": 1.26 }, { "text": "We'll have the same two tables--", "start": 3449.11, "duration": 1.5 }, { "text": "artists, collections, and created.", "start": 3450.61, "duration": 3.0 }, { "text": "Now again, artist_id references\nthis primary key in artists.", "start": 3453.61, "duration": 5.82 }, { "text": "Now, I'm going to try.", "start": 3459.43, "duration": 1.08 }, { "text": "I want to delete\nUnidentified artist here.", "start": 3460.51, "duration": 3.04 }, { "text": "Well, I could do that.", "start": 3463.55, "duration": 0.96 }, { "text": "I could just delete their row.", "start": 3464.51, "duration": 1.43 }, { "text": "And now, instead of a FOREIGN\nKEY constraint error, what I get", "start": 3465.94, "duration": 4.5 }, { "text": "is the following.", "start": 3470.44, "duration": 1.05 }, { "text": "[INAUDIBLE] [? created ?]\nthat this row is also gone.", "start": 3471.49, "duration": 4.2 }, { "text": "The row that had the\nartist_id of 3 gets removed.", "start": 3475.69, "duration": 4.27 }, { "text": "We've cascaded the deletion\nonward to the created table.", "start": 3479.96, "duration": 5.0 }, { "text": "So let's try this now with\na new database schema.", "start": 3484.96, "duration": 3.8 }, { "text": "I'll go back to my computer here.", "start": 3488.76, "duration": 1.83 }, { "text": "So I'm back in my [? TERMINAL ?] here.", "start": 3494.497, "duration": 1.583 }, { "text": "And I can type sqlite3 mfa.db\nto reopen this database.", "start": 3496.08, "duration": 5.1 }, { "text": "And notice how if I type .schema, I've\nupdated this schema to now have ON", "start": 3501.18, "duration": 4.98 }, { "text": "DELETE CASCADE.", "start": 3506.16, "duration": 0.99 }, { "text": "Let me show you over here on the screen.", "start": 3507.15, "duration": 1.98 }, { "text": "Here on the created table, I now have\nthe very same kind of table schema,", "start": 3509.13, "duration": 5.16 }, { "text": "but now my only difference is\nI've applied this ON DELETE action", "start": 3514.29, "duration": 4.32 }, { "text": "to my [? foreign ?]\n[? key ?] constraints.", "start": 3518.61, "duration": 1.95 }, { "text": "In particular, I'm going\nto [? cascade ?] the delete", "start": 3520.56, "duration": 2.31 }, { "text": "from the artist's table\nto the created table.", "start": 3522.87, "duration": 3.7 }, { "text": "So let's try that out in SQLite.", "start": 3526.57, "duration": 2.54 }, { "text": "I'll come back over here,\nand I will now try just", "start": 3529.11, "duration": 3.66 }, { "text": "DELETE FROM the \"artists\" table WHERE\nthe \"name\" equals \"Unidentified artist.", "start": 3532.77, "duration": 8.67 }, { "text": "semicolon.", "start": 3541.44, "duration": 0.99 }, { "text": "Now, I'll hit Enter, and I don't get\na FOREIGN KEY constraint anymore.", "start": 3542.43, "duration": 5.85 }, { "text": "But if I say SELECT [? star ?]\nFROM \"created\" semicolon,", "start": 3548.28, "duration": 4.5 }, { "text": "notice how I've also deleted the\nartist's affiliation with their work.", "start": 3552.78, "duration": 5.35 }, { "text": "So that is wherever in\nthe artist_id column,", "start": 3558.13, "duration": 3.83 }, { "text": "I saw the id for the artist I\ndeleted, I would to delete that row,", "start": 3561.96, "duration": 5.98 }, { "text": "so I have no references to that primary\nkey, which is now gone from my table.", "start": 3567.94, "duration": 6.35 }, { "text": "So let me ask here.", "start": 3574.29, "duration": 2.05 }, { "text": "We've seen how to delete single\nrows, how to delete multiple rows,", "start": 3576.34, "duration": 5.27 }, { "text": "and now how to delete data\namong some constraints,", "start": 3581.61, "duration": 2.985 }, { "text": "like our [? foreign ?]\n[? key ?] constraints.", "start": 3584.595, "duration": 1.875 }, { "text": "What questions do we\nhave on those techniques?", "start": 3586.47, "duration": 5.16 }, { "text": "Let's go to [? Han. ?]", "start": 3591.63, "duration": 1.02 }, { "text": "SPEAKER 13: I have a\nquestion regarding deleting.", "start": 3592.65, "duration": 3.09 }, { "text": "The [? id ?] numbers have been removed\nwhile we were removing the record.", "start": 3595.74, "duration": 5.55 }, { "text": "I was wondering if do we\nhave to clean it up somehow", "start": 3601.29, "duration": 2.61 }, { "text": "later or will they be populated\nwith the new data as it comes along?", "start": 3603.9, "duration": 4.017 }, { "text": "SPEAKER 1: Yeah, great question.", "start": 3607.917, "duration": 1.333 }, { "text": "So the question is, what happens to our\nprimary keys when we delete our data.", "start": 3609.25, "duration": 4.44 }, { "text": "So for this one, let\nme show you the visual", "start": 3613.69, "duration": 2.21 }, { "text": "again that we had before of our\ntables nicely printed on the side.", "start": 3615.9, "duration": 3.78 }, { "text": "So I'll come back here, and I will go\nback to our idea of these joint tables", "start": 3619.68, "duration": 7.11 }, { "text": "where we had an artists table, a\ncollections table, and a created table.", "start": 3626.79, "duration": 4.65 }, { "text": "And we saw before that\nwe're going to delete", "start": 3631.44, "duration": 1.89 }, { "text": "this artist called Unidentified artist.", "start": 3633.33, "duration": 2.67 }, { "text": "So I'll delete them, and to your\npoint, [? well ?] the idea of 3", "start": 3636.0, "duration": 4.29 }, { "text": "no longer exists in this case.", "start": 3640.29, "duration": 3.12 }, { "text": "Now by default, at least in SQLite,\nif I insert a new row, what I'll do", "start": 3643.41, "duration": 6.69 }, { "text": "is take the highest id\nvalue and I will then", "start": 3650.1, "duration": 3.84 }, { "text": "make that the new primary key\nfor the new row that I insert.", "start": 3653.94, "duration": 4.5 }, { "text": "That is the default situation.", "start": 3658.44, "duration": 2.7 }, { "text": "I could if I wanted\nto get more specific,", "start": 3661.14, "duration": 3.03 }, { "text": "and if I had done in my id integer\ncolumn here in my [? create ?]", "start": 3664.17, "duration": 7.38 }, { "text": "[? table, ?] if I had also said\nthis [? keyword ?] [? call ?]", "start": 3671.55, "duration": 3.15 }, { "text": "[? autoincrement, ?] all one word,\nwhat would happen instead is I would", "start": 3674.7, "duration": 4.98 }, { "text": "actually reinsert an\nID that is not used.", "start": 3679.68, "duration": 4.03 }, { "text": "So in this case 3 is not used,\nI could insert that one here.", "start": 3683.71, "duration": 4.38 }, { "text": "So up to you what you want to do.", "start": 3688.09, "duration": 2.27 }, { "text": "In general, SQlite by default will take\nthe highest ID, add one from there.", "start": 3690.36, "duration": 5.31 }, { "text": "If you specify this constraint\n[? called ?] [? autoincrement, ?] you", "start": 3695.67, "duration": 3.27 }, { "text": "will instead take whatever id is not\nbeing used and use that in your insert", "start": 3698.94, "duration": 5.37 }, { "text": "afterwards.", "start": 3704.31, "duration": 2.15 }, { "text": "OK, so now we've seen how to\ninsert, and how to delete data.", "start": 3706.46, "duration": 5.07 }, { "text": "But of course, we make mistakes when we\nadd data or even when we delete data.", "start": 3711.53, "duration": 4.03 }, { "text": "So we'll see in just a minute\nhow to update our values", "start": 3715.56, "duration": 3.02 }, { "text": "as well to correct typos and\neven to update associations", "start": 3718.58, "duration": 3.33 }, { "text": "between artists and artwork.", "start": 3721.91, "duration": 2.88 }, { "text": "And we're back.", "start": 3724.79, "duration": 1.72 }, { "text": "So we've so far seen how to\ninsert some data into our tables", "start": 3726.51, "duration": 3.74 }, { "text": "and how to delete it.", "start": 3730.25, "duration": 1.83 }, { "text": "But sometimes, we don't want\nto fully delete something,", "start": 3732.08, "duration": 3.39 }, { "text": "we just want to change its\nvalue to correct some typo", "start": 3735.47, "duration": 3.03 }, { "text": "or correct some association.", "start": 3738.5, "duration": 1.98 }, { "text": "So let's think to our MFA example,\n[? where ?] the Museum of Fine Arts", "start": 3740.48, "duration": 4.47 }, { "text": "has some tables that look like this.", "start": 3744.95, "duration": 2.32 }, { "text": "They have artists in their\ncollection, and they also", "start": 3747.27, "duration": 2.78 }, { "text": "have art work in their collection.", "start": 3750.05, "duration": 2.13 }, { "text": "They also have a created table to\nassociate artists with their artwork.", "start": 3752.18, "duration": 4.77 }, { "text": "Now in this case, we know that\nI have this Unidentified artist.", "start": 3756.95, "duration": 6.24 }, { "text": "And we can see that they authored\nthis item in the collections,", "start": 3763.19, "duration": 4.23 }, { "text": "\"Farmers working at dawn.\"", "start": 3767.42, "duration": 2.58 }, { "text": "But let's say that later on, we find\nout it wasn't an Unidentified artist.", "start": 3770.0, "duration": 5.73 }, { "text": "It was instead Li Yin, who\ncreated \"Farmers working at dawn.\"", "start": 3775.73, "duration": 4.29 }, { "text": "How could we update our tables to\nmake sure it's Li Yin, who we have", "start": 3780.02, "duration": 6.33 }, { "text": "creating \"Farmers working at dawn.\"", "start": 3786.35, "duration": 4.31 }, { "text": "SPEAKER 14: So what we could do is--", "start": 3790.66, "duration": 4.494 }, { "text": "we know that in created, we have our\nartist_id and [? collection id, ?]", "start": 3795.154, "duration": 5.536 }, { "text": "so we could update it there.", "start": 3800.69, "duration": 3.23 }, { "text": "But somehow, we need to cascade it over\nto the [? artists ?] [INAUDIBLE] now", "start": 3803.92, "duration": 4.92 }, { "text": "this unidentified [? artist ?]\n[? through-- ?] I think we probably", "start": 3808.84, "duration": 3.54 }, { "text": "don't need it.", "start": 3812.38, "duration": 0.727 }, { "text": "SPEAKER 1: Yeah, so a good point here.", "start": 3813.107, "duration": 1.583 }, { "text": "We could actually probably change the\ncreated table to [? re-associate ?]", "start": 3814.69, "duration": 4.56 }, { "text": "an artist with some new artwork here.", "start": 3819.25, "duration": 2.5 }, { "text": "So let's visualize this.", "start": 3821.75, "duration": 1.13 }, { "text": "I go to my created table.", "start": 3822.88, "duration": 1.66 }, { "text": "And here the artist_id is currently\n3, but I want it really to be 1.", "start": 3824.54, "duration": 6.47 }, { "text": "I want Li Yin to be associated with this\npiece called \"Farmers working at dawn.\"", "start": 3831.01, "duration": 4.86 }, { "text": "So I could update the created table\nto instead of having 3 here have 1.", "start": 3835.87, "duration": 5.73 }, { "text": "Now, we see Li Yin created \"Farmers\nworking at dawn.\" [? And if ?]", "start": 3841.6, "duration": 4.62 }, { "text": "you're concerned about this\nUnidentified artist here,", "start": 3846.22, "duration": 2.61 }, { "text": "I think it's OK to have\nan artist in our table", "start": 3848.83, "duration": 2.55 }, { "text": "who may or may not have\nan item in collections.", "start": 3851.38, "duration": 2.97 }, { "text": "We'll say that's OK at least for now.", "start": 3854.35, "duration": 2.89 }, { "text": "So if we can update our associations\nbetween artists and collections", "start": 3857.24, "duration": 5.03 }, { "text": "like this, let's actually try to do\nthat in our very own database here.", "start": 3862.27, "duration": 4.5 }, { "text": "I'll go back to my SQLite environment,\nand now, let me try to open it first.", "start": 3866.77, "duration": 6.24 }, { "text": "I'll use sqlite3 mfa.db.", "start": 3873.01, "duration": 2.55 }, { "text": "And now let me type .schema to show you\n[INAUDIBLE] the very same schema from", "start": 3875.56, "duration": 3.9 }, { "text": "before.", "start": 3879.46, "duration": 0.79 }, { "text": "So I want to update the artists\nassociation between Li Yin and \"Farmers", "start": 3880.25, "duration": 5.09 }, { "text": "working at dawn.\"", "start": 3885.34, "duration": 1.23 }, { "text": "So let's say, I'll SELECT [? star ?]\nFROM \"created,\" like this, semicolon.", "start": 3886.57, "duration": 4.8 }, { "text": "Here, I have my artist_ids\nand my collection_ids.", "start": 3891.37, "duration": 4.62 }, { "text": "I see that we have the\nUnidentified artist, id of 3,", "start": 3895.99, "duration": 4.35 }, { "text": "creating \"Farmers working at dawn,\"\nwith the id of 1 in our collection here.", "start": 3900.34, "duration": 5.35 }, { "text": "So now, let's try to update the artist\nwho created this particular painting.", "start": 3905.69, "duration": 5.48 }, { "text": "Here, I have 3 associated with 1,\nbut I want 1 associated with 1,", "start": 3911.17, "duration": 5.49 }, { "text": "where 1 is the id for Li Yin.", "start": 3916.66, "duration": 1.92 }, { "text": "And [? 1 ?] [? in ?] my collection\nid is the id for this artwork here.", "start": 3918.58, "duration": 4.278 }, { "text": "So let me try this.", "start": 3922.858, "duration": 0.792 }, { "text": "I'll say UPDATE \"created\" and\nSET, let's say, \"artist_id\"", "start": 3923.65, "duration": 5.97 }, { "text": "equal to some particular value.", "start": 3929.62, "duration": 3.15 }, { "text": "Well, what value should I set it to?", "start": 3932.77, "duration": 2.88 }, { "text": "I could try to set it to Li Yin's\nid, which we know is just 1.", "start": 3935.65, "duration": 4.29 }, { "text": "But let me try instead to\nuse a [? subquery ?] here.", "start": 3939.94, "duration": 3.36 }, { "text": "Let me try to say parentheses, and then\nwrite some query to update this value.", "start": 3943.3, "duration": 5.82 }, { "text": "I'll say SELECT \"id\" from\n\"artists\" like this Enter again,", "start": 3949.12, "duration": 6.18 }, { "text": "WHERE the \"name\" equals Li Yin.", "start": 3955.3, "duration": 4.89 }, { "text": "And then let me close\nthis [? subquery. ?] Well,", "start": 3960.19, "duration": 3.9 }, { "text": "if I try to run this query, what might\nHappen I'm updating the created table,", "start": 3964.09, "duration": 6.42 }, { "text": "I'm setting the artist_id equal to the\nid for Li Yin, but what I've forgotten", "start": 3970.51, "duration": 6.09 }, { "text": "is this WHERE to only\nchoose some rows to update.", "start": 3976.6, "duration": 4.92 }, { "text": "[INAUDIBLE] not close it yet.", "start": 3981.52, "duration": 1.68 }, { "text": "I'll instead say WHERE, in this\ncase, the \"collection_id,\" the piece", "start": 3983.2, "duration": 5.94 }, { "text": "of artwork in our collection,\nis equal to [? while ?] the id", "start": 3989.14, "duration": 4.98 }, { "text": "for this painting.", "start": 3994.12, "duration": 1.21 }, { "text": "I'll say SELECT \"id\" from\n\"collections,\" and then I'll", "start": 3995.33, "duration": 3.77 }, { "text": "say WHERE the \"title\" equals\n\"Farmers working at dawn.\"", "start": 3999.1, "duration": 6.61 }, { "text": "Now, I'll close this\n[? subquery, ?] and hit semicolon.", "start": 4005.71, "duration": 3.99 }, { "text": "And here, we've seen our first\nexample of an UPDATE query.", "start": 4009.7, "duration": 3.9 }, { "text": "I'm trying to update\nthe artist_id column", "start": 4013.6, "duration": 2.91 }, { "text": "in created to be the id for Li Yin.", "start": 4016.51, "duration": 3.48 }, { "text": "I only want to do that\nthough on the row where", "start": 4019.99, "duration": 3.06 }, { "text": "collection_id is equal to the\nid for this particular painting.", "start": 4023.05, "duration": 4.41 }, { "text": "[INAUDIBLE] change the attribution for.", "start": 4027.46, "duration": 2.01 }, { "text": "So now, I'll hit Enter.", "start": 4029.47, "duration": 1.89 }, { "text": "And if I SELECT\n[? star ?] FROM \"created,\"", "start": 4031.36, "duration": 3.01 }, { "text": "I should hopefully see in this\ncase that the artist_id associated", "start": 4034.37, "duration": 5.9 }, { "text": "with this painting is 2 and also\ndown below here is 1 as well.", "start": 4040.27, "duration": 5.41 }, { "text": "So I have Li Yin associated\nwith now two paintings overall.", "start": 4045.68, "duration": 5.61 }, { "text": "So let's get a grasp on what\nthis [? UPDATE ?] syntax really", "start": 4051.29, "duration": 3.47 }, { "text": "looks like in general.", "start": 4054.76, "duration": 1.38 }, { "text": "And for that, let's show the slide here.", "start": 4056.14, "duration": 2.58 }, { "text": "We have this [? UPDATE ?] [? keyword, ?]\n[? UPDATE ?] statement in SQL to take", "start": 4058.72, "duration": 5.37 }, { "text": "a table name and update\nthe columns inside of it.", "start": 4064.09, "duration": 3.42 }, { "text": "I say UPDATE, then the name\nof the table I want to update,", "start": 4067.51, "duration": 3.9 }, { "text": "then I say SET some column\nequal to some value.", "start": 4071.41, "duration": 5.4 }, { "text": "I could if I wanted to have\nmore than one column here.", "start": 4076.81, "duration": 3.1 }, { "text": "I could say maybe title\nand even maybe if we're", "start": 4079.91, "duration": 3.71 }, { "text": "talking about authors, authors\nover here, or even acquired date.", "start": 4083.62, "duration": 3.42 }, { "text": "I could update more than one column\nin my [? SET ?] [? portion ?] here.", "start": 4087.04, "duration": 4.89 }, { "text": "Then comes this WHERE portion,\nwhere some condition is true.", "start": 4091.93, "duration": 4.44 }, { "text": "I want to make sure I don't\nupdate all of my rows.", "start": 4096.37, "duration": 3.6 }, { "text": "I only update those where some\ncondition is actually true.", "start": 4099.97, "duration": 4.3 }, { "text": "So this is your syntax\nfor updating some columns.", "start": 4104.27, "duration": 3.799 }, { "text": "Let's say, if you want to\nchange an artist's attribution", "start": 4108.069, "duration": 2.79 }, { "text": "or if you want to change\na typo you've made.", "start": 4110.859, "duration": 4.091 }, { "text": "So let's see this now, not\njust in terms of changing", "start": 4114.95, "duration": 3.86 }, { "text": "artists and their attributions,\nlet's see a use case for update", "start": 4118.81, "duration": 3.584 }, { "text": "where we've made some\nmistakes in our data.", "start": 4122.394, "duration": 2.906 }, { "text": "And let's say the museum decides\nto host some kind of event", "start": 4125.3, "duration": 4.309 }, { "text": "where people vote on their\nfavorite piece of artwork,", "start": 4129.609, "duration": 2.551 }, { "text": "they kind of [? handwrite ?] or\ntype it into some online form.", "start": 4132.16, "duration": 2.76 }, { "text": "Well, when I get back, a\nCSV of those responses,", "start": 4134.92, "duration": 3.09 }, { "text": "some Comma Separated Values, one\nline for each vote from our people", "start": 4138.01, "duration": 4.59 }, { "text": "who've attended this convention.", "start": 4142.6, "duration": 2.04 }, { "text": "Let's go back over here,\nand I'll show you that CSV.", "start": 4144.64, "duration": 3.839 }, { "text": "Let me go to my environment.", "start": 4148.479, "duration": 2.581 }, { "text": "And I'll type code mfa.csv to\nopen this CSV that I already have.", "start": 4151.06, "duration": 5.879 }, { "text": "And here, actually,\nit's not called mfa.csv.", "start": 4156.939, "duration": 2.491 }, { "text": "It's called code votes.csv.", "start": 4159.43, "duration": 3.0 }, { "text": "And now here we can see, I\nhave a table of one column", "start": 4162.43, "duration": 5.46 }, { "text": "that has several votes inside of it.", "start": 4167.89, "duration": 1.98 }, { "text": "Let's see, we have maybe\n20 votes to be exact.", "start": 4169.87, "duration": 3.27 }, { "text": "So the first row is the header row.", "start": 4173.14, "duration": 2.7 }, { "text": "I have in this CSV, one\ncolumn called title.", "start": 4175.84, "duration": 3.809 }, { "text": "And each line here is one vote for\na [? museum-goers ?] favorite piece", "start": 4179.649, "duration": 4.38 }, { "text": "of artwork.", "start": 4184.029, "duration": 0.721 }, { "text": "We see \"Farmers working at dawn,\"\nwe see \"Imaginative landscape,\"", "start": 4184.75, "duration": 3.569 }, { "text": "\"Profusion of flowers.\"", "start": 4188.319, "duration": 1.471 }, { "text": "And our goal is to count up these\nvotes to see which is the most popular.", "start": 4189.79, "duration": 5.49 }, { "text": "Well, let's try using .import again to\nactually turn this CSV into our very", "start": 4195.28, "duration": 6.39 }, { "text": "own SQLite database.", "start": 4201.67, "duration": 1.86 }, { "text": "Let me go back to my [? TERMINAL, ?]\nand I'll type sqlite3 votes.db", "start": 4203.53, "duration": 4.71 }, { "text": "to create a votes database.", "start": 4208.24, "duration": 2.34 }, { "text": "I'll hit Enter.", "start": 4210.58, "duration": 1.05 }, { "text": "And now, I can use .import.", "start": 4211.63, "duration": 2.76 }, { "text": "I could say .import a CSV called\nvotes.csv into a table also called", "start": 4214.39, "duration": 7.05 }, { "text": "votes.", "start": 4221.44, "duration": 0.93 }, { "text": "I'll Enter now.", "start": 4222.37, "duration": 1.02 }, { "text": "And now if I SELECT [? star ?]\nFROM \"votes\" semicolon,", "start": 4223.39, "duration": 4.89 }, { "text": "I'll see all of my votes\nnow in a single table.", "start": 4228.28, "duration": 4.93 }, { "text": "So I want to tally these votes.", "start": 4233.21, "duration": 1.83 }, { "text": "And we could do that using a\ntechnique we learned back in the week", "start": 4235.04, "duration": 2.75 }, { "text": "on querying and relating.", "start": 4237.79, "duration": 2.22 }, { "text": "What if I wanted to group by my\ntitles and count up those groups,", "start": 4240.01, "duration": 4.92 }, { "text": "I could do that.", "start": 4244.93, "duration": 1.06 }, { "text": "I could say SELECT, let's say,\n\"title,\" and also the COUNT of \"title,\"", "start": 4245.99, "duration": 5.0 }, { "text": "and [? they ?] count up the votes\nfor each title FROM, in this case,", "start": 4250.99, "duration": 4.54 }, { "text": "my \"votes\" table like this.", "start": 4255.53, "duration": 2.52 }, { "text": "Now, I want to count not just each vote\nindividually, but I want to group them.", "start": 4258.05, "duration": 5.46 }, { "text": "I want to count votes by group\nwhere that group is, in this case,", "start": 4263.51, "duration": 4.36 }, { "text": "the \"title\" itself.", "start": 4267.87, "duration": 1.74 }, { "text": "So let me try running this\nquery and hitting Enter.", "start": 4269.61, "duration": 3.5 }, { "text": "Well, [? I'll ?] [? see ?] I've\nstarted to count some votes,", "start": 4273.11, "duration": 4.26 }, { "text": "like I have Imaginative landscape here,\nSpring outing, Farmers working at dawn,", "start": 4277.37, "duration": 5.76 }, { "text": "Spring outing down below here too.", "start": 4283.13, "duration": 1.98 }, { "text": "But I seem to be missing something,\nlike I shouldn't have this many groups.", "start": 4285.11, "duration": 3.84 }, { "text": "I should only have four groups.", "start": 4288.95, "duration": 1.62 }, { "text": "Why do I have so many?", "start": 4290.57, "duration": 2.37 }, { "text": "Well, it seems like there\nare some typos here.", "start": 4292.94, "duration": 2.07 }, { "text": "What typos do you see?", "start": 4295.01, "duration": 2.65 }, { "text": "Feel free to raise your hand.", "start": 4297.66, "duration": 3.02 }, { "text": "SPEAKER 15: So I think\nthat it is a title--", "start": 4300.68, "duration": 3.24 }, { "text": "when you count it is case sensitive,\nso maybe it is counting one by one.", "start": 4303.92, "duration": 5.295 }, { "text": "SPEAKER 1: Yeah, good idea.", "start": 4309.215, "duration": 1.125 }, { "text": "So maybe it's case sensitive our\n[? GROUP ?] [? BY, ?] and you'd be", "start": 4310.34, "duration": 3.33 }, { "text": "correct if you said that. [? GROUP ?]\n[? BY ?] is, in this case,", "start": 4313.67, "duration": 2.88 }, { "text": "case sensitive.", "start": 4316.55, "duration": 1.36 }, { "text": "So if we see here, we see\nFarmers Working at Dawn,", "start": 4317.91, "duration": 3.11 }, { "text": "all capitalized, at least the\nFarmers Working and Dawn capitalized.", "start": 4321.02, "duration": 3.87 }, { "text": "Well, that won't be\npart of the same group", "start": 4324.89, "duration": 2.34 }, { "text": "as let's say Farmers working at dawn\nall properly capitalized, in this case,", "start": 4327.23, "duration": 6.36 }, { "text": "with lowercase working and lowercase\ndawn as it is the MFA collections.", "start": 4333.59, "duration": 4.35 }, { "text": "So we could probably fix\nsome capitalization here.", "start": 4337.94, "duration": 3.42 }, { "text": "What other things could we fix?", "start": 4341.36, "duration": 1.88 }, { "text": "So let's hear from one more person.", "start": 4343.24, "duration": 2.425 }, { "text": "Let's go to [? Lawrence. ?]", "start": 4345.665, "duration": 1.125 }, { "text": "SPEAKER 16: [? The ?]\n[? count ?] on each typo.", "start": 4346.79, "duration": 2.873 }, { "text": "SPEAKER 1: There are some\ntypos in here, absolutely.", "start": 4349.663, "duration": 2.167 }, { "text": "So if I look at some of\nthese, I see not farmers,", "start": 4351.83, "duration": 3.57 }, { "text": "but [? Famers ?] working at dawn.", "start": 4355.4, "duration": 2.22 }, { "text": "I might see instead of\nImaginative landscape,", "start": 4357.62, "duration": 2.79 }, { "text": "I just see [? Imagintive ?]\nlandscape, so definitely some", "start": 4360.41, "duration": 3.06 }, { "text": "typos in here to fix as well.", "start": 4363.47, "duration": 2.65 }, { "text": "So let's start working on using our\n[? UPDATE ?] tool or [? UPDATE ?]", "start": 4366.12, "duration": 3.26 }, { "text": "keyword to fix some of this data,\nto clean it up, so to speak,", "start": 4369.38, "duration": 3.85 }, { "text": "so we can count up our\nvotes for these artworks.", "start": 4373.23, "duration": 3.23 }, { "text": "Come back to my [? TERMINAL. ?] And now,\nlet's try updating to at least remove", "start": 4376.46, "duration": 6.09 }, { "text": "some of this [? whitespace ?] here.", "start": 4382.55, "duration": 1.62 }, { "text": "You might notice that Imaginative\nlandscape has a space before it,", "start": 4384.17, "duration": 3.73 }, { "text": "and so does Spring outing here.", "start": 4387.9, "duration": 1.67 }, { "text": "That space counts as part of the\ntitle, and I want to remove it.", "start": 4389.57, "duration": 4.29 }, { "text": "So what could I do?", "start": 4393.86, "duration": 1.99 }, { "text": "I could try using a function in SQLite,\none called [? trim. ?] [? Trim ?] takes", "start": 4395.85, "duration": 5.9 }, { "text": "a string and just removes leading and\ntrailing [? whitespace, ?] [INAUDIBLE]", "start": 4401.75, "duration": 4.27 }, { "text": "back only the characters in the middle.", "start": 4406.02, "duration": 2.63 }, { "text": "So let me try that.", "start": 4408.65, "duration": 1.18 }, { "text": "I'll say UPDATE \"votes,\"\nmy table here, and SET", "start": 4409.83, "duration": 4.58 }, { "text": "the \"title\" column equal to what?", "start": 4414.41, "duration": 4.3 }, { "text": "Well, the trimmed version of title.", "start": 4418.71, "duration": 2.42 }, { "text": "Trim looks like this--", "start": 4421.13, "duration": 1.68 }, { "text": "trim, and then some parentheses\nto say the input to this function.", "start": 4422.81, "duration": 4.69 }, { "text": "I'll give it the \"title\" column.", "start": 4427.5, "duration": 2.12 }, { "text": "Now, I'll hit semicolon.", "start": 4429.62, "duration": 1.99 }, { "text": "And this query will run as follows.", "start": 4431.61, "duration": 2.64 }, { "text": "It will update the votes table.", "start": 4434.25, "duration": 2.12 }, { "text": "And inside the votes table\nset, the title column", "start": 4436.37, "duration": 3.51 }, { "text": "equal to the trimmed version of\nwhatever values are inside that column,", "start": 4439.88, "duration": 5.07 }, { "text": "trimming here means remove trailing\nand leading [? whitespace. ?]", "start": 4444.95, "duration": 4.26 }, { "text": "I don't need to apply any condition.", "start": 4449.21, "duration": 2.64 }, { "text": "I don't need to say WHERE dot\ndot dot, because I want this", "start": 4451.85, "duration": 3.42 }, { "text": "to apply to all rows in my data set.", "start": 4455.27, "duration": 3.64 }, { "text": "Now, I'll hit Enter.", "start": 4458.91, "duration": 1.31 }, { "text": "And let me try running this again.", "start": 4460.22, "duration": 1.53 }, { "text": "I'll say up arrow to\nget back my old query.", "start": 4461.75, "duration": 3.09 }, { "text": "Now, I'll try counting again, and\nI think we're making some progress,", "start": 4464.84, "duration": 4.59 }, { "text": "like I can see that I've removed--", "start": 4469.43, "duration": 2.19 }, { "text": "[? removed ?] [? quote ?] [? unquote, ?]\nthose titles that had leading", "start": 4471.62, "duration": 3.27 }, { "text": "or trailing [? whitespace. ?]\nThey're now part of a group", "start": 4474.89, "duration": 2.91 }, { "text": "because their titles\nnow match those groups.", "start": 4477.8, "duration": 3.6 }, { "text": "Let's keep going.", "start": 4481.4, "duration": 0.72 }, { "text": "We noticed before, we had\nsome capitalization issues.", "start": 4482.12, "duration": 2.5 }, { "text": "So let's fix those as well.", "start": 4484.62, "duration": 2.31 }, { "text": "I can go back to my [? TERMINAL, ?] and\nwe could introduce this other function,", "start": 4486.93, "duration": 3.74 }, { "text": "one called [? upper. ?]", "start": 4490.67, "duration": 1.5 }, { "text": "[? Upper ?] converts\na string like Farmers", "start": 4492.17, "duration": 3.45 }, { "text": "working at dawn to be all in\nuppercase, kind of shout it out.", "start": 4495.62, "duration": 3.87 }, { "text": "And that's useful for us here because\nwe could kind of force everything", "start": 4499.49, "duration": 3.57 }, { "text": "to uppercase, thereby removing any\ninconsistencies in capitalization.", "start": 4503.06, "duration": 4.78 }, { "text": "So let me try that.", "start": 4507.84, "duration": 1.11 }, { "text": "I'll say UPDATE, again,\nthe \"votes\" table.", "start": 4508.95, "duration": 3.18 }, { "text": "And this time, I'll set \"title\" equal\nto the uppercase version of \"title.\"", "start": 4512.13, "duration": 6.35 }, { "text": "So upper here is a\nfunction in SQLite that", "start": 4518.48, "duration": 3.72 }, { "text": "takes some values associated with\nthe column and [? uppercases ?]", "start": 4522.2, "duration": 3.33 }, { "text": "the entire thing to remove any\ninconsistencies in capitalization.", "start": 4525.53, "duration": 4.89 }, { "text": "Again, no condition here, because\nI want to apply this to all rows.", "start": 4530.42, "duration": 4.2 }, { "text": "I'll hit Enter.", "start": 4534.62, "duration": 1.11 }, { "text": "And now if I SELECT [? star ?]\nFROM \"collections\" semicolon--", "start": 4535.73, "duration": 5.61 }, { "text": "oops, SELECT [? star ?]\nFROM \"votes,\" in this case--", "start": 4541.34, "duration": 2.278 }, { "text": "no longer as collections table.", "start": 4543.618, "duration": 1.292 }, { "text": "I'll say SELECT [? star ?]\nFROM \"votes\" semicolon,", "start": 4544.91, "duration": 3.12 }, { "text": "I should see all of\nmy titles capitalized.", "start": 4548.03, "duration": 3.36 }, { "text": "But now, I want to group by.", "start": 4551.39, "duration": 1.74 }, { "text": "So let's try this.", "start": 4553.13, "duration": 0.93 }, { "text": "I'll go up arrow to get back to my old\nquery that GROUP BY \"title,\" hit Enter.", "start": 4554.06, "duration": 6.06 }, { "text": "And now, we're still\nmaking some more progress.", "start": 4560.12, "duration": 2.64 }, { "text": "I'm reducing my number of\ngroups until I get to 4.", "start": 4562.76, "duration": 3.21 }, { "text": "But there's still some typos, like\nwe see Famers working at dawn,", "start": 4565.97, "duration": 4.29 }, { "text": "or Farmers working, or\nFarmesr working at dawn.", "start": 4570.26, "duration": 3.87 }, { "text": "We want these to be included\nas part of this group--", "start": 4574.13, "duration": 2.43 }, { "text": "Farmers working at dawn.", "start": 4576.56, "duration": 2.31 }, { "text": "So we should fix some\nof these typos here.", "start": 4578.87, "duration": 3.37 }, { "text": "Let me go back to my [? TERMINAL. ?]\nNow, let's try this out.", "start": 4582.24, "duration": 3.14 }, { "text": "I could, in this case, just\nmanually correct these.", "start": 4585.38, "duration": 3.66 }, { "text": "You could imagine me\ndoing something like this.", "start": 4589.04, "duration": 2.13 }, { "text": "UPDATE \"votes\" and SET the\n\"title\" equal to all caps", "start": 4591.17, "duration": 5.73 }, { "text": "\"FARMERS WORKING AT DAWN,\" now\nincluding a condition, WHERE the \"title\"", "start": 4596.9, "duration": 6.27 }, { "text": "presently is equal to for \"FAMERS\nWORKING\" like this, semicolon--", "start": 4603.17, "duration": 7.447 }, { "text": "or not Famers work--", "start": 4610.617, "duration": 0.833 }, { "text": "I think it's Farmers Working, just all--", "start": 4611.45, "duration": 3.03 }, { "text": "in two words.", "start": 4614.48, "duration": 1.14 }, { "text": "That was one title we saw.", "start": 4615.62, "duration": 1.083 }, { "text": "[? TRANSCRIPT BREAK ?]", "start": 4616.703, "duration": 0.917 }, { "text": "SPEAKER: I could manually\nupdate it a bit like this.", "start": 4617.62, "duration": 2.33 }, { "text": "Let me hit Enter, and\nlet me do the same query", "start": 4619.95, "duration": 2.66 }, { "text": "by hitting the up arrow on my keyboard.", "start": 4622.61, "duration": 2.94 }, { "text": "Let me find this one,\nhit Enter, and now we", "start": 4625.55, "duration": 2.36 }, { "text": "can see that that title,\n\"Farmers Working,\"", "start": 4627.91, "duration": 2.97 }, { "text": "is now just part of the main group.", "start": 4630.88, "duration": 2.16 }, { "text": "\"Farmers Working At Dawn.\"", "start": 4633.04, "duration": 2.43 }, { "text": "I could keep going here.", "start": 4635.47, "duration": 1.57 }, { "text": "I could try to convert \"Farmers Working\nAt Dawn\" to \"Farmers Working At Dawn.\"", "start": 4637.04, "duration": 4.52 }, { "text": "Let me try it.", "start": 4641.56, "duration": 0.94 }, { "text": "I could say select.", "start": 4642.5, "duration": 1.61 }, { "text": "Let's say-- not select.", "start": 4644.11, "duration": 1.842 }, { "text": "Let me update.", "start": 4645.952, "duration": 0.583 }, { "text": "Let me update in this\ncase votes, and set", "start": 4646.535, "duration": 3.155 }, { "text": "title equal to \"Farmers Working\nAt Dawn\" where the title equals", "start": 4649.69, "duration": 7.65 }, { "text": "in this case \"Farmers Working At Dawn.\"", "start": 4657.34, "duration": 3.99 }, { "text": "Semicolon, Enter, let me hit up arrow\nto go back to my old query to group by,", "start": 4661.33, "duration": 6.0 }, { "text": "and now we're getting somewhere.", "start": 4667.33, "duration": 1.56 }, { "text": "I'm seeing now only two groups\nfor \"Farmers Working At Dawn,\"", "start": 4668.89, "duration": 3.48 }, { "text": "but one more to fix.", "start": 4672.37, "duration": 2.82 }, { "text": "Now we'd be here for a while\nif I spend all this time", "start": 4675.19, "duration": 3.96 }, { "text": "one by one fixing these typos,\nso there's probably a better way.", "start": 4679.15, "duration": 5.23 }, { "text": "There is.", "start": 4684.38, "duration": 0.77 }, { "text": "This one is going to be\nusing the like keyword.", "start": 4685.15, "duration": 2.94 }, { "text": "We don't have to say equal so much.", "start": 4688.09, "duration": 2.25 }, { "text": "We can just say like.", "start": 4690.34, "duration": 1.5 }, { "text": "Now, I could try to match any title that\nlooks like \"Farmers Working At Dawn\"", "start": 4691.84, "duration": 5.01 }, { "text": "and update it to be the correct title.", "start": 4696.85, "duration": 2.55 }, { "text": "So let's try that here.", "start": 4699.4, "duration": 1.99 }, { "text": "I could now try update votes and\nset title equal to \"Farmers Working", "start": 4701.39, "duration": 7.76 }, { "text": "At Dawn.\"", "start": 4709.15, "duration": 1.83 }, { "text": "\"Farmers Working At Dawn\"\nwhere the title is like--", "start": 4710.98, "duration": 7.51 }, { "text": "the title is like some\npattern I could give.", "start": 4718.49, "duration": 3.51 }, { "text": "Now for this data set, it turns out that\nthe only artwork that begins with F-A", "start": 4722.0, "duration": 6.21 }, { "text": "is Farmers Working At Dawn.", "start": 4728.21, "duration": 1.77 }, { "text": "So to match any title with F-A\nand then anything afterwards,", "start": 4729.98, "duration": 5.13 }, { "text": "I could use F-A percent, and\nthen close out that pattern.", "start": 4735.11, "duration": 6.0 }, { "text": "Notice how I don't have to\nhave consistent capitalization.", "start": 4741.11, "duration": 3.69 }, { "text": "Like is case insensitive, and\nnotice how, too, the percent matches", "start": 4744.8, "duration": 4.62 }, { "text": "anything that comes after F-A.\nNow in this data set, this is OK,", "start": 4749.42, "duration": 6.45 }, { "text": "but in a larger data set, you probably\nwant to avoid this kind of query", "start": 4755.87, "duration": 4.62 }, { "text": "because you could match much more\nthan Farmers Working At Dawn.", "start": 4760.49, "duration": 3.09 }, { "text": "You'd match anything that begins\nwith F-A, so be careful with this", "start": 4763.58, "duration": 4.02 }, { "text": "and think through what pattern\nshould you use to fix up your titles.", "start": 4767.6, "duration": 4.77 }, { "text": "Let me just hit Enter here, and now\nlet me try hitting up arrow again", "start": 4772.37, "duration": 3.75 }, { "text": "to show you the result, and now\nwe're in a pretty good place.", "start": 4776.12, "duration": 4.41 }, { "text": "I see all of my titles like Farmers\nWorking At Dawn are correctly", "start": 4780.53, "duration": 4.29 }, { "text": "capitalized, consistently\nspelled, and all in one place.", "start": 4784.82, "duration": 5.53 }, { "text": "OK, let's try the others here.", "start": 4790.35, "duration": 1.85 }, { "text": "I also have \"Imaginative landscape\nwhich could be a little bit", "start": 4792.2, "duration": 3.33 }, { "text": "better designed here.", "start": 4795.53, "duration": 1.26 }, { "text": "A little bit better grouped as\nwell by fixing some of these typos.", "start": 4796.79, "duration": 3.42 }, { "text": "Well, here, I could try to\nmatch on to maybe imaginative.", "start": 4800.21, "duration": 4.98 }, { "text": "That seems like a long enough word\nthat it could be useful for me here.", "start": 4805.19, "duration": 3.19 }, { "text": "So I'll say something like select--", "start": 4808.38, "duration": 1.88 }, { "text": "not select.", "start": 4810.26, "duration": 0.51 }, { "text": "I'll do update votes and set the title\nequal to \"Imaginative Landscape.\"", "start": 4810.77, "duration": 8.28 }, { "text": "Now I want to match any rows that\nhave a title like \"Imaginative space", "start": 4819.05, "duration": 7.59 }, { "text": "percent,\" end quote, semicolon.", "start": 4826.64, "duration": 3.51 }, { "text": "Now I'll match any title that\nhas imaginative as a full word", "start": 4830.15, "duration": 4.86 }, { "text": "at the very beginning of this phrase.", "start": 4835.01, "duration": 2.64 }, { "text": "This is, I would say,\na better query than F-A", "start": 4837.65, "duration": 3.45 }, { "text": "because F-A could match much\nmore than this painting name.", "start": 4841.1, "duration": 3.0 }, { "text": "Here, if we have only one painting\nthat begins with imaginative,", "start": 4844.1, "duration": 4.05 }, { "text": "this could work for us now.", "start": 4848.15, "duration": 2.082 }, { "text": "So I'll try this.", "start": 4850.232, "duration": 0.708 }, { "text": "I'll say Enter, and now,\nlet me rerun the query.", "start": 4850.94, "duration": 4.16 }, { "text": "Group by, and we're so close.", "start": 4855.1, "duration": 2.732 }, { "text": "There's still one more.", "start": 4857.832, "duration": 0.958 }, { "text": "\"Imagintive Landscape,\"\nso for this one, I", "start": 4858.79, "duration": 3.3 }, { "text": "might just need to\nmanually update that piece.", "start": 4862.09, "duration": 2.88 }, { "text": "I could say let's do update,\nvotes, and set title equal to", "start": 4864.97, "duration": 6.51 }, { "text": "in this case \"Imaginative Landscape\"\nwhere, of course, the title currently", "start": 4871.48, "duration": 4.98 }, { "text": "is \"Imagintive Landscape\"\nwithout the A in there.", "start": 4876.46, "duration": 4.68 }, { "text": "Now hit semicolon to run this query.", "start": 4881.14, "duration": 2.1 }, { "text": "I'll show you the results, and now,\nI think we're literally almost there.", "start": 4883.24, "duration": 4.56 }, { "text": "There's only one more to do.", "start": 4887.8, "duration": 1.35 }, { "text": "Profusion of Flowers.", "start": 4889.15, "duration": 1.38 }, { "text": "Let's fix that one now.", "start": 4890.53, "duration": 1.56 }, { "text": "I could say select votes--", "start": 4892.09, "duration": 2.79 }, { "text": "not select.", "start": 4894.88, "duration": 0.84 }, { "text": "Update.", "start": 4895.72, "duration": 0.57 }, { "text": "I'm always updating here.", "start": 4896.29, "duration": 1.042 }, { "text": "Update votes, and then\nwe want to set the title", "start": 4897.332, "duration": 4.178 }, { "text": "column to be equal to profusion--", "start": 4901.51, "duration": 3.6 }, { "text": "\"Profusion of Flowers\" like this.", "start": 4905.11, "duration": 4.05 }, { "text": "Then where the title is like, in this\ncase, profusion at the beginning.", "start": 4909.16, "duration": 6.72 }, { "text": "Notice how I can't use\nperfusion space and then", "start": 4915.88, "duration": 3.45 }, { "text": "afterwards because we have one title\nthat is literally just \"Profusion,\"", "start": 4919.33, "duration": 4.86 }, { "text": "plain and simple.", "start": 4924.19, "duration": 0.84 }, { "text": "No spaces afterwards.", "start": 4925.03, "duration": 1.065 }, { "text": "No spaces at the beginning.", "start": 4926.095, "duration": 1.125 }, { "text": "To match that, I should just\nsay profusion at the beginning.", "start": 4927.22, "duration": 3.64 }, { "text": "Now I can hit Enter, and\nif I do my usual query", "start": 4930.86, "duration": 3.8 }, { "text": "to show you the votes\ngrouped by title, I", "start": 4934.66, "duration": 2.61 }, { "text": "see I'm finally at my correct,\nfully-formatted data set.", "start": 4937.27, "duration": 4.53 }, { "text": "Farmers Working At Dawn has six votes.", "start": 4941.8, "duration": 2.43 }, { "text": "Imaginative Landscape has\nfive, and so on down the row.", "start": 4944.23, "duration": 4.23 }, { "text": "So here we've seen update being\nused not just to associate artists", "start": 4948.46, "duration": 4.62 }, { "text": "with new paintings, but\nalso to clean our data,", "start": 4953.08, "duration": 3.06 }, { "text": "to make sure we can actually\ntally votes up in a way that's", "start": 4956.14, "duration": 2.97 }, { "text": "consistent and clean in this case.", "start": 4959.11, "duration": 3.64 }, { "text": "What questions do we have on update now?", "start": 4962.75, "duration": 2.67 }, { "text": "Let's go to [? Vixay. ?]", "start": 4968.38, "duration": 1.82 }, { "text": "[? VIXAY: ?] Yeah, so\nwhat I wanted to ask", "start": 4970.2, "duration": 1.75 }, { "text": "is if there is a specific\nfunction for like lowercase.", "start": 4971.95, "duration": 3.48 }, { "text": "So there's a function for upper, and\nthese are the ones for all lowercase.", "start": 4975.43, "duration": 4.607 }, { "text": "SPEAKER: Yeah, a good\ncorresponding question here.", "start": 4980.037, "duration": 2.083 }, { "text": "We used upper, but is there lower?", "start": 4982.12, "duration": 1.86 }, { "text": "Turns out, there is a lower function.", "start": 4983.98, "duration": 1.62 }, { "text": "It's spelled lower, and is in\nall lowercase like upper was,", "start": 4985.6, "duration": 4.08 }, { "text": "so I could use that as well.", "start": 4989.68, "duration": 1.89 }, { "text": "There is a whole list of what SQLite\ncalls these scalar functions, functions", "start": 4991.57, "duration": 4.38 }, { "text": "that take in some set of values\nand return to you some modified", "start": 4995.95, "duration": 3.99 }, { "text": "version of those values.", "start": 4999.94, "duration": 1.54 }, { "text": "So if you do some googling, look\nfor SQLite scalar functions.", "start": 5001.48, "duration": 2.69 }, { "text": "You can find all those functions in\none place in the SQLite documentation.", "start": 5004.17, "duration": 6.25 }, { "text": "Let's take one more question here.", "start": 5010.42, "duration": 1.78 }, { "text": "Let's go to Rupinder.", "start": 5012.2, "duration": 1.42 }, { "text": "RUPINDER: So, yeah, my question is so\ninstead of updating all the titles,", "start": 5013.62, "duration": 4.77 }, { "text": "is it possible to create a new\ncolumn with the four categories", "start": 5018.39, "duration": 3.93 }, { "text": "that we want to have actually?", "start": 5022.32, "duration": 3.035 }, { "text": "SPEAKER: Yeah, I could\nsee that working, too.", "start": 5025.355, "duration": 1.875 }, { "text": "So there's a few ways to fix this data.", "start": 5027.23, "duration": 1.625 }, { "text": "You could imagine adding a\nnew column, and maybe you", "start": 5028.855, "duration": 4.205 }, { "text": "assign values to the rows in that column\nbased on what you see in the title", "start": 5033.06, "duration": 6.482 }, { "text": "that they've given you.", "start": 5039.542, "duration": 0.958 }, { "text": "So if you can match\nsomething like imaginative,", "start": 5040.5, "duration": 2.76 }, { "text": "and you'd know that all your\npaintings associated with imaginative", "start": 5043.26, "duration": 3.24 }, { "text": "have the same title, you could group\nthem into the one category, the two", "start": 5046.5, "duration": 3.84 }, { "text": "category, and so on.", "start": 5050.34, "duration": 1.77 }, { "text": "Here, you wouldn't be\nmodifying those titles.", "start": 5052.11, "duration": 2.16 }, { "text": "You'd just be trying\nto strategically apply", "start": 5054.27, "duration": 2.7 }, { "text": "different categories, which could\nalso work as a solution here,", "start": 5056.97, "duration": 2.64 }, { "text": "too, so good thinking.", "start": 5059.61, "duration": 2.82 }, { "text": "OK, so we've seen now how to\ninsert data, how to update data,", "start": 5062.43, "duration": 5.31 }, { "text": "and previously, how to delete data.", "start": 5067.74, "duration": 2.51 }, { "text": "What we can still do though, are\nlearn how to have other SQL command--", "start": 5070.25, "duration": 5.65 }, { "text": "other SQL statements that can run\nafter we make our very own SQL", "start": 5075.9, "duration": 3.918 }, { "text": "statements as well.", "start": 5079.818, "duration": 0.792 }, { "text": "We'll come back and talk about\nthese things called triggers.", "start": 5080.61, "duration": 3.54 }, { "text": "And we're back.", "start": 5084.15, "duration": 1.69 }, { "text": "So we've seen now a whole collection\nof ways to write data to a database.", "start": 5085.84, "duration": 4.79 }, { "text": "We've seen how to insert data, how\nto update data, and even delete data.", "start": 5090.63, "duration": 5.45 }, { "text": "What we'll see now though,\nthis idea of a trigger,", "start": 5096.08, "duration": 2.92 }, { "text": "a way of writing a SQL statement to run\nin response to some other SQL statement", "start": 5099.0, "duration": 5.67 }, { "text": "like an insert, an update, or a delete.", "start": 5104.67, "duration": 4.11 }, { "text": "Let's consider our museum\nyet again, and let's say they", "start": 5108.78, "duration": 4.11 }, { "text": "have a schema a bit like this one now.", "start": 5112.89, "duration": 2.97 }, { "text": "They have items in their\ncollections, but they also", "start": 5115.86, "duration": 3.06 }, { "text": "have a transactions table.", "start": 5118.92, "duration": 2.37 }, { "text": "Now wouldn't it be nice if whenever I\ndeleted something from the collections", "start": 5121.29, "duration": 5.1 }, { "text": "table, it would show up in\ntransactions as sold, having been", "start": 5126.39, "duration": 4.8 }, { "text": "sold from the museum's collections.", "start": 5131.19, "duration": 1.93 }, { "text": "Well, I could use a\ntrigger to do just that.", "start": 5133.12, "duration": 3.0 }, { "text": "Let's see it visually first though.", "start": 5136.12, "duration": 2.15 }, { "text": "I'll say, let's try to delete \"Spring\nOuting\" from our collections table.", "start": 5138.27, "duration": 4.89 }, { "text": "Well, if I do that, I could pretty\neasily do it with delete from, right?", "start": 5143.16, "duration": 4.14 }, { "text": "Now it's no longer part\nof my database, but if I", "start": 5147.3, "duration": 3.24 }, { "text": "have created a trigger to\nrun an insert whenever I hear", "start": 5150.54, "duration": 5.22 }, { "text": "or have listened to a delete\non my collections table,", "start": 5155.76, "duration": 3.45 }, { "text": "I could then see that same\ntitle in my transactions table", "start": 5159.21, "duration": 5.16 }, { "text": "as sold all automatically.", "start": 5164.37, "duration": 3.88 }, { "text": "Let's think to maybe the museum\nacquires some piece of artwork.", "start": 5168.25, "duration": 4.07 }, { "text": "Maybe they actually add something\nnew to their collection.", "start": 5172.32, "duration": 3.12 }, { "text": "We want that to show up in our\ntransactions as well with the action of", "start": 5175.44, "duration": 4.44 }, { "text": "bought.", "start": 5179.88, "duration": 0.69 }, { "text": "Well, let's try it.", "start": 5180.57, "duration": 0.87 }, { "text": "I could probably add a new row here.", "start": 5181.44, "duration": 2.05 }, { "text": "I could say let's add \"Peonies and\nButterfly\" to my collection here.", "start": 5183.49, "duration": 4.79 }, { "text": "Now, if I have the\nappropriate trigger, I", "start": 5188.28, "duration": 3.42 }, { "text": "could run an insert into on\ntransactions automatically", "start": 5191.7, "duration": 4.62 }, { "text": "that would insert this same\ntitle with the action of bot,", "start": 5196.32, "duration": 3.87 }, { "text": "thereby keeping a log of all of our\nsold pieces of artwork and all of our", "start": 5200.19, "duration": 4.92 }, { "text": "bought pieces of artwork.", "start": 5205.11, "duration": 2.25 }, { "text": "Now to create what we're\ncalling a trigger here,", "start": 5207.36, "duration": 2.64 }, { "text": "we can use the following syntax.", "start": 5210.0, "duration": 2.43 }, { "text": "I could say first create\ntrigger, and then some name.", "start": 5212.43, "duration": 5.19 }, { "text": "So I tend to give triggers\nnames to identify them", "start": 5217.62, "duration": 2.64 }, { "text": "among all of my database [INAUDIBLE],,\nlike my tables and so on.", "start": 5220.26, "duration": 4.41 }, { "text": "Here, after I say create\ntrigger, I have to specify.", "start": 5224.67, "duration": 4.17 }, { "text": "Should this trigger, this statement,\nrun after or before some other SQL", "start": 5228.84, "duration": 6.24 }, { "text": "statement?", "start": 5235.08, "duration": 1.11 }, { "text": "Let's keep going with before here.", "start": 5236.19, "duration": 2.16 }, { "text": "Maybe it runs before an\ninsert on some table.", "start": 5238.35, "duration": 4.5 }, { "text": "That's fine.", "start": 5242.85, "duration": 1.11 }, { "text": "It could also run before an\nupdate of a column on some table.", "start": 5243.96, "duration": 5.23 }, { "text": "It could even run before a delete\non some table, and keep in mind,", "start": 5249.19, "duration": 5.0 }, { "text": "this could be before or after any\nof these kinds of SQL statements.", "start": 5254.19, "duration": 4.26 }, { "text": "In this case, we have\ninsert, update, and delete.", "start": 5258.45, "duration": 4.32 }, { "text": "Now after we say this, what we\nshould say, too, is for each row.", "start": 5262.77, "duration": 5.28 }, { "text": "For each row means that if I were\nto maybe delete multiple rows,", "start": 5268.05, "duration": 5.16 }, { "text": "I should run my SQL statement\nfor each row that I delete.", "start": 5273.21, "duration": 4.2 }, { "text": "If I delete two rows, I should run\nthis statement coming soon two times.", "start": 5277.41, "duration": 6.57 }, { "text": "OK, so now we've set up our trigger,\nat least kind of the data, the setup", "start": 5283.98, "duration": 5.49 }, { "text": "for it here.", "start": 5289.47, "duration": 1.08 }, { "text": "What we haven't done yet is specify\nwhat statement should run whenever", "start": 5290.55, "duration": 5.07 }, { "text": "we see a delete on particular table.", "start": 5295.62, "duration": 2.4 }, { "text": "Well, let's try this.", "start": 5298.02, "duration": 1.62 }, { "text": "Here I could say begin.", "start": 5299.64, "duration": 1.95 }, { "text": "Begin means here comes my\nstatement I want to run", "start": 5301.59, "duration": 3.27 }, { "text": "whenever I hear a delete on this table.", "start": 5304.86, "duration": 3.51 }, { "text": "Now, all right.", "start": 5308.37, "duration": 1.47 }, { "text": "That query down below.", "start": 5309.84, "duration": 1.59 }, { "text": "That statement inside my\nbegin, and I'll finish it off", "start": 5311.43, "duration": 3.27 }, { "text": "with an end to say this is\nthe end of my statement,", "start": 5314.7, "duration": 3.21 }, { "text": "and my entire trigger looks like\nall of this lines of code here", "start": 5317.91, "duration": 3.69 }, { "text": "to say listen for a delete and run\nthis statement in here for each row", "start": 5321.6, "duration": 5.61 }, { "text": "that I delete, or update, or insert.", "start": 5327.21, "duration": 3.32 }, { "text": "So let's try actually\nimplementing in this case", "start": 5330.53, "duration": 3.37 }, { "text": "the museum's collections where\nthey're trying to sell or buy", "start": 5333.9, "duration": 3.56 }, { "text": "pieces of artwork, and\nautomatically add them", "start": 5337.46, "duration": 2.34 }, { "text": "to their transaction table whenever\nthey create or delete some piece of data", "start": 5339.8, "duration": 3.96 }, { "text": "from their collections.", "start": 5343.76, "duration": 1.95 }, { "text": "Go back to my terminal here,\nand let me pull up a database.", "start": 5345.71, "duration": 4.59 }, { "text": "I'll do SQLite 3 mfa.db,\nand you'll notice", "start": 5350.3, "duration": 3.96 }, { "text": "that this is the same schema\nthat we've had before.", "start": 5354.26, "duration": 3.12 }, { "text": "We have collections, artists,\nand the created table.", "start": 5357.38, "duration": 4.11 }, { "text": "What's missing here, though,\nis our transactions table.", "start": 5361.49, "duration": 3.13 }, { "text": "So let me make that for us here.", "start": 5364.62, "duration": 1.91 }, { "text": "I'll say let me go create\ntable, transactions,", "start": 5366.53, "duration": 4.05 }, { "text": "and now let me give it an\nID column of type integer.", "start": 5370.58, "duration": 5.42 }, { "text": "Now, I'll also give it a title column\nas we saw before called \"Text.\"", "start": 5376.0, "duration": 5.03 }, { "text": "It's in this column\n[INAUDIBLE],, the title", "start": 5381.03, "duration": 2.28 }, { "text": "of the piece we either bought or sold.", "start": 5383.31, "duration": 2.79 }, { "text": "Now, let me also include the\naction column, also of type text", "start": 5386.1, "duration": 4.86 }, { "text": "to say whether we bought\nor sold this piece.", "start": 5390.96, "duration": 4.25 }, { "text": "OK, finally, I'll make the\nID column my primary key.", "start": 5395.21, "duration": 4.84 }, { "text": "And if I say semicolon here and hit\nEnter, I now have created this table.", "start": 5400.05, "duration": 6.03 }, { "text": "So if I say dot schema, notice\nhow I can now see my transactions", "start": 5406.08, "duration": 4.13 }, { "text": "table down below, and\nI'm safe to run insert", "start": 5410.21, "duration": 3.78 }, { "text": "on this table to add some data to it.", "start": 5413.99, "duration": 4.11 }, { "text": "Well, let's try now creating\nour very first trigger.", "start": 5418.1, "duration": 3.39 }, { "text": "We're going to try to create a trigger\nthat whenever an item is deleted", "start": 5421.49, "duration": 3.81 }, { "text": "from our collections table, we actually\nadd it to our transactions table", "start": 5425.3, "duration": 3.69 }, { "text": "with the action of sold, meaning\nwe sold this particular item.", "start": 5428.99, "duration": 4.21 }, { "text": "So let me say create\ntrigger, as we saw before,", "start": 5433.2, "duration": 3.35 }, { "text": "and give it some name to identify it.", "start": 5436.55, "duration": 2.28 }, { "text": "I'll call this one sell.", "start": 5438.83, "duration": 2.2 }, { "text": "Now, I'll say I want to run this trigger\nbefore I delete on the collections", "start": 5441.03, "duration": 6.26 }, { "text": "table.", "start": 5447.29, "duration": 0.88 }, { "text": "So before I run the delete\nquery on my collections table,", "start": 5448.17, "duration": 4.7 }, { "text": "I want to run this statement instead.", "start": 5452.87, "duration": 4.11 }, { "text": "I'll say for each row, begin.", "start": 5456.98, "duration": 3.85 }, { "text": "I want to now give you some\nstatement to run for each row", "start": 5460.83, "duration": 3.35 }, { "text": "that I delete from collections.", "start": 5464.18, "duration": 2.55 }, { "text": "Let's make it this query here.", "start": 5466.73, "duration": 1.62 }, { "text": "On 1, 2, 3, 4 spaces\nto indent and make sure", "start": 5468.35, "duration": 3.81 }, { "text": "this is clearly the query I want to run\nafter I run the delete on collections.", "start": 5472.16, "duration": 6.69 }, { "text": "I'll say I want to\ninsert into transactions.", "start": 5478.85, "duration": 4.17 }, { "text": "I want to insert into the\ntitle and action columns.", "start": 5483.02, "duration": 4.71 }, { "text": "What value should I insert?", "start": 5487.73, "duration": 1.68 }, { "text": "Well, I should insert the old\ntitle, and actually, in triggers,", "start": 5489.41, "duration": 4.8 }, { "text": "you get access to this\nkeyword called old.", "start": 5494.21, "duration": 3.12 }, { "text": "This is the title that we've\njust deleted from our collection.", "start": 5497.33, "duration": 4.27 }, { "text": "So old.title gives me access to\nthe old row, the row we deleted,", "start": 5501.6, "duration": 4.94 }, { "text": "its title column in particular.", "start": 5506.54, "duration": 2.62 }, { "text": "Now, let me add in the action, which\nis just plain and simple \"sold.\"", "start": 5509.16, "duration": 5.05 }, { "text": "So this is the query to run\nbefore I delete on collections.", "start": 5514.21, "duration": 4.61 }, { "text": "I will take the title of the row\nI'm about to delete, and insert it", "start": 5518.82, "duration": 4.53 }, { "text": "into transactions along\nwith the action sold.", "start": 5523.35, "duration": 3.6 }, { "text": "Let me type a semicolon here to say this\nis my entire query, hit Enter again,", "start": 5526.95, "duration": 4.12 }, { "text": "and now I can end that\nparticular statement", "start": 5531.07, "duration": 2.93 }, { "text": "I want to run after I hear\na delete on collections.", "start": 5534.0, "duration": 4.11 }, { "text": "So I'll hit Enter now, and\nlet me type dot schema.", "start": 5538.11, "duration": 3.66 }, { "text": "I can actually see this\ntrigger is part of my schema.", "start": 5541.77, "duration": 4.71 }, { "text": "I can see create trigger sell\nbefore delete on transactions", "start": 5546.48, "duration": 3.33 }, { "text": "underneath the transactions table.", "start": 5549.81, "duration": 3.6 }, { "text": "OK, let's try putting\nthis into action here.", "start": 5553.41, "duration": 3.27 }, { "text": "Let me try actually deleting\nsomething from our collections table.", "start": 5556.68, "duration": 2.79 }, { "text": "I'll say delete from collections\nwhere the title equals", "start": 5559.47, "duration": 4.62 }, { "text": "\"Profusion of Flowers.\"", "start": 5564.09, "duration": 1.83 }, { "text": "I want to sell this particular piece\nof artwork from our collection.", "start": 5565.92, "duration": 3.54 }, { "text": "I'll hit semicolon here, and now\nnothing seems to happen as usual,", "start": 5569.46, "duration": 5.07 }, { "text": "but if I select star from collections--", "start": 5574.53, "duration": 3.76 }, { "text": "from collections, I\nsee that piece is gone.", "start": 5578.29, "duration": 4.53 }, { "text": "It's no longer in here, and if I\nselect star from transactions--", "start": 5582.82, "duration": 5.25 }, { "text": "select star from transactions,\nI actually see it's been sold.", "start": 5588.07, "duration": 4.65 }, { "text": "So automatically, I added\n\"Profusion of Flowers\"", "start": 5592.72, "duration": 3.12 }, { "text": "with the action of sold\nto my transactions table.", "start": 5595.84, "duration": 2.97 }, { "text": "I did not type insert myself.", "start": 5598.81, "duration": 2.59 }, { "text": "I instead created a trigger\nto run this statement,", "start": 5601.4, "duration": 3.56 }, { "text": "insert into, whenever I\ndelete it on collections.", "start": 5604.96, "duration": 4.76 }, { "text": "Let's try one more.", "start": 5609.72, "duration": 1.29 }, { "text": "We want to not just be\nable to sell artwork,", "start": 5611.01, "duration": 1.89 }, { "text": "but also add it to our\ncollection by buying it.", "start": 5612.9, "duration": 3.36 }, { "text": "So I create a trigger called buy.", "start": 5616.26, "duration": 2.79 }, { "text": "Let me type create\ntrigger \"buy,\" and now I", "start": 5619.05, "duration": 5.7 }, { "text": "want to run this statement forthcoming\nafter an insert on collections.", "start": 5624.75, "duration": 7.71 }, { "text": "So I want to first insert a new item to\ncollections, and when I do, after I do,", "start": 5632.46, "duration": 6.36 }, { "text": "I want to run this query coming next.", "start": 5638.82, "duration": 2.79 }, { "text": "Now, I want to, for each row,\nbegin the statement I want to run.", "start": 5641.61, "duration": 5.98 }, { "text": "So as we said before, I want to\ninsert into the transactions table.", "start": 5647.59, "duration": 4.79 }, { "text": "I want to insert into the title\nand action columns, just like this.", "start": 5652.38, "duration": 5.71 }, { "text": "Now, I want to insert some values.", "start": 5658.09, "duration": 3.29 }, { "text": "The values I want to insert--", "start": 5661.38, "duration": 1.68 }, { "text": "well, I want to insert the new\ntitle, the new row I've just added.", "start": 5663.06, "duration": 4.35 }, { "text": "So in addition to old,\nwe also have new when", "start": 5667.41, "duration": 3.33 }, { "text": "I'm talking when I'm using\na trigger on an insert.", "start": 5670.74, "duration": 2.58 }, { "text": "So I'll say new.title, where again, new\nis the new row or rows I'm inserting,", "start": 5673.32, "duration": 6.78 }, { "text": "and the dot title is\na column called title.", "start": 5680.1, "duration": 4.83 }, { "text": "Now, here I'll say\nthe action was bought.", "start": 5684.93, "duration": 2.43 }, { "text": "We bought this artwork.", "start": 5687.36, "duration": 1.71 }, { "text": "I'll hit semicolon here,\nand now, I'll say end", "start": 5689.07, "duration": 3.18 }, { "text": "to say this is the end of my statement\nI want to run for every row I", "start": 5692.25, "duration": 4.26 }, { "text": "insert on collections.", "start": 5696.51, "duration": 2.07 }, { "text": "Let me hit Enter here.", "start": 5698.58, "duration": 1.62 }, { "text": "I've created this trigger.", "start": 5700.2, "duration": 1.32 }, { "text": "I can type dot schema to\nsee it in my actual schema.", "start": 5701.52, "duration": 4.66 }, { "text": "Now, let's try inserting.", "start": 5706.18, "duration": 1.73 }, { "text": "I'll say insert into collections.", "start": 5707.91, "duration": 3.72 }, { "text": "I want to insert into the\ntitle and accession number,", "start": 5711.63, "duration": 4.05 }, { "text": "as well as the acquired columns here.", "start": 5715.68, "duration": 3.27 }, { "text": "I want to insert some new artwork.", "start": 5718.95, "duration": 2.31 }, { "text": "Maybe I sold Profusion of Flowers,\nbut now I want to just buy it back.", "start": 5721.26, "duration": 3.37 }, { "text": "So I'll say I'll insert \"Profusion\nof Flowers\" is the title.", "start": 5724.63, "duration": 5.88 }, { "text": "The accession number was 56.257,\nand the date we acquired it, well,", "start": 5730.51, "duration": 6.65 }, { "text": "that was in 1956/04/12.", "start": 5737.16, "duration": 4.2 }, { "text": "Let's hit semicolon here.", "start": 5741.36, "duration": 1.95 }, { "text": "Now, I'll insert, and I'll say\nselect star from collections.", "start": 5743.31, "duration": 5.46 }, { "text": "I should see Profusion of\nFlowers back in my table.", "start": 5748.77, "duration": 4.54 }, { "text": "Now though, if I say select\nstar from transactions--", "start": 5753.31, "duration": 3.47 }, { "text": "select star from\ntransactions, I should see", "start": 5756.78, "duration": 3.24 }, { "text": "I previously sold Profusion of\nFlowers, and later bought it back.", "start": 5760.02, "duration": 3.96 }, { "text": "So again, I didn't run\nthis insert myself.", "start": 5763.98, "duration": 4.06 }, { "text": "I instead triggered it by making my very\nown insert on the collections table.", "start": 5768.04, "duration": 6.12 }, { "text": "So this is the power of triggers.", "start": 5774.16, "duration": 2.27 }, { "text": "You're able to run\nstatements that I myself", "start": 5776.43, "duration": 2.4 }, { "text": "didn't create in response to\nstatements that I actually did.", "start": 5778.83, "duration": 4.2 }, { "text": "So let me ask here, what questions\ndo we have on these triggers", "start": 5783.03, "duration": 4.41 }, { "text": "and what they can do for us?", "start": 5787.44, "duration": 1.56 }, { "text": "Let's go to Simon.", "start": 5791.65, "duration": 1.58 }, { "text": "SIMON: I'd like to know\nif there are only--", "start": 5793.23, "duration": 5.16 }, { "text": "there is only one SQL\ninquiry that you can write", "start": 5798.39, "duration": 4.05 }, { "text": "in a trigger or multiple inquiries.", "start": 5802.44, "duration": 3.73 }, { "text": "SPEAKER: Yeah, we saw begin\nand end in our triggers", "start": 5806.17, "duration": 3.53 }, { "text": "here, where begin indicates\nthe start of our statement,", "start": 5809.7, "duration": 2.59 }, { "text": "and end indicates the end.", "start": 5812.29, "duration": 1.73 }, { "text": "You actually can have\nmultiple statements", "start": 5814.02, "duration": 2.46 }, { "text": "inside of the begin\nand end separated by,", "start": 5816.48, "duration": 2.97 }, { "text": "of course, that usual\nsemicolon that we have.", "start": 5819.45, "duration": 3.68 }, { "text": "OK, so one final idea here\nthat we're getting at-- we're", "start": 5823.13, "duration": 3.9 }, { "text": "kind of touching at with\nthis transactions table is", "start": 5827.03, "duration": 2.34 }, { "text": "this idea of having a soft deletion.", "start": 5829.37, "duration": 3.33 }, { "text": "So when I dropped something\nfrom my collections table,", "start": 5832.7, "duration": 4.68 }, { "text": "it actually ended up in\nmy transactions table,", "start": 5837.38, "duration": 3.51 }, { "text": "so it wasn't fully deleted in the\nsense that I'm keeping a record of it", "start": 5840.89, "duration": 3.6 }, { "text": "somewhere else.", "start": 5844.49, "duration": 1.84 }, { "text": "There's also one more\nway among many others", "start": 5846.33, "duration": 2.6 }, { "text": "to implement this idea of soft\ndeletions, that is not quite fully", "start": 5848.93, "duration": 3.193 }, { "text": "deleting something from my\ndatabase but instead keeping it", "start": 5852.123, "duration": 2.417 }, { "text": "around with a log that we've\nremoved it in some way.", "start": 5854.54, "duration": 3.43 }, { "text": "Let's look at this table here.", "start": 5857.97, "duration": 1.97 }, { "text": "I have collections, but I also\nhave this column called \"deleted,\"", "start": 5859.94, "duration": 5.31 }, { "text": "and by default, the\nvalue here will be 0.", "start": 5865.25, "duration": 3.75 }, { "text": "That is, by default, I'll add some\nartwork to my collections table", "start": 5869.0, "duration": 5.19 }, { "text": "and deleted will be 0.", "start": 5874.19, "duration": 2.79 }, { "text": "If I want to delete a piece of\nartwork though, what could I do?", "start": 5876.98, "duration": 4.23 }, { "text": "Instead of deleting it fully from\nmy table or removing the row,", "start": 5881.21, "duration": 3.84 }, { "text": "I could mark it as deleted.", "start": 5885.05, "duration": 2.04 }, { "text": "I could change the deleted\ncolumn from a 0 to a 1.", "start": 5887.09, "duration": 3.54 }, { "text": "Let's say I wanted to delete\nFarmers Working At Dawn", "start": 5890.63, "duration": 2.64 }, { "text": "instead of writing literally\ndelete from collection", "start": 5893.27, "duration": 3.06 }, { "text": "where title equals\nFarmers Working At Dawn, I", "start": 5896.33, "duration": 2.34 }, { "text": "could say update the deleted column of\ncollections, where the title is Farmers", "start": 5898.67, "duration": 4.83 }, { "text": "Working At Dawn, and\nmake it not 0, but 1.", "start": 5903.5, "duration": 3.99 }, { "text": "Now, I've marked this item as deleted.", "start": 5907.49, "duration": 3.0 }, { "text": "I could run a query that\nexcludes deleted items,", "start": 5910.49, "duration": 2.94 }, { "text": "but this row is still\naround in my table.", "start": 5913.43, "duration": 4.45 }, { "text": "So let's try implementing\nthis idea of a soft deletion", "start": 5917.88, "duration": 3.53 }, { "text": "inside of our collections\ntable so that we", "start": 5921.41, "duration": 2.55 }, { "text": "don't lose records of things that\nactually had in our collection.", "start": 5923.96, "duration": 4.35 }, { "text": "I'll come back to my\ncomputer here, and let's work", "start": 5928.31, "duration": 2.94 }, { "text": "on altering our table\nfor collections here.", "start": 5931.25, "duration": 3.69 }, { "text": "If I type dot schema collections--", "start": 5934.94, "duration": 3.42 }, { "text": "dot schema collections, you should see\nI have a table \"collections,\" and also", "start": 5938.36, "duration": 6.12 }, { "text": "the triggers associated with it.", "start": 5944.48, "duration": 1.95 }, { "text": "What I don't have yet in collections,\nif I go over here and show you,", "start": 5946.43, "duration": 3.99 }, { "text": "I don't have a deleted column.", "start": 5950.42, "duration": 2.49 }, { "text": "I have ID, title, accession number,\nand acquired, but I don't have deleted.", "start": 5952.91, "duration": 5.73 }, { "text": "So as you learned last\nweek, we can use alter table", "start": 5958.64, "duration": 3.75 }, { "text": "to add a column to our collections\ntable, and by default, make the value", "start": 5962.39, "duration": 5.16 }, { "text": "0.", "start": 5967.55, "duration": 0.75 }, { "text": "So let's try that.", "start": 5968.3, "duration": 1.64 }, { "text": "I'll go back to my environment,\nand now I'll run alter table.", "start": 5969.94, "duration": 4.51 }, { "text": "I might say alter table, and a table I\nwant to alter, which is \"collections.\"", "start": 5974.45, "duration": 6.99 }, { "text": "Now I want to add the column\n\"deleted\" to collections,", "start": 5981.44, "duration": 5.49 }, { "text": "and I want to make this type\ninteger, so our whole numbers", "start": 5986.93, "duration": 3.48 }, { "text": "whether positive or negative, and the\ndefault value will be 0 in this case.", "start": 5990.41, "duration": 5.61 }, { "text": "Now, I'll hit Enter, and if I\ntype dot schema collections,", "start": 5996.02, "duration": 5.19 }, { "text": "I should see I have my very own\ndeleted column inside of collections.", "start": 6001.21, "duration": 6.42 }, { "text": "If I type, for instance, select\nstar from collections semicolon,", "start": 6007.63, "duration": 6.49 }, { "text": "I'll see that by default, all of these\nvalues are 0 in my new deleted column.", "start": 6014.12, "duration": 6.21 }, { "text": "So instead of trying to literally\ndelete from collections,", "start": 6020.33, "duration": 5.25 }, { "text": "I could instead just update it.", "start": 6025.58, "duration": 1.65 }, { "text": "I could say take the updated\ncolumn, and flip it from a 0 to a 1.", "start": 6027.23, "duration": 4.65 }, { "text": "Let me try update collections\nand set the deleted column", "start": 6031.88, "duration": 6.45 }, { "text": "equal to 1, where let's say the\ntitle equals Farmers Working At Dawn.", "start": 6038.33, "duration": 7.5 }, { "text": "And my query wraps here, but\nit should still work just fine.", "start": 6045.83, "duration": 3.07 }, { "text": "I'll hit semicolon, Enter.", "start": 6048.9, "duration": 2.15 }, { "text": "Now, I didn't use delete from.", "start": 6051.05, "duration": 2.55 }, { "text": "I used update.", "start": 6053.6, "duration": 1.14 }, { "text": "If I select star from\ncollections semicolon here,", "start": 6054.74, "duration": 4.83 }, { "text": "I'll see, well, Farmers Working\nAt Dawn is in this table,", "start": 6059.57, "duration": 3.85 }, { "text": "but technically-- at least\nwe marked it as deleted.", "start": 6063.42, "duration": 4.14 }, { "text": "So it seems now, I don't want to use\njust select star from collections", "start": 6067.56, "duration": 5.27 }, { "text": "to see what's inside my collection.", "start": 6072.83, "duration": 1.71 }, { "text": "I want to apply a filter\nto remove those that have", "start": 6074.54, "duration": 3.69 }, { "text": "a deleted value that's not equal to 0.", "start": 6078.23, "duration": 3.515 }, { "text": "Let's try this.", "start": 6081.745, "duration": 0.625 }, { "text": "I'll say select star from collections\nwhere deleted does not equal 1.", "start": 6082.37, "duration": 8.28 }, { "text": "If I hit semicolon here, what do I see?", "start": 6090.65, "duration": 3.06 }, { "text": "Only those values that are not deleted.", "start": 6093.71, "duration": 3.808 }, { "text": "I could go ahead and find them.", "start": 6097.518, "duration": 1.292 }, { "text": "I could say maybe select star from\ncollections where deleted actually", "start": 6098.81, "duration": 6.81 }, { "text": "is 1.", "start": 6105.62, "duration": 1.08 }, { "text": "Really equals 1 like this,\nand now I see all those rows", "start": 6106.7, "duration": 4.17 }, { "text": "that I marked as deleted.", "start": 6110.87, "duration": 2.76 }, { "text": "Now this has some advantages.", "start": 6113.63, "duration": 2.31 }, { "text": "One of them is we keep data around.", "start": 6115.94, "duration": 2.03 }, { "text": "We don't actually formally delete it.", "start": 6117.97, "duration": 1.56 }, { "text": "We can still recover it later on, but it\nalso has some tricky ethical questions,", "start": 6119.53, "duration": 4.93 }, { "text": "too.", "start": 6124.46, "duration": 0.5 }, { "text": "Like it's OK if we're\ntalking about artwork here,", "start": 6124.96, "duration": 2.28 }, { "text": "but if you're talking about user data,\nis it right to only soft delete it", "start": 6127.24, "duration": 5.28 }, { "text": "if they ask you to delete it?", "start": 6132.52, "duration": 1.5 }, { "text": "Particularly in conversation\nwith new frameworks like GDPR,", "start": 6134.02, "duration": 3.78 }, { "text": "and the right to be forgotten,\nand so on, it's up to you", "start": 6137.8, "duration": 2.77 }, { "text": "as a programmer to make\nthe right decision here.", "start": 6140.57, "duration": 2.0 }, { "text": "When should you find data that\n[INAUDIBLE] and actually delete it,", "start": 6142.57, "duration": 2.91 }, { "text": "or when is it better to just\nsoft delete it and keep it around", "start": 6145.48, "duration": 3.09 }, { "text": "in case you need it for later?", "start": 6148.57, "duration": 2.64 }, { "text": "So we'll end here on this note where\nif I say select star from collections,", "start": 6151.21, "duration": 5.28 }, { "text": "and I want to find those only where\ndeleted is not equal to 0 like this--", "start": 6156.49, "duration": 7.08 }, { "text": "or not equal to 1, this gives me back\nall of the items that are not deleted.", "start": 6163.57, "duration": 5.58 }, { "text": "But wouldn't it be nice\nif I could actually", "start": 6169.15, "duration": 2.52 }, { "text": "run a query on some brand new temporary\ntable that actually only will ever have", "start": 6171.67, "duration": 5.91 }, { "text": "those items that are not soft deleted?", "start": 6177.58, "duration": 2.67 }, { "text": "Turns out, we can do that\nwith an idea called views,", "start": 6180.25, "duration": 2.89 }, { "text": "and we'll see those in\nmuch more depth next week.", "start": 6183.14, "duration": 2.99 }, { "text": "We'll see you then.", "start": 6186.13, "duration": 1.88 } ]