Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,440 --> 00:00:03,630
In this lesson, we're
looking at views.
2
00:00:03,630 --> 00:00:07,770
So the queries that we do here
are fairly simple, but queries
3
00:00:07,770 --> 00:00:10,180
can be very, very complex.
4
00:00:10,180 --> 00:00:13,970
I myself have seen queries
that were pages and pages long,
5
00:00:13,970 --> 00:00:16,350
just of a single SELECT
statement, that's
6
00:00:16,350 --> 00:00:20,220
doing lots of joins and
subqueries and various things
7
00:00:20,220 --> 00:00:23,140
so that it makes it
very, very complex.
8
00:00:23,140 --> 00:00:29,730
So using a view allows us
to encapsulate a SQL query
9
00:00:29,730 --> 00:00:31,230
as a database object.
10
00:00:31,230 --> 00:00:35,950
So views are queries that are
stored as database objects.
11
00:00:35,950 --> 00:00:38,370
So what do we use views for?
12
00:00:38,370 --> 00:00:41,550
Well, mainly to reduce
query complexity.
13
00:00:41,550 --> 00:00:46,290
So rather than having someone
that runs this enormous SQL
14
00:00:46,290 --> 00:00:48,120
statement, this
SELECT statement,
15
00:00:48,120 --> 00:00:51,290
on a number of different times,
maybe having to type it in,
16
00:00:51,290 --> 00:00:54,060
maybe having to store
it in a file somewhere
17
00:00:54,060 --> 00:00:55,920
and then look for
it and go get it,
18
00:00:55,920 --> 00:00:58,800
we can actually
encapsulate that query--
19
00:00:58,800 --> 00:01:01,770
that SELECT statement--
into a view that's actually
20
00:01:01,770 --> 00:01:03,510
stored in the database.
21
00:01:03,510 --> 00:01:07,320
And then other people can access
it, and the user can access it.
22
00:01:07,320 --> 00:01:09,990
The other things that
we use views to do
23
00:01:09,990 --> 00:01:12,150
is what's called data hiding.
24
00:01:12,150 --> 00:01:16,230
So let's say that we wanted a
certain user to be able to see
25
00:01:16,230 --> 00:01:19,080
certain things in a table--
26
00:01:19,080 --> 00:01:22,050
certain columns, but
maybe not other columns.
27
00:01:22,050 --> 00:01:24,360
So maybe it's an
employee table, and we
28
00:01:24,360 --> 00:01:25,890
want them to be
able to see things
29
00:01:25,890 --> 00:01:28,410
like the first
name and last name,
30
00:01:28,410 --> 00:01:30,240
but maybe not their salary.
31
00:01:30,240 --> 00:01:33,720
Then we can use a
view to mask that,
32
00:01:33,720 --> 00:01:39,220
and we let the user look at the
view and not the table itself.
33
00:01:39,220 --> 00:01:41,040
And it's also
important to understand
34
00:01:41,040 --> 00:01:45,000
that views can be updatable
in certain situations,
35
00:01:45,000 --> 00:01:46,860
although that's not
always advisable,
36
00:01:46,860 --> 00:01:51,040
because it comes with a
certain amount of complexity.
37
00:01:51,040 --> 00:01:53,960
So let's go ahead,
connect to Scott.
38
00:01:56,770 --> 00:01:58,580
Now, just to be a
little different,
39
00:01:58,580 --> 00:02:00,070
it's important,
in this situation,
40
00:02:00,070 --> 00:02:02,530
that the Scott user
has the permissions
41
00:02:02,530 --> 00:02:05,200
to create a view on his tables.
42
00:02:05,200 --> 00:02:07,900
Just to show you a little bit
different way to do it here,
43
00:02:07,900 --> 00:02:12,280
let's log in with SQL*Plus
rather than SQL Developer,
44
00:02:12,280 --> 00:02:16,350
as a privileged user to
grant Scott that privilege.
45
00:02:16,350 --> 00:02:19,640
So we're going to
do sqlplus system
46
00:02:19,640 --> 00:02:23,270
and grant create view to Scott.
47
00:02:27,340 --> 00:02:28,970
All right, so let's
begin by looking
48
00:02:28,970 --> 00:02:31,610
at a little more complex query.
49
00:02:31,610 --> 00:02:33,500
This is going to be
a join from the emp
50
00:02:33,500 --> 00:02:35,600
table and the dept table.
51
00:02:35,600 --> 00:02:36,730
So let's put this together.
52
00:02:40,260 --> 00:02:44,100
So so far I have the empno,
ename, and job columns
53
00:02:44,100 --> 00:02:49,410
from the employee table, and
the dname and loc columns
54
00:02:49,410 --> 00:02:52,210
from the dept table.
55
00:02:52,210 --> 00:03:01,750
Aliased as e, dep aliased as d,
and join on the deptno column.
56
00:03:01,750 --> 00:03:04,340
All right, so it's
joining data from the emp
57
00:03:04,340 --> 00:03:07,350
table and the dept table.
58
00:03:07,350 --> 00:03:11,490
So rather than having to type
this query in anytime we need
59
00:03:11,490 --> 00:03:14,550
to do this particular join,
we can essentially just
60
00:03:14,550 --> 00:03:19,460
grab the SELECT statement
itself and create a view.
61
00:03:19,460 --> 00:03:21,820
So create view,
and then the name,
62
00:03:21,820 --> 00:03:24,890
as, and then the
SELECT statement.
63
00:03:24,890 --> 00:03:27,390
So remembering what
this data looks like,
64
00:03:27,390 --> 00:03:33,520
we'll create the view, and then
simply select * from emp_view.
65
00:03:33,520 --> 00:03:35,920
When we execute this
statement, it's basically
66
00:03:35,920 --> 00:03:37,930
going to come back to
the emp_view and it says,
67
00:03:37,930 --> 00:03:40,750
oh, that's defined
as this query.
68
00:03:40,750 --> 00:03:42,670
And it's going to
return the same data--
69
00:03:42,670 --> 00:03:46,460
data from emp and dept.
We're using a view
70
00:03:46,460 --> 00:03:50,030
to simplify query complexity.
71
00:03:50,030 --> 00:03:53,900
And the other type of view
we do is the selective view
72
00:03:53,900 --> 00:03:55,160
for data hiding.
73
00:03:55,160 --> 00:03:58,260
So let's look at the emp table.
74
00:03:58,260 --> 00:04:01,160
So let's say that
I want a user to be
75
00:04:01,160 --> 00:04:05,000
able to see ename
and job, manager,
76
00:04:05,000 --> 00:04:08,880
and hire date, but not
these other columns.
77
00:04:08,880 --> 00:04:12,170
Then I can employ a
view for data hiding.
78
00:04:12,170 --> 00:04:19,320
So I'll do create view
emp_sel_view as select ename,
79
00:04:19,320 --> 00:04:22,190
job, mgr-- for manager--
80
00:04:22,190 --> 00:04:24,200
and hire date-- so
the columns that I do
81
00:04:24,200 --> 00:04:29,330
want the user to be
able to see from emp.
82
00:04:29,330 --> 00:04:34,950
And now the user would
simply use this query.
83
00:04:34,950 --> 00:04:38,070
And we can see that only those
columns that we've specified
84
00:04:38,070 --> 00:04:42,150
in the selective view are
available to that user,
85
00:04:42,150 --> 00:04:45,240
and thereby we're
accomplishing data hiding.
86
00:04:45,240 --> 00:04:48,870
So those are just two ways
that we can use views.
6987
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.