Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,460 --> 00:00:04,820
In this lesson, we're going to
be taking a look at the DELETE
2
00:00:04,820 --> 00:00:05,930
statement.
3
00:00:05,930 --> 00:00:08,030
So the DELETE statement
is the command
4
00:00:08,030 --> 00:00:11,600
that we use in
SQL to remove data
5
00:00:11,600 --> 00:00:17,070
or to remove a row or rows
of data from a given table.
6
00:00:17,070 --> 00:00:22,290
So let's connect, and
look at our table list,
7
00:00:22,290 --> 00:00:28,650
and start out by selecting
star from the bonus table.
8
00:00:28,650 --> 00:00:30,390
Run.
9
00:00:30,390 --> 00:00:34,090
And this is the data that
we have to work with.
10
00:00:34,090 --> 00:00:37,770
So let's type in a DELETE
statement and then dissect it.
11
00:00:43,470 --> 00:00:45,360
So when we run a
DELETE statement,
12
00:00:45,360 --> 00:00:48,780
we'll use the DELETE
keyword and the FROM
13
00:00:48,780 --> 00:00:51,810
keyword, much as the same
as a SELECT, and then
14
00:00:51,810 --> 00:00:55,390
the table name, so we
delete from the table name.
15
00:00:55,390 --> 00:00:57,840
And then we have a limiting
condition where our E
16
00:00:57,840 --> 00:01:01,050
name is Sam, equal to Sam.
17
00:01:01,050 --> 00:01:03,060
So we're targeting
this row right here
18
00:01:03,060 --> 00:01:05,310
with our DELETE statement.
19
00:01:05,310 --> 00:01:07,840
We click Run.
20
00:01:07,840 --> 00:01:11,890
It tells us one row deleted.
21
00:01:11,890 --> 00:01:14,470
And we'll select from the table.
22
00:01:14,470 --> 00:01:18,380
And we see that the Sam row has
been deleted from the table.
23
00:01:20,920 --> 00:01:25,360
Now we can also
illustrate the importance
24
00:01:25,360 --> 00:01:28,100
of the limiting condition.
25
00:01:28,100 --> 00:01:30,650
What if we were to
type delete from bonus?
26
00:01:34,880 --> 00:01:40,170
Now we look at the bonus table,
and there's no rows left.
27
00:01:40,170 --> 00:01:42,960
Now, this was a small table,
but what if this had been
28
00:01:42,960 --> 00:01:45,510
a million-row production table?
29
00:01:45,510 --> 00:01:47,920
Then this is a serious problem.
30
00:01:47,920 --> 00:01:50,220
And it happens more often
than you might think.
31
00:01:50,220 --> 00:01:53,280
So we just deleted all
of our customer data
32
00:01:53,280 --> 00:01:56,960
from the bonus table, or
maybe our employee data.
33
00:01:56,960 --> 00:01:58,260
And that's a bad thing.
34
00:01:58,260 --> 00:02:00,510
So what do we do
to get that back?
35
00:02:00,510 --> 00:02:04,140
Is there some kind of database
recovery involved, or what?
36
00:02:04,140 --> 00:02:06,690
Well, that's the importance
of a transaction.
37
00:02:06,690 --> 00:02:10,200
So in Oracle and in
relational databases,
38
00:02:10,200 --> 00:02:13,380
commands are centered
around transactions.
39
00:02:13,380 --> 00:02:17,990
A transaction isn't finished
until a commit actually occurs.
40
00:02:20,620 --> 00:02:24,650
Now we can use the
commands commit,
41
00:02:24,650 --> 00:02:26,420
but we don't want to
commit in this case.
42
00:02:26,420 --> 00:02:29,360
Because let's say by doing
this DELETE statement,
43
00:02:29,360 --> 00:02:31,070
we've actually made a mistake.
44
00:02:31,070 --> 00:02:32,720
We want it to come back.
45
00:02:32,720 --> 00:02:36,650
So it's not real, truly
processed in the database,
46
00:02:36,650 --> 00:02:38,090
until we do a commit.
47
00:02:38,090 --> 00:02:40,430
Even though when we
select it, we see it,
48
00:02:40,430 --> 00:02:43,130
we're not seeing the
actual image of the data
49
00:02:43,130 --> 00:02:45,210
at the end of the transaction.
50
00:02:45,210 --> 00:02:49,400
So we want to use a
command called rollback.
51
00:02:49,400 --> 00:02:53,880
When we execute rollback,
it says rollback complete.
52
00:02:53,880 --> 00:02:57,720
And now we select star from
bonus, and we've rolled back.
53
00:02:57,720 --> 00:03:00,780
But look-- not only do
we have Fred and Sue,
54
00:03:00,780 --> 00:03:02,370
but we also have Sam.
55
00:03:02,370 --> 00:03:06,210
So Sam was our first statement,
and then the second statement
56
00:03:06,210 --> 00:03:07,560
of DELETE--
57
00:03:07,560 --> 00:03:11,100
from bonus, the entire
table deleted all the rows.
58
00:03:11,100 --> 00:03:15,360
And yet since no commit occurred
after those two statements were
59
00:03:15,360 --> 00:03:19,230
issued, it rolls
back all of them.
60
00:03:19,230 --> 00:03:22,530
So this is the importance
of commit and roll back.
61
00:03:22,530 --> 00:03:26,420
So this time, let's delete
from the bonus table.
62
00:03:26,420 --> 00:03:28,610
And we'll use a commit.
63
00:03:28,610 --> 00:03:32,400
So we say delete from bonus.
64
00:03:32,400 --> 00:03:35,010
The previous example
used an E name
65
00:03:35,010 --> 00:03:37,680
of Sam, which
deleted only one row.
66
00:03:37,680 --> 00:03:41,760
Let's say we want to
delete multiple rows.
67
00:03:41,760 --> 00:03:56,250
We say where E name equals
Sue and comm equals 2.
68
00:03:56,250 --> 00:03:58,800
I'll change that to or.
69
00:03:58,800 --> 00:03:59,300
All right.
70
00:03:59,300 --> 00:04:03,920
So this DELETE statement will
delete from the bonus table
71
00:04:03,920 --> 00:04:06,380
where one of two things occurs--
72
00:04:06,380 --> 00:04:10,190
if the E name is Sue,
it will delete the row,
73
00:04:10,190 --> 00:04:15,020
or if the value for comm is
2, it will delete the row.
74
00:04:15,020 --> 00:04:16,950
Execute.
75
00:04:16,950 --> 00:04:20,270
It tells us two
rows were deleted.
76
00:04:20,270 --> 00:04:24,150
We select star from
bonus, and those are gone.
77
00:04:24,150 --> 00:04:26,760
Now, at this point,
we could do a rollback
78
00:04:26,760 --> 00:04:30,030
and bring those values
back or those rows back.
79
00:04:30,030 --> 00:04:33,350
But instead, we'll do a commit.
80
00:04:33,350 --> 00:04:35,640
Execute.
81
00:04:35,640 --> 00:04:40,950
And now when we do a select,
we see again only one row.
82
00:04:40,950 --> 00:04:44,970
What if we try a
rollback at this point?
83
00:04:44,970 --> 00:04:47,080
And we select.
84
00:04:47,080 --> 00:04:49,930
That row-- those rows,
rather, are still gone.
85
00:04:49,930 --> 00:04:52,930
Because once we
executed a commit,
86
00:04:52,930 --> 00:04:55,240
that made the
transaction committed
87
00:04:55,240 --> 00:04:56,980
in the database and real.
88
00:04:56,980 --> 00:04:59,800
And you can't roll
back after a commit.
89
00:04:59,800 --> 00:05:02,170
Now any statements that
we had between the commit
90
00:05:02,170 --> 00:05:05,890
and the rollback would
be rollback, but not
91
00:05:05,890 --> 00:05:09,310
the transactions
prior to the commit.
92
00:05:09,310 --> 00:05:10,930
And I want to point
out two things here
93
00:05:10,930 --> 00:05:12,670
about commit and rollback.
94
00:05:12,670 --> 00:05:15,370
First of all is there
is a Commit button
95
00:05:15,370 --> 00:05:18,940
on the toolbar here for
SQL Developer and also
96
00:05:18,940 --> 00:05:21,530
a Rollback button.
97
00:05:21,530 --> 00:05:23,680
The last thing is
that whenever we
98
00:05:23,680 --> 00:05:26,950
use a tool to connect
to a database,
99
00:05:26,950 --> 00:05:29,650
we have to be aware
that some tools are
100
00:05:29,650 --> 00:05:32,830
designed to do an auto commit.
101
00:05:32,830 --> 00:05:35,080
So if this tool,
SQL Developer, had
102
00:05:35,080 --> 00:05:36,850
been set up with auto commit--
103
00:05:36,850 --> 00:05:40,270
and indeed, you can change
that in the preferences,
104
00:05:40,270 --> 00:05:42,100
but it's not by default--
105
00:05:42,100 --> 00:05:44,500
if you had an auto
commit, then every time
106
00:05:44,500 --> 00:05:46,210
you execute a statement--
107
00:05:46,210 --> 00:05:49,630
INSERT, UPDATE, DELETE--
it's automatically committed.
108
00:05:49,630 --> 00:05:52,780
So keep that in mind when you're
using a tool that connects
109
00:05:52,780 --> 00:05:54,930
to the Oracle database.
8513
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.