Chapter 15-1 Views Indexes
Chapter 15-1 Views Indexes
Chapter 15
Base Table
• A base table (or simply table) is a permanent
database object that stores actual data
2
View
• A stored SELECT statement
• Does not contain data
• Used to access data stored in one or more base
tables
• Returns a result set based on the stored query
• The rows and columns in a view are based on the
base tables that the view is built upon
3
Advantages of Views
• Consume very little space
• Sequence data from a database in a different order
• Provide additional security - restrict access to
sensitive data by selecting or omitting specific rows
or columns from base tables
• Combine data from two or more tables into a single
virtual table that can be queried using basic
statements
• Separate a complex table into multiple virtual
tables that are simpler to query 4
Creating and Modifying a View
• CREATE (OR REPLACE) VIEW command
– Followed by the name of the view
– Followed by the keyword AS
– Followed by the subquery that defines the view
• The subquery can be run by itself
5
Dropping a View
• DROP VIEW command
6
No ORDER BY Clause with View
• The subquery that defines the view cannot contain
an ORDER BY clause
– The ORDER BY clause is specified when data is retrieved
from the view
7
Creating a Simple View Over Base Table
8
Running a query against a view
9
Modifying a View
• Use the OR REPLACE option in the CREATE VIEW
statement
10
Using Aliases in a View
Method 1
11
Using Aliases in a View
Method 2
12
Including a WHERE Clause in a View
• WHERE clause can be used to restrict rows in a
view
13
Including a WHERE Clause in a View
14
Accessing the View
15
Restricting Rows from a View
using a WHERE Clause
• A WHERE clause can be used when executing a
view to restrict rows normally returned by the view
16
Restricting Rows from a View
using a WHERE Clause
17
Restricting Rows from a View
using a WHERE Clause
• Only those rows that meet both conditions shown
below are returned in the result set:
18
Data Manipulation Language (DML)
and Views
• DML operations INSERT, UPDATE and DELETE can
be performed on simple views
19
Using a View to Update Data
• Views can be used in update statements
20
Using GROUP BY with a View
• The GROUP BY clause can be used in a view
21
Using JOINs in a View
• A view can contain JOINs
22
WITH CHECK OPTION
• Prevents a row from being inserted or updated
through a view that subsequently cannot be
retrieved through the view
23
WITH CHECK OPTION
24
WITH READ ONLY
• Ensures no DML statements occur through the view
• Any attempt to execute an INSERT, UPDATE or
DELETE statement will result in an error
25
Indexes
• An object that provides direct and fast access to
rows in a table
• Do not contain data
• Can be based on one column, multiple columns,
functions, or expressions
• If an index is not built over the column being
searched, then a full table scan occurs
26
Indexes
• Use an index when:
– One or more columns are frequently used together in a
join condition
– The table is large and most queries are expected to
retrieve less than 2 to 4 percent of the rows
27
Types of Indexes
• Unique index
– Automatically created when a column(s) in a table is
defined as PRIMARY KEY or UNIQUE KEY constraint
– The name of the index is the name given to the
constraint
• Nonunique index
– An index created to speed up access to the rows
– To optimize joins, create an index on the FOREIGN KEY
column, which speeds up the search to match rows to
the PRIMARY KEY column
28
Creating an Index
• The CREATE INDEX keywords
– Followed by the index name
– Followed by the keyword ON
– Followed by the name of the table
– Followed by the columns that are to be included in the
index; listed in parentheses
29
Creating an Index
30
Dropping an Index
31
32