All righty.
So we've covered the C and the R in crud create and read.
Now we're moving on to the U, which stands for Update.
So how do we update or alter existing data in our database?
We've inserted something and maybe we made a mistake.
We want to change it, but it's not always a mistake in our applications.
Let's say we have users.
Think about when you're updating your password, you forgot a password as a user.
So you click the forget password link and you reset your password.
Will That needs to be changed in the database or you change your profile picture on Facebook or you
change your, I don't know, relationship status or something.
All of that requires updating.
So how do we do it?
And the answer is that we use probably the longest SQL Command you've seen so far.
There's multiple parts, but the key words are update set and where.
So we've already seen where this is where we tell my SQL, which items in particular we want to select
to update.
So in this case, this entire line here is going to find in the cat's table all the cats who have a
breed of tabby.
And we're going to change that breed to be short hair.
So again, we have update and a table name then set and this is a list of the changes we want to make.
So in this case, I'm setting the breed to be short here.
And then who do we want to or who is the wrong way of saying it?
But what do we want to perform it on?
What is the actual data?
In this case, breed equals tabby.
So I will go ahead and just copy this line.
Move over here.
And if I do a select star from Cats, you can see that we have two tabbies right now, Ringo and Misty.
If I paste this line in update, cats change their breed to be short hair if their breed is tabby.
And now we select Star again.
You can see that both tabbies are previously tabbies are now short hairs.
Misty and excuse me.
Ringo and Misty.
So here's another example.
Let's say I wanted to change Misty's age to be 14.
Right now, Misty is 13.
And we realize I don't know.
We realize that her dental records are wrong.
Apparently, you can tell how old a cat is from their dental or from their teeth.
So we misjudged how old she was and we want to make her 14 now.
So to do that, it looks like this update the cat's table set age to be 14.
We don't need quotes this time because we're working with a number, an integer, and we only want to
do that if name is Misty.
So let's try it.
Now select Starr again, or if actually you want to be more specific, we could do select star from
cat where name equals misty.
Let's try that again without that extra L at the end.
And you can see Misty is now 14 rather than 13.
The last thing that I'll mention about updating is that there's a good rule of thumb that you should
follow when you're updating something, you should make sure that you're targeting the right data before
you actually do the update.
So basically run the select the equivalent select using your where query.
And once you make sure that returns the data you expect, then update it just so that you avoid accidentally
updating the wrong data.
And the same holds true when we talk about deleting, which we'll do in just a moment.
But the core idea is that there's no undo button, so you could update something back manually if you
messed it up.
But you always want to just make sure you're targeting what you mean to target before you hit.
Enter on your update.