1 00:00:00,360 --> 00:00:00,960 All righty. 2 00:00:00,960 --> 00:00:04,610 So we've covered the C and the R in crud create and read. 3 00:00:04,620 --> 00:00:07,680 Now we're moving on to the U, which stands for Update. 4 00:00:07,980 --> 00:00:12,030 So how do we update or alter existing data in our database? 5 00:00:12,030 --> 00:00:14,880 We've inserted something and maybe we made a mistake. 6 00:00:14,880 --> 00:00:18,900 We want to change it, but it's not always a mistake in our applications. 7 00:00:18,900 --> 00:00:20,100 Let's say we have users. 8 00:00:20,100 --> 00:00:25,470 Think about when you're updating your password, you forgot a password as a user. 9 00:00:25,470 --> 00:00:29,130 So you click the forget password link and you reset your password. 10 00:00:29,130 --> 00:00:34,170 Will That needs to be changed in the database or you change your profile picture on Facebook or you 11 00:00:34,170 --> 00:00:37,440 change your, I don't know, relationship status or something. 12 00:00:37,590 --> 00:00:39,630 All of that requires updating. 13 00:00:39,630 --> 00:00:40,650 So how do we do it? 14 00:00:41,460 --> 00:00:46,980 And the answer is that we use probably the longest SQL Command you've seen so far. 15 00:00:46,980 --> 00:00:54,510 There's multiple parts, but the key words are update set and where. 16 00:00:54,990 --> 00:01:02,220 So we've already seen where this is where we tell my SQL, which items in particular we want to select 17 00:01:02,220 --> 00:01:03,000 to update. 18 00:01:03,000 --> 00:01:10,650 So in this case, this entire line here is going to find in the cat's table all the cats who have a 19 00:01:10,650 --> 00:01:12,120 breed of tabby. 20 00:01:12,450 --> 00:01:15,690 And we're going to change that breed to be short hair. 21 00:01:16,290 --> 00:01:23,160 So again, we have update and a table name then set and this is a list of the changes we want to make. 22 00:01:23,160 --> 00:01:26,100 So in this case, I'm setting the breed to be short here. 23 00:01:26,940 --> 00:01:30,570 And then who do we want to or who is the wrong way of saying it? 24 00:01:30,570 --> 00:01:33,480 But what do we want to perform it on? 25 00:01:33,810 --> 00:01:34,860 What is the actual data? 26 00:01:34,860 --> 00:01:36,900 In this case, breed equals tabby. 27 00:01:37,230 --> 00:01:39,630 So I will go ahead and just copy this line. 28 00:01:40,350 --> 00:01:41,580 Move over here. 29 00:01:41,850 --> 00:01:49,890 And if I do a select star from Cats, you can see that we have two tabbies right now, Ringo and Misty. 30 00:01:50,610 --> 00:01:58,140 If I paste this line in update, cats change their breed to be short hair if their breed is tabby. 31 00:01:59,900 --> 00:02:01,790 And now we select Star again. 32 00:02:04,140 --> 00:02:08,940 You can see that both tabbies are previously tabbies are now short hairs. 33 00:02:09,150 --> 00:02:11,280 Misty and excuse me. 34 00:02:11,280 --> 00:02:12,510 Ringo and Misty. 35 00:02:15,020 --> 00:02:16,220 So here's another example. 36 00:02:16,640 --> 00:02:20,570 Let's say I wanted to change Misty's age to be 14. 37 00:02:20,780 --> 00:02:23,450 Right now, Misty is 13. 38 00:02:23,450 --> 00:02:25,550 And we realize I don't know. 39 00:02:25,550 --> 00:02:28,430 We realize that her dental records are wrong. 40 00:02:28,460 --> 00:02:31,580 Apparently, you can tell how old a cat is from their dental or from their teeth. 41 00:02:31,580 --> 00:02:35,600 So we misjudged how old she was and we want to make her 14 now. 42 00:02:36,050 --> 00:02:43,140 So to do that, it looks like this update the cat's table set age to be 14. 43 00:02:43,160 --> 00:02:47,690 We don't need quotes this time because we're working with a number, an integer, and we only want to 44 00:02:47,690 --> 00:02:49,430 do that if name is Misty. 45 00:02:50,600 --> 00:02:51,560 So let's try it. 46 00:02:54,780 --> 00:03:05,070 Now select Starr again, or if actually you want to be more specific, we could do select star from 47 00:03:05,080 --> 00:03:09,750 cat where name equals misty. 48 00:03:10,740 --> 00:03:14,280 Let's try that again without that extra L at the end. 49 00:03:15,030 --> 00:03:18,900 And you can see Misty is now 14 rather than 13. 50 00:03:19,380 --> 00:03:23,610 The last thing that I'll mention about updating is that there's a good rule of thumb that you should 51 00:03:23,610 --> 00:03:29,100 follow when you're updating something, you should make sure that you're targeting the right data before 52 00:03:29,100 --> 00:03:30,630 you actually do the update. 53 00:03:31,050 --> 00:03:36,930 So basically run the select the equivalent select using your where query. 54 00:03:37,020 --> 00:03:42,780 And once you make sure that returns the data you expect, then update it just so that you avoid accidentally 55 00:03:42,780 --> 00:03:43,860 updating the wrong data. 56 00:03:43,860 --> 00:03:47,340 And the same holds true when we talk about deleting, which we'll do in just a moment. 57 00:03:47,340 --> 00:03:52,890 But the core idea is that there's no undo button, so you could update something back manually if you 58 00:03:52,890 --> 00:03:53,490 messed it up. 59 00:03:53,490 --> 00:03:57,360 But you always want to just make sure you're targeting what you mean to target before you hit. 60 00:03:57,360 --> 00:03:58,440 Enter on your update.