WEBVTT

00:00.330 --> 00:06.190
All right so we've covered the C and the and crud create and read now we're moving onto the you.

00:06.240 --> 00:07.770
Which stands for update.

00:08.100 --> 00:12.120
So how do we update or alter existing data in our database.

00:12.120 --> 00:14.870
We've inserted something and maybe we made a mistake.

00:14.910 --> 00:18.870
We want to change it but it's not always a mistake in our application.

00:18.870 --> 00:25.500
Let's say we have users think about when you're updating your password you forgot to password as a user.

00:25.620 --> 00:29.130
So you click the forget password link and you reset your password.

00:29.160 --> 00:34.110
Well that needs to be changed to the database or you change your profile picture on Facebook or you

00:34.110 --> 00:37.740
change your ID on a relationship status or something.

00:37.740 --> 00:39.750
All of that requires updating.

00:39.750 --> 00:41.010
So how do we do it.

00:41.610 --> 00:47.070
And the answer is that we use probably the longest sequel command you've seen so far.

00:47.130 --> 00:54.720
There's multiple parts but the key words are update set and where.

00:55.120 --> 01:01.800
So we've already seen where this is where we tell my Eskew will which items in particular we want to

01:01.800 --> 01:03.170
select to update.

01:03.180 --> 01:10.920
So in this case this entire line here is going to find in the cafe table all the cast who have a breed

01:10.920 --> 01:16.440
of Tabby and we're going to change that breed to be short hair.

01:16.440 --> 01:20.380
So again we have update and a table name then set.

01:20.430 --> 01:23.340
And this is a list of the changes we want to make.

01:23.340 --> 01:30.060
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

01:30.060 --> 01:33.870
of saying it but what do we want to perform it on.

01:33.960 --> 01:37.360
What does the actual data in this case breed equals tabby.

01:37.360 --> 01:44.640
So I will go ahead and just copy this line move over here and if I do a select star from cat you can

01:44.640 --> 01:50.520
see that we have two Tabby's right now Ringo and misty.

01:50.700 --> 02:00.300
I paste this line and update cat changed their breed to be short hair if their breed is Tabby and now

02:00.300 --> 02:01.880
we select star again.

02:04.170 --> 02:11.580
You can see that both Tabby's are previously Tabby's are now shorthairs Misty and or excuse me Ringo

02:11.580 --> 02:14.930
and Misty.

02:15.000 --> 02:16.240
So here's another example.

02:16.680 --> 02:25.470
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

02:25.490 --> 02:28.460
we realize that her dental records are wrong.

02:28.470 --> 02:31.560
Apparently you can tell how well the cat is from their dental or from their teeth.

02:31.710 --> 02:38.910
So we misjudged how old she was and we want to make her 14 now so to do that looks like this.

02:38.910 --> 02:43.220
Update The cat's table set age to be 14.

02:43.230 --> 02:47.790
We don't need quotes this time because we're working with a number and integer and we only want to do

02:47.790 --> 02:50.380
that if name is Mysti.

02:50.640 --> 02:51.330
So let's try it

02:54.910 --> 02:55.180
now.

02:55.180 --> 02:56.440
So let's start again.

02:57.280 --> 02:59.740
Or actually you want to be more specific.

02:59.800 --> 03:10.550
We could do select star from cat where name equals Misty.

03:10.750 --> 03:15.060
Let's try that again without that extra L at the end.

03:15.130 --> 03:19.410
You can see Misty is now 14 rather than 13.

03:19.420 --> 03:23.560
The last thing that I'll mention about updating is that there is a good rule of thumb that you should

03:23.560 --> 03:29.050
follow when you're updating something you should make sure that you're targeting the right data before

03:29.050 --> 03:31.100
you actually do the update.

03:31.150 --> 03:38.200
So basically run the Select the equivalent select using your where query and once you make sure that

03:38.200 --> 03:43.810
returns the data you expect then update it just so that you avoid accidentally updating the wrong data.

03:43.900 --> 03:47.860
And the same holds true when we're talking about deleting which will do in just a moment but the core

03:47.860 --> 03:53.350
idea is that there is no undo button so you could update something back manually if you messed it up

03:53.590 --> 03:57.610
but you always want to just make sure you're targeting what you mean to target before you hit enter

03:57.610 --> 03:58.120
on your update.
