Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,370 --> 00:00:04,960
In this lesson, we'll be taking
a look at the update statement.
2
00:00:04,960 --> 00:00:07,550
So this is the
DML statement, DML
3
00:00:07,550 --> 00:00:11,270
being a sub-language of SQL.
4
00:00:11,270 --> 00:00:13,250
Update is the
statement we use when
5
00:00:13,250 --> 00:00:17,430
we want to modify
existing data in a table.
6
00:00:17,430 --> 00:00:20,340
So let's go ahead and open a
connection to our database.
7
00:00:20,340 --> 00:00:22,600
We'll use the scott_orcl.
8
00:00:22,600 --> 00:00:28,140
We'll open this table list again
just to have it in front of us.
9
00:00:28,140 --> 00:00:32,600
And actually, in this case,
let's open the bonus as well.
10
00:00:32,600 --> 00:00:35,420
So this is going to
show us the columns that
11
00:00:35,420 --> 00:00:37,160
are in the bonus table.
12
00:00:37,160 --> 00:00:39,800
And it gives us data types
and various other pieces
13
00:00:39,800 --> 00:00:41,090
of information.
14
00:00:41,090 --> 00:00:45,690
We click this tab come back
and do our update statement.
15
00:00:45,690 --> 00:00:48,470
So let's type the statement in.
16
00:00:48,470 --> 00:00:51,370
And then, we'll explain and
break down what it means.
17
00:00:59,290 --> 00:01:01,330
So the format of
an update statement
18
00:01:01,330 --> 00:01:05,060
is to use the update
clause then the table name
19
00:01:05,060 --> 00:01:05,930
that you're updating.
20
00:01:05,930 --> 00:01:09,370
So this is the table that
has the data we'll update.
21
00:01:09,370 --> 00:01:15,250
Set the column name that we
want to change to this value
22
00:01:15,250 --> 00:01:18,520
where some row is returned.
23
00:01:18,520 --> 00:01:21,250
So having this
where clause enables
24
00:01:21,250 --> 00:01:24,790
us to pinpoint the exact
row that we want to change.
25
00:01:24,790 --> 00:01:29,200
So "set" is showing us
the column to change.
26
00:01:29,200 --> 00:01:31,890
And "where" is
showing us the row.
27
00:01:31,890 --> 00:01:35,550
So before we execute this,
let's look at the existing
28
00:01:35,550 --> 00:01:37,530
state of the data.
29
00:01:37,530 --> 00:01:40,460
Select star from bonus.
30
00:01:40,460 --> 00:01:40,960
All right.
31
00:01:40,960 --> 00:01:43,330
Here is the row
that's our target.
32
00:01:43,330 --> 00:01:46,030
Sam's job currently is clerk.
33
00:01:46,030 --> 00:01:47,170
We want to change this.
34
00:01:47,170 --> 00:01:51,970
We want to update this table
and set his job to analyst.
35
00:01:51,970 --> 00:01:56,690
We're going to click up
here in this row, execute.
36
00:01:56,690 --> 00:02:00,020
It says one row was updated.
37
00:02:00,020 --> 00:02:03,070
And now, we'll look
at the table again.
38
00:02:03,070 --> 00:02:06,790
Notice now that
Sam is an analyst.
39
00:02:06,790 --> 00:02:09,710
So we've updated
that specific row.
40
00:02:09,710 --> 00:02:12,790
So what if we didn't
have this where clause?
41
00:02:12,790 --> 00:02:14,800
Well, that's an important
thing to point out.
42
00:02:14,800 --> 00:02:17,680
Because without this
limiting condition,
43
00:02:17,680 --> 00:02:22,900
the job column would be set
to analyst for every row.
44
00:02:22,900 --> 00:02:25,690
And that's probably not
what we want in this case.
45
00:02:25,690 --> 00:02:28,570
So remember that we always
need to not only set
46
00:02:28,570 --> 00:02:31,540
the column for a
new value, but we
47
00:02:31,540 --> 00:02:34,780
need to use a where clause
as a limiting condition
48
00:02:34,780 --> 00:02:38,580
to only bring back
the rows that we want.
49
00:02:38,580 --> 00:02:42,660
Now, let's try updating
a couple of columns.
50
00:02:42,660 --> 00:02:56,450
We'll set job to manager and
sal equal to where ename is Sue.
51
00:02:56,450 --> 00:02:56,950
All right.
52
00:02:56,950 --> 00:02:59,110
So what we've done
here is, we've
53
00:02:59,110 --> 00:03:01,580
updated two column values.
54
00:03:01,580 --> 00:03:07,990
So we've set Sue's job to
manager and her sal to 1700.
55
00:03:07,990 --> 00:03:11,510
And we click Run
to see the table.
56
00:03:11,510 --> 00:03:16,820
And we notice now that Sue is
a manager with a sal of 1700.
57
00:03:16,820 --> 00:03:19,310
So what we've done
is, we've updated
58
00:03:19,310 --> 00:03:22,110
two columns in that row.
59
00:03:22,110 --> 00:03:26,390
Now, our where clause can
allow for more than one
60
00:03:26,390 --> 00:03:27,900
row to be updated.
61
00:03:27,900 --> 00:03:31,250
Here, this limiting
condition brings back
62
00:03:31,250 --> 00:03:33,830
just the row for Sue.
63
00:03:33,830 --> 00:03:36,140
But we could use a different
limiting condition.
64
00:03:36,140 --> 00:03:40,480
For instance, now,
let's try this.
65
00:03:40,480 --> 00:03:46,530
Let's set COMM--
that's this column--
66
00:03:46,530 --> 00:03:53,850
equal to 3 where
sal equals 1700.
67
00:03:53,850 --> 00:03:56,350
So based on the data
that we have here,
68
00:03:56,350 --> 00:04:00,930
there are two rows that have
a value of 1700 for the sal
69
00:04:00,930 --> 00:04:02,160
column.
70
00:04:02,160 --> 00:04:05,670
So if we set this COMM
to 3, both of these
71
00:04:05,670 --> 00:04:07,260
should be changed to 3.
72
00:04:07,260 --> 00:04:11,390
And just to make it clear,
we'll set them to 1.
73
00:04:11,390 --> 00:04:13,770
We click Run.
74
00:04:13,770 --> 00:04:17,510
And it says two
rows were updated.
75
00:04:17,510 --> 00:04:20,350
Click in the select statement.
76
00:04:20,350 --> 00:04:25,460
And we see, indeed, that the
column was set to one or both.
5936
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.