All right so we've covered the C and the and crud create and read now we're moving onto the you.
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 application.
Let's say we have users think about when you're updating your password you forgot to password as a user.
So you click the forget password link and you reset your password.
Well that needs to be changed to the database or you change your profile picture on Facebook or you
change your ID on a 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 sequel 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 Eskew will which items in particular we want to
select to update.
So in this case this entire line here is going to find in the cafe table all the cast 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 wrong way
of saying it but what do we want to perform it on.
What does 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 cat you can
see that we have two Tabby's right now Ringo and misty.
I paste this line and update cat changed their breed to be short hair if their breed is Tabby and now
we select star again.
You can see that both Tabby's are previously Tabby's are now shorthairs Misty and or 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 well the 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 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 and integer and we only want to do
that if name is Mysti.
So let's try it
now.
So let's start again.
Or 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.
You can see Misty is now 14 rather than 13.
The last thing that I'll mention about updating is that there is 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're talking about deleting which will do in just a moment but the core
idea is that there is 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.